From 79638140d9776f9baf60f2f66f7532574d60f6da Mon Sep 17 00:00:00 2001 From: hayden v Date: Tue, 3 Mar 2020 11:10:59 +1000 Subject: [PATCH 1/8] moved around shader code and other vulkan stuff --- .classpath | 2 +- .../github/hydos/ginger/VulkanStarter.java | 411 ++++++++---------- .../vulkan/{utils => }/VKConstants.java | 2 +- .../engine/vulkan/shaders/Pipeline.java | 166 +++++++ .../vulkan/shaders/VKShaderManager.java | 48 ++ .../vulkan/utils/VKDeviceProperties.java | 98 +++++ .../ginger/engine/vulkan/utils/VKLoader.java | 2 + .../ginger/engine/vulkan/utils/VKUtils.java | 68 ++- .../vulkan/utils/VulkanFuncWrapper.java | 7 +- 9 files changed, 560 insertions(+), 244 deletions(-) rename src/main/java/com/github/hydos/ginger/engine/vulkan/{utils => }/VKConstants.java (88%) create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java diff --git a/.classpath b/.classpath index febc371..a93fc90 100644 --- a/.classpath +++ b/.classpath @@ -5,7 +5,7 @@ - + diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index 4dc3d3a..e85842e 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -1,25 +1,194 @@ package com.github.hydos.ginger; -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.glfw.GLFWVulkan.*; -import static org.lwjgl.system.MemoryUtil.*; -import static org.lwjgl.vulkan.EXTDebugReport.*; -import static org.lwjgl.vulkan.KHRSurface.*; -import static org.lwjgl.vulkan.KHRSwapchain.*; -import static org.lwjgl.vulkan.VK10.*; +import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE; +import static org.lwjgl.glfw.GLFW.GLFW_RELEASE; +import static org.lwjgl.glfw.GLFW.glfwDestroyWindow; +import static org.lwjgl.glfw.GLFW.glfwPollEvents; +import static org.lwjgl.glfw.GLFW.glfwSetFramebufferSizeCallback; +import static org.lwjgl.glfw.GLFW.glfwSetKeyCallback; +import static org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose; +import static org.lwjgl.glfw.GLFW.glfwShowWindow; +import static org.lwjgl.glfw.GLFW.glfwTerminate; +import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose; +import static org.lwjgl.glfw.GLFWVulkan.glfwCreateWindowSurface; +import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions; +import static org.lwjgl.system.MemoryUtil.NULL; +import static org.lwjgl.system.MemoryUtil.memAddress; +import static org.lwjgl.system.MemoryUtil.memAlloc; +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memAllocPointer; +import static org.lwjgl.system.MemoryUtil.memByteBuffer; +import static org.lwjgl.system.MemoryUtil.memCopy; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.system.MemoryUtil.memUTF8; +import static org.lwjgl.vulkan.EXTDebugReport.VK_DEBUG_REPORT_ERROR_BIT_EXT; +import static org.lwjgl.vulkan.EXTDebugReport.VK_DEBUG_REPORT_WARNING_BIT_EXT; +import static org.lwjgl.vulkan.EXTDebugReport.vkDestroyDebugReportCallbackEXT; +import static org.lwjgl.vulkan.KHRSurface.VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_FIFO_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_IMMEDIATE_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_MAILBOX_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR; +import static org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; +import static org.lwjgl.vulkan.KHRSwapchain.VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; +import static org.lwjgl.vulkan.KHRSwapchain.VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkAcquireNextImageKHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkCreateSwapchainKHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkDestroySwapchainKHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkGetSwapchainImagesKHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkQueuePresentKHR; +import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_LOAD_OP_CLEAR; +import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_LOAD_OP_DONT_CARE; +import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_STORE_OP_DONT_CARE; +import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_STORE_OP_STORE; +import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; +import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; +import static org.lwjgl.vulkan.VK10.VK_COMMAND_BUFFER_LEVEL_PRIMARY; +import static org.lwjgl.vulkan.VK10.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; +import static org.lwjgl.vulkan.VK10.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_B8G8R8A8_UNORM; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_D16_UNORM; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_D16_UNORM_S8_UINT; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_D24_UNORM_S8_UINT; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_D32_SFLOAT; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_D32_SFLOAT_S8_UINT; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_R32G32B32_SFLOAT; +import static org.lwjgl.vulkan.VK10.VK_FORMAT_UNDEFINED; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_ASPECT_COLOR_BIT; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_ASPECT_DEPTH_BIT; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_ASPECT_STENCIL_BIT; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_LAYOUT_UNDEFINED; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_TILING_OPTIMAL; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_TYPE_2D; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_USAGE_TRANSFER_SRC_BIT; +import static org.lwjgl.vulkan.VK10.VK_IMAGE_VIEW_TYPE_2D; +import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; +import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; +import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE; +import static org.lwjgl.vulkan.VK10.VK_PIPELINE_BIND_POINT_GRAPHICS; +import static org.lwjgl.vulkan.VK10.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; +import static org.lwjgl.vulkan.VK10.VK_QUEUE_GRAPHICS_BIT; +import static org.lwjgl.vulkan.VK10.VK_SAMPLE_COUNT_1_BIT; +import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_VERTEX_BIT; +import static org.lwjgl.vulkan.VK10.VK_SHARING_MODE_EXCLUSIVE; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SUBMIT_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; +import static org.lwjgl.vulkan.VK10.VK_SUCCESS; +import static org.lwjgl.vulkan.VK10.VK_TRUE; +import static org.lwjgl.vulkan.VK10.VK_VERTEX_INPUT_RATE_VERTEX; +import static org.lwjgl.vulkan.VK10.vkAllocateCommandBuffers; +import static org.lwjgl.vulkan.VK10.vkAllocateDescriptorSets; +import static org.lwjgl.vulkan.VK10.vkAllocateMemory; +import static org.lwjgl.vulkan.VK10.vkBeginCommandBuffer; +import static org.lwjgl.vulkan.VK10.vkBindBufferMemory; +import static org.lwjgl.vulkan.VK10.vkBindImageMemory; +import static org.lwjgl.vulkan.VK10.vkCreateBuffer; +import static org.lwjgl.vulkan.VK10.vkCreateCommandPool; +import static org.lwjgl.vulkan.VK10.vkCreateDescriptorPool; +import static org.lwjgl.vulkan.VK10.vkCreateDescriptorSetLayout; +import static org.lwjgl.vulkan.VK10.vkCreateFramebuffer; +import static org.lwjgl.vulkan.VK10.vkCreateImage; +import static org.lwjgl.vulkan.VK10.vkCreateImageView; +import static org.lwjgl.vulkan.VK10.vkCreateRenderPass; +import static org.lwjgl.vulkan.VK10.vkCreateSemaphore; +import static org.lwjgl.vulkan.VK10.vkDestroyFramebuffer; +import static org.lwjgl.vulkan.VK10.vkDestroySemaphore; +import static org.lwjgl.vulkan.VK10.vkEndCommandBuffer; +import static org.lwjgl.vulkan.VK10.vkGetBufferMemoryRequirements; +import static org.lwjgl.vulkan.VK10.vkGetDeviceQueue; +import static org.lwjgl.vulkan.VK10.vkGetImageMemoryRequirements; +import static org.lwjgl.vulkan.VK10.vkGetPhysicalDeviceFormatProperties; +import static org.lwjgl.vulkan.VK10.vkGetPhysicalDeviceQueueFamilyProperties; +import static org.lwjgl.vulkan.VK10.vkMapMemory; +import static org.lwjgl.vulkan.VK10.vkQueueSubmit; +import static org.lwjgl.vulkan.VK10.vkQueueWaitIdle; +import static org.lwjgl.vulkan.VK10.vkResetCommandPool; +import static org.lwjgl.vulkan.VK10.vkUnmapMemory; +import static org.lwjgl.vulkan.VK10.vkUpdateDescriptorSets; import java.io.IOException; -import java.nio.*; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; import org.joml.Matrix4f; import org.lwjgl.PointerBuffer; -import org.lwjgl.glfw.*; -import org.lwjgl.vulkan.*; +import org.lwjgl.glfw.GLFWFramebufferSizeCallback; +import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.vulkan.VkAttachmentDescription; +import org.lwjgl.vulkan.VkAttachmentReference; +import org.lwjgl.vulkan.VkBufferCreateInfo; +import org.lwjgl.vulkan.VkCommandBuffer; +import org.lwjgl.vulkan.VkCommandBufferAllocateInfo; +import org.lwjgl.vulkan.VkCommandBufferBeginInfo; +import org.lwjgl.vulkan.VkCommandPoolCreateInfo; +import org.lwjgl.vulkan.VkDescriptorBufferInfo; +import org.lwjgl.vulkan.VkDescriptorPoolCreateInfo; +import org.lwjgl.vulkan.VkDescriptorPoolSize; +import org.lwjgl.vulkan.VkDescriptorSetAllocateInfo; +import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding; +import org.lwjgl.vulkan.VkDescriptorSetLayoutCreateInfo; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkExtent2D; +import org.lwjgl.vulkan.VkFormatProperties; +import org.lwjgl.vulkan.VkFramebufferCreateInfo; +import org.lwjgl.vulkan.VkImageCreateInfo; +import org.lwjgl.vulkan.VkImageViewCreateInfo; +import org.lwjgl.vulkan.VkInstance; +import org.lwjgl.vulkan.VkMemoryAllocateInfo; +import org.lwjgl.vulkan.VkMemoryRequirements; +import org.lwjgl.vulkan.VkPhysicalDevice; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo; +import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; +import org.lwjgl.vulkan.VkPresentInfoKHR; +import org.lwjgl.vulkan.VkQueue; +import org.lwjgl.vulkan.VkQueueFamilyProperties; +import org.lwjgl.vulkan.VkRenderPassCreateInfo; +import org.lwjgl.vulkan.VkSemaphoreCreateInfo; +import org.lwjgl.vulkan.VkSubmitInfo; +import org.lwjgl.vulkan.VkSubpassDescription; +import org.lwjgl.vulkan.VkSurfaceCapabilitiesKHR; +import org.lwjgl.vulkan.VkSurfaceFormatKHR; +import org.lwjgl.vulkan.VkSwapchainCreateInfoKHR; +import org.lwjgl.vulkan.VkVertexInputAttributeDescription; +import org.lwjgl.vulkan.VkVertexInputBindingDescription; +import org.lwjgl.vulkan.VkWriteDescriptorSet; 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.*; -import com.github.hydos.ginger.engine.vulkan.utils.VKUtils.Pipeline; +import com.github.hydos.ginger.engine.vulkan.VKConstants; +import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; +import com.github.hydos.ginger.engine.vulkan.shaders.VKShaderManager; +import com.github.hydos.ginger.engine.vulkan.utils.VKDeviceProperties; +import com.github.hydos.ginger.engine.vulkan.utils.VKLoader; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; /** * * @author hydos06 @@ -28,80 +197,6 @@ import com.github.hydos.ginger.engine.vulkan.utils.VKUtils.Pipeline; */ public class VulkanStarter { - private static VkPhysicalDevice getFirstPhysicalDevice(VkInstance instance) - { - IntBuffer pPhysicalDeviceCount = memAllocInt(1); - int err = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, null); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get number of physical devices: " + VKUtils.translateVulkanResult(err)); } - PointerBuffer pPhysicalDevices = memAllocPointer(pPhysicalDeviceCount.get(0)); - err = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); - long physicalDevice = pPhysicalDevices.get(0); - memFree(pPhysicalDeviceCount); - memFree(pPhysicalDevices); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get physical devices: " + VKUtils.translateVulkanResult(err)); } - return new VkPhysicalDevice(physicalDevice, instance); - } - - private static class DeviceAndGraphicsQueueFamily - { - VkDevice device; - int queueFamilyIndex; - VkPhysicalDeviceMemoryProperties memoryProperties; - } - - private static DeviceAndGraphicsQueueFamily createDeviceAndGetGraphicsQueueFamily(VkPhysicalDevice physicalDevice) - { - IntBuffer pQueueFamilyPropertyCount = memAllocInt(1); - vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, null); - int queueCount = pQueueFamilyPropertyCount.get(0); - VkQueueFamilyProperties.Buffer queueProps = VkQueueFamilyProperties.calloc(queueCount); - vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, queueProps); - memFree(pQueueFamilyPropertyCount); - int graphicsQueueFamilyIndex; - for (graphicsQueueFamilyIndex = 0; graphicsQueueFamilyIndex < queueCount; graphicsQueueFamilyIndex++) - { if ((queueProps.get(graphicsQueueFamilyIndex).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) - break; } - queueProps.free(); - FloatBuffer pQueuePriorities = memAllocFloat(1).put(0.0f); - pQueuePriorities.flip(); - VkDeviceQueueCreateInfo.Buffer queueCreateInfo = VkDeviceQueueCreateInfo.calloc(1) - .sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO) - .queueFamilyIndex(graphicsQueueFamilyIndex) - .pQueuePriorities(pQueuePriorities); - PointerBuffer extensions = memAllocPointer(1); - ByteBuffer VK_KHR_SWAPCHAIN_EXTENSION = memUTF8(VK_KHR_SWAPCHAIN_EXTENSION_NAME); - extensions.put(VK_KHR_SWAPCHAIN_EXTENSION); - extensions.flip(); - PointerBuffer ppEnabledLayerNames = memAllocPointer(VKConstants.layers.length); - for (int i = 0; VKConstants.debug && i < VKConstants.layers.length; i++) - ppEnabledLayerNames.put(VKConstants.layers[i]); - ppEnabledLayerNames.flip(); - VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO) - .pQueueCreateInfos(queueCreateInfo) - .ppEnabledExtensionNames(extensions) - .ppEnabledLayerNames(ppEnabledLayerNames); - PointerBuffer pDevice = memAllocPointer(1); - int err = vkCreateDevice(physicalDevice, deviceCreateInfo, null, pDevice); - long device = pDevice.get(0); - memFree(pDevice); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create device: " + VKUtils.translateVulkanResult(err)); } - VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties.calloc(); - vkGetPhysicalDeviceMemoryProperties(physicalDevice, memoryProperties); - DeviceAndGraphicsQueueFamily ret = new DeviceAndGraphicsQueueFamily(); - ret.device = new VkDevice(device, physicalDevice, deviceCreateInfo); - ret.queueFamilyIndex = graphicsQueueFamilyIndex; - ret.memoryProperties = memoryProperties; - deviceCreateInfo.free(); - memFree(ppEnabledLayerNames); - memFree(VK_KHR_SWAPCHAIN_EXTENSION); - memFree(extensions); - memFree(pQueuePriorities); - return ret; - } private static boolean getSupportedDepthFormat(VkPhysicalDevice physicalDevice, IntBuffer depthFormat) { @@ -562,32 +657,6 @@ public class VulkanStarter { throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); } } - private static long loadShader(String classPath, VkDevice device, int stage) throws IOException - { - ByteBuffer shaderCode = VKUtils.glslToSpirv(classPath, stage); - int err; - VkShaderModuleCreateInfo moduleCreateInfo = VkShaderModuleCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) - .pCode(shaderCode); - LongBuffer pShaderModule = memAllocLong(1); - err = vkCreateShaderModule(device, moduleCreateInfo, null, pShaderModule); - long shaderModule = pShaderModule.get(0); - memFree(pShaderModule); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create shader module: " + VKUtils.translateVulkanResult(err)); } - return shaderModule; - } - - private static VkPipelineShaderStageCreateInfo loadShader(VkDevice device, String classPath, int stage) throws IOException - { - VkPipelineShaderStageCreateInfo shaderStage = VkPipelineShaderStageCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) - .stage(stage) - .module(loadShader(classPath, device, stage)) - .pName(memUTF8("main")); - return shaderStage; - } - private static boolean getMemoryType(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, int typeBits, int properties, IntBuffer typeIndex) { int bits = typeBits; @@ -849,116 +918,6 @@ public class VulkanStarter return descriptorSetLayout; } - private static Pipeline createPipeline(VkDevice device, long renderPass, VkPipelineVertexInputStateCreateInfo vi, long descriptorSetLayout) throws IOException - { - int err; - // Vertex input state - // Describes the topoloy used with this pipeline - VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO) - .topology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); - // Rasterization state - VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO) - .polygonMode(VK_POLYGON_MODE_FILL) - .cullMode(VK_CULL_MODE_NONE) // <- VK_CULL_MODE_BACK_BIT would work here, too! - .frontFace(VK_FRONT_FACE_COUNTER_CLOCKWISE) - .lineWidth(1.0f); - // Color blend state - // Describes blend modes and color masks - VkPipelineColorBlendAttachmentState.Buffer colorWriteMask = VkPipelineColorBlendAttachmentState.calloc(1) - .colorWriteMask(0xF); // <- RGBA - VkPipelineColorBlendStateCreateInfo colorBlendState = VkPipelineColorBlendStateCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO) - .pAttachments(colorWriteMask); - // Viewport state - VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO) - .viewportCount(1) // <- one viewport - .scissorCount(1); // <- one scissor rectangle - // Enable dynamic states - // Describes the dynamic states to be used with this pipeline - // Dynamic states can be set even after the pipeline has been created - // So there is no need to create new pipelines just for changing - // a viewport's dimensions or a scissor box - IntBuffer pDynamicStates = memAllocInt(2); - pDynamicStates.put(VK_DYNAMIC_STATE_VIEWPORT).put(VK_DYNAMIC_STATE_SCISSOR).flip(); - VkPipelineDynamicStateCreateInfo dynamicState = VkPipelineDynamicStateCreateInfo.calloc() - // The dynamic state properties themselves are stored in the command buffer - .sType(VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO) - .pDynamicStates(pDynamicStates); - // Depth and stencil state - // Describes depth and stenctil test and compare ops - VkPipelineDepthStencilStateCreateInfo depthStencilState = VkPipelineDepthStencilStateCreateInfo.calloc() - // No depth test/write and no stencil used - .sType(VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO) - .depthTestEnable(true) - .depthWriteEnable(true) - .depthCompareOp(VK_COMPARE_OP_LESS_OR_EQUAL); - depthStencilState.back() - .failOp(VK_STENCIL_OP_KEEP) - .passOp(VK_STENCIL_OP_KEEP) - .compareOp(VK_COMPARE_OP_ALWAYS); - depthStencilState.front(depthStencilState.back()); - // Multi sampling state - // No multi sampling used in this example - VkPipelineMultisampleStateCreateInfo multisampleState = VkPipelineMultisampleStateCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO) - .rasterizationSamples(VK_SAMPLE_COUNT_1_BIT); - // Load shaders - VkPipelineShaderStageCreateInfo.Buffer shaderStages = VkPipelineShaderStageCreateInfo.calloc(2); - shaderStages.get(0).set(loadShader(device, "/vulkan/shaders/entityVertexShader.glsl", VK_SHADER_STAGE_VERTEX_BIT)); - shaderStages.get(1).set(loadShader(device, "/vulkan/shaders/entityFragmentShader.glsl", VK_SHADER_STAGE_FRAGMENT_BIT)); - // Create the pipeline layout that is used to generate the rendering pipelines that - // are based on this descriptor set layout - LongBuffer pDescriptorSetLayout = memAllocLong(1).put(0, descriptorSetLayout); - VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO) - .pSetLayouts(pDescriptorSetLayout); - LongBuffer pPipelineLayout = memAllocLong(1); - err = vkCreatePipelineLayout(device, pipelineLayoutCreateInfo, null, pPipelineLayout); - long layout = pPipelineLayout.get(0); - memFree(pPipelineLayout); - pipelineLayoutCreateInfo.free(); - memFree(pDescriptorSetLayout); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create pipeline layout: " + VKUtils.translateVulkanResult(err)); } - // Assign states - VkGraphicsPipelineCreateInfo.Buffer pipelineCreateInfo = VkGraphicsPipelineCreateInfo.calloc(1) - .sType(VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) - .layout(layout) // <- the layout used for this pipeline (NEEDS TO BE SET! even though it is basically empty) - .renderPass(renderPass) // <- renderpass this pipeline is attached to - .pVertexInputState(vi) - .pInputAssemblyState(inputAssemblyState) - .pRasterizationState(rasterizationState) - .pColorBlendState(colorBlendState) - .pMultisampleState(multisampleState) - .pViewportState(viewportState) - .pDepthStencilState(depthStencilState) - .pStages(shaderStages) - .pDynamicState(dynamicState); - // Create rendering pipeline - LongBuffer pPipelines = memAllocLong(1); - err = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, pipelineCreateInfo, null, pPipelines); - long pipeline = pPipelines.get(0); - shaderStages.free(); - multisampleState.free(); - depthStencilState.free(); - dynamicState.free(); - memFree(pDynamicStates); - viewportState.free(); - colorBlendState.free(); - colorWriteMask.free(); - rasterizationState.free(); - inputAssemblyState.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create pipeline: " + VKUtils.translateVulkanResult(err)); } - Pipeline ret = new Pipeline(); - ret.layout = layout; - ret.pipeline = pipeline; - return ret; - } - private static void updateUbo(VkDevice device, UboDescriptor ubo, float angle) { //a UBO is a uniform buffer object Matrix4f m = new Matrix4f() @@ -998,8 +957,8 @@ public class VulkanStarter final VkInstance instance = VKLoader.createInstance(requiredExtensions); VKUtils.setupVulkanDebugCallback(); final long debugCallbackHandle = VKUtils.startVulkanDebugging(instance, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, VKConstants.debugCallback); - final VkPhysicalDevice physicalDevice = getFirstPhysicalDevice(instance); - final DeviceAndGraphicsQueueFamily deviceAndGraphicsQueueFamily = createDeviceAndGetGraphicsQueueFamily(physicalDevice); + final VkPhysicalDevice physicalDevice = VKDeviceProperties.getFirstPhysicalDevice(instance); + final VKDeviceProperties deviceAndGraphicsQueueFamily = VKDeviceProperties.initDeviceProperties(physicalDevice); final VkDevice device = deviceAndGraphicsQueueFamily.device; int queueFamilyIndex = deviceAndGraphicsQueueFamily.queueFamilyIndex; final VkPhysicalDeviceMemoryProperties memoryProperties = deviceAndGraphicsQueueFamily.memoryProperties; @@ -1026,12 +985,12 @@ public class VulkanStarter final VkQueue queue = createDeviceQueue(device, queueFamilyIndex); final long renderPass = createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat); final long renderCommandPool = createCommandPool(device, queueFamilyIndex); - final Vertices vertices = createVertices(memoryProperties, device); + Vertices vertices = createVertices(memoryProperties, device); UboDescriptor uboDescriptor = createUniformBuffer(memoryProperties, device); final long descriptorPool = createDescriptorPool(device); final long descriptorSetLayout = createDescriptorSetLayout(device); final long descriptorSet = createDescriptorSet(device, descriptorPool, descriptorSetLayout, uboDescriptor); - final Pipeline pipeline = createPipeline(device, renderPass, vertices.createInfo, descriptorSetLayout); + final Pipeline pipeline = Pipeline.createPipeline(device, renderPass, vertices.createInfo, descriptorSetLayout); final class SwapchainRecreator { boolean mustRecreate = true; diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKConstants.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/VKConstants.java similarity index 88% rename from src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKConstants.java rename to src/main/java/com/github/hydos/ginger/engine/vulkan/VKConstants.java index 07987b2..c1d7647 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKConstants.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/VKConstants.java @@ -1,4 +1,4 @@ -package com.github.hydos.ginger.engine.vulkan.utils; +package com.github.hydos.ginger.engine.vulkan; import java.nio.ByteBuffer; diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java new file mode 100644 index 0000000..ee04e31 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java @@ -0,0 +1,166 @@ +package com.github.hydos.ginger.engine.vulkan.shaders; + +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.vulkan.VK10.VK_COMPARE_OP_ALWAYS; +import static org.lwjgl.vulkan.VK10.VK_COMPARE_OP_LESS_OR_EQUAL; +import static org.lwjgl.vulkan.VK10.VK_CULL_MODE_NONE; +import static org.lwjgl.vulkan.VK10.VK_DYNAMIC_STATE_SCISSOR; +import static org.lwjgl.vulkan.VK10.VK_DYNAMIC_STATE_VIEWPORT; +import static org.lwjgl.vulkan.VK10.VK_FRONT_FACE_COUNTER_CLOCKWISE; +import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE; +import static org.lwjgl.vulkan.VK10.VK_POLYGON_MODE_FILL; +import static org.lwjgl.vulkan.VK10.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; +import static org.lwjgl.vulkan.VK10.VK_SAMPLE_COUNT_1_BIT; +import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_FRAGMENT_BIT; +import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_VERTEX_BIT; +import static org.lwjgl.vulkan.VK10.VK_STENCIL_OP_KEEP; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_SUCCESS; +import static org.lwjgl.vulkan.VK10.vkCreateGraphicsPipelines; +import static org.lwjgl.vulkan.VK10.vkCreatePipelineLayout; + +import java.io.IOException; +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkGraphicsPipelineCreateInfo; +import org.lwjgl.vulkan.VkPipelineColorBlendAttachmentState; +import org.lwjgl.vulkan.VkPipelineColorBlendStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineDepthStencilStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineDynamicStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineInputAssemblyStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineLayoutCreateInfo; +import org.lwjgl.vulkan.VkPipelineMultisampleStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineRasterizationStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo; +import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; +import org.lwjgl.vulkan.VkPipelineViewportStateCreateInfo; + +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; + +public class Pipeline +{ + public long pipeline; + public long layout; + + public static Pipeline createPipeline(VkDevice device, long renderPass, VkPipelineVertexInputStateCreateInfo vi, long descriptorSetLayout) throws IOException + { + int err; + // Vertex input state + // Describes the topoloy used with this pipeline + VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO) + .topology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); + // Rasterization state + VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO) + .polygonMode(VK_POLYGON_MODE_FILL) + .cullMode(VK_CULL_MODE_NONE) // <- VK_CULL_MODE_BACK_BIT would work here, too! + .frontFace(VK_FRONT_FACE_COUNTER_CLOCKWISE) + .lineWidth(1.0f); + // Color blend state + // Describes blend modes and color masks + VkPipelineColorBlendAttachmentState.Buffer colorWriteMask = VkPipelineColorBlendAttachmentState.calloc(1) + .colorWriteMask(0xF); // <- RGBA + VkPipelineColorBlendStateCreateInfo colorBlendState = VkPipelineColorBlendStateCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO) + .pAttachments(colorWriteMask); + // Viewport state + VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO) + .viewportCount(1) // <- one viewport + .scissorCount(1); // <- one scissor rectangle + // Enable dynamic states + // Describes the dynamic states to be used with this pipeline + // Dynamic states can be set even after the pipeline has been created + // So there is no need to create new pipelines just for changing + // a viewport's dimensions or a scissor box + IntBuffer pDynamicStates = memAllocInt(2); + pDynamicStates.put(VK_DYNAMIC_STATE_VIEWPORT).put(VK_DYNAMIC_STATE_SCISSOR).flip(); + VkPipelineDynamicStateCreateInfo dynamicState = VkPipelineDynamicStateCreateInfo.calloc() + // The dynamic state properties themselves are stored in the command buffer + .sType(VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO) + .pDynamicStates(pDynamicStates); + // Depth and stencil state + // Describes depth and stenctil test and compare ops + VkPipelineDepthStencilStateCreateInfo depthStencilState = VkPipelineDepthStencilStateCreateInfo.calloc() + // No depth test/write and no stencil used + .sType(VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO) + .depthTestEnable(true) + .depthWriteEnable(true) + .depthCompareOp(VK_COMPARE_OP_LESS_OR_EQUAL); + depthStencilState.back() + .failOp(VK_STENCIL_OP_KEEP) + .passOp(VK_STENCIL_OP_KEEP) + .compareOp(VK_COMPARE_OP_ALWAYS); + depthStencilState.front(depthStencilState.back()); + // Multi sampling state + // No multi sampling used in this example + VkPipelineMultisampleStateCreateInfo multisampleState = VkPipelineMultisampleStateCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO) + .rasterizationSamples(VK_SAMPLE_COUNT_1_BIT); + // Load shaders + VkPipelineShaderStageCreateInfo.Buffer shaderStages = VkPipelineShaderStageCreateInfo.calloc(2); + shaderStages.get(0).set(VKShaderManager.loadShader(device, "/vulkan/shaders/entityVertexShader.glsl", VK_SHADER_STAGE_VERTEX_BIT)); + shaderStages.get(1).set(VKShaderManager.loadShader(device, "/vulkan/shaders/entityFragmentShader.glsl", VK_SHADER_STAGE_FRAGMENT_BIT)); + // Create the pipeline layout that is used to generate the rendering pipelines that + // are based on this descriptor set layout + LongBuffer pDescriptorSetLayout = memAllocLong(1).put(0, descriptorSetLayout); + VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO) + .pSetLayouts(pDescriptorSetLayout); + LongBuffer pPipelineLayout = memAllocLong(1); + err = vkCreatePipelineLayout(device, pipelineLayoutCreateInfo, null, pPipelineLayout); + long layout = pPipelineLayout.get(0); + memFree(pPipelineLayout); + pipelineLayoutCreateInfo.free(); + memFree(pDescriptorSetLayout); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to create pipeline layout: " + VKUtils.translateVulkanResult(err)); } + // Assign states + VkGraphicsPipelineCreateInfo.Buffer pipelineCreateInfo = VkGraphicsPipelineCreateInfo.calloc(1) + .sType(VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) + .layout(layout) // <- the layout used for this pipeline (NEEDS TO BE SET! even though it is basically empty) + .renderPass(renderPass) // <- renderpass this pipeline is attached to + .pVertexInputState(vi) + .pInputAssemblyState(inputAssemblyState) + .pRasterizationState(rasterizationState) + .pColorBlendState(colorBlendState) + .pMultisampleState(multisampleState) + .pViewportState(viewportState) + .pDepthStencilState(depthStencilState) + .pStages(shaderStages) + .pDynamicState(dynamicState); + // Create rendering pipeline + LongBuffer pPipelines = memAllocLong(1); + err = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, pipelineCreateInfo, null, pPipelines); + long pipeline = pPipelines.get(0); + shaderStages.free(); + multisampleState.free(); + depthStencilState.free(); + dynamicState.free(); + memFree(pDynamicStates); + viewportState.free(); + colorBlendState.free(); + colorWriteMask.free(); + rasterizationState.free(); + inputAssemblyState.free(); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to create pipeline: " + VKUtils.translateVulkanResult(err)); } + com.github.hydos.ginger.engine.vulkan.shaders.Pipeline ret = new com.github.hydos.ginger.engine.vulkan.shaders.Pipeline(); + ret.layout = layout; + ret.pipeline = pipeline; + return ret; + } +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java new file mode 100644 index 0000000..51e4b63 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java @@ -0,0 +1,48 @@ +package com.github.hydos.ginger.engine.vulkan.shaders; + +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.system.MemoryUtil.memUTF8; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_SUCCESS; +import static org.lwjgl.vulkan.VK10.vkCreateShaderModule; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.LongBuffer; + +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo; +import org.lwjgl.vulkan.VkShaderModuleCreateInfo; + +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; + +public class VKShaderManager { + + public static VkPipelineShaderStageCreateInfo loadShader(VkDevice device, String classPath, int stage) throws IOException + { + VkPipelineShaderStageCreateInfo shaderStage = VkPipelineShaderStageCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) + .stage(stage) + .module(VKShaderManager.loadShader(classPath, device, stage)) + .pName(memUTF8("main")); + return shaderStage; + } + + public static long loadShader(String classPath, VkDevice device, int stage) throws IOException + { + ByteBuffer shaderCode = VKUtils.glslToSpirv(classPath, stage); + int err; + VkShaderModuleCreateInfo moduleCreateInfo = VkShaderModuleCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) + .pCode(shaderCode); + LongBuffer pShaderModule = memAllocLong(1); + err = vkCreateShaderModule(device, moduleCreateInfo, null, pShaderModule); + long shaderModule = pShaderModule.get(0); + memFree(pShaderModule); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to create shader module: " + VKUtils.translateVulkanResult(err)); } + return shaderModule; + } +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java new file mode 100644 index 0000000..d9f0c16 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java @@ -0,0 +1,98 @@ +package com.github.hydos.ginger.engine.vulkan.utils; + +import static org.lwjgl.system.MemoryUtil.memAllocFloat; +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocPointer; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.system.MemoryUtil.memUTF8; +import static org.lwjgl.vulkan.KHRSwapchain.VK_KHR_SWAPCHAIN_EXTENSION_NAME; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +import org.lwjgl.PointerBuffer; +import org.lwjgl.vulkan.VK12; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkDeviceCreateInfo; +import org.lwjgl.vulkan.VkDeviceQueueCreateInfo; +import org.lwjgl.vulkan.VkInstance; +import org.lwjgl.vulkan.VkPhysicalDevice; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.VkQueueFamilyProperties; + +import com.github.hydos.ginger.engine.vulkan.VKConstants; + +public class VKDeviceProperties { + public VkDevice device; + public int queueFamilyIndex; + public VkPhysicalDeviceMemoryProperties memoryProperties; + + public static VkPhysicalDevice getFirstPhysicalDevice(VkInstance instance) + { + IntBuffer pPhysicalDeviceCount = memAllocInt(1); + int err = VK12.vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, null); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get number of physical devices: " + VKUtils.translateVulkanResult(err)); } + PointerBuffer pPhysicalDevices = memAllocPointer(pPhysicalDeviceCount.get(0)); + err = VK12.vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); + long physicalDevice = pPhysicalDevices.get(0); + memFree(pPhysicalDeviceCount); + memFree(pPhysicalDevices); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get physical devices: " + VKUtils.translateVulkanResult(err)); } + return new VkPhysicalDevice(physicalDevice, instance); + } + + public static VKDeviceProperties initDeviceProperties(VkPhysicalDevice physicalDevice) + { + IntBuffer pQueueFamilyPropertyCount = memAllocInt(1); + VK12.vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, null); + int queueCount = pQueueFamilyPropertyCount.get(0); + VkQueueFamilyProperties.Buffer queueProps = VkQueueFamilyProperties.calloc(queueCount); + VK12.vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, queueProps); + memFree(pQueueFamilyPropertyCount); + int graphicsQueueFamilyIndex; + for (graphicsQueueFamilyIndex = 0; graphicsQueueFamilyIndex < queueCount; graphicsQueueFamilyIndex++) + { if ((queueProps.get(graphicsQueueFamilyIndex).queueFlags() & VK12.VK_QUEUE_GRAPHICS_BIT) != 0) + break; } + queueProps.free(); + FloatBuffer pQueuePriorities = memAllocFloat(1).put(0.0f); + pQueuePriorities.flip(); + VkDeviceQueueCreateInfo.Buffer queueCreateInfo = VkDeviceQueueCreateInfo.calloc(1) + .sType(VK12.VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO) + .queueFamilyIndex(graphicsQueueFamilyIndex) + .pQueuePriorities(pQueuePriorities); + PointerBuffer extensions = memAllocPointer(1); + ByteBuffer VK_KHR_SWAPCHAIN_EXTENSION = memUTF8(VK_KHR_SWAPCHAIN_EXTENSION_NAME); + extensions.put(VK_KHR_SWAPCHAIN_EXTENSION); + extensions.flip(); + PointerBuffer ppEnabledLayerNames = memAllocPointer(VKConstants.layers.length); + for (int i = 0; VKConstants.debug && i < VKConstants.layers.length; i++) + ppEnabledLayerNames.put(VKConstants.layers[i]); + ppEnabledLayerNames.flip(); + VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO) + .pQueueCreateInfos(queueCreateInfo) + .ppEnabledExtensionNames(extensions) + .ppEnabledLayerNames(ppEnabledLayerNames); + PointerBuffer pDevice = memAllocPointer(1); + int err = VK12.vkCreateDevice(physicalDevice, deviceCreateInfo, null, pDevice); + long device = pDevice.get(0); + memFree(pDevice); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create device: " + VKUtils.translateVulkanResult(err)); } + VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties.calloc(); + VK12.vkGetPhysicalDeviceMemoryProperties(physicalDevice, memoryProperties); + VKDeviceProperties ret = new VKDeviceProperties(); + ret.device = new VkDevice(device, physicalDevice, deviceCreateInfo); + ret.queueFamilyIndex = graphicsQueueFamilyIndex; + ret.memoryProperties = memoryProperties; + deviceCreateInfo.free(); + memFree(ppEnabledLayerNames); + memFree(VK_KHR_SWAPCHAIN_EXTENSION); + memFree(extensions); + memFree(pQueuePriorities); + return ret; + } +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKLoader.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKLoader.java index 880298d..58c3e64 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKLoader.java @@ -10,6 +10,8 @@ import java.nio.ByteBuffer; import org.lwjgl.PointerBuffer; import org.lwjgl.vulkan.*; +import com.github.hydos.ginger.engine.vulkan.VKConstants; + /** @author hydos * used to load vulkan related objects such as textures */ public class VKLoader 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 6a7fa22..f11cc66 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 @@ -1,21 +1,65 @@ package com.github.hydos.ginger.engine.vulkan.utils; -import static org.lwjgl.system.MemoryUtil.*; -import static org.lwjgl.util.shaderc.Shaderc.*; -import static org.lwjgl.vulkan.EXTDebugReport.*; -import static org.lwjgl.vulkan.KHRSurface.*; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memAllocPointer; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.system.MemoryUtil.memUTF8; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_anyhit_shader; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_closesthit_shader; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compilation_status_success; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_into_spv; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_initialize; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_set_include_callbacks; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_set_optimization_level; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compiler_initialize; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_compiler_release; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_fragment_shader; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_miss_shader; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_optimization_level_performance; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_raygen_shader; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_bytes; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_compilation_status; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_error_message; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_length; +import static org.lwjgl.util.shaderc.Shaderc.shaderc_vertex_shader; +import static org.lwjgl.vulkan.EXTDebugReport.VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; +import static org.lwjgl.vulkan.EXTDebugReport.vkCreateDebugReportCallbackEXT; +import static org.lwjgl.vulkan.KHRSurface.VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_ERROR_SURFACE_LOST_KHR; import java.io.IOException; -import java.nio.*; +import java.nio.ByteBuffer; +import java.nio.LongBuffer; -import org.lwjgl.*; +import org.lwjgl.BufferUtils; +import org.lwjgl.PointerBuffer; import org.lwjgl.system.MemoryStack; -import org.lwjgl.util.shaderc.*; -import org.lwjgl.vulkan.*; +import org.lwjgl.util.shaderc.ShadercIncludeResolve; +import org.lwjgl.util.shaderc.ShadercIncludeResult; +import org.lwjgl.util.shaderc.ShadercIncludeResultRelease; +import org.lwjgl.vulkan.EXTDebugReport; +import org.lwjgl.vulkan.KHRDisplaySwapchain; +import org.lwjgl.vulkan.KHRSwapchain; +import org.lwjgl.vulkan.NVRayTracing; +import org.lwjgl.vulkan.VK10; +import org.lwjgl.vulkan.VK12; +import org.lwjgl.vulkan.VkClearValue; +import org.lwjgl.vulkan.VkCommandBuffer; +import org.lwjgl.vulkan.VkCommandBufferAllocateInfo; +import org.lwjgl.vulkan.VkCommandBufferBeginInfo; +import org.lwjgl.vulkan.VkDebugReportCallbackCreateInfoEXT; +import org.lwjgl.vulkan.VkDebugReportCallbackEXT; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkInstance; +import org.lwjgl.vulkan.VkRect2D; +import org.lwjgl.vulkan.VkRenderPassBeginInfo; +import org.lwjgl.vulkan.VkViewport; import com.github.hydos.ginger.engine.common.tools.IOUtil; +import com.github.hydos.ginger.engine.vulkan.VKConstants; +import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; -/** @author hydos06 +/** @author hydos * a util library for Vulkan */ public class VKUtils { @@ -164,12 +208,6 @@ public class VKUtils } } - public static class Pipeline - { - public long pipeline; - public long layout; - } - public static VkCommandBuffer[] initRenderCommandBuffers(VkDevice device, long commandPool, long[] framebuffers, long renderPass, int width, int height, Pipeline pipeline, long descriptorSet, long verticesBuf) { diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VulkanFuncWrapper.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VulkanFuncWrapper.java index b8dea9d..64f1163 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VulkanFuncWrapper.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VulkanFuncWrapper.java @@ -12,7 +12,12 @@ import static org.lwjgl.vulkan.VK10.*; import org.lwjgl.system.MemoryStack; import org.lwjgl.util.vma.*; import org.lwjgl.vulkan.*; - +/** + * + * @author hydos + * used to make the vulkan api more readable + * + */ public class VulkanFuncWrapper { public static VmaVulkanFunctions VmaVulkanFunctions(MemoryStack stack) From e27fe5a8d18eef57131f6bc995a92c4af067e40c Mon Sep 17 00:00:00 2001 From: hayden v Date: Tue, 3 Mar 2020 12:33:06 +1000 Subject: [PATCH 2/8] Proper implementation of Uniform Buffer Objects --- .../github/hydos/ginger/VulkanStarter.java | 115 ++--------------- .../ginger/engine/vulkan/memory/VKMemory.java | 28 +++++ .../ginger/engine/vulkan/render/ubo/Ubo.java | 116 ++++++++++++++++++ .../vulkan/render/ubo/UboDescriptor.java | 9 ++ 4 files changed, 161 insertions(+), 107 deletions(-) create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/memory/VKMemory.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/UboDescriptor.java diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index e85842e..f6406a6 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -21,7 +21,6 @@ import static org.lwjgl.system.MemoryUtil.memAllocPointer; import static org.lwjgl.system.MemoryUtil.memByteBuffer; import static org.lwjgl.system.MemoryUtil.memCopy; import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.system.MemoryUtil.memUTF8; import static org.lwjgl.vulkan.EXTDebugReport.VK_DEBUG_REPORT_ERROR_BIT_EXT; import static org.lwjgl.vulkan.EXTDebugReport.VK_DEBUG_REPORT_WARNING_BIT_EXT; import static org.lwjgl.vulkan.EXTDebugReport.vkDestroyDebugReportCallbackEXT; @@ -46,7 +45,6 @@ import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_LOAD_OP_CLEAR; import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_LOAD_OP_DONT_CARE; import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_STORE_OP_DONT_CARE; import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_STORE_OP_STORE; -import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; import static org.lwjgl.vulkan.VK10.VK_COMMAND_BUFFER_LEVEL_PRIMARY; import static org.lwjgl.vulkan.VK10.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; @@ -92,7 +90,6 @@ import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; @@ -165,7 +162,6 @@ import org.lwjgl.vulkan.VkMemoryAllocateInfo; import org.lwjgl.vulkan.VkMemoryRequirements; import org.lwjgl.vulkan.VkPhysicalDevice; import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; -import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo; import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; import org.lwjgl.vulkan.VkPresentInfoKHR; import org.lwjgl.vulkan.VkQueue; @@ -184,8 +180,10 @@ import org.lwjgl.vulkan.VkWriteDescriptorSet; 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.VKConstants; +import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.render.ubo.Ubo; +import com.github.hydos.ginger.engine.vulkan.render.ubo.UboDescriptor; import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; -import com.github.hydos.ginger.engine.vulkan.shaders.VKShaderManager; import com.github.hydos.ginger.engine.vulkan.utils.VKDeviceProperties; import com.github.hydos.ginger.engine.vulkan.utils.VKLoader; import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; @@ -534,7 +532,7 @@ public class VulkanStarter vkGetImageMemoryRequirements(device, depthStencilImage, memReqs); mem_alloc.allocationSize(memReqs.size()); IntBuffer pMemoryTypeIndex = memAllocInt(1); - getMemoryType(physicalDeviceMemoryProperties, memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, pMemoryTypeIndex); + VKMemory.getMemoryType(physicalDeviceMemoryProperties, memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, pMemoryTypeIndex); mem_alloc.memoryTypeIndex(pMemoryTypeIndex.get(0)); memFree(pMemoryTypeIndex); LongBuffer pDepthStencilMem = memAllocLong(1); @@ -657,24 +655,6 @@ public class VulkanStarter { throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); } } - private static boolean getMemoryType(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, int typeBits, int properties, IntBuffer typeIndex) - { - int bits = typeBits; - for (int i = 0; i < 32; i++) - { - if ((bits & 1) == 1) - { - if ((deviceMemoryProperties.memoryTypes(i).propertyFlags() & properties) == properties) - { - typeIndex.put(0, i); - return true; - } - } - bits >>= 1; - } - return false; - } - private static class Vertices { long verticesBuf; @@ -713,7 +693,7 @@ public class VulkanStarter vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); memAlloc.allocationSize(memReqs.size()); IntBuffer memoryTypeIndex = memAllocInt(1); - getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); + VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); memAlloc.memoryTypeIndex(memoryTypeIndex.get(0)); memFree(memoryTypeIndex); memReqs.free(); @@ -798,65 +778,6 @@ public class VulkanStarter return descriptorPool; } - private static class UboDescriptor - { - long memory; - long buffer; - long offset; - long range; - } - - private static UboDescriptor createUniformBuffer(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) - { - int err; - // Create a new buffer - VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) - .size(16 * 4) - .usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); - LongBuffer pUniformDataVSBuffer = memAllocLong(1); - err = vkCreateBuffer(device, bufferInfo, null, pUniformDataVSBuffer); - long uniformDataVSBuffer = pUniformDataVSBuffer.get(0); - memFree(pUniformDataVSBuffer); - bufferInfo.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create UBO buffer: " + VKUtils.translateVulkanResult(err)); } - // Get memory requirements including size, alignment and memory type - VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); - vkGetBufferMemoryRequirements(device, uniformDataVSBuffer, memReqs); - long memSize = memReqs.size(); - int memoryTypeBits = memReqs.memoryTypeBits(); - memReqs.free(); - // Gets the appropriate memory type for this type of buffer allocation - // Only memory types that are visible to the host - IntBuffer pMemoryTypeIndex = memAllocInt(1); - getMemoryType(deviceMemoryProperties, memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, pMemoryTypeIndex); - int memoryTypeIndex = pMemoryTypeIndex.get(0); - memFree(pMemoryTypeIndex); - // Allocate memory for the uniform buffer - LongBuffer pUniformDataVSMemory = memAllocLong(1); - VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO) - .allocationSize(memSize) - .memoryTypeIndex(memoryTypeIndex); - err = vkAllocateMemory(device, allocInfo, null, pUniformDataVSMemory); - long uniformDataVSMemory = pUniformDataVSMemory.get(0); - memFree(pUniformDataVSMemory); - allocInfo.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to allocate UBO memory: " + VKUtils.translateVulkanResult(err)); } - // Bind memory to buffer - err = vkBindBufferMemory(device, uniformDataVSBuffer, uniformDataVSMemory, 0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to bind UBO memory: " + VKUtils.translateVulkanResult(err)); } - UboDescriptor ret = new UboDescriptor(); - ret.memory = uniformDataVSMemory; - ret.buffer = uniformDataVSBuffer; - ret.offset = 0L; - ret.range = 16 * 4; - return ret; - } - private static long createDescriptorSet(VkDevice device, long descriptorPool, long descriptorSetLayout, UboDescriptor uniformDataVSDescriptor) { LongBuffer pDescriptorSetLayout = memAllocLong(1); @@ -918,26 +839,6 @@ public class VulkanStarter return descriptorSetLayout; } - private static void updateUbo(VkDevice device, UboDescriptor ubo, float angle) - { //a UBO is a uniform buffer object - Matrix4f m = new Matrix4f() - .scale(1, -1, 1) // <- correcting viewport transformation (what Direct3D does, too) - .perspective((float) Math.toRadians(45.0f), (float) Window.getWidth() / Window.getHeight(), 0.1f, 10.0f, true) - .lookAt(0, 1, 3, - 0, 0, 0, - 0, 1, 0) - .rotateY(angle); - PointerBuffer pData = memAllocPointer(1); - int err = vkMapMemory(device, ubo.memory, 0, 16 * 4, 0, pData); - long data = pData.get(0); - memFree(pData); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to map UBO memory: " + VKUtils.translateVulkanResult(err)); } - ByteBuffer matrixBuffer = memByteBuffer(data, 16 * 4); - m.get(matrixBuffer); - vkUnmapMemory(device, ubo.memory); - } - /* * All resources that must be reallocated on window resize. */ @@ -986,10 +887,10 @@ public class VulkanStarter final long renderPass = createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat); final long renderCommandPool = createCommandPool(device, queueFamilyIndex); Vertices vertices = createVertices(memoryProperties, device); - UboDescriptor uboDescriptor = createUniformBuffer(memoryProperties, device); + Ubo ubo = new Ubo(memoryProperties, device); final long descriptorPool = createDescriptorPool(device); final long descriptorSetLayout = createDescriptorSetLayout(device); - final long descriptorSet = createDescriptorSet(device, descriptorPool, descriptorSetLayout, uboDescriptor); + final long descriptorSet = createDescriptorSet(device, descriptorPool, descriptorSetLayout, ubo.uboData); final Pipeline pipeline = Pipeline.createPipeline(device, renderPass, vertices.createInfo, descriptorSetLayout); final class SwapchainRecreator { @@ -1098,7 +999,7 @@ public class VulkanStarter long thisTime = System.nanoTime(); time += (thisTime - lastTime) / 1E9f; lastTime = thisTime; - updateUbo(device, uboDescriptor, time); + ubo.updateUbo(device, time); // Submit to the graphics queue err = vkQueueSubmit(queue, submitInfo, VK_NULL_HANDLE); if (err != VK_SUCCESS) diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/memory/VKMemory.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/memory/VKMemory.java new file mode 100644 index 0000000..4d1d0fd --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/memory/VKMemory.java @@ -0,0 +1,28 @@ +package com.github.hydos.ginger.engine.vulkan.memory; + +import java.nio.IntBuffer; + +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; + +public class VKMemory { + + public static boolean getMemoryType(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, int typeBits, int properties, IntBuffer typeIndex) + { + int bits = typeBits; + for (int i = 0; i < 32; i++) + { + if ((bits & 1) == 1) + { + if ((deviceMemoryProperties.memoryTypes(i).propertyFlags() & properties) == properties) + { + typeIndex.put(0, i); + return true; + } + } + bits >>= 1; + } + return false; + } + + +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java new file mode 100644 index 0000000..317d1fb --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java @@ -0,0 +1,116 @@ +package com.github.hydos.ginger.engine.vulkan.render.ubo; + +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memAllocPointer; +import static org.lwjgl.system.MemoryUtil.memByteBuffer; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; +import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; +import static org.lwjgl.vulkan.VK10.VK_SUCCESS; +import static org.lwjgl.vulkan.VK10.vkAllocateMemory; +import static org.lwjgl.vulkan.VK10.vkBindBufferMemory; +import static org.lwjgl.vulkan.VK10.vkCreateBuffer; +import static org.lwjgl.vulkan.VK10.vkGetBufferMemoryRequirements; +import static org.lwjgl.vulkan.VK10.vkMapMemory; +import static org.lwjgl.vulkan.VK10.vkUnmapMemory; + +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.joml.Matrix4f; +import org.lwjgl.PointerBuffer; +import org.lwjgl.vulkan.VkBufferCreateInfo; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkMemoryAllocateInfo; +import org.lwjgl.vulkan.VkMemoryRequirements; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; + +import com.github.hydos.ginger.engine.common.io.Window; +import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; +/** + * A UBO is a uniform buffer object + * i believe its used to give data from code to the shaders + * @author hydos + * + */ +public class Ubo { + + public UboDescriptor uboData; + + public Ubo(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) + { + int err; + // Create a new buffer + VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) + .size(16 * 4) + .usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + LongBuffer pUniformDataVSBuffer = memAllocLong(1); + err = vkCreateBuffer(device, bufferInfo, null, pUniformDataVSBuffer); + long uniformDataVSBuffer = pUniformDataVSBuffer.get(0); + memFree(pUniformDataVSBuffer); + bufferInfo.free(); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to create UBO buffer: " + VKUtils.translateVulkanResult(err)); } + // Get memory requirements including size, alignment and memory type + VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); + vkGetBufferMemoryRequirements(device, uniformDataVSBuffer, memReqs); + long memSize = memReqs.size(); + int memoryTypeBits = memReqs.memoryTypeBits(); + memReqs.free(); + // Gets the appropriate memory type for this type of buffer allocation + // Only memory types that are visible to the host + IntBuffer pMemoryTypeIndex = memAllocInt(1); + VKMemory.getMemoryType(deviceMemoryProperties, memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, pMemoryTypeIndex); + int memoryTypeIndex = pMemoryTypeIndex.get(0); + memFree(pMemoryTypeIndex); + // Allocate memory for the uniform buffer + LongBuffer pUniformDataVSMemory = memAllocLong(1); + VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.calloc() + .sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO) + .allocationSize(memSize) + .memoryTypeIndex(memoryTypeIndex); + err = vkAllocateMemory(device, allocInfo, null, pUniformDataVSMemory); + long uniformDataVSMemory = pUniformDataVSMemory.get(0); + memFree(pUniformDataVSMemory); + allocInfo.free(); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to allocate UBO memory: " + VKUtils.translateVulkanResult(err)); } + // Bind memory to buffer + err = vkBindBufferMemory(device, uniformDataVSBuffer, uniformDataVSMemory, 0); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to bind UBO memory: " + VKUtils.translateVulkanResult(err)); } + UboDescriptor ret = new UboDescriptor(); + ret.memory = uniformDataVSMemory; + ret.buffer = uniformDataVSBuffer; + ret.offset = 0L; + ret.range = 16 * 4; + this.uboData = ret; + } + + public void updateUbo(VkDevice device, float angle) + { + Matrix4f m = new Matrix4f() + .scale(1, -1, 1) // <- correcting viewport transformation (what Direct3D does, too) + .perspective((float) Math.toRadians(45.0f), (float) Window.getWidth() / Window.getHeight(), 0.1f, 10.0f, true) + .lookAt(0, 1, 3, + 0, 0, 0, + 0, 1, 0) + .rotateY(angle); + PointerBuffer pData = memAllocPointer(1); + int err = vkMapMemory(device, uboData.memory, 0, 16 * 4, 0, pData); + long data = pData.get(0); + memFree(pData); + if (err != VK_SUCCESS) + { throw new AssertionError("Failed to map UBO memory: " + VKUtils.translateVulkanResult(err)); } + ByteBuffer matrixBuffer = memByteBuffer(data, 16 * 4); + m.get(matrixBuffer); + vkUnmapMemory(device, uboData.memory); + } + +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/UboDescriptor.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/UboDescriptor.java new file mode 100644 index 0000000..83f0ee0 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/UboDescriptor.java @@ -0,0 +1,9 @@ +package com.github.hydos.ginger.engine.vulkan.render.ubo; + +public class UboDescriptor +{ + public long memory; //Memory location + public long buffer; //the Buffer? + public long offset; //some offset from the memory location + public long range; // the size +} \ No newline at end of file From f151525643ff2485b0c60bf0cfea54bc652e522e Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Mon, 2 Mar 2020 18:48:09 -0800 Subject: [PATCH 3/8] Organize your god damn imports @hYdos! --- .../litecraft/screens/ExitGameScreen.java | 4 +- .../litecraft/screens/IngameHUD.java | 2 +- .../halotroop/litecraft/world/World.java | 7 +- .../litecraft/world/dimension/Dimension.java | 2 +- .../litecraft/world/dimension/Dimensions.java | 2 +- .../litecraft/world/gen/ChunkGenerator.java | 3 +- .../world/gen/EarthChunkGenerator.java | 3 +- .../world/gen/modifier/CavesModifier.java | 2 +- .../github/hydos/ginger/VulkanStarter.java | 189 ++---------------- .../ginger/engine/opengl/utils/GlLoader.java | 2 +- .../ginger/engine/vulkan/render/ubo/Ubo.java | 28 +-- .../engine/vulkan/shaders/Pipeline.java | 47 +---- .../vulkan/shaders/VKShaderManager.java | 16 +- .../vulkan/utils/VKDeviceProperties.java | 19 +- .../ginger/engine/vulkan/utils/VKUtils.java | 58 +----- .../java/tk/valoeghese/sod/BinaryData.java | 11 +- .../valoeghese/sod/ByteArrayDataSection.java | 3 +- .../java/tk/valoeghese/sod/DataSection.java | 4 +- .../sod/DoubleArrayDataSection.java | 3 +- .../valoeghese/sod/FloatArrayDataSection.java | 3 +- .../valoeghese/sod/IntArrayDataSection.java | 3 +- .../valoeghese/sod/LongArrayDataSection.java | 3 +- src/main/java/tk/valoeghese/sod/Parser.java | 7 +- .../valoeghese/sod/ShortArrayDataSection.java | 3 +- .../sod/StringArrayDataSection.java | 4 +- src/main/java/tk/valoeghese/sod/TestMain.java | 3 +- 26 files changed, 59 insertions(+), 372 deletions(-) diff --git a/src/main/java/com/github/halotroop/litecraft/screens/ExitGameScreen.java b/src/main/java/com/github/halotroop/litecraft/screens/ExitGameScreen.java index 3b51f10..4e12e6f 100644 --- a/src/main/java/com/github/halotroop/litecraft/screens/ExitGameScreen.java +++ b/src/main/java/com/github/halotroop/litecraft/screens/ExitGameScreen.java @@ -1,11 +1,11 @@ package com.github.halotroop.litecraft.screens; -import org.joml.*; +import org.joml.Vector2f; import com.github.hydos.ginger.engine.common.font.GUIText; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.common.screen.Screen; -import com.github.hydos.ginger.engine.opengl.api.*; +import com.github.hydos.ginger.engine.opengl.api.GingerGL; public class ExitGameScreen extends Screen { diff --git a/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java b/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java index c889012..ed18dde 100644 --- a/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java +++ b/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java @@ -6,7 +6,7 @@ import com.github.halotroop.litecraft.Litecraft; import com.github.hydos.ginger.engine.common.api.GingerRegister; import com.github.hydos.ginger.engine.common.font.GUIText; import com.github.hydos.ginger.engine.common.screen.Screen; -import com.github.hydos.ginger.engine.opengl.api.*; +import com.github.hydos.ginger.engine.opengl.api.GingerGL; public class IngameHUD extends Screen { diff --git a/src/main/java/com/github/halotroop/litecraft/world/World.java b/src/main/java/com/github/halotroop/litecraft/world/World.java index 5fa5f60..77b7aa7 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/World.java +++ b/src/main/java/com/github/halotroop/litecraft/world/World.java @@ -1,10 +1,7 @@ package com.github.halotroop.litecraft.world; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ForkJoinPool; +import java.util.*; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.LongConsumer; diff --git a/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java index 9739796..23c0cea 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java +++ b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java @@ -2,7 +2,7 @@ package com.github.halotroop.litecraft.world.dimension; import java.util.*; -import com.github.halotroop.litecraft.world.gen.*; +import com.github.halotroop.litecraft.world.gen.ChunkGenerator; import com.github.halotroop.litecraft.world.gen.modifier.WorldModifier; import it.unimi.dsi.fastutil.ints.*; diff --git a/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java index 87c9939..2876d01 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java +++ b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java @@ -1,6 +1,6 @@ package com.github.halotroop.litecraft.world.dimension; -import com.github.halotroop.litecraft.world.gen.*; +import com.github.halotroop.litecraft.world.gen.EarthChunkGenerator; import com.github.halotroop.litecraft.world.gen.modifier.CavesModifier; public final class Dimensions diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java b/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java index 3903540..fbd76ff 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java @@ -1,7 +1,6 @@ package com.github.halotroop.litecraft.world.gen; -import com.github.halotroop.litecraft.world.Chunk; -import com.github.halotroop.litecraft.world.World; +import com.github.halotroop.litecraft.world.*; public interface ChunkGenerator { diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java b/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java index 4e1d04a..1c4d0e6 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java @@ -4,8 +4,7 @@ import java.util.Random; import com.github.halotroop.litecraft.types.block.*; import com.github.halotroop.litecraft.util.noise.OctaveSimplexNoise; -import com.github.halotroop.litecraft.world.Chunk; -import com.github.halotroop.litecraft.world.World; +import com.github.halotroop.litecraft.world.*; public class EarthChunkGenerator implements ChunkGenerator, WorldGenConstants { diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/modifier/CavesModifier.java b/src/main/java/com/github/halotroop/litecraft/world/gen/modifier/CavesModifier.java index 4bacc16..83f71d4 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/modifier/CavesModifier.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/modifier/CavesModifier.java @@ -2,7 +2,7 @@ package com.github.halotroop.litecraft.world.gen.modifier; import java.util.Random; -import com.github.halotroop.litecraft.types.block.*; +import com.github.halotroop.litecraft.types.block.Blocks; import com.github.halotroop.litecraft.util.noise.OctaveSimplexNoise; import com.github.halotroop.litecraft.world.BlockAccess; import com.github.halotroop.litecraft.world.gen.WorldGenConstants; diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index f6406a6..9fe23cc 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -1,192 +1,27 @@ package com.github.hydos.ginger; -import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE; -import static org.lwjgl.glfw.GLFW.GLFW_RELEASE; -import static org.lwjgl.glfw.GLFW.glfwDestroyWindow; -import static org.lwjgl.glfw.GLFW.glfwPollEvents; -import static org.lwjgl.glfw.GLFW.glfwSetFramebufferSizeCallback; -import static org.lwjgl.glfw.GLFW.glfwSetKeyCallback; -import static org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose; -import static org.lwjgl.glfw.GLFW.glfwShowWindow; -import static org.lwjgl.glfw.GLFW.glfwTerminate; -import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose; -import static org.lwjgl.glfw.GLFWVulkan.glfwCreateWindowSurface; -import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions; -import static org.lwjgl.system.MemoryUtil.NULL; -import static org.lwjgl.system.MemoryUtil.memAddress; -import static org.lwjgl.system.MemoryUtil.memAlloc; -import static org.lwjgl.system.MemoryUtil.memAllocInt; -import static org.lwjgl.system.MemoryUtil.memAllocLong; -import static org.lwjgl.system.MemoryUtil.memAllocPointer; -import static org.lwjgl.system.MemoryUtil.memByteBuffer; -import static org.lwjgl.system.MemoryUtil.memCopy; -import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.vulkan.EXTDebugReport.VK_DEBUG_REPORT_ERROR_BIT_EXT; -import static org.lwjgl.vulkan.EXTDebugReport.VK_DEBUG_REPORT_WARNING_BIT_EXT; -import static org.lwjgl.vulkan.EXTDebugReport.vkDestroyDebugReportCallbackEXT; -import static org.lwjgl.vulkan.KHRSurface.VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; -import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_FIFO_KHR; -import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_IMMEDIATE_KHR; -import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_MAILBOX_KHR; -import static org.lwjgl.vulkan.KHRSurface.VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; -import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR; -import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR; -import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR; -import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR; -import static org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; -import static org.lwjgl.vulkan.KHRSwapchain.VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; -import static org.lwjgl.vulkan.KHRSwapchain.VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; -import static org.lwjgl.vulkan.KHRSwapchain.vkAcquireNextImageKHR; -import static org.lwjgl.vulkan.KHRSwapchain.vkCreateSwapchainKHR; -import static org.lwjgl.vulkan.KHRSwapchain.vkDestroySwapchainKHR; -import static org.lwjgl.vulkan.KHRSwapchain.vkGetSwapchainImagesKHR; -import static org.lwjgl.vulkan.KHRSwapchain.vkQueuePresentKHR; -import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_LOAD_OP_CLEAR; -import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_LOAD_OP_DONT_CARE; -import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_STORE_OP_DONT_CARE; -import static org.lwjgl.vulkan.VK10.VK_ATTACHMENT_STORE_OP_STORE; -import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; -import static org.lwjgl.vulkan.VK10.VK_COMMAND_BUFFER_LEVEL_PRIMARY; -import static org.lwjgl.vulkan.VK10.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; -import static org.lwjgl.vulkan.VK10.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_B8G8R8A8_UNORM; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_D16_UNORM; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_D16_UNORM_S8_UINT; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_D24_UNORM_S8_UINT; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_D32_SFLOAT; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_D32_SFLOAT_S8_UINT; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_R32G32B32_SFLOAT; -import static org.lwjgl.vulkan.VK10.VK_FORMAT_UNDEFINED; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_ASPECT_COLOR_BIT; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_ASPECT_DEPTH_BIT; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_ASPECT_STENCIL_BIT; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_LAYOUT_UNDEFINED; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_TILING_OPTIMAL; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_TYPE_2D; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_USAGE_TRANSFER_SRC_BIT; -import static org.lwjgl.vulkan.VK10.VK_IMAGE_VIEW_TYPE_2D; -import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; -import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; -import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE; -import static org.lwjgl.vulkan.VK10.VK_PIPELINE_BIND_POINT_GRAPHICS; -import static org.lwjgl.vulkan.VK10.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; -import static org.lwjgl.vulkan.VK10.VK_QUEUE_GRAPHICS_BIT; -import static org.lwjgl.vulkan.VK10.VK_SAMPLE_COUNT_1_BIT; -import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_VERTEX_BIT; -import static org.lwjgl.vulkan.VK10.VK_SHARING_MODE_EXCLUSIVE; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SUBMIT_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; -import static org.lwjgl.vulkan.VK10.VK_SUCCESS; -import static org.lwjgl.vulkan.VK10.VK_TRUE; -import static org.lwjgl.vulkan.VK10.VK_VERTEX_INPUT_RATE_VERTEX; -import static org.lwjgl.vulkan.VK10.vkAllocateCommandBuffers; -import static org.lwjgl.vulkan.VK10.vkAllocateDescriptorSets; -import static org.lwjgl.vulkan.VK10.vkAllocateMemory; -import static org.lwjgl.vulkan.VK10.vkBeginCommandBuffer; -import static org.lwjgl.vulkan.VK10.vkBindBufferMemory; -import static org.lwjgl.vulkan.VK10.vkBindImageMemory; -import static org.lwjgl.vulkan.VK10.vkCreateBuffer; -import static org.lwjgl.vulkan.VK10.vkCreateCommandPool; -import static org.lwjgl.vulkan.VK10.vkCreateDescriptorPool; -import static org.lwjgl.vulkan.VK10.vkCreateDescriptorSetLayout; -import static org.lwjgl.vulkan.VK10.vkCreateFramebuffer; -import static org.lwjgl.vulkan.VK10.vkCreateImage; -import static org.lwjgl.vulkan.VK10.vkCreateImageView; -import static org.lwjgl.vulkan.VK10.vkCreateRenderPass; -import static org.lwjgl.vulkan.VK10.vkCreateSemaphore; -import static org.lwjgl.vulkan.VK10.vkDestroyFramebuffer; -import static org.lwjgl.vulkan.VK10.vkDestroySemaphore; -import static org.lwjgl.vulkan.VK10.vkEndCommandBuffer; -import static org.lwjgl.vulkan.VK10.vkGetBufferMemoryRequirements; -import static org.lwjgl.vulkan.VK10.vkGetDeviceQueue; -import static org.lwjgl.vulkan.VK10.vkGetImageMemoryRequirements; -import static org.lwjgl.vulkan.VK10.vkGetPhysicalDeviceFormatProperties; -import static org.lwjgl.vulkan.VK10.vkGetPhysicalDeviceQueueFamilyProperties; -import static org.lwjgl.vulkan.VK10.vkMapMemory; -import static org.lwjgl.vulkan.VK10.vkQueueSubmit; -import static org.lwjgl.vulkan.VK10.vkQueueWaitIdle; -import static org.lwjgl.vulkan.VK10.vkResetCommandPool; -import static org.lwjgl.vulkan.VK10.vkUnmapMemory; -import static org.lwjgl.vulkan.VK10.vkUpdateDescriptorSets; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.glfw.GLFWVulkan.*; +import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.vulkan.EXTDebugReport.*; +import static org.lwjgl.vulkan.KHRSurface.*; +import static org.lwjgl.vulkan.KHRSwapchain.*; +import static org.lwjgl.vulkan.VK10.*; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.LongBuffer; +import java.nio.*; -import org.joml.Matrix4f; import org.lwjgl.PointerBuffer; -import org.lwjgl.glfw.GLFWFramebufferSizeCallback; -import org.lwjgl.glfw.GLFWKeyCallback; -import org.lwjgl.vulkan.VkAttachmentDescription; -import org.lwjgl.vulkan.VkAttachmentReference; -import org.lwjgl.vulkan.VkBufferCreateInfo; -import org.lwjgl.vulkan.VkCommandBuffer; -import org.lwjgl.vulkan.VkCommandBufferAllocateInfo; -import org.lwjgl.vulkan.VkCommandBufferBeginInfo; -import org.lwjgl.vulkan.VkCommandPoolCreateInfo; -import org.lwjgl.vulkan.VkDescriptorBufferInfo; -import org.lwjgl.vulkan.VkDescriptorPoolCreateInfo; -import org.lwjgl.vulkan.VkDescriptorPoolSize; -import org.lwjgl.vulkan.VkDescriptorSetAllocateInfo; -import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding; -import org.lwjgl.vulkan.VkDescriptorSetLayoutCreateInfo; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkExtent2D; -import org.lwjgl.vulkan.VkFormatProperties; -import org.lwjgl.vulkan.VkFramebufferCreateInfo; -import org.lwjgl.vulkan.VkImageCreateInfo; -import org.lwjgl.vulkan.VkImageViewCreateInfo; -import org.lwjgl.vulkan.VkInstance; -import org.lwjgl.vulkan.VkMemoryAllocateInfo; -import org.lwjgl.vulkan.VkMemoryRequirements; -import org.lwjgl.vulkan.VkPhysicalDevice; -import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; -import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; -import org.lwjgl.vulkan.VkPresentInfoKHR; -import org.lwjgl.vulkan.VkQueue; -import org.lwjgl.vulkan.VkQueueFamilyProperties; -import org.lwjgl.vulkan.VkRenderPassCreateInfo; -import org.lwjgl.vulkan.VkSemaphoreCreateInfo; -import org.lwjgl.vulkan.VkSubmitInfo; -import org.lwjgl.vulkan.VkSubpassDescription; -import org.lwjgl.vulkan.VkSurfaceCapabilitiesKHR; -import org.lwjgl.vulkan.VkSurfaceFormatKHR; -import org.lwjgl.vulkan.VkSwapchainCreateInfoKHR; -import org.lwjgl.vulkan.VkVertexInputAttributeDescription; -import org.lwjgl.vulkan.VkVertexInputBindingDescription; -import org.lwjgl.vulkan.VkWriteDescriptorSet; +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.VKConstants; import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; -import com.github.hydos.ginger.engine.vulkan.render.ubo.Ubo; -import com.github.hydos.ginger.engine.vulkan.render.ubo.UboDescriptor; +import com.github.hydos.ginger.engine.vulkan.render.ubo.*; import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; -import com.github.hydos.ginger.engine.vulkan.utils.VKDeviceProperties; -import com.github.hydos.ginger.engine.vulkan.utils.VKLoader; -import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; +import com.github.hydos.ginger.engine.vulkan.utils.*; /** * * @author hydos06 diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java b/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java index ed35c8a..c741fb7 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java @@ -9,7 +9,7 @@ import org.lwjgl.opengl.*; import com.github.halotroop.litecraft.types.block.*; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; -import com.github.hydos.ginger.engine.opengl.render.texture.*; +import com.github.hydos.ginger.engine.opengl.render.texture.Image; public class GlLoader { diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java index 317d1fb..fe34629 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/ubo/Ubo.java @@ -1,33 +1,13 @@ package com.github.hydos.ginger.engine.vulkan.render.ubo; -import static org.lwjgl.system.MemoryUtil.memAllocInt; -import static org.lwjgl.system.MemoryUtil.memAllocLong; -import static org.lwjgl.system.MemoryUtil.memAllocPointer; -import static org.lwjgl.system.MemoryUtil.memByteBuffer; -import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; -import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_SUCCESS; -import static org.lwjgl.vulkan.VK10.vkAllocateMemory; -import static org.lwjgl.vulkan.VK10.vkBindBufferMemory; -import static org.lwjgl.vulkan.VK10.vkCreateBuffer; -import static org.lwjgl.vulkan.VK10.vkGetBufferMemoryRequirements; -import static org.lwjgl.vulkan.VK10.vkMapMemory; -import static org.lwjgl.vulkan.VK10.vkUnmapMemory; +import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.vulkan.VK10.*; -import java.nio.ByteBuffer; -import java.nio.IntBuffer; -import java.nio.LongBuffer; +import java.nio.*; import org.joml.Matrix4f; import org.lwjgl.PointerBuffer; -import org.lwjgl.vulkan.VkBufferCreateInfo; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkMemoryAllocateInfo; -import org.lwjgl.vulkan.VkMemoryRequirements; -import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.*; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java index ee04e31..4a424d7 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/Pipeline.java @@ -1,51 +1,12 @@ package com.github.hydos.ginger.engine.vulkan.shaders; -import static org.lwjgl.system.MemoryUtil.memAllocInt; -import static org.lwjgl.system.MemoryUtil.memAllocLong; -import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.vulkan.VK10.VK_COMPARE_OP_ALWAYS; -import static org.lwjgl.vulkan.VK10.VK_COMPARE_OP_LESS_OR_EQUAL; -import static org.lwjgl.vulkan.VK10.VK_CULL_MODE_NONE; -import static org.lwjgl.vulkan.VK10.VK_DYNAMIC_STATE_SCISSOR; -import static org.lwjgl.vulkan.VK10.VK_DYNAMIC_STATE_VIEWPORT; -import static org.lwjgl.vulkan.VK10.VK_FRONT_FACE_COUNTER_CLOCKWISE; -import static org.lwjgl.vulkan.VK10.VK_NULL_HANDLE; -import static org.lwjgl.vulkan.VK10.VK_POLYGON_MODE_FILL; -import static org.lwjgl.vulkan.VK10.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; -import static org.lwjgl.vulkan.VK10.VK_SAMPLE_COUNT_1_BIT; -import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_FRAGMENT_BIT; -import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_VERTEX_BIT; -import static org.lwjgl.vulkan.VK10.VK_STENCIL_OP_KEEP; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_SUCCESS; -import static org.lwjgl.vulkan.VK10.vkCreateGraphicsPipelines; -import static org.lwjgl.vulkan.VK10.vkCreatePipelineLayout; +import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.vulkan.VK10.*; import java.io.IOException; -import java.nio.IntBuffer; -import java.nio.LongBuffer; +import java.nio.*; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkGraphicsPipelineCreateInfo; -import org.lwjgl.vulkan.VkPipelineColorBlendAttachmentState; -import org.lwjgl.vulkan.VkPipelineColorBlendStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineDepthStencilStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineDynamicStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineInputAssemblyStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineLayoutCreateInfo; -import org.lwjgl.vulkan.VkPipelineMultisampleStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineRasterizationStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo; -import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; -import org.lwjgl.vulkan.VkPipelineViewportStateCreateInfo; +import org.lwjgl.vulkan.*; import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java index 51e4b63..8e6a74f 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/VKShaderManager.java @@ -1,20 +1,12 @@ package com.github.hydos.ginger.engine.vulkan.shaders; -import static org.lwjgl.system.MemoryUtil.memAllocLong; -import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.system.MemoryUtil.memUTF8; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; -import static org.lwjgl.vulkan.VK10.VK_SUCCESS; -import static org.lwjgl.vulkan.VK10.vkCreateShaderModule; +import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.vulkan.VK10.*; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.LongBuffer; +import java.nio.*; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkPipelineShaderStageCreateInfo; -import org.lwjgl.vulkan.VkShaderModuleCreateInfo; +import org.lwjgl.vulkan.*; import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java index d9f0c16..0c50ae1 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/utils/VKDeviceProperties.java @@ -1,25 +1,12 @@ package com.github.hydos.ginger.engine.vulkan.utils; -import static org.lwjgl.system.MemoryUtil.memAllocFloat; -import static org.lwjgl.system.MemoryUtil.memAllocInt; -import static org.lwjgl.system.MemoryUtil.memAllocPointer; -import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.system.MemoryUtil.memUTF8; +import static org.lwjgl.system.MemoryUtil.*; import static org.lwjgl.vulkan.KHRSwapchain.VK_KHR_SWAPCHAIN_EXTENSION_NAME; -import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; +import java.nio.*; import org.lwjgl.PointerBuffer; -import org.lwjgl.vulkan.VK12; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkDeviceCreateInfo; -import org.lwjgl.vulkan.VkDeviceQueueCreateInfo; -import org.lwjgl.vulkan.VkInstance; -import org.lwjgl.vulkan.VkPhysicalDevice; -import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; -import org.lwjgl.vulkan.VkQueueFamilyProperties; +import org.lwjgl.vulkan.*; import com.github.hydos.ginger.engine.vulkan.VKConstants; 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 f11cc66..1582261 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 @@ -1,59 +1,17 @@ package com.github.hydos.ginger.engine.vulkan.utils; -import static org.lwjgl.system.MemoryUtil.memAllocLong; -import static org.lwjgl.system.MemoryUtil.memAllocPointer; -import static org.lwjgl.system.MemoryUtil.memFree; -import static org.lwjgl.system.MemoryUtil.memUTF8; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_anyhit_shader; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_closesthit_shader; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compilation_status_success; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_into_spv; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_initialize; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_set_include_callbacks; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_set_optimization_level; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compiler_initialize; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_compiler_release; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_fragment_shader; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_miss_shader; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_optimization_level_performance; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_raygen_shader; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_bytes; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_compilation_status; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_error_message; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_result_get_length; -import static org.lwjgl.util.shaderc.Shaderc.shaderc_vertex_shader; -import static org.lwjgl.vulkan.EXTDebugReport.VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; -import static org.lwjgl.vulkan.EXTDebugReport.vkCreateDebugReportCallbackEXT; -import static org.lwjgl.vulkan.KHRSurface.VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; -import static org.lwjgl.vulkan.KHRSurface.VK_ERROR_SURFACE_LOST_KHR; +import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.util.shaderc.Shaderc.*; +import static org.lwjgl.vulkan.EXTDebugReport.*; +import static org.lwjgl.vulkan.KHRSurface.*; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.LongBuffer; +import java.nio.*; -import org.lwjgl.BufferUtils; -import org.lwjgl.PointerBuffer; +import org.lwjgl.*; import org.lwjgl.system.MemoryStack; -import org.lwjgl.util.shaderc.ShadercIncludeResolve; -import org.lwjgl.util.shaderc.ShadercIncludeResult; -import org.lwjgl.util.shaderc.ShadercIncludeResultRelease; -import org.lwjgl.vulkan.EXTDebugReport; -import org.lwjgl.vulkan.KHRDisplaySwapchain; -import org.lwjgl.vulkan.KHRSwapchain; -import org.lwjgl.vulkan.NVRayTracing; -import org.lwjgl.vulkan.VK10; -import org.lwjgl.vulkan.VK12; -import org.lwjgl.vulkan.VkClearValue; -import org.lwjgl.vulkan.VkCommandBuffer; -import org.lwjgl.vulkan.VkCommandBufferAllocateInfo; -import org.lwjgl.vulkan.VkCommandBufferBeginInfo; -import org.lwjgl.vulkan.VkDebugReportCallbackCreateInfoEXT; -import org.lwjgl.vulkan.VkDebugReportCallbackEXT; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkInstance; -import org.lwjgl.vulkan.VkRect2D; -import org.lwjgl.vulkan.VkRenderPassBeginInfo; -import org.lwjgl.vulkan.VkViewport; +import org.lwjgl.util.shaderc.*; +import org.lwjgl.vulkan.*; import com.github.hydos.ginger.engine.common.tools.IOUtil; import com.github.hydos.ginger.engine.vulkan.VKConstants; diff --git a/src/main/java/tk/valoeghese/sod/BinaryData.java b/src/main/java/tk/valoeghese/sod/BinaryData.java index 9c7ef29..a391cff 100644 --- a/src/main/java/tk/valoeghese/sod/BinaryData.java +++ b/src/main/java/tk/valoeghese/sod/BinaryData.java @@ -1,14 +1,7 @@ package tk.valoeghese.sod; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; +import java.io.*; +import java.util.*; import tk.valoeghese.sod.exception.SODParseException; diff --git a/src/main/java/tk/valoeghese/sod/ByteArrayDataSection.java b/src/main/java/tk/valoeghese/sod/ByteArrayDataSection.java index 3fcbc22..5ac7ecd 100644 --- a/src/main/java/tk/valoeghese/sod/ByteArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/ByteArrayDataSection.java @@ -2,8 +2,7 @@ package tk.valoeghese.sod; import java.util.Iterator; -import it.unimi.dsi.fastutil.bytes.ByteArrayList; -import it.unimi.dsi.fastutil.bytes.ByteList; +import it.unimi.dsi.fastutil.bytes.*; public class ByteArrayDataSection implements BaseDataSection { public ByteArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/DataSection.java b/src/main/java/tk/valoeghese/sod/DataSection.java index 34c1084..55a1336 100644 --- a/src/main/java/tk/valoeghese/sod/DataSection.java +++ b/src/main/java/tk/valoeghese/sod/DataSection.java @@ -1,8 +1,6 @@ package tk.valoeghese.sod; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; +import java.util.*; /** * Represents a section of SOD data. diff --git a/src/main/java/tk/valoeghese/sod/DoubleArrayDataSection.java b/src/main/java/tk/valoeghese/sod/DoubleArrayDataSection.java index 9fca1ad..dda91f6 100644 --- a/src/main/java/tk/valoeghese/sod/DoubleArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/DoubleArrayDataSection.java @@ -2,8 +2,7 @@ package tk.valoeghese.sod; import java.util.Iterator; -import it.unimi.dsi.fastutil.doubles.DoubleArrayList; -import it.unimi.dsi.fastutil.doubles.DoubleList; +import it.unimi.dsi.fastutil.doubles.*; public class DoubleArrayDataSection implements BaseDataSection { public DoubleArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/FloatArrayDataSection.java b/src/main/java/tk/valoeghese/sod/FloatArrayDataSection.java index 2afe075..ebc4497 100644 --- a/src/main/java/tk/valoeghese/sod/FloatArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/FloatArrayDataSection.java @@ -2,8 +2,7 @@ package tk.valoeghese.sod; import java.util.Iterator; -import it.unimi.dsi.fastutil.floats.FloatArrayList; -import it.unimi.dsi.fastutil.floats.FloatList; +import it.unimi.dsi.fastutil.floats.*; public class FloatArrayDataSection implements BaseDataSection { public FloatArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/IntArrayDataSection.java b/src/main/java/tk/valoeghese/sod/IntArrayDataSection.java index 77a2be6..8072118 100644 --- a/src/main/java/tk/valoeghese/sod/IntArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/IntArrayDataSection.java @@ -2,8 +2,7 @@ package tk.valoeghese.sod; import java.util.Iterator; -import it.unimi.dsi.fastutil.ints.IntArrayList; -import it.unimi.dsi.fastutil.ints.IntList; +import it.unimi.dsi.fastutil.ints.*; public class IntArrayDataSection implements BaseDataSection { public IntArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/LongArrayDataSection.java b/src/main/java/tk/valoeghese/sod/LongArrayDataSection.java index 346d37d..c4044dc 100644 --- a/src/main/java/tk/valoeghese/sod/LongArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/LongArrayDataSection.java @@ -2,8 +2,7 @@ package tk.valoeghese.sod; import java.util.Iterator; -import it.unimi.dsi.fastutil.longs.LongArrayList; -import it.unimi.dsi.fastutil.longs.LongList; +import it.unimi.dsi.fastutil.longs.*; public class LongArrayDataSection implements BaseDataSection { public LongArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/Parser.java b/src/main/java/tk/valoeghese/sod/Parser.java index 327b005..46f9b81 100644 --- a/src/main/java/tk/valoeghese/sod/Parser.java +++ b/src/main/java/tk/valoeghese/sod/Parser.java @@ -1,10 +1,7 @@ package tk.valoeghese.sod; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.Iterator; -import java.util.Map; +import java.io.*; +import java.util.*; import tk.valoeghese.sod.exception.SODParseException; diff --git a/src/main/java/tk/valoeghese/sod/ShortArrayDataSection.java b/src/main/java/tk/valoeghese/sod/ShortArrayDataSection.java index 1e4b2fa..6cc99c4 100644 --- a/src/main/java/tk/valoeghese/sod/ShortArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/ShortArrayDataSection.java @@ -2,8 +2,7 @@ package tk.valoeghese.sod; import java.util.Iterator; -import it.unimi.dsi.fastutil.shorts.ShortArrayList; -import it.unimi.dsi.fastutil.shorts.ShortList; +import it.unimi.dsi.fastutil.shorts.*; public class ShortArrayDataSection implements BaseDataSection { public ShortArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/StringArrayDataSection.java b/src/main/java/tk/valoeghese/sod/StringArrayDataSection.java index b27c684..d360d4a 100644 --- a/src/main/java/tk/valoeghese/sod/StringArrayDataSection.java +++ b/src/main/java/tk/valoeghese/sod/StringArrayDataSection.java @@ -1,8 +1,6 @@ package tk.valoeghese.sod; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; +import java.util.*; public class StringArrayDataSection implements BaseDataSection { public StringArrayDataSection() { diff --git a/src/main/java/tk/valoeghese/sod/TestMain.java b/src/main/java/tk/valoeghese/sod/TestMain.java index 6bb296a..c9940d6 100644 --- a/src/main/java/tk/valoeghese/sod/TestMain.java +++ b/src/main/java/tk/valoeghese/sod/TestMain.java @@ -1,7 +1,6 @@ package tk.valoeghese.sod; -import java.io.File; -import java.io.IOException; +import java.io.*; class TestMain { From 6f8f1f07cee1424ec7780ff13769a5f2b8ba7539 Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Mon, 2 Mar 2020 19:05:00 -0800 Subject: [PATCH 4/8] Single-line "if"-statements --- .../github/hydos/ginger/VulkanStarter.java | 116 ++++++------------ 1 file changed, 39 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index 9fe23cc..0ae49b1 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -22,6 +22,7 @@ import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; import com.github.hydos.ginger.engine.vulkan.render.ubo.*; import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; import com.github.hydos.ginger.engine.vulkan.utils.*; + /** * * @author hydos06 @@ -129,16 +130,12 @@ public class VulkanStarter err = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, pFormatCount, surfFormats); memFree(pFormatCount); if (err != VK_SUCCESS) - { throw new AssertionError("Failed to query physical device surface formats: " + VKUtils.translateVulkanResult(err)); } + throw new AssertionError("Failed to query physical device surface formats: " + VKUtils.translateVulkanResult(err)); int colorFormat; if (formatCount == 1 && surfFormats.get(0).format() == VK_FORMAT_UNDEFINED) - { colorFormat = VK_FORMAT_B8G8R8A8_UNORM; - } else - { colorFormat = surfFormats.get(0).format(); - } int colorSpace = surfFormats.get(0).colorSpace(); surfFormats.free(); // Find suitable depth format @@ -163,8 +160,7 @@ public class VulkanStarter long commandPool = pCmdPool.get(0); cmdPoolInfo.free(); memFree(pCmdPool); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create command pool: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create command pool: " + VKUtils.translateVulkanResult(err)); return commandPool; } @@ -189,8 +185,7 @@ public class VulkanStarter cmdBufAllocateInfo.free(); long commandBuffer = pCommandBuffer.get(0); memFree(pCommandBuffer); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to allocate command buffer: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to allocate command buffer: " + VKUtils.translateVulkanResult(err)); return new VkCommandBuffer(commandBuffer, device); } @@ -208,18 +203,15 @@ public class VulkanStarter // Get physical device surface properties and formats VkSurfaceCapabilitiesKHR surfCaps = VkSurfaceCapabilitiesKHR.calloc(); err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, surfCaps); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get physical device surface capabilities: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to get physical device surface capabilities: " + VKUtils.translateVulkanResult(err)); IntBuffer pPresentModeCount = memAllocInt(1); err = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount, null); int presentModeCount = pPresentModeCount.get(0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get number of physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to get number of physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); IntBuffer pPresentModes = memAllocInt(presentModeCount); err = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount, pPresentModes); memFree(pPresentModeCount); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to get physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); // Try to use mailbox mode. Low latency and non-tearing int swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; for (int i = 0; i < presentModeCount; i++) @@ -230,13 +222,13 @@ public class VulkanStarter break; } if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (pPresentModes.get(i) == VK_PRESENT_MODE_IMMEDIATE_KHR)) - { swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; } + swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; } memFree(pPresentModes); // Determine the number of images int desiredNumberOfSwapchainImages = surfCaps.minImageCount() + 1; if ((surfCaps.maxImageCount() > 0) && (desiredNumberOfSwapchainImages > surfCaps.maxImageCount())) - { desiredNumberOfSwapchainImages = surfCaps.maxImageCount(); } + desiredNumberOfSwapchainImages = surfCaps.maxImageCount(); VkExtent2D currentExtent = surfCaps.currentExtent(); int currentWidth = currentExtent.width(); int currentHeight = currentExtent.height(); @@ -252,13 +244,8 @@ public class VulkanStarter } int preTransform; if ((surfCaps.supportedTransforms() & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) != 0) - { preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; - } - else - { - preTransform = surfCaps.currentTransform(); - } + else preTransform = surfCaps.currentTransform(); surfCaps.free(); VkSwapchainCreateInfoKHR swapchainCI = VkSwapchainCreateInfoKHR.calloc() .sType(VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR) @@ -282,21 +269,17 @@ public class VulkanStarter swapchainCI.free(); long swapChain = pSwapChain.get(0); memFree(pSwapChain); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create swap chain: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create swap chain: " + VKUtils.translateVulkanResult(err)); // If we just re-created an existing swapchain, we should destroy the old swapchain at this point. // Note: destroying the swapchain also cleans up all its associated presentable images once the platform is done with them. - if (oldSwapChain != VK_NULL_HANDLE) - { vkDestroySwapchainKHR(device, oldSwapChain, null); } + if (oldSwapChain != VK_NULL_HANDLE) vkDestroySwapchainKHR(device, oldSwapChain, null); IntBuffer pImageCount = memAllocInt(1); err = vkGetSwapchainImagesKHR(device, swapChain, pImageCount, null); int imageCount = pImageCount.get(0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get number of swapchain images: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to get number of swapchain images: " + VKUtils.translateVulkanResult(err)); LongBuffer pSwapchainImages = memAllocLong(imageCount); err = vkGetSwapchainImagesKHR(device, swapChain, pImageCount, pSwapchainImages); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to get swapchain images: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to get swapchain images: " + VKUtils.translateVulkanResult(err)); memFree(pImageCount); long[] images = new long[imageCount]; long[] imageViews = new long[imageCount]; @@ -315,8 +298,7 @@ public class VulkanStarter colorAttachmentView.image(images[i]); err = vkCreateImageView(device, colorAttachmentView, null, pBufferView); imageViews[i] = pBufferView.get(0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create image view: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create image view: " + VKUtils.translateVulkanResult(err)); } colorAttachmentView.free(); memFree(pBufferView); @@ -330,6 +312,7 @@ public class VulkanStarter private static class DepthStencil { + // What is this? - Caroline long view; } @@ -362,8 +345,7 @@ public class VulkanStarter long depthStencilImage = pDepthStencilImage.get(0); memFree(pDepthStencilImage); imageCreateInfo.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create depth-stencil image: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create depth-stencil image: " + VKUtils.translateVulkanResult(err)); vkGetImageMemoryRequirements(device, depthStencilImage, memReqs); mem_alloc.allocationSize(memReqs.size()); IntBuffer pMemoryTypeIndex = memAllocInt(1); @@ -375,19 +357,16 @@ public class VulkanStarter long depthStencilMem = pDepthStencilMem.get(0); memFree(pDepthStencilMem); mem_alloc.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create depth-stencil memory: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create depth-stencil memory: " + VKUtils.translateVulkanResult(err)); err = vkBindImageMemory(device, depthStencilImage, depthStencilMem, 0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to bind depth-stencil image to memory: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to bind depth-stencil image to memory: " + VKUtils.translateVulkanResult(err)); depthStencilViewCreateInfo.image(depthStencilImage); LongBuffer pDepthStencilView = memAllocLong(1); err = vkCreateImageView(device, depthStencilViewCreateInfo, null, pDepthStencilView); long depthStencilView = pDepthStencilView.get(0); memFree(pDepthStencilView); depthStencilViewCreateInfo.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create depth-stencil image view: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create depth-stencil image view: " + VKUtils.translateVulkanResult(err)); DepthStencil ret = new DepthStencil(); ret.view = depthStencilView; return ret; @@ -439,8 +418,7 @@ public class VulkanStarter colorReference.free(); subpass.free(); attachments.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create clear render pass: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create clear render pass: " + VKUtils.translateVulkanResult(err)); return renderPass; } @@ -463,8 +441,7 @@ public class VulkanStarter attachments.put(0, swapchain.imageViews[i]); int err = vkCreateFramebuffer(device, fci, null, pFramebuffer); long framebuffer = pFramebuffer.get(0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create framebuffer: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create framebuffer: " + VKUtils.translateVulkanResult(err)); framebuffers[i] = framebuffer; } memFree(attachments); @@ -487,7 +464,7 @@ public class VulkanStarter memFree(pCommandBuffers); submitInfo.free(); if (err != VK_SUCCESS) - { throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); } + throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); } private static class Vertices @@ -523,8 +500,7 @@ public class VulkanStarter long verticesBuf = pBuffer.get(0); memFree(pBuffer); bufInfo.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); memAlloc.allocationSize(memReqs.size()); IntBuffer memoryTypeIndex = memAllocInt(1); @@ -536,21 +512,18 @@ public class VulkanStarter err = vkAllocateMemory(device, memAlloc, null, pMemory); long verticesMem = pMemory.get(0); memFree(pMemory); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); PointerBuffer pData = memAllocPointer(1); err = vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData); memAlloc.free(); long data = pData.get(0); memFree(pData); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining()); memFree(vertexBuffer); vkUnmapMemory(device, verticesMem); err = vkBindBufferMemory(device, verticesBuf, verticesMem, 0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); // Binding description VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1) .binding(0) // <- we bind our vertex buffer to point 0 @@ -608,8 +581,7 @@ public class VulkanStarter memFree(pDescriptorPool); descriptorPoolInfo.free(); typeCounts.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create descriptor pool: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create descriptor pool: " + VKUtils.translateVulkanResult(err)); return descriptorPool; } @@ -627,8 +599,7 @@ public class VulkanStarter memFree(pDescriptorSet); allocInfo.free(); memFree(pDescriptorSetLayout); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create descriptor set: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create descriptor set: " + VKUtils.translateVulkanResult(err)); // Update descriptor sets determining the shader binding points // For every binding point used in a shader there needs to be one // descriptor set matching that binding point @@ -669,8 +640,7 @@ public class VulkanStarter memFree(pDescriptorSetLayout); descriptorLayout.free(); layoutBinding.free(); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create descriptor set layout: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create descriptor set layout: " + VKUtils.translateVulkanResult(err)); return descriptorSetLayout; } @@ -684,7 +654,7 @@ public class VulkanStarter public static void main(String[] args) throws IOException { - Window.create(1200, 600, "Vulkan Ginger3D", 60, RenderAPI.Vulkan); + Window.create(1280, 720, "Vulkan Ginger3D", 60, RenderAPI.Vulkan); /* Look for instance extensions */ PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions(); if (requiredExtensions == null) @@ -756,8 +726,7 @@ public class VulkanStarter vkDestroyFramebuffer(device, framebuffers[i], null); } framebuffers = createFramebuffers(device, swapchain, renderPass, Window.getWidth(), Window.getHeight(), depthStencil); // Create render command buffers - if (renderCommandBuffers != null) - { vkResetCommandPool(device, renderCommandPool, VKUtils.VK_FLAGS_NONE); } + if (renderCommandBuffers != null) vkResetCommandPool(device, renderCommandPool, VKUtils.VK_FLAGS_NONE); renderCommandBuffers = VKUtils.initRenderCommandBuffers(device, renderCommandPool, framebuffers, renderPass, Window.getWidth(), Window.getHeight(), pipeline, descriptorSet, vertices.verticesBuf); mustRecreate = false; @@ -769,8 +738,7 @@ public class VulkanStarter { public void invoke(long window, int width, int height) { - if (width <= 0 || height <= 0) - return; + if (width <= 0 || height <= 0) return; swapchainRecreator.mustRecreate = true; } }; @@ -812,22 +780,18 @@ public class VulkanStarter // Handle window messages. Resize events happen exactly here. // So it is safe to use the new swapchain images and framebuffers afterwards. glfwPollEvents(); - if (swapchainRecreator.mustRecreate) - swapchainRecreator.recreate(); + if (swapchainRecreator.mustRecreate) swapchainRecreator.recreate(); // Create a semaphore to wait for the swapchain to acquire the next image err = vkCreateSemaphore(device, semaphoreCreateInfo, null, pImageAcquiredSemaphore); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create image acquired semaphore: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create image acquired semaphore: " + VKUtils.translateVulkanResult(err)); // Create a semaphore to wait for the render to complete, before presenting err = vkCreateSemaphore(device, semaphoreCreateInfo, null, pRenderCompleteSemaphore); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to create render complete semaphore: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to create render complete semaphore: " + VKUtils.translateVulkanResult(err)); // Get next image from the swap chain (back/front buffer). // This will setup the imageAquiredSemaphore to be signalled when the operation is complete err = vkAcquireNextImageKHR(device, swapchain.swapchainHandle, VKConstants.MAX_UNSIGNED_INT, pImageAcquiredSemaphore.get(0), VK_NULL_HANDLE, pImageIndex); currentBuffer = pImageIndex.get(0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to acquire next swapchain image: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to acquire next swapchain image: " + VKUtils.translateVulkanResult(err)); // Select the command buffer for the current framebuffer image/attachment pCommandBuffers.put(0, renderCommandBuffers[currentBuffer]); // Update UBO @@ -837,14 +801,12 @@ public class VulkanStarter ubo.updateUbo(device, time); // Submit to the graphics queue err = vkQueueSubmit(queue, submitInfo, VK_NULL_HANDLE); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to submit render queue: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to submit render queue: " + VKUtils.translateVulkanResult(err)); // Present the current buffer to the swap chain // This will display the image pSwapchains.put(0, swapchain.swapchainHandle); err = vkQueuePresentKHR(queue, presentInfo); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to present the swapchain image: " + VKUtils.translateVulkanResult(err)); } + if (err != VK_SUCCESS) throw new AssertionError("Failed to present the swapchain image: " + VKUtils.translateVulkanResult(err)); // Create and submit post present barrier vkQueueWaitIdle(queue); // Destroy this semaphore (we will create a new one in the next frame) From 40a9e3d7c7006d509e47fa838703ab776e87fc32 Mon Sep 17 00:00:00 2001 From: hayden v Date: Tue, 3 Mar 2020 14:05:33 +1000 Subject: [PATCH 5/8] even more classes but a much smaller main method --- .../github/hydos/ginger/VulkanStarter.java | 752 +++++------------- .../ginger/engine/vulkan/TempMethods.java | 120 +++ .../ginger/engine/vulkan/api/VKGinger.java | 43 + .../render/renderers/ExampleRenderer.java | 207 +++++ .../render/renderers/VKMasterRenderer.java | 324 ++++++++ .../engine/vulkan/shaders/ShaderType.java | 7 + 6 files changed, 881 insertions(+), 572 deletions(-) create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/VKMasterRenderer.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/ShaderType.java diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index 0ae49b1..2c963a1 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -1,55 +1,77 @@ package com.github.hydos.ginger; -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.glfw.GLFWVulkan.*; -import static org.lwjgl.system.MemoryUtil.*; -import static org.lwjgl.vulkan.EXTDebugReport.*; -import static org.lwjgl.vulkan.KHRSurface.*; -import static org.lwjgl.vulkan.KHRSwapchain.*; -import static org.lwjgl.vulkan.VK10.*; - import java.io.IOException; -import java.nio.*; +import java.nio.IntBuffer; +import java.nio.LongBuffer; import org.lwjgl.PointerBuffer; -import org.lwjgl.glfw.*; -import org.lwjgl.vulkan.*; +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWFramebufferSizeCallback; +import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.glfw.GLFWVulkan; +import org.lwjgl.system.MemoryUtil; +import org.lwjgl.vulkan.EXTDebugReport; +import org.lwjgl.vulkan.KHRSwapchain; +import org.lwjgl.vulkan.VK12; +import org.lwjgl.vulkan.VkCommandBuffer; +import org.lwjgl.vulkan.VkCommandBufferAllocateInfo; +import org.lwjgl.vulkan.VkCommandBufferBeginInfo; +import org.lwjgl.vulkan.VkCommandPoolCreateInfo; +import org.lwjgl.vulkan.VkDescriptorBufferInfo; +import org.lwjgl.vulkan.VkDescriptorPoolCreateInfo; +import org.lwjgl.vulkan.VkDescriptorPoolSize; +import org.lwjgl.vulkan.VkDescriptorSetAllocateInfo; +import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding; +import org.lwjgl.vulkan.VkDescriptorSetLayoutCreateInfo; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkFormatProperties; +import org.lwjgl.vulkan.VkInstance; +import org.lwjgl.vulkan.VkPhysicalDevice; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; +import org.lwjgl.vulkan.VkPresentInfoKHR; +import org.lwjgl.vulkan.VkQueue; +import org.lwjgl.vulkan.VkSemaphoreCreateInfo; +import org.lwjgl.vulkan.VkSubmitInfo; +import org.lwjgl.vulkan.VkWriteDescriptorSet; 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.TempMethods; import com.github.hydos.ginger.engine.vulkan.VKConstants; -import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; -import com.github.hydos.ginger.engine.vulkan.render.ubo.*; +import com.github.hydos.ginger.engine.vulkan.api.VKGinger; +import com.github.hydos.ginger.engine.vulkan.render.renderers.ExampleRenderer; +import com.github.hydos.ginger.engine.vulkan.render.renderers.VKMasterRenderer; +import com.github.hydos.ginger.engine.vulkan.render.ubo.Ubo; +import com.github.hydos.ginger.engine.vulkan.render.ubo.UboDescriptor; import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; -import com.github.hydos.ginger.engine.vulkan.utils.*; +import com.github.hydos.ginger.engine.vulkan.shaders.ShaderType; +import com.github.hydos.ginger.engine.vulkan.utils.VKDeviceProperties; +import com.github.hydos.ginger.engine.vulkan.utils.VKLoader; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; -/** - * - * @author hydos06 - * the non ARR vulkan test example - * - */ +/** @author hydos06 + * the non ARR vulkan test example */ public class VulkanStarter { - - private static boolean getSupportedDepthFormat(VkPhysicalDevice physicalDevice, IntBuffer depthFormat) + public static boolean getSupportedDepthFormat(VkPhysicalDevice physicalDevice, IntBuffer depthFormat) { // Since all depth formats may be optional, we need to find a suitable depth format to use // Start with the highest precision packed format int[] depthFormats = { - VK_FORMAT_D32_SFLOAT_S8_UINT, - VK_FORMAT_D32_SFLOAT, - VK_FORMAT_D24_UNORM_S8_UINT, - VK_FORMAT_D16_UNORM_S8_UINT, - VK_FORMAT_D16_UNORM + VK12.VK_FORMAT_D32_SFLOAT_S8_UINT, + VK12.VK_FORMAT_D32_SFLOAT, + VK12.VK_FORMAT_D24_UNORM_S8_UINT, + VK12.VK_FORMAT_D16_UNORM_S8_UINT, + VK12.VK_FORMAT_D16_UNORM }; VkFormatProperties formatProps = VkFormatProperties.calloc(); for (int format : depthFormats) { - vkGetPhysicalDeviceFormatProperties(physicalDevice, format, formatProps); + VK12.vkGetPhysicalDeviceFormatProperties(physicalDevice, format, formatProps); // Format must support depth stencil attachment for optimal tiling - if ((formatProps.optimalTilingFeatures() & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) != 0) + if ((formatProps.optimalTilingFeatures() & VK12.VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) != 0) { depthFormat.put(0, format); return true; @@ -58,501 +80,88 @@ public class VulkanStarter return false; } - private static class ColorAndDepthFormatAndSpace + public static class ColorAndDepthFormatAndSpace { - int colorFormat; - int colorSpace; - int depthFormat; - } - - private static ColorAndDepthFormatAndSpace getColorFormatAndSpace(VkPhysicalDevice physicalDevice, long surface) - { - IntBuffer pQueueFamilyPropertyCount = memAllocInt(1); - vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, null); - int queueCount = pQueueFamilyPropertyCount.get(0); - VkQueueFamilyProperties.Buffer queueProps = VkQueueFamilyProperties.calloc(queueCount); - vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, queueProps); - memFree(pQueueFamilyPropertyCount); - // Iterate over each queue to learn whether it supports presenting: - IntBuffer supportsPresent = memAllocInt(queueCount); - for (int i = 0; i < queueCount; i++) - { - supportsPresent.position(i); - int err = vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, supportsPresent); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to physical device surface support: " + VKUtils.translateVulkanResult(err)); } - } - // Search for a graphics and a present queue in the array of queue families, try to find one that supports both - int graphicsQueueNodeIndex = Integer.MAX_VALUE; - int presentQueueNodeIndex = Integer.MAX_VALUE; - for (int i = 0; i < queueCount; i++) - { - if ((queueProps.get(i).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) - { - if (graphicsQueueNodeIndex == Integer.MAX_VALUE) - { graphicsQueueNodeIndex = i; } - if (supportsPresent.get(i) == VK_TRUE) - { - graphicsQueueNodeIndex = i; - presentQueueNodeIndex = i; - break; - } - } - } - queueProps.free(); - if (presentQueueNodeIndex == Integer.MAX_VALUE) - { - // If there's no queue that supports both present and graphics try to find a separate present queue - for (int i = 0; i < queueCount; ++i) - { - if (supportsPresent.get(i) == VK_TRUE) - { - presentQueueNodeIndex = i; - break; - } - } - } - memFree(supportsPresent); - // Generate error if could not find both a graphics and a present queue - if (graphicsQueueNodeIndex == Integer.MAX_VALUE) - { throw new AssertionError("No graphics queue found"); } - if (presentQueueNodeIndex == Integer.MAX_VALUE) - { throw new AssertionError("No presentation queue found"); } - if (graphicsQueueNodeIndex != presentQueueNodeIndex) - { throw new AssertionError("Presentation queue != graphics queue"); } - // Get list of supported formats - IntBuffer pFormatCount = memAllocInt(1); - int err = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, pFormatCount, null); - int formatCount = pFormatCount.get(0); - if (err != VK_SUCCESS) - { throw new AssertionError("Failed to query number of physical device surface formats: " + VKUtils.translateVulkanResult(err)); } - VkSurfaceFormatKHR.Buffer surfFormats = VkSurfaceFormatKHR.calloc(formatCount); - err = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, pFormatCount, surfFormats); - memFree(pFormatCount); - if (err != VK_SUCCESS) - throw new AssertionError("Failed to query physical device surface formats: " + VKUtils.translateVulkanResult(err)); - int colorFormat; - if (formatCount == 1 && surfFormats.get(0).format() == VK_FORMAT_UNDEFINED) - colorFormat = VK_FORMAT_B8G8R8A8_UNORM; - else - colorFormat = surfFormats.get(0).format(); - int colorSpace = surfFormats.get(0).colorSpace(); - surfFormats.free(); - // Find suitable depth format - IntBuffer pDepthFormat = memAllocInt(1).put(0, -1); - getSupportedDepthFormat(physicalDevice, pDepthFormat); - int depthFormat = pDepthFormat.get(0); - ColorAndDepthFormatAndSpace ret = new ColorAndDepthFormatAndSpace(); - ret.colorFormat = colorFormat; - ret.colorSpace = colorSpace; - ret.depthFormat = depthFormat; - return ret; + public int colorFormat; + public int colorSpace; + public int depthFormat; } private static long createCommandPool(VkDevice device, int queueNodeIndex) { VkCommandPoolCreateInfo cmdPoolInfo = VkCommandPoolCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO) + .sType(VK12.VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO) .queueFamilyIndex(queueNodeIndex) - .flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT); - LongBuffer pCmdPool = memAllocLong(1); - int err = vkCreateCommandPool(device, cmdPoolInfo, null, pCmdPool); + .flags(VK12.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT); + LongBuffer pCmdPool = MemoryUtil.memAllocLong(1); + int err = VK12.vkCreateCommandPool(device, cmdPoolInfo, null, pCmdPool); long commandPool = pCmdPool.get(0); cmdPoolInfo.free(); - memFree(pCmdPool); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create command pool: " + VKUtils.translateVulkanResult(err)); + MemoryUtil.memFree(pCmdPool); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create command pool: " + VKUtils.translateVulkanResult(err)); } return commandPool; } private static VkQueue createDeviceQueue(VkDevice device, int queueFamilyIndex) { - PointerBuffer pQueue = memAllocPointer(1); - vkGetDeviceQueue(device, queueFamilyIndex, 0, pQueue); + PointerBuffer pQueue = MemoryUtil.memAllocPointer(1); + VK12.vkGetDeviceQueue(device, queueFamilyIndex, 0, pQueue); long queue = pQueue.get(0); - memFree(pQueue); + MemoryUtil.memFree(pQueue); return new VkQueue(queue, device); } private static VkCommandBuffer createCommandBuffer(VkDevice device, long commandPool) { VkCommandBufferAllocateInfo cmdBufAllocateInfo = VkCommandBufferAllocateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO) + .sType(VK12.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO) .commandPool(commandPool) - .level(VK_COMMAND_BUFFER_LEVEL_PRIMARY) + .level(VK12.VK_COMMAND_BUFFER_LEVEL_PRIMARY) .commandBufferCount(1); - PointerBuffer pCommandBuffer = memAllocPointer(1); - int err = vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCommandBuffer); + PointerBuffer pCommandBuffer = MemoryUtil.memAllocPointer(1); + int err = VK12.vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCommandBuffer); cmdBufAllocateInfo.free(); long commandBuffer = pCommandBuffer.get(0); - memFree(pCommandBuffer); - if (err != VK_SUCCESS) throw new AssertionError("Failed to allocate command buffer: " + VKUtils.translateVulkanResult(err)); + MemoryUtil.memFree(pCommandBuffer); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to allocate command buffer: " + VKUtils.translateVulkanResult(err)); } return new VkCommandBuffer(commandBuffer, device); } public static class Swapchain { - long swapchainHandle; - long[] images; - long[] imageViews; + public long swapchainHandle; + public long[] images; + public long[] imageViews; } - private static Swapchain createSwapChain(VkDevice device, VkPhysicalDevice physicalDevice, long surface, long oldSwapChain, VkCommandBuffer commandBuffer, int newWidth, - int newHeight, int colorFormat, int colorSpace) + public static class DepthStencil { - int err; - // Get physical device surface properties and formats - VkSurfaceCapabilitiesKHR surfCaps = VkSurfaceCapabilitiesKHR.calloc(); - err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, surfCaps); - if (err != VK_SUCCESS) throw new AssertionError("Failed to get physical device surface capabilities: " + VKUtils.translateVulkanResult(err)); - IntBuffer pPresentModeCount = memAllocInt(1); - err = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount, null); - int presentModeCount = pPresentModeCount.get(0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to get number of physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); - IntBuffer pPresentModes = memAllocInt(presentModeCount); - err = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount, pPresentModes); - memFree(pPresentModeCount); - if (err != VK_SUCCESS) throw new AssertionError("Failed to get physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); - // Try to use mailbox mode. Low latency and non-tearing - int swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; - for (int i = 0; i < presentModeCount; i++) - { - if (pPresentModes.get(i) == VK_PRESENT_MODE_MAILBOX_KHR) - { - swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; - break; - } - if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (pPresentModes.get(i) == VK_PRESENT_MODE_IMMEDIATE_KHR)) - swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; - } - memFree(pPresentModes); - // Determine the number of images - int desiredNumberOfSwapchainImages = surfCaps.minImageCount() + 1; - if ((surfCaps.maxImageCount() > 0) && (desiredNumberOfSwapchainImages > surfCaps.maxImageCount())) - desiredNumberOfSwapchainImages = surfCaps.maxImageCount(); - VkExtent2D currentExtent = surfCaps.currentExtent(); - int currentWidth = currentExtent.width(); - int currentHeight = currentExtent.height(); - if (currentWidth != -1 && currentHeight != -1) - { - Window.width = currentWidth; - Window.height = currentHeight; - } - else - { - Window.width = newWidth; - Window.height = newHeight; - } - int preTransform; - if ((surfCaps.supportedTransforms() & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) != 0) - preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; - else preTransform = surfCaps.currentTransform(); - surfCaps.free(); - VkSwapchainCreateInfoKHR swapchainCI = VkSwapchainCreateInfoKHR.calloc() - .sType(VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR) - .surface(surface) - .minImageCount(desiredNumberOfSwapchainImages) - .imageFormat(colorFormat) - .imageColorSpace(colorSpace) - .imageUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) - .preTransform(preTransform) - .imageArrayLayers(1) - .imageSharingMode(VK_SHARING_MODE_EXCLUSIVE) - .presentMode(swapchainPresentMode) - .oldSwapchain(oldSwapChain) - .clipped(true) - .compositeAlpha(VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR); - swapchainCI.imageExtent() - .width(Window.getWidth()) - .height(Window.getHeight()); - LongBuffer pSwapChain = memAllocLong(1); - err = vkCreateSwapchainKHR(device, swapchainCI, null, pSwapChain); - swapchainCI.free(); - long swapChain = pSwapChain.get(0); - memFree(pSwapChain); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create swap chain: " + VKUtils.translateVulkanResult(err)); - // If we just re-created an existing swapchain, we should destroy the old swapchain at this point. - // Note: destroying the swapchain also cleans up all its associated presentable images once the platform is done with them. - if (oldSwapChain != VK_NULL_HANDLE) vkDestroySwapchainKHR(device, oldSwapChain, null); - IntBuffer pImageCount = memAllocInt(1); - err = vkGetSwapchainImagesKHR(device, swapChain, pImageCount, null); - int imageCount = pImageCount.get(0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to get number of swapchain images: " + VKUtils.translateVulkanResult(err)); - LongBuffer pSwapchainImages = memAllocLong(imageCount); - err = vkGetSwapchainImagesKHR(device, swapChain, pImageCount, pSwapchainImages); - if (err != VK_SUCCESS) throw new AssertionError("Failed to get swapchain images: " + VKUtils.translateVulkanResult(err)); - memFree(pImageCount); - long[] images = new long[imageCount]; - long[] imageViews = new long[imageCount]; - LongBuffer pBufferView = memAllocLong(1); - VkImageViewCreateInfo colorAttachmentView = VkImageViewCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO) - .format(colorFormat) - .viewType(VK_IMAGE_VIEW_TYPE_2D); - colorAttachmentView.subresourceRange() - .aspectMask(VK_IMAGE_ASPECT_COLOR_BIT) - .levelCount(1) - .layerCount(1); - for (int i = 0; i < imageCount; i++) - { - images[i] = pSwapchainImages.get(i); - colorAttachmentView.image(images[i]); - err = vkCreateImageView(device, colorAttachmentView, null, pBufferView); - imageViews[i] = pBufferView.get(0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create image view: " + VKUtils.translateVulkanResult(err)); - } - colorAttachmentView.free(); - memFree(pBufferView); - memFree(pSwapchainImages); - Swapchain ret = new Swapchain(); - ret.images = images; - ret.imageViews = imageViews; - ret.swapchainHandle = swapChain; - return ret; - } - - private static class DepthStencil - { - // What is this? - Caroline - long view; - } - - private static DepthStencil createDepthStencil(VkDevice device, VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties, int depthFormat, VkCommandBuffer setupCmdBuffer) - { - VkImageCreateInfo imageCreateInfo = VkImageCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO) - .imageType(VK_IMAGE_TYPE_2D) - .format(depthFormat) - .mipLevels(1) - .arrayLayers(1) - .samples(VK_SAMPLE_COUNT_1_BIT) - .tiling(VK_IMAGE_TILING_OPTIMAL) - .usage(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT); - imageCreateInfo.extent().width(Window.getWidth()).height(Window.getHeight()).depth(1); - VkMemoryAllocateInfo mem_alloc = VkMemoryAllocateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); - VkImageViewCreateInfo depthStencilViewCreateInfo = VkImageViewCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO) - .viewType(VK_IMAGE_VIEW_TYPE_2D) - .format(depthFormat); - depthStencilViewCreateInfo.subresourceRange() - .aspectMask(VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) - .levelCount(1) - .layerCount(1); - VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); - int err; - LongBuffer pDepthStencilImage = memAllocLong(1); - err = vkCreateImage(device, imageCreateInfo, null, pDepthStencilImage); - long depthStencilImage = pDepthStencilImage.get(0); - memFree(pDepthStencilImage); - imageCreateInfo.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create depth-stencil image: " + VKUtils.translateVulkanResult(err)); - vkGetImageMemoryRequirements(device, depthStencilImage, memReqs); - mem_alloc.allocationSize(memReqs.size()); - IntBuffer pMemoryTypeIndex = memAllocInt(1); - VKMemory.getMemoryType(physicalDeviceMemoryProperties, memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, pMemoryTypeIndex); - mem_alloc.memoryTypeIndex(pMemoryTypeIndex.get(0)); - memFree(pMemoryTypeIndex); - LongBuffer pDepthStencilMem = memAllocLong(1); - err = vkAllocateMemory(device, mem_alloc, null, pDepthStencilMem); - long depthStencilMem = pDepthStencilMem.get(0); - memFree(pDepthStencilMem); - mem_alloc.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create depth-stencil memory: " + VKUtils.translateVulkanResult(err)); - err = vkBindImageMemory(device, depthStencilImage, depthStencilMem, 0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to bind depth-stencil image to memory: " + VKUtils.translateVulkanResult(err)); - depthStencilViewCreateInfo.image(depthStencilImage); - LongBuffer pDepthStencilView = memAllocLong(1); - err = vkCreateImageView(device, depthStencilViewCreateInfo, null, pDepthStencilView); - long depthStencilView = pDepthStencilView.get(0); - memFree(pDepthStencilView); - depthStencilViewCreateInfo.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create depth-stencil image view: " + VKUtils.translateVulkanResult(err)); - DepthStencil ret = new DepthStencil(); - ret.view = depthStencilView; - return ret; - } - - private static long createRenderPass(VkDevice device, int colorFormat, int depthFormat) - { - VkAttachmentDescription.Buffer attachments = VkAttachmentDescription.calloc(2); - attachments.get(0) // <- color attachment - .format(colorFormat) - .samples(VK_SAMPLE_COUNT_1_BIT) - .loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR) - .storeOp(VK_ATTACHMENT_STORE_OP_STORE) - .stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE) - .stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE) - .initialLayout(VK_IMAGE_LAYOUT_UNDEFINED) - .finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); - attachments.get(1) // <- depth-stencil attachment - .format(depthFormat) - .samples(VK_SAMPLE_COUNT_1_BIT) - .loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR) - .storeOp(VK_ATTACHMENT_STORE_OP_STORE) - .stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE) - .stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE) - .initialLayout(VK_IMAGE_LAYOUT_UNDEFINED) - .finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); - VkAttachmentReference.Buffer colorReference = VkAttachmentReference.calloc(1) - .attachment(0) - .layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); - VkAttachmentReference depthReference = VkAttachmentReference.calloc() - .attachment(1) - .layout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); - VkSubpassDescription.Buffer subpass = VkSubpassDescription.calloc(1) - .pipelineBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS) - .colorAttachmentCount(colorReference.remaining()) - .pColorAttachments(colorReference) // <- only color attachment - .pDepthStencilAttachment(depthReference) // <- and depth-stencil - ; - VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO) - .pAttachments(attachments) - .pSubpasses(subpass); - LongBuffer pRenderPass = memAllocLong(1); - int err = vkCreateRenderPass(device, renderPassInfo, null, pRenderPass); - long renderPass = pRenderPass.get(0); - memFree(pRenderPass); - renderPassInfo.free(); - depthReference.free(); - colorReference.free(); - subpass.free(); - attachments.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create clear render pass: " + VKUtils.translateVulkanResult(err)); - return renderPass; - } - - private static long[] createFramebuffers(VkDevice device, Swapchain swapchain, long renderPass, int width, int height, DepthStencil depthStencil) - { - LongBuffer attachments = memAllocLong(2); - attachments.put(1, depthStencil.view); - VkFramebufferCreateInfo fci = VkFramebufferCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO) - .pAttachments(attachments) - .height(height) - .width(width) - .layers(1) - .renderPass(renderPass); - // Create a framebuffer for each swapchain image - long[] framebuffers = new long[swapchain.images.length]; - LongBuffer pFramebuffer = memAllocLong(1); - for (int i = 0; i < swapchain.images.length; i++) - { - attachments.put(0, swapchain.imageViews[i]); - int err = vkCreateFramebuffer(device, fci, null, pFramebuffer); - long framebuffer = pFramebuffer.get(0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create framebuffer: " + VKUtils.translateVulkanResult(err)); - framebuffers[i] = framebuffer; - } - memFree(attachments); - memFree(pFramebuffer); - fci.free(); - return framebuffers; + public long view; } private static void submitCommandBuffer(VkQueue queue, VkCommandBuffer commandBuffer) { - if (commandBuffer == null || commandBuffer.address() == NULL) + if (commandBuffer == null || commandBuffer.address() == MemoryUtil.NULL) return; VkSubmitInfo submitInfo = VkSubmitInfo.calloc() - .sType(VK_STRUCTURE_TYPE_SUBMIT_INFO); - PointerBuffer pCommandBuffers = memAllocPointer(1) + .sType(VK12.VK_STRUCTURE_TYPE_SUBMIT_INFO); + PointerBuffer pCommandBuffers = MemoryUtil.memAllocPointer(1) .put(commandBuffer) .flip(); submitInfo.pCommandBuffers(pCommandBuffers); - int err = vkQueueSubmit(queue, submitInfo, VK_NULL_HANDLE); - memFree(pCommandBuffers); + int err = VK12.vkQueueSubmit(queue, submitInfo, VK12.VK_NULL_HANDLE); + MemoryUtil.memFree(pCommandBuffers); submitInfo.free(); - if (err != VK_SUCCESS) - throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); } } - private static class Vertices + public static class Vertices { - long verticesBuf; - VkPipelineVertexInputStateCreateInfo createInfo; - } - - private static Vertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) - { - ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4); - FloatBuffer fb = vertexBuffer.asFloatBuffer(); - // first triangle - fb.put(-0.5f).put(-0.5f).put(0.5f).put(1.0f).put(0.0f).put(0.0f); - fb.put(0.5f).put(-0.5f).put(0.5f).put(0.0f).put(1.0f).put(0.0f); - fb.put(0.0f).put(0.5f).put(0.5f).put(0.0f).put(0.0f).put(1.0f); - // second triangle - fb.put(0.5f).put(-0.5f).put(-0.5f).put(1.0f).put(1.0f).put(0.0f); - fb.put(-0.5f).put(-0.5f).put(-0.5f).put(0.0f).put(1.0f).put(1.0f); - fb.put(0.0f).put(0.5f).put(-0.5f).put(1.0f).put(0.0f).put(1.0f); - VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); - VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); - int err; - // Generate vertex buffer - // Setup - VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) - .size(vertexBuffer.remaining()) - .usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); - LongBuffer pBuffer = memAllocLong(1); - err = vkCreateBuffer(device, bufInfo, null, pBuffer); - long verticesBuf = pBuffer.get(0); - memFree(pBuffer); - bufInfo.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); - vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); - memAlloc.allocationSize(memReqs.size()); - IntBuffer memoryTypeIndex = memAllocInt(1); - VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); - memAlloc.memoryTypeIndex(memoryTypeIndex.get(0)); - memFree(memoryTypeIndex); - memReqs.free(); - LongBuffer pMemory = memAllocLong(1); - err = vkAllocateMemory(device, memAlloc, null, pMemory); - long verticesMem = pMemory.get(0); - memFree(pMemory); - if (err != VK_SUCCESS) throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); - PointerBuffer pData = memAllocPointer(1); - err = vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData); - memAlloc.free(); - long data = pData.get(0); - memFree(pData); - if (err != VK_SUCCESS) throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); - memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining()); - memFree(vertexBuffer); - vkUnmapMemory(device, verticesMem); - err = vkBindBufferMemory(device, verticesBuf, verticesMem, 0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); - // Binding description - VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1) - .binding(0) // <- we bind our vertex buffer to point 0 - .stride((3 + 3) * 4) - .inputRate(VK_VERTEX_INPUT_RATE_VERTEX); - // Attribute descriptions - // Describes memory layout and shader attribute locations - VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2); - // Location 0 : Position - attributeDescriptions.get(0) - .binding(0) // <- binding point used in the VkVertexInputBindingDescription - .location(0) // <- location in the shader's attribute layout (inside the shader source) - .format(VK_FORMAT_R32G32B32_SFLOAT) - .offset(0); - // Location 1 : Color - attributeDescriptions.get(1) - .binding(0) // <- binding point used in the VkVertexInputBindingDescription - .location(1) // <- location in the shader's attribute layout (inside the shader source) - .format(VK_FORMAT_R32G32B32_SFLOAT) - .offset(3 * 4); - // Assign to vertex buffer - VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc(); - vi.sType(VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); - vi.pVertexBindingDescriptions(bindingDescriptor); - vi.pVertexAttributeDescriptions(attributeDescriptions); - Vertices ret = new Vertices(); - ret.createInfo = vi; - ret.verticesBuf = verticesBuf; - return ret; + public long verticesBuf; + public VkPipelineVertexInputStateCreateInfo createInfo; } private static long createDescriptorPool(VkDevice device) @@ -561,7 +170,7 @@ public class VulkanStarter VkDescriptorPoolSize.Buffer typeCounts = VkDescriptorPoolSize.calloc(1) // This example only uses one descriptor type (uniform buffer) and only // requests one descriptor of this type - .type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) + .type(VK12.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) .descriptorCount(1); // For additional types you need to add new entries in the type count list // E.g. for two combined image samplers : @@ -570,36 +179,38 @@ public class VulkanStarter // Create the global descriptor pool // All descriptors used in this example are allocated from this pool VkDescriptorPoolCreateInfo descriptorPoolInfo = VkDescriptorPoolCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO) + .sType(VK12.VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO) .pPoolSizes(typeCounts) // Set the max. number of sets that can be requested // Requesting descriptors beyond maxSets will result in an error .maxSets(1); - LongBuffer pDescriptorPool = memAllocLong(1); - int err = vkCreateDescriptorPool(device, descriptorPoolInfo, null, pDescriptorPool); + LongBuffer pDescriptorPool = MemoryUtil.memAllocLong(1); + int err = VK12.vkCreateDescriptorPool(device, descriptorPoolInfo, null, pDescriptorPool); long descriptorPool = pDescriptorPool.get(0); - memFree(pDescriptorPool); + MemoryUtil.memFree(pDescriptorPool); descriptorPoolInfo.free(); typeCounts.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create descriptor pool: " + VKUtils.translateVulkanResult(err)); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create descriptor pool: " + VKUtils.translateVulkanResult(err)); } return descriptorPool; } private static long createDescriptorSet(VkDevice device, long descriptorPool, long descriptorSetLayout, UboDescriptor uniformDataVSDescriptor) { - LongBuffer pDescriptorSetLayout = memAllocLong(1); + LongBuffer pDescriptorSetLayout = MemoryUtil.memAllocLong(1); pDescriptorSetLayout.put(0, descriptorSetLayout); VkDescriptorSetAllocateInfo allocInfo = VkDescriptorSetAllocateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO) + .sType(VK12.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO) .descriptorPool(descriptorPool) .pSetLayouts(pDescriptorSetLayout); - LongBuffer pDescriptorSet = memAllocLong(1); - int err = vkAllocateDescriptorSets(device, allocInfo, pDescriptorSet); + LongBuffer pDescriptorSet = MemoryUtil.memAllocLong(1); + int err = VK12.vkAllocateDescriptorSets(device, allocInfo, pDescriptorSet); long descriptorSet = pDescriptorSet.get(0); - memFree(pDescriptorSet); + MemoryUtil.memFree(pDescriptorSet); allocInfo.free(); - memFree(pDescriptorSetLayout); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create descriptor set: " + VKUtils.translateVulkanResult(err)); + MemoryUtil.memFree(pDescriptorSetLayout); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create descriptor set: " + VKUtils.translateVulkanResult(err)); } // Update descriptor sets determining the shader binding points // For every binding point used in a shader there needs to be one // descriptor set matching that binding point @@ -609,13 +220,13 @@ public class VulkanStarter .offset(uniformDataVSDescriptor.offset); // Binding 0 : Uniform buffer VkWriteDescriptorSet.Buffer writeDescriptorSet = VkWriteDescriptorSet.calloc(1) - .sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET) + .sType(VK12.VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET) .dstSet(descriptorSet) .descriptorCount(1) - .descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) + .descriptorType(VK12.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) .pBufferInfo(descriptor) - .dstBinding(0); // <- Binds this uniform buffer to binding point 0 - vkUpdateDescriptorSets(device, writeDescriptorSet, null); + .dstBinding(0); // Binds this uniform buffer to binding point 0 + VK12.vkUpdateDescriptorSets(device, writeDescriptorSet, null); writeDescriptorSet.free(); descriptor.free(); return descriptorSet; @@ -626,21 +237,22 @@ public class VulkanStarter int err; // One binding for a UBO used in a vertex shader VkDescriptorSetLayoutBinding.Buffer layoutBinding = VkDescriptorSetLayoutBinding.calloc(1) - .binding(0) // <- Binding 0 : Uniform buffer (Vertex shader) - .descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) + .binding(ShaderType.vertexShader) // <- Binding 0 : Uniform buffer (Vertex shader) + .descriptorType(VK12.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) .descriptorCount(1) - .stageFlags(VK_SHADER_STAGE_VERTEX_BIT); + .stageFlags(VK12.VK_SHADER_STAGE_VERTEX_BIT); // Build a create-info struct to create the descriptor set layout VkDescriptorSetLayoutCreateInfo descriptorLayout = VkDescriptorSetLayoutCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO) + .sType(VK12.VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO) .pBindings(layoutBinding); - LongBuffer pDescriptorSetLayout = memAllocLong(1); - err = vkCreateDescriptorSetLayout(device, descriptorLayout, null, pDescriptorSetLayout); + LongBuffer pDescriptorSetLayout = MemoryUtil.memAllocLong(1); + err = VK12.vkCreateDescriptorSetLayout(device, descriptorLayout, null, pDescriptorSetLayout); long descriptorSetLayout = pDescriptorSetLayout.get(0); - memFree(pDescriptorSetLayout); + MemoryUtil.memFree(pDescriptorSetLayout); descriptorLayout.free(); layoutBinding.free(); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create descriptor set layout: " + VKUtils.translateVulkanResult(err)); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create descriptor set layout: " + VKUtils.translateVulkanResult(err)); } return descriptorSetLayout; } @@ -654,44 +266,44 @@ public class VulkanStarter public static void main(String[] args) throws IOException { - Window.create(1280, 720, "Vulkan Ginger3D", 60, RenderAPI.Vulkan); + Window.create(1200, 600, "Vulkan Ginger3D", 60, RenderAPI.Vulkan); /* Look for instance extensions */ - PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions(); + PointerBuffer requiredExtensions = GLFWVulkan.glfwGetRequiredInstanceExtensions(); if (requiredExtensions == null) { throw new AssertionError("Failed to find list of required Vulkan extensions"); } // Create the Vulkan instance - final VkInstance instance = VKLoader.createInstance(requiredExtensions); + final VkInstance vulkanInstance = VKLoader.createInstance(requiredExtensions); VKUtils.setupVulkanDebugCallback(); - final long debugCallbackHandle = VKUtils.startVulkanDebugging(instance, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, VKConstants.debugCallback); - final VkPhysicalDevice physicalDevice = VKDeviceProperties.getFirstPhysicalDevice(instance); + final long debugCallbackHandle = VKUtils.startVulkanDebugging(vulkanInstance, EXTDebugReport.VK_DEBUG_REPORT_ERROR_BIT_EXT | EXTDebugReport.VK_DEBUG_REPORT_WARNING_BIT_EXT, VKConstants.debugCallback); + final VkPhysicalDevice physicalDevice = VKDeviceProperties.getFirstPhysicalDevice(vulkanInstance); final VKDeviceProperties deviceAndGraphicsQueueFamily = VKDeviceProperties.initDeviceProperties(physicalDevice); final VkDevice device = deviceAndGraphicsQueueFamily.device; int queueFamilyIndex = deviceAndGraphicsQueueFamily.queueFamilyIndex; final VkPhysicalDeviceMemoryProperties memoryProperties = deviceAndGraphicsQueueFamily.memoryProperties; GLFWKeyCallback keyCallback; - glfwSetKeyCallback(Window.getWindow(), keyCallback = new GLFWKeyCallback() + GLFW.glfwSetKeyCallback(Window.getWindow(), keyCallback = new GLFWKeyCallback() { public void invoke(long window, int key, int scancode, int action, int mods) { - if (action != GLFW_RELEASE) + if (action != GLFW.GLFW_RELEASE) return; - if (key == GLFW_KEY_ESCAPE) - glfwSetWindowShouldClose(window, true); + if (key == GLFW.GLFW_KEY_ESCAPE) + GLFW.glfwSetWindowShouldClose(window, true); } }); - LongBuffer pSurface = memAllocLong(1); - int err = glfwCreateWindowSurface(instance, Window.getWindow(), null, pSurface); + LongBuffer pSurface = MemoryUtil.memAllocLong(1); + int err = GLFWVulkan.glfwCreateWindowSurface(vulkanInstance, Window.getWindow(), null, pSurface); final long surface = pSurface.get(0); - if (err != VK_SUCCESS) + if (err != VK12.VK_SUCCESS) { throw new AssertionError("Failed to create surface: " + VKUtils.translateVulkanResult(err)); } // Create static Vulkan resources - final ColorAndDepthFormatAndSpace colorAndDepthFormatAndSpace = getColorFormatAndSpace(physicalDevice, surface); + final ColorAndDepthFormatAndSpace colorAndDepthFormatAndSpace = VKMasterRenderer.getColorFormatAndSpace(physicalDevice, surface); final long commandPool = createCommandPool(device, queueFamilyIndex); final VkCommandBuffer setupCommandBuffer = createCommandBuffer(device, commandPool); final VkQueue queue = createDeviceQueue(device, queueFamilyIndex); - final long renderPass = createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat); + final long renderPass = ExampleRenderer.createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat); final long renderCommandPool = createCommandPool(device, queueFamilyIndex); - Vertices vertices = createVertices(memoryProperties, device); + Vertices vertices = TempMethods.createVertices(memoryProperties, device); Ubo ubo = new Ubo(memoryProperties, device); final long descriptorPool = createDescriptorPool(device); final long descriptorSetLayout = createDescriptorSetLayout(device); @@ -705,28 +317,29 @@ public class VulkanStarter { // Begin the setup command buffer (the one we will use for swapchain/framebuffer creation) VkCommandBufferBeginInfo cmdBufInfo = VkCommandBufferBeginInfo.calloc() - .sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO); - int err = vkBeginCommandBuffer(setupCommandBuffer, cmdBufInfo); + .sType(VK12.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO); + int err = VK12.vkBeginCommandBuffer(setupCommandBuffer, cmdBufInfo); cmdBufInfo.free(); - if (err != VK_SUCCESS) + if (err != VK12.VK_SUCCESS) { throw new AssertionError("Failed to begin setup command buffer: " + VKUtils.translateVulkanResult(err)); } - long oldChain = swapchain != null ? swapchain.swapchainHandle : VK_NULL_HANDLE; + long oldChain = swapchain != null ? swapchain.swapchainHandle : VK12.VK_NULL_HANDLE; // Create the swapchain (this will also add a memory barrier to initialize the framebuffer images) - swapchain = createSwapChain(device, physicalDevice, surface, oldChain, setupCommandBuffer, + swapchain = VKMasterRenderer.createSwapChain(device, physicalDevice, surface, oldChain, setupCommandBuffer, Window.getWidth(), Window.getHeight(), colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.colorSpace); // Create depth-stencil image - depthStencil = createDepthStencil(device, memoryProperties, colorAndDepthFormatAndSpace.depthFormat, setupCommandBuffer); - err = vkEndCommandBuffer(setupCommandBuffer); - if (err != VK_SUCCESS) + depthStencil = VKMasterRenderer.createDepthStencil(device, memoryProperties, colorAndDepthFormatAndSpace.depthFormat, setupCommandBuffer); + err = VK12.vkEndCommandBuffer(setupCommandBuffer); + if (err != VK12.VK_SUCCESS) { throw new AssertionError("Failed to end setup command buffer: " + VKUtils.translateVulkanResult(err)); } submitCommandBuffer(queue, setupCommandBuffer); - vkQueueWaitIdle(queue); + VK12.vkQueueWaitIdle(queue); if (framebuffers != null) { for (int i = 0; i < framebuffers.length; i++) - vkDestroyFramebuffer(device, framebuffers[i], null); } - framebuffers = createFramebuffers(device, swapchain, renderPass, Window.getWidth(), Window.getHeight(), depthStencil); + VK12.vkDestroyFramebuffer(device, framebuffers[i], null); } + framebuffers = ExampleRenderer.createFramebuffers(device, swapchain, renderPass, Window.getWidth(), Window.getHeight(), depthStencil); // Create render command buffers - if (renderCommandBuffers != null) vkResetCommandPool(device, renderCommandPool, VKUtils.VK_FLAGS_NONE); + if (renderCommandBuffers != null) + { VK12.vkResetCommandPool(device, renderCommandPool, VKUtils.VK_FLAGS_NONE); } renderCommandBuffers = VKUtils.initRenderCommandBuffers(device, renderCommandPool, framebuffers, renderPass, Window.getWidth(), Window.getHeight(), pipeline, descriptorSet, vertices.verticesBuf); mustRecreate = false; @@ -738,27 +351,28 @@ public class VulkanStarter { public void invoke(long window, int width, int height) { - if (width <= 0 || height <= 0) return; + if (width <= 0 || height <= 0) + return; swapchainRecreator.mustRecreate = true; } }; - glfwSetFramebufferSizeCallback(Window.getWindow(), framebufferSizeCallback); - glfwShowWindow(Window.getWindow()); + GLFW.glfwSetFramebufferSizeCallback(Window.getWindow(), framebufferSizeCallback); + GLFW.glfwShowWindow(Window.getWindow()); // Pre-allocate everything needed in the render loop - IntBuffer pImageIndex = memAllocInt(1); + IntBuffer pImageIndex = MemoryUtil.memAllocInt(1); int currentBuffer = 0; - PointerBuffer pCommandBuffers = memAllocPointer(1); - LongBuffer pSwapchains = memAllocLong(1); - LongBuffer pImageAcquiredSemaphore = memAllocLong(1); - LongBuffer pRenderCompleteSemaphore = memAllocLong(1); + PointerBuffer pCommandBuffers = MemoryUtil.memAllocPointer(1); + LongBuffer pSwapchains = MemoryUtil.memAllocLong(1); + LongBuffer pImageAcquiredSemaphore = MemoryUtil.memAllocLong(1); + LongBuffer pRenderCompleteSemaphore = MemoryUtil.memAllocLong(1); // Info struct to create a semaphore VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc() - .sType(VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO); + .sType(VK12.VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO); // Info struct to submit a command buffer which will wait on the semaphore - IntBuffer pWaitDstStageMask = memAllocInt(1); - pWaitDstStageMask.put(0, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT); + IntBuffer pWaitDstStageMask = MemoryUtil.memAllocInt(1); + pWaitDstStageMask.put(0, VK12.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT); VkSubmitInfo submitInfo = VkSubmitInfo.calloc() - .sType(VK_STRUCTURE_TYPE_SUBMIT_INFO) + .sType(VK12.VK_STRUCTURE_TYPE_SUBMIT_INFO) .waitSemaphoreCount(pImageAcquiredSemaphore.remaining()) .pWaitSemaphores(pImageAcquiredSemaphore) .pWaitDstStageMask(pWaitDstStageMask) @@ -766,7 +380,7 @@ public class VulkanStarter .pSignalSemaphores(pRenderCompleteSemaphore); // Info struct to present the current swapchain image to the display VkPresentInfoKHR presentInfo = VkPresentInfoKHR.calloc() - .sType(VK_STRUCTURE_TYPE_PRESENT_INFO_KHR) + .sType(KHRSwapchain.VK_STRUCTURE_TYPE_PRESENT_INFO_KHR) .pWaitSemaphores(pRenderCompleteSemaphore) .swapchainCount(pSwapchains.remaining()) .pSwapchains(pSwapchains) @@ -775,23 +389,27 @@ public class VulkanStarter // The render loop long lastTime = System.nanoTime(); float time = 0.0f; - while (!glfwWindowShouldClose(Window.getWindow())) + while (!GLFW.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(); - if (swapchainRecreator.mustRecreate) swapchainRecreator.recreate(); + GLFW.glfwPollEvents(); + if (swapchainRecreator.mustRecreate) + swapchainRecreator.recreate(); // Create a semaphore to wait for the swapchain to acquire the next image - err = vkCreateSemaphore(device, semaphoreCreateInfo, null, pImageAcquiredSemaphore); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create image acquired semaphore: " + VKUtils.translateVulkanResult(err)); + err = VK12.vkCreateSemaphore(device, semaphoreCreateInfo, null, pImageAcquiredSemaphore); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create image acquired semaphore: " + VKUtils.translateVulkanResult(err)); } // Create a semaphore to wait for the render to complete, before presenting - err = vkCreateSemaphore(device, semaphoreCreateInfo, null, pRenderCompleteSemaphore); - if (err != VK_SUCCESS) throw new AssertionError("Failed to create render complete semaphore: " + VKUtils.translateVulkanResult(err)); + err = VK12.vkCreateSemaphore(device, semaphoreCreateInfo, null, pRenderCompleteSemaphore); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create render complete semaphore: " + VKUtils.translateVulkanResult(err)); } // Get next image from the swap chain (back/front buffer). // This will setup the imageAquiredSemaphore to be signalled when the operation is complete - err = vkAcquireNextImageKHR(device, swapchain.swapchainHandle, VKConstants.MAX_UNSIGNED_INT, pImageAcquiredSemaphore.get(0), VK_NULL_HANDLE, pImageIndex); + err = KHRSwapchain.vkAcquireNextImageKHR(device, swapchain.swapchainHandle, VKConstants.MAX_UNSIGNED_INT, pImageAcquiredSemaphore.get(0), VK12.VK_NULL_HANDLE, pImageIndex); currentBuffer = pImageIndex.get(0); - if (err != VK_SUCCESS) throw new AssertionError("Failed to acquire next swapchain image: " + VKUtils.translateVulkanResult(err)); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to acquire next swapchain image: " + VKUtils.translateVulkanResult(err)); } // Select the command buffer for the current framebuffer image/attachment pCommandBuffers.put(0, renderCommandBuffers[currentBuffer]); // Update UBO @@ -800,31 +418,21 @@ public class VulkanStarter lastTime = thisTime; ubo.updateUbo(device, time); // Submit to the graphics queue - err = vkQueueSubmit(queue, submitInfo, VK_NULL_HANDLE); - if (err != VK_SUCCESS) throw new AssertionError("Failed to submit render queue: " + VKUtils.translateVulkanResult(err)); + err = VK12.vkQueueSubmit(queue, submitInfo, VK12.VK_NULL_HANDLE); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to submit render queue: " + VKUtils.translateVulkanResult(err)); } // Present the current buffer to the swap chain // This will display the image pSwapchains.put(0, swapchain.swapchainHandle); - err = vkQueuePresentKHR(queue, presentInfo); - if (err != VK_SUCCESS) throw new AssertionError("Failed to present the swapchain image: " + VKUtils.translateVulkanResult(err)); + err = KHRSwapchain.vkQueuePresentKHR(queue, presentInfo); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to present the swapchain image: " + VKUtils.translateVulkanResult(err)); } // Create and submit post present barrier - vkQueueWaitIdle(queue); + VK12.vkQueueWaitIdle(queue); // Destroy this semaphore (we will create a new one in the next frame) - vkDestroySemaphore(device, pImageAcquiredSemaphore.get(0), null); - vkDestroySemaphore(device, pRenderCompleteSemaphore.get(0), null); + VK12.vkDestroySemaphore(device, pImageAcquiredSemaphore.get(0), null); + VK12.vkDestroySemaphore(device, pRenderCompleteSemaphore.get(0), null); } - presentInfo.free(); - memFree(pWaitDstStageMask); - submitInfo.free(); - memFree(pImageAcquiredSemaphore); - memFree(pRenderCompleteSemaphore); - semaphoreCreateInfo.free(); - memFree(pSwapchains); - memFree(pCommandBuffers); - vkDestroyDebugReportCallbackEXT(instance, debugCallbackHandle, null); - framebufferSizeCallback.free(); - keyCallback.free(); - glfwDestroyWindow(Window.getWindow()); - glfwTerminate(); + VKGinger.getInstance().end(pWaitDstStageMask, pImageAcquiredSemaphore, pRenderCompleteSemaphore, pSwapchains, pCommandBuffers, semaphoreCreateInfo, submitInfo, presentInfo, vulkanInstance, debugCallbackHandle, framebufferSizeCallback, keyCallback); } } \ No newline at end of file diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java new file mode 100644 index 0000000..44b5840 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java @@ -0,0 +1,120 @@ +package com.github.hydos.ginger.engine.vulkan; + +import static org.lwjgl.system.MemoryUtil.memAddress; +import static org.lwjgl.system.MemoryUtil.memAlloc; +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memAllocPointer; +import static org.lwjgl.system.MemoryUtil.memCopy; +import static org.lwjgl.system.MemoryUtil.memFree; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.lwjgl.PointerBuffer; +import org.lwjgl.vulkan.VK12; +import org.lwjgl.vulkan.VkBufferCreateInfo; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkMemoryAllocateInfo; +import org.lwjgl.vulkan.VkMemoryRequirements; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; +import org.lwjgl.vulkan.VkVertexInputAttributeDescription; +import org.lwjgl.vulkan.VkVertexInputBindingDescription; + +import com.github.hydos.ginger.VulkanStarter.Vertices; +import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; + +public class TempMethods +{ + + public static Vertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) + { + ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4); + FloatBuffer fb = vertexBuffer.asFloatBuffer(); + // first triangle + fb.put(-0.5f).put(-0.5f).put(0.5f).put(1.0f).put(0.0f).put(0.0f); + fb.put(0.5f).put(-0.5f).put(0.5f).put(0.0f).put(1.0f).put(0.0f); + fb.put(0.0f).put(0.5f).put(0.5f).put(0.0f).put(0.0f).put(1.0f); + // second triangle + fb.put(0.5f).put(-0.5f).put(-0.5f).put(1.0f).put(1.0f).put(0.0f); + fb.put(-0.5f).put(-0.5f).put(-0.5f).put(0.0f).put(1.0f).put(1.0f); + fb.put(0.0f).put(0.5f).put(-0.5f).put(1.0f).put(0.0f).put(1.0f); + VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); + VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); + int err; + // Generate vertex buffer + // Setup + VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) + .size(vertexBuffer.remaining()) + .usage(VK12.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + LongBuffer pBuffer = memAllocLong(1); + err = VK12.vkCreateBuffer(device, bufInfo, null, pBuffer); + long verticesBuf = pBuffer.get(0); + memFree(pBuffer); + bufInfo.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); } + VK12.vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); + memAlloc.allocationSize(memReqs.size()); + IntBuffer memoryTypeIndex = memAllocInt(1); + VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); + memAlloc.memoryTypeIndex(memoryTypeIndex.get(0)); + memFree(memoryTypeIndex); + memReqs.free(); + LongBuffer pMemory = memAllocLong(1); + err = VK12.vkAllocateMemory(device, memAlloc, null, pMemory); + long verticesMem = pMemory.get(0); + memFree(pMemory); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); } + PointerBuffer pData = memAllocPointer(1); + err = VK12.vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData); + memAlloc.free(); + long data = pData.get(0); + memFree(pData); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); } + memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining()); + memFree(vertexBuffer); + VK12.vkUnmapMemory(device, verticesMem); + err = VK12.vkBindBufferMemory(device, verticesBuf, verticesMem, 0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); } + // Binding description + VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1) + .binding(0) // <- we bind our vertex buffer to point 0 + .stride((3 + 3) * 4) + .inputRate(VK12.VK_VERTEX_INPUT_RATE_VERTEX); + // Attribute descriptions + // Describes memory layout and shader attribute locations + VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2); + // Location 0 : Position + attributeDescriptions.get(0) + .binding(0) // <- binding point used in the VkVertexInputBindingDescription + .location(0) // <- location in the shader's attribute layout (inside the shader source) + .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) + .offset(0); + // Location 1 : Color + attributeDescriptions.get(1) + .binding(0) // <- binding point used in the VkVertexInputBindingDescription + .location(1) // <- location in the shader's attribute layout (inside the shader source) + .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) + .offset(3 * 4); + // Assign to vertex buffer + VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc(); + vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); + vi.pVertexBindingDescriptions(bindingDescriptor); + vi.pVertexAttributeDescriptions(attributeDescriptions); + Vertices ret = new Vertices(); + ret.createInfo = vi; + ret.verticesBuf = verticesBuf; + return ret; + } + +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/api/VKGinger.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/api/VKGinger.java index ce6d3d5..28b4c15 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/api/VKGinger.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/api/VKGinger.java @@ -1,5 +1,48 @@ package com.github.hydos.ginger.engine.vulkan.api; +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.lwjgl.PointerBuffer; +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWFramebufferSizeCallback; +import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.system.MemoryUtil; +import org.lwjgl.vulkan.EXTDebugReport; +import org.lwjgl.vulkan.VkInstance; +import org.lwjgl.vulkan.VkPresentInfoKHR; +import org.lwjgl.vulkan.VkSemaphoreCreateInfo; +import org.lwjgl.vulkan.VkSubmitInfo; + +import com.github.hydos.ginger.engine.common.io.Window; + public class VKGinger { + + private static VKGinger INSTANCE; + + public VKGinger() + { + INSTANCE = this; + } + + public static VKGinger getInstance() + {return INSTANCE; } + + public void end(IntBuffer pWaitDstStageMask, LongBuffer pImageAcquiredSemaphore, LongBuffer pRenderCompleteSemaphore, LongBuffer pSwapchains, PointerBuffer pCommandBuffers, VkSemaphoreCreateInfo semaphoreCreateInfo, VkSubmitInfo submitInfo, VkPresentInfoKHR presentInfo, VkInstance vulkanInstance, long debugCallbackHandle, GLFWFramebufferSizeCallback framebufferSizeCallback, GLFWKeyCallback keyCallback) + { + MemoryUtil.memFree(pWaitDstStageMask); + MemoryUtil.memFree(pImageAcquiredSemaphore); + MemoryUtil.memFree(pRenderCompleteSemaphore); + MemoryUtil.memFree(pSwapchains); + MemoryUtil.memFree(pCommandBuffers); + semaphoreCreateInfo.free(); + submitInfo.free(); + presentInfo.free(); + EXTDebugReport.vkDestroyDebugReportCallbackEXT(vulkanInstance, debugCallbackHandle, null); + framebufferSizeCallback.free(); + keyCallback.free(); + GLFW.glfwDestroyWindow(Window.getWindow()); + GLFW.glfwTerminate(); + } } diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java new file mode 100644 index 0000000..39833a7 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java @@ -0,0 +1,207 @@ +package com.github.hydos.ginger.engine.vulkan.render.renderers; + +import static org.lwjgl.system.MemoryUtil.memAddress; +import static org.lwjgl.system.MemoryUtil.memAlloc; +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memAllocPointer; +import static org.lwjgl.system.MemoryUtil.memCopy; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.lwjgl.PointerBuffer; +import org.lwjgl.vulkan.VK12; +import org.lwjgl.vulkan.VkAttachmentDescription; +import org.lwjgl.vulkan.VkAttachmentReference; +import org.lwjgl.vulkan.VkBufferCreateInfo; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkFramebufferCreateInfo; +import org.lwjgl.vulkan.VkMemoryAllocateInfo; +import org.lwjgl.vulkan.VkMemoryRequirements; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; +import org.lwjgl.vulkan.VkRenderPassCreateInfo; +import org.lwjgl.vulkan.VkSubpassDescription; +import org.lwjgl.vulkan.VkVertexInputAttributeDescription; +import org.lwjgl.vulkan.VkVertexInputBindingDescription; + +import com.github.hydos.ginger.VulkanStarter.DepthStencil; +import com.github.hydos.ginger.VulkanStarter.Swapchain; +import com.github.hydos.ginger.VulkanStarter.Vertices; +import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; + +public class ExampleRenderer +{ + + public static long createRenderPass(VkDevice device, int colorFormat, int depthFormat) + { + VkAttachmentDescription.Buffer attachments = VkAttachmentDescription.calloc(2); + attachments.get(0) // <- color attachment + .format(colorFormat) + .samples(VK12.VK_SAMPLE_COUNT_1_BIT) + .loadOp(VK12.VK_ATTACHMENT_LOAD_OP_CLEAR) + .storeOp(VK12.VK_ATTACHMENT_STORE_OP_STORE) + .stencilLoadOp(VK12.VK_ATTACHMENT_LOAD_OP_DONT_CARE) + .stencilStoreOp(VK12.VK_ATTACHMENT_STORE_OP_DONT_CARE) + .initialLayout(VK12.VK_IMAGE_LAYOUT_UNDEFINED) + .finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); + attachments.get(1) // <- depth-stencil attachment + .format(depthFormat) + .samples(VK12.VK_SAMPLE_COUNT_1_BIT) + .loadOp(VK12.VK_ATTACHMENT_LOAD_OP_CLEAR) + .storeOp(VK12.VK_ATTACHMENT_STORE_OP_STORE) + .stencilLoadOp(VK12.VK_ATTACHMENT_LOAD_OP_DONT_CARE) + .stencilStoreOp(VK12.VK_ATTACHMENT_STORE_OP_DONT_CARE) + .initialLayout(VK12.VK_IMAGE_LAYOUT_UNDEFINED) + .finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); + VkAttachmentReference.Buffer colorReference = VkAttachmentReference.calloc(1) + .attachment(0) + .layout(VK12.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + VkAttachmentReference depthReference = VkAttachmentReference.calloc() + .attachment(1) + .layout(VK12.VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + VkSubpassDescription.Buffer subpass = VkSubpassDescription.calloc(1) + .pipelineBindPoint(VK12.VK_PIPELINE_BIND_POINT_GRAPHICS) + .colorAttachmentCount(colorReference.remaining()) + .pColorAttachments(colorReference) // <- only color attachment + .pDepthStencilAttachment(depthReference) // <- and depth-stencil + ; + VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO) + .pAttachments(attachments) + .pSubpasses(subpass); + LongBuffer pRenderPass = memAllocLong(1); + int err = VK12.vkCreateRenderPass(device, renderPassInfo, null, pRenderPass); + long renderPass = pRenderPass.get(0); + memFree(pRenderPass); + renderPassInfo.free(); + depthReference.free(); + colorReference.free(); + subpass.free(); + attachments.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create clear render pass: " + VKUtils.translateVulkanResult(err)); } + return renderPass; + } + + public static long[] createFramebuffers(VkDevice device, Swapchain swapchain, long renderPass, int width, int height, DepthStencil depthStencil) + { + LongBuffer attachments = memAllocLong(2); + attachments.put(1, depthStencil.view); + VkFramebufferCreateInfo fci = VkFramebufferCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO) + .pAttachments(attachments) + .height(height) + .width(width) + .layers(1) + .renderPass(renderPass); + // Create a framebuffer for each swapchain image + long[] framebuffers = new long[swapchain.images.length]; + LongBuffer pFramebuffer = memAllocLong(1); + for (int i = 0; i < swapchain.images.length; i++) + { + attachments.put(0, swapchain.imageViews[i]); + int err = VK12.vkCreateFramebuffer(device, fci, null, pFramebuffer); + long framebuffer = pFramebuffer.get(0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create framebuffer: " + VKUtils.translateVulkanResult(err)); } + framebuffers[i] = framebuffer; + } + memFree(attachments); + memFree(pFramebuffer); + fci.free(); + return framebuffers; + } + + public static Vertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) + { + ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4); + FloatBuffer fb = vertexBuffer.asFloatBuffer(); + // first triangle + fb.put(-0.5f).put(-0.5f).put(0.5f).put(1.0f).put(0.0f).put(0.0f); + fb.put(0.5f).put(-0.5f).put(0.5f).put(0.0f).put(1.0f).put(0.0f); + fb.put(0.0f).put(0.5f).put(0.5f).put(0.0f).put(0.0f).put(1.0f); + // second triangle + fb.put(0.5f).put(-0.5f).put(-0.5f).put(1.0f).put(1.0f).put(0.0f); + fb.put(-0.5f).put(-0.5f).put(-0.5f).put(0.0f).put(1.0f).put(1.0f); + fb.put(0.0f).put(0.5f).put(-0.5f).put(1.0f).put(0.0f).put(1.0f); + VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); + VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); + int err; + // Generate vertex buffer + // Setup + VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) + .size(vertexBuffer.remaining()) + .usage(VK12.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + LongBuffer pBuffer = memAllocLong(1); + err = VK12.vkCreateBuffer(device, bufInfo, null, pBuffer); + long verticesBuf = pBuffer.get(0); + memFree(pBuffer); + bufInfo.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); } + VK12.vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); + memAlloc.allocationSize(memReqs.size()); + IntBuffer memoryTypeIndex = memAllocInt(1); + VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); + memAlloc.memoryTypeIndex(memoryTypeIndex.get(0)); + memFree(memoryTypeIndex); + memReqs.free(); + LongBuffer pMemory = memAllocLong(1); + err = VK12.vkAllocateMemory(device, memAlloc, null, pMemory); + long verticesMem = pMemory.get(0); + memFree(pMemory); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); } + PointerBuffer pData = memAllocPointer(1); + err = VK12.vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData); + memAlloc.free(); + long data = pData.get(0); + memFree(pData); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); } + memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining()); + memFree(vertexBuffer); + VK12.vkUnmapMemory(device, verticesMem); + err = VK12.vkBindBufferMemory(device, verticesBuf, verticesMem, 0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); } + // Binding description + VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1) + .binding(0) // <- we bind our vertex buffer to point 0 + .stride((3 + 3) * 4) + .inputRate(VK12.VK_VERTEX_INPUT_RATE_VERTEX); + // Attribute descriptions + // Describes memory layout and shader attribute locations + VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2); + // Location 0 : Position + attributeDescriptions.get(0) + .binding(0) // <- binding point used in the VkVertexInputBindingDescription + .location(0) // <- location in the shader's attribute layout (inside the shader source) + .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) + .offset(0); + // Location 1 : Color + attributeDescriptions.get(1) + .binding(0) // <- binding point used in the VkVertexInputBindingDescription + .location(1) // <- location in the shader's attribute layout (inside the shader source) + .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) + .offset(3 * 4); + // Assign to vertex buffer + VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc(); + vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); + vi.pVertexBindingDescriptions(bindingDescriptor); + vi.pVertexAttributeDescriptions(attributeDescriptions); + Vertices ret = new Vertices(); + ret.createInfo = vi; + ret.verticesBuf = verticesBuf; + return ret; + } +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/VKMasterRenderer.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/VKMasterRenderer.java new file mode 100644 index 0000000..ca33d17 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/VKMasterRenderer.java @@ -0,0 +1,324 @@ +package com.github.hydos.ginger.engine.vulkan.render.renderers; + +import static org.lwjgl.system.MemoryUtil.memAllocInt; +import static org.lwjgl.system.MemoryUtil.memAllocLong; +import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.vulkan.KHRSurface.VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_FIFO_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_IMMEDIATE_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_MAILBOX_KHR; +import static org.lwjgl.vulkan.KHRSurface.VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR; +import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR; +import static org.lwjgl.vulkan.KHRSwapchain.VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkCreateSwapchainKHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkDestroySwapchainKHR; +import static org.lwjgl.vulkan.KHRSwapchain.vkGetSwapchainImagesKHR; + +import java.nio.IntBuffer; +import java.nio.LongBuffer; + +import org.lwjgl.vulkan.VK12; +import org.lwjgl.vulkan.VkCommandBuffer; +import org.lwjgl.vulkan.VkDevice; +import org.lwjgl.vulkan.VkExtent2D; +import org.lwjgl.vulkan.VkImageCreateInfo; +import org.lwjgl.vulkan.VkImageViewCreateInfo; +import org.lwjgl.vulkan.VkMemoryAllocateInfo; +import org.lwjgl.vulkan.VkMemoryRequirements; +import org.lwjgl.vulkan.VkPhysicalDevice; +import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; +import org.lwjgl.vulkan.VkQueueFamilyProperties; +import org.lwjgl.vulkan.VkSurfaceCapabilitiesKHR; +import org.lwjgl.vulkan.VkSurfaceFormatKHR; +import org.lwjgl.vulkan.VkSwapchainCreateInfoKHR; + +import com.github.hydos.ginger.VulkanStarter; +import com.github.hydos.ginger.VulkanStarter.ColorAndDepthFormatAndSpace; +import com.github.hydos.ginger.VulkanStarter.DepthStencil; +import com.github.hydos.ginger.VulkanStarter.Swapchain; +import com.github.hydos.ginger.engine.common.io.Window; +import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; + +public class VKMasterRenderer +{ + + public static ColorAndDepthFormatAndSpace getColorFormatAndSpace(VkPhysicalDevice physicalDevice, long surface) + { + IntBuffer pQueueFamilyPropertyCount = memAllocInt(1); + VK12.vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, null); + int queueCount = pQueueFamilyPropertyCount.get(0); + VkQueueFamilyProperties.Buffer queueProps = VkQueueFamilyProperties.calloc(queueCount); + VK12.vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, queueProps); + memFree(pQueueFamilyPropertyCount); + // Iterate over each queue to learn whether it supports presenting: + IntBuffer supportsPresent = memAllocInt(queueCount); + for (int i = 0; i < queueCount; i++) + { + supportsPresent.position(i); + int err = vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, supportsPresent); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to physical device surface support: " + VKUtils.translateVulkanResult(err)); } + } + // Search for a graphics and a present queue in the array of queue families, try to find one that supports both + int graphicsQueueNodeIndex = Integer.MAX_VALUE; + int presentQueueNodeIndex = Integer.MAX_VALUE; + for (int i = 0; i < queueCount; i++) + { + if ((queueProps.get(i).queueFlags() & VK12.VK_QUEUE_GRAPHICS_BIT) != 0) + { + if (graphicsQueueNodeIndex == Integer.MAX_VALUE) + { graphicsQueueNodeIndex = i; } + if (supportsPresent.get(i) == VK12.VK_TRUE) + { + graphicsQueueNodeIndex = i; + presentQueueNodeIndex = i; + break; + } + } + } + queueProps.free(); + if (presentQueueNodeIndex == Integer.MAX_VALUE) + { + // If there's no queue that supports both present and graphics try to find a separate present queue + for (int i = 0; i < queueCount; ++i) + { + if (supportsPresent.get(i) == VK12.VK_TRUE) + { + presentQueueNodeIndex = i; + break; + } + } + } + memFree(supportsPresent); + // Generate error if could not find both a graphics and a present queue + if (graphicsQueueNodeIndex == Integer.MAX_VALUE) + { throw new AssertionError("No graphics queue found"); } + if (presentQueueNodeIndex == Integer.MAX_VALUE) + { throw new AssertionError("No presentation queue found"); } + if (graphicsQueueNodeIndex != presentQueueNodeIndex) + { throw new AssertionError("Presentation queue != graphics queue"); } + // Get list of supported formats + IntBuffer pFormatCount = memAllocInt(1); + int err = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, pFormatCount, null); + int formatCount = pFormatCount.get(0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to query number of physical device surface formats: " + VKUtils.translateVulkanResult(err)); } + VkSurfaceFormatKHR.Buffer surfFormats = VkSurfaceFormatKHR.calloc(formatCount); + err = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, pFormatCount, surfFormats); + memFree(pFormatCount); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to query physical device surface formats: " + VKUtils.translateVulkanResult(err)); } + int colorFormat; + if (formatCount == 1 && surfFormats.get(0).format() == VK12.VK_FORMAT_UNDEFINED) + { + colorFormat = VK12.VK_FORMAT_B8G8R8A8_UNORM; + } + else + { + colorFormat = surfFormats.get(0).format(); + } + int colorSpace = surfFormats.get(0).colorSpace(); + surfFormats.free(); + // Find suitable depth format + IntBuffer pDepthFormat = memAllocInt(1).put(0, -1); + VulkanStarter.getSupportedDepthFormat(physicalDevice, pDepthFormat); + int depthFormat = pDepthFormat.get(0); + ColorAndDepthFormatAndSpace ret = new ColorAndDepthFormatAndSpace(); + ret.colorFormat = colorFormat; + ret.colorSpace = colorSpace; + ret.depthFormat = depthFormat; + return ret; + } + + public static DepthStencil createDepthStencil(VkDevice device, VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties, int depthFormat, VkCommandBuffer setupCmdBuffer) + { + VkImageCreateInfo imageCreateInfo = VkImageCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO) + .imageType(VK12.VK_IMAGE_TYPE_2D) + .format(depthFormat) + .mipLevels(1) + .arrayLayers(1) + .samples(VK12.VK_SAMPLE_COUNT_1_BIT) + .tiling(VK12.VK_IMAGE_TILING_OPTIMAL) + .usage(VK12.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK12.VK_IMAGE_USAGE_TRANSFER_SRC_BIT); + imageCreateInfo.extent().width(Window.getWidth()).height(Window.getHeight()).depth(1); + VkMemoryAllocateInfo mem_alloc = VkMemoryAllocateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); + VkImageViewCreateInfo depthStencilViewCreateInfo = VkImageViewCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO) + .viewType(VK12.VK_IMAGE_VIEW_TYPE_2D) + .format(depthFormat); + depthStencilViewCreateInfo.subresourceRange() + .aspectMask(VK12.VK_IMAGE_ASPECT_DEPTH_BIT | VK12.VK_IMAGE_ASPECT_STENCIL_BIT) + .levelCount(1) + .layerCount(1); + VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); + int err; + LongBuffer pDepthStencilImage = memAllocLong(1); + err = VK12.vkCreateImage(device, imageCreateInfo, null, pDepthStencilImage); + long depthStencilImage = pDepthStencilImage.get(0); + memFree(pDepthStencilImage); + imageCreateInfo.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create depth-stencil image: " + VKUtils.translateVulkanResult(err)); } + VK12.vkGetImageMemoryRequirements(device, depthStencilImage, memReqs); + mem_alloc.allocationSize(memReqs.size()); + IntBuffer pMemoryTypeIndex = memAllocInt(1); + VKMemory.getMemoryType(physicalDeviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, pMemoryTypeIndex); + mem_alloc.memoryTypeIndex(pMemoryTypeIndex.get(0)); + memFree(pMemoryTypeIndex); + LongBuffer pDepthStencilMem = memAllocLong(1); + err = VK12.vkAllocateMemory(device, mem_alloc, null, pDepthStencilMem); + long depthStencilMem = pDepthStencilMem.get(0); + memFree(pDepthStencilMem); + mem_alloc.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create depth-stencil memory: " + VKUtils.translateVulkanResult(err)); } + err = VK12.vkBindImageMemory(device, depthStencilImage, depthStencilMem, 0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to bind depth-stencil image to memory: " + VKUtils.translateVulkanResult(err)); } + depthStencilViewCreateInfo.image(depthStencilImage); + LongBuffer pDepthStencilView = memAllocLong(1); + err = VK12.vkCreateImageView(device, depthStencilViewCreateInfo, null, pDepthStencilView); + long depthStencilView = pDepthStencilView.get(0); + memFree(pDepthStencilView); + depthStencilViewCreateInfo.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create depth-stencil image view: " + VKUtils.translateVulkanResult(err)); } + DepthStencil ret = new DepthStencil(); + ret.view = depthStencilView; + return ret; + } + + public static Swapchain createSwapChain(VkDevice device, VkPhysicalDevice physicalDevice, long surface, long oldSwapChain, VkCommandBuffer commandBuffer, int newWidth, + int newHeight, int colorFormat, int colorSpace) + { + int err; + // Get physical device surface properties and formats + VkSurfaceCapabilitiesKHR surfCaps = VkSurfaceCapabilitiesKHR.calloc(); + err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, surfCaps); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get physical device surface capabilities: " + VKUtils.translateVulkanResult(err)); } + IntBuffer pPresentModeCount = memAllocInt(1); + err = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount, null); + int presentModeCount = pPresentModeCount.get(0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get number of physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); } + IntBuffer pPresentModes = memAllocInt(presentModeCount); + err = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, pPresentModeCount, pPresentModes); + memFree(pPresentModeCount); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get physical device surface presentation modes: " + VKUtils.translateVulkanResult(err)); } + // Try to use mailbox mode. Low latency and non-tearing + int swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; + for (int i = 0; i < presentModeCount; i++) + { + if (pPresentModes.get(i) == VK_PRESENT_MODE_MAILBOX_KHR) + { + swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; + break; + } + if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (pPresentModes.get(i) == VK_PRESENT_MODE_IMMEDIATE_KHR)) + { swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; } + } + memFree(pPresentModes); + // Determine the number of images + int desiredNumberOfSwapchainImages = surfCaps.minImageCount() + 1; + if ((surfCaps.maxImageCount() > 0) && (desiredNumberOfSwapchainImages > surfCaps.maxImageCount())) + { desiredNumberOfSwapchainImages = surfCaps.maxImageCount(); } + VkExtent2D currentExtent = surfCaps.currentExtent(); + int currentWidth = currentExtent.width(); + int currentHeight = currentExtent.height(); + if (currentWidth != -1 && currentHeight != -1) + { + Window.width = currentWidth; + Window.height = currentHeight; + } + else + { + Window.width = newWidth; + Window.height = newHeight; + } + int preTransform; + if ((surfCaps.supportedTransforms() & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) != 0) + { + preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; + } + else + { + preTransform = surfCaps.currentTransform(); + } + surfCaps.free(); + VkSwapchainCreateInfoKHR swapchainCI = VkSwapchainCreateInfoKHR.calloc() + .sType(VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR) + .surface(surface) + .minImageCount(desiredNumberOfSwapchainImages) + .imageFormat(colorFormat) + .imageColorSpace(colorSpace) + .imageUsage(VK12.VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) + .preTransform(preTransform) + .imageArrayLayers(1) + .imageSharingMode(VK12.VK_SHARING_MODE_EXCLUSIVE) + .presentMode(swapchainPresentMode) + .oldSwapchain(oldSwapChain) + .clipped(true) + .compositeAlpha(VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR); + swapchainCI.imageExtent() + .width(Window.getWidth()) + .height(Window.getHeight()); + LongBuffer pSwapChain = memAllocLong(1); + err = vkCreateSwapchainKHR(device, swapchainCI, null, pSwapChain); + swapchainCI.free(); + long swapChain = pSwapChain.get(0); + memFree(pSwapChain); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create swap chain: " + VKUtils.translateVulkanResult(err)); } + // If we just re-created an existing swapchain, we should destroy the old swapchain at this point. + // Note: destroying the swapchain also cleans up all its associated presentable images once the platform is done with them. + if (oldSwapChain != VK12.VK_NULL_HANDLE) + { vkDestroySwapchainKHR(device, oldSwapChain, null); } + IntBuffer pImageCount = memAllocInt(1); + err = vkGetSwapchainImagesKHR(device, swapChain, pImageCount, null); + int imageCount = pImageCount.get(0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get number of swapchain images: " + VKUtils.translateVulkanResult(err)); } + LongBuffer pSwapchainImages = memAllocLong(imageCount); + err = vkGetSwapchainImagesKHR(device, swapChain, pImageCount, pSwapchainImages); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to get swapchain images: " + VKUtils.translateVulkanResult(err)); } + memFree(pImageCount); + long[] images = new long[imageCount]; + long[] imageViews = new long[imageCount]; + LongBuffer pBufferView = memAllocLong(1); + VkImageViewCreateInfo colorAttachmentView = VkImageViewCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO) + .format(colorFormat) + .viewType(VK12.VK_IMAGE_VIEW_TYPE_2D); + colorAttachmentView.subresourceRange() + .aspectMask(VK12.VK_IMAGE_ASPECT_COLOR_BIT) + .levelCount(1) + .layerCount(1); + for (int i = 0; i < imageCount; i++) + { + images[i] = pSwapchainImages.get(i); + colorAttachmentView.image(images[i]); + err = VK12.vkCreateImageView(device, colorAttachmentView, null, pBufferView); + imageViews[i] = pBufferView.get(0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create image view: " + VKUtils.translateVulkanResult(err)); } + } + colorAttachmentView.free(); + memFree(pBufferView); + memFree(pSwapchainImages); + Swapchain ret = new Swapchain(); + ret.images = images; + ret.imageViews = imageViews; + ret.swapchainHandle = swapChain; + return ret; + } + +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/ShaderType.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/ShaderType.java new file mode 100644 index 0000000..32daf94 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/shaders/ShaderType.java @@ -0,0 +1,7 @@ +package com.github.hydos.ginger.engine.vulkan.shaders; + +public class ShaderType +{ + public final static int vertexShader = 0; + +} From 75f7f37b41a5bf49f89703bed7457a9af29cf525 Mon Sep 17 00:00:00 2001 From: hYdos Date: Tue, 3 Mar 2020 15:43:19 +1000 Subject: [PATCH 6/8] yay improvments --- .../github/hydos/ginger/VulkanStarter.java | 60 ++++--------------- .../ginger/engine/vulkan/TempMethods.java | 31 +++------- .../engine/vulkan/model/VKModelConverter.java | 11 ++++ .../engine/vulkan/model/VKVertices.java | 10 ++++ .../render/renderers/ExampleRenderer.java | 8 +-- 5 files changed, 43 insertions(+), 77 deletions(-) create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKVertices.java diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index 2c963a1..f4010ef 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -1,54 +1,22 @@ package com.github.hydos.ginger; import java.io.IOException; -import java.nio.IntBuffer; -import java.nio.LongBuffer; +import java.nio.*; import org.lwjgl.PointerBuffer; -import org.lwjgl.glfw.GLFW; -import org.lwjgl.glfw.GLFWFramebufferSizeCallback; -import org.lwjgl.glfw.GLFWKeyCallback; -import org.lwjgl.glfw.GLFWVulkan; +import org.lwjgl.glfw.*; import org.lwjgl.system.MemoryUtil; -import org.lwjgl.vulkan.EXTDebugReport; -import org.lwjgl.vulkan.KHRSwapchain; -import org.lwjgl.vulkan.VK12; -import org.lwjgl.vulkan.VkCommandBuffer; -import org.lwjgl.vulkan.VkCommandBufferAllocateInfo; -import org.lwjgl.vulkan.VkCommandBufferBeginInfo; -import org.lwjgl.vulkan.VkCommandPoolCreateInfo; -import org.lwjgl.vulkan.VkDescriptorBufferInfo; -import org.lwjgl.vulkan.VkDescriptorPoolCreateInfo; -import org.lwjgl.vulkan.VkDescriptorPoolSize; -import org.lwjgl.vulkan.VkDescriptorSetAllocateInfo; -import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding; -import org.lwjgl.vulkan.VkDescriptorSetLayoutCreateInfo; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkFormatProperties; -import org.lwjgl.vulkan.VkInstance; -import org.lwjgl.vulkan.VkPhysicalDevice; -import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; -import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; -import org.lwjgl.vulkan.VkPresentInfoKHR; -import org.lwjgl.vulkan.VkQueue; -import org.lwjgl.vulkan.VkSemaphoreCreateInfo; -import org.lwjgl.vulkan.VkSubmitInfo; -import org.lwjgl.vulkan.VkWriteDescriptorSet; +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.TempMethods; -import com.github.hydos.ginger.engine.vulkan.VKConstants; +import com.github.hydos.ginger.engine.vulkan.*; import com.github.hydos.ginger.engine.vulkan.api.VKGinger; -import com.github.hydos.ginger.engine.vulkan.render.renderers.ExampleRenderer; -import com.github.hydos.ginger.engine.vulkan.render.renderers.VKMasterRenderer; -import com.github.hydos.ginger.engine.vulkan.render.ubo.Ubo; -import com.github.hydos.ginger.engine.vulkan.render.ubo.UboDescriptor; -import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline; -import com.github.hydos.ginger.engine.vulkan.shaders.ShaderType; -import com.github.hydos.ginger.engine.vulkan.utils.VKDeviceProperties; -import com.github.hydos.ginger.engine.vulkan.utils.VKLoader; -import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; +import com.github.hydos.ginger.engine.vulkan.model.VKVertices; +import com.github.hydos.ginger.engine.vulkan.render.renderers.*; +import com.github.hydos.ginger.engine.vulkan.render.ubo.*; +import com.github.hydos.ginger.engine.vulkan.shaders.*; +import com.github.hydos.ginger.engine.vulkan.utils.*; /** @author hydos06 * the non ARR vulkan test example */ @@ -158,12 +126,6 @@ public class VulkanStarter { throw new AssertionError("Failed to submit command buffer: " + VKUtils.translateVulkanResult(err)); } } - public static class Vertices - { - public long verticesBuf; - public VkPipelineVertexInputStateCreateInfo createInfo; - } - private static long createDescriptorPool(VkDevice device) { // We need to tell the API the number of max. requested descriptors per type @@ -303,7 +265,7 @@ public class VulkanStarter final VkQueue queue = createDeviceQueue(device, queueFamilyIndex); final long renderPass = ExampleRenderer.createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat); final long renderCommandPool = createCommandPool(device, queueFamilyIndex); - Vertices vertices = TempMethods.createVertices(memoryProperties, device); + VKVertices vertices = TempMethods.createVertices(memoryProperties, device); Ubo ubo = new Ubo(memoryProperties, device); final long descriptorPool = createDescriptorPool(device); final long descriptorSetLayout = createDescriptorSetLayout(device); @@ -341,7 +303,7 @@ public class VulkanStarter if (renderCommandBuffers != null) { VK12.vkResetCommandPool(device, renderCommandPool, VKUtils.VK_FLAGS_NONE); } renderCommandBuffers = VKUtils.initRenderCommandBuffers(device, renderCommandPool, framebuffers, renderPass, Window.getWidth(), Window.getHeight(), pipeline, descriptorSet, - vertices.verticesBuf); + vertices.vkVerticiesBuffer); mustRecreate = false; } } diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java index 44b5840..db55cea 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java @@ -1,37 +1,20 @@ package com.github.hydos.ginger.engine.vulkan; -import static org.lwjgl.system.MemoryUtil.memAddress; -import static org.lwjgl.system.MemoryUtil.memAlloc; -import static org.lwjgl.system.MemoryUtil.memAllocInt; -import static org.lwjgl.system.MemoryUtil.memAllocLong; -import static org.lwjgl.system.MemoryUtil.memAllocPointer; -import static org.lwjgl.system.MemoryUtil.memCopy; -import static org.lwjgl.system.MemoryUtil.memFree; +import static org.lwjgl.system.MemoryUtil.*; -import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.LongBuffer; +import java.nio.*; import org.lwjgl.PointerBuffer; -import org.lwjgl.vulkan.VK12; -import org.lwjgl.vulkan.VkBufferCreateInfo; -import org.lwjgl.vulkan.VkDevice; -import org.lwjgl.vulkan.VkMemoryAllocateInfo; -import org.lwjgl.vulkan.VkMemoryRequirements; -import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties; -import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; -import org.lwjgl.vulkan.VkVertexInputAttributeDescription; -import org.lwjgl.vulkan.VkVertexInputBindingDescription; +import org.lwjgl.vulkan.*; -import com.github.hydos.ginger.VulkanStarter.Vertices; import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.model.VKVertices; import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; public class TempMethods { - public static Vertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) + public static VKVertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) { ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4); FloatBuffer fb = vertexBuffer.asFloatBuffer(); @@ -111,9 +94,9 @@ public class TempMethods vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); vi.pVertexBindingDescriptions(bindingDescriptor); vi.pVertexAttributeDescriptions(attributeDescriptions); - Vertices ret = new Vertices(); + VKVertices ret = new VKVertices(); ret.createInfo = vi; - ret.verticesBuf = verticesBuf; + ret.vkVerticiesBuffer = verticesBuf; return ret; } diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java new file mode 100644 index 0000000..4635436 --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java @@ -0,0 +1,11 @@ +package com.github.hydos.ginger.engine.vulkan.model; + +public class VKModelConverter +{ + + public VKVertices toVKVerticies() + { + return null; + } + +} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKVertices.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKVertices.java new file mode 100644 index 0000000..213895b --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKVertices.java @@ -0,0 +1,10 @@ +package com.github.hydos.ginger.engine.vulkan.model; + +import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo; + +public class VKVertices +{ + public long vkVerticiesBuffer; + public VkPipelineVertexInputStateCreateInfo createInfo; + public float[] commonVerticies; +} \ No newline at end of file diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java index 39833a7..9719667 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/render/renderers/ExampleRenderer.java @@ -32,8 +32,8 @@ import org.lwjgl.vulkan.VkVertexInputBindingDescription; import com.github.hydos.ginger.VulkanStarter.DepthStencil; import com.github.hydos.ginger.VulkanStarter.Swapchain; -import com.github.hydos.ginger.VulkanStarter.Vertices; import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.model.VKVertices; import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; public class ExampleRenderer @@ -119,7 +119,7 @@ public class ExampleRenderer return framebuffers; } - public static Vertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) + public static VKVertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) { ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4); FloatBuffer fb = vertexBuffer.asFloatBuffer(); @@ -199,9 +199,9 @@ public class ExampleRenderer vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); vi.pVertexBindingDescriptions(bindingDescriptor); vi.pVertexAttributeDescriptions(attributeDescriptions); - Vertices ret = new Vertices(); + VKVertices ret = new VKVertices(); ret.createInfo = vi; - ret.verticesBuf = verticesBuf; + ret.vkVerticiesBuffer = verticesBuf; return ret; } } From 9a8fdc44a7a7e41fcbf5fda915f52f6439a588d1 Mon Sep 17 00:00:00 2001 From: hYdos Date: Tue, 3 Mar 2020 15:56:37 +1000 Subject: [PATCH 7/8] yEs --- .../github/halotroop/litecraft/Litecraft.java | 4 +- .../litecraft/types/block/Block.java | 8 +- .../litecraft/types/entity/Entity.java | 4 +- .../halotroop/litecraft/world/World.java | 4 +- .../litecraft/world/block/BlockRenderer.java | 6 +- .../github/hydos/ginger/VulkanStarter.java | 5 +- .../common/elements/objects/Player.java | 4 +- .../common/elements/objects/RenderObject.java | 10 +- .../ginger/engine/common/obj/ModelLoader.java | 14 ++- .../ginger/engine/opengl/api/GingerUtils.java | 8 +- .../engine/opengl/render/MasterRenderer.java | 10 +- ...exturedModel.java => GLTexturedModel.java} | 4 +- .../renderers/NormalMappingRenderer.java | 6 +- .../render/renderers/ObjectRenderer.java | 8 +- .../shadow/ShadowMapEntityRenderer.java | 4 +- .../shadow/ShadowMapMasterRenderer.java | 6 +- .../ginger/engine/vulkan/TempMethods.java | 103 ------------------ .../engine/vulkan/model/VKModelConverter.java | 91 +++++++++++++++- 18 files changed, 144 insertions(+), 155 deletions(-) rename src/main/java/com/github/hydos/ginger/engine/opengl/render/models/{TexturedModel.java => GLTexturedModel.java} (80%) delete mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java diff --git a/src/main/java/com/github/halotroop/litecraft/Litecraft.java b/src/main/java/com/github/halotroop/litecraft/Litecraft.java index ed3cc86..347d5b6 100644 --- a/src/main/java/com/github/halotroop/litecraft/Litecraft.java +++ b/src/main/java/com/github/halotroop/litecraft/Litecraft.java @@ -18,7 +18,7 @@ import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.common.obj.ModelLoader; import com.github.hydos.ginger.engine.opengl.api.*; import com.github.hydos.ginger.engine.opengl.render.MasterRenderer; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import com.github.hydos.ginger.engine.opengl.utils.GlLoader; import tk.valoeghese.gateways.client.io.*; @@ -121,7 +121,7 @@ public class Litecraft extends Game // set up ginger utilities GingerUtils.init(); //Set the player model - TexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png"); + GLTexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png"); Light sun = new Light(new Vector3f(0, 105, 0), new Vector3f(0.9765625f, 0.98828125f, 0.05859375f), new Vector3f(0.002f, 0.002f, 0.002f)); FontType font = new FontType(GlLoader.loadFontAtlas("candara.png"), "candara.fnt"); this.engine = new GingerGL(); diff --git a/src/main/java/com/github/halotroop/litecraft/types/block/Block.java b/src/main/java/com/github/halotroop/litecraft/types/block/Block.java index 0882b9e..b380a31 100644 --- a/src/main/java/com/github/halotroop/litecraft/types/block/Block.java +++ b/src/main/java/com/github/halotroop/litecraft/types/block/Block.java @@ -3,7 +3,7 @@ package com.github.halotroop.litecraft.types.block; import java.util.*; import com.github.hydos.ginger.engine.common.obj.ModelLoader; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; public class Block { @@ -36,7 +36,7 @@ public class Block } } - public TexturedModel model; + public GLTexturedModel model; private final boolean visible, fullCube; private final float caveCarveThreshold; public final String identifier; @@ -52,7 +52,7 @@ public class Block { return this.caveCarveThreshold; } protected Block(Properties properties) - { this((TexturedModel) null, properties); } + { this((GLTexturedModel) null, properties); } protected Block(String texture, Properties properties) { @@ -60,7 +60,7 @@ public class Block this.texture = texture; } - protected Block(TexturedModel model, Properties properties) + protected Block(GLTexturedModel model, Properties properties) { this.model = model; this.visible = properties.visible; diff --git a/src/main/java/com/github/halotroop/litecraft/types/entity/Entity.java b/src/main/java/com/github/halotroop/litecraft/types/entity/Entity.java index dc6b0d6..3cbe9a3 100644 --- a/src/main/java/com/github/halotroop/litecraft/types/entity/Entity.java +++ b/src/main/java/com/github/halotroop/litecraft/types/entity/Entity.java @@ -3,10 +3,10 @@ package com.github.halotroop.litecraft.types.entity; import org.joml.Vector3f; import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; public abstract class Entity extends RenderObject { - public Entity(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) + public Entity(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) { super(model, position, rotX, rotY, rotZ, scale); } } diff --git a/src/main/java/com/github/halotroop/litecraft/world/World.java b/src/main/java/com/github/halotroop/litecraft/world/World.java index 77b7aa7..659f71a 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/World.java +++ b/src/main/java/com/github/halotroop/litecraft/world/World.java @@ -15,7 +15,7 @@ import com.github.halotroop.litecraft.world.gen.*; import com.github.halotroop.litecraft.world.gen.modifier.WorldModifier; import com.github.hydos.ginger.engine.common.elements.objects.Player; import com.github.hydos.ginger.engine.common.obj.ModelLoader; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import it.unimi.dsi.fastutil.longs.*; @@ -86,7 +86,7 @@ public class World implements BlockAccess, WorldGenConstants public Player spawnPlayer(float x, float y, float z) { // Player model and stuff - TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/soil/dirt.png"); + GLTexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/soil/dirt.png"); this.player = new Player(dirtModel, new Vector3f(x, y, z), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f)); this.player.setVisible(false); // Generate world around player diff --git a/src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java b/src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java index ed9f30e..793d99a 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java +++ b/src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java @@ -10,7 +10,7 @@ import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.common.math.Maths; import com.github.hydos.ginger.engine.opengl.render.Renderer; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import com.github.hydos.ginger.engine.opengl.render.shaders.StaticShader; import com.github.hydos.ginger.engine.opengl.utils.GlLoader; @@ -35,7 +35,7 @@ public class BlockRenderer extends Renderer implements WorldGenConstants shader.loadTransformationMatrix(transformationMatrix); } - public void prepareModel(TexturedModel model) + public void prepareModel(GLTexturedModel model) { GL30.glBindVertexArray(model.getRawModel().getVaoID()); GL20.glEnableVertexAttribArray(0); @@ -79,7 +79,7 @@ public class BlockRenderer extends Renderer implements WorldGenConstants for (BlockInstance entity : renderList) { if (entity != null && entity.getModel() != null) { - TexturedModel blockModel = entity.getModel(); + GLTexturedModel blockModel = entity.getModel(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, blockModel.getTexture().getTextureID()); prepBlockInstance(entity); GL11.glDrawElements(GL11.GL_TRIANGLES, blockModel.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index f4010ef..b739c78 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -10,9 +10,10 @@ 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.common.obj.ModelLoader; import com.github.hydos.ginger.engine.vulkan.*; import com.github.hydos.ginger.engine.vulkan.api.VKGinger; -import com.github.hydos.ginger.engine.vulkan.model.VKVertices; +import com.github.hydos.ginger.engine.vulkan.model.*; import com.github.hydos.ginger.engine.vulkan.render.renderers.*; import com.github.hydos.ginger.engine.vulkan.render.ubo.*; import com.github.hydos.ginger.engine.vulkan.shaders.*; @@ -265,7 +266,7 @@ public class VulkanStarter final VkQueue queue = createDeviceQueue(device, queueFamilyIndex); final long renderPass = ExampleRenderer.createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat); final long renderCommandPool = createCommandPool(device, queueFamilyIndex); - VKVertices vertices = TempMethods.createVertices(memoryProperties, device); + VKVertices vertices = VKModelConverter.convertModel(ModelLoader.getCubeMesh(), memoryProperties, device); Ubo ubo = new Ubo(memoryProperties, device); final long descriptorPool = createDescriptorPool(device); final long descriptorSetLayout = createDescriptorSetLayout(device); diff --git a/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java b/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java index 992ce5b..1304786 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java @@ -8,7 +8,7 @@ import com.github.halotroop.litecraft.world.gen.WorldGenConstants; import com.github.hydos.ginger.engine.common.Constants; import com.github.hydos.ginger.engine.common.api.GingerRegister; import com.github.hydos.ginger.engine.common.io.Window; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; public class Player extends RenderObject implements WorldGenConstants { @@ -17,7 +17,7 @@ public class Player extends RenderObject implements WorldGenConstants private boolean noWeight = true; // because the force of gravity on an object's mass is called WEIGHT, not gravity private int chunkX, chunkY, chunkZ; - public Player(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) + public Player(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) { super(model, position, rotX, rotY, rotZ, scale); this.chunkX = (int) position.x >> POS_SHIFT; diff --git a/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/RenderObject.java b/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/RenderObject.java index b473240..7b7bf92 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/RenderObject.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/RenderObject.java @@ -2,17 +2,17 @@ package com.github.hydos.ginger.engine.common.elements.objects; import org.joml.Vector3f; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; public class RenderObject { - private TexturedModel model; + private GLTexturedModel model; public Vector3f position; private float rotX = 0, rotY = 0, rotZ = 0; private Vector3f scale; private boolean visible = true; - public RenderObject(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) + public RenderObject(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) { this.model = model; this.position = position; @@ -31,7 +31,7 @@ public class RenderObject public void z(float z) { this.position.z = z; } - public TexturedModel getModel() + public GLTexturedModel getModel() { return model; } public Vector3f getPosition() @@ -63,7 +63,7 @@ public class RenderObject this.rotZ += dz; } - public void setModel(TexturedModel model) + public void setModel(GLTexturedModel model) { this.model = model; } public void setPosition(Vector3f position) diff --git a/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java b/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java index d642bd2..9523545 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java @@ -1,22 +1,26 @@ package com.github.hydos.ginger.engine.common.obj; import com.github.hydos.ginger.engine.common.obj.shapes.StaticCube; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture; import com.github.hydos.ginger.engine.opengl.utils.GlLoader; public class ModelLoader { - public static TexturedModel loadGenericCube(String cubeTexture) + public static GLTexturedModel loadGenericCube(String cubeTexture) { Mesh data = StaticCube.getCube(); - TexturedModel tm = new TexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(cubeTexture)); + GLTexturedModel tm = new GLTexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(cubeTexture)); return tm; } + + public static Mesh getCubeMesh() { + return StaticCube.getCube(); + } - public static TexturedModel loadModel(String objPath, String texturePath) + public static GLTexturedModel loadModel(String objPath, String texturePath) { Mesh data = OBJFileLoader.loadModel(objPath); - return new TexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath)); + return new GLTexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath)); } } diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerUtils.java b/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerUtils.java index 23e00fe..460c4ec 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerUtils.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerUtils.java @@ -9,16 +9,16 @@ import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture; public class GingerUtils { - public static TexturedModel createTexturedModel(String texturePath, String modelPath) + public static GLTexturedModel createTexturedModel(String texturePath, String modelPath) { - TexturedModel model = ModelLoader.loadModel(modelPath, texturePath); + GLTexturedModel model = ModelLoader.loadModel(modelPath, texturePath); return model; } - public static TexturedModel createTexturedModel(String texturePath, String modelPath, String normalMapPath) + public static GLTexturedModel createTexturedModel(String texturePath, String modelPath, String normalMapPath) { RawModel model = NormalMappedObjLoader.loadOBJ(modelPath); - TexturedModel texturedModel = new TexturedModel(model, new ModelTexture(texturePath)); + GLTexturedModel texturedModel = new GLTexturedModel(model, new ModelTexture(texturePath)); return texturedModel; } diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java index ebd0801..8786099 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java @@ -12,7 +12,7 @@ import com.github.hydos.ginger.engine.common.cameras.Camera; import com.github.hydos.ginger.engine.common.elements.GuiTexture; import com.github.hydos.ginger.engine.common.elements.objects.*; import com.github.hydos.ginger.engine.common.io.Window; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import com.github.hydos.ginger.engine.opengl.render.renderers.*; import com.github.hydos.ginger.engine.opengl.render.shaders.*; import com.github.hydos.ginger.engine.opengl.shadow.ShadowMapMasterRenderer; @@ -41,8 +41,8 @@ public class MasterRenderer private NormalMappingRenderer normalRenderer; private Matrix4f projectionMatrix; private ShadowMapMasterRenderer shadowMapRenderer; - private Map> entities = new HashMap>(); - private Map> normalMapEntities = new HashMap>(); + private Map> entities = new HashMap>(); + private Map> normalMapEntities = new HashMap>(); public MasterRenderer(Camera camera) { @@ -95,7 +95,7 @@ public class MasterRenderer private void processEntity(RenderObject entity) { - TexturedModel entityModel = entity.getModel(); + GLTexturedModel entityModel = entity.getModel(); List batch = entities.get(entityModel); if (batch != null) { @@ -111,7 +111,7 @@ public class MasterRenderer private void processEntityWithNormal(RenderObject entity) { - TexturedModel entityModel = entity.getModel(); + GLTexturedModel entityModel = entity.getModel(); List batch = normalMapEntities.get(entityModel); if (batch != null) { diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/models/TexturedModel.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/models/GLTexturedModel.java similarity index 80% rename from src/main/java/com/github/hydos/ginger/engine/opengl/render/models/TexturedModel.java rename to src/main/java/com/github/hydos/ginger/engine/opengl/render/models/GLTexturedModel.java index 708acfd..b86f9bf 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/models/TexturedModel.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/models/GLTexturedModel.java @@ -2,12 +2,12 @@ package com.github.hydos.ginger.engine.opengl.render.models; import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture; -public class TexturedModel +public class GLTexturedModel { private RawModel rawModel; private ModelTexture texture; - public TexturedModel(RawModel model, ModelTexture texture) + public GLTexturedModel(RawModel model, ModelTexture texture) { this.rawModel = model; this.texture = texture; diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/NormalMappingRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/NormalMappingRenderer.java index 01de0cd..d8af592 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/NormalMappingRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/NormalMappingRenderer.java @@ -48,7 +48,7 @@ public class NormalMappingRenderer extends Renderer shader.loadOffset(0, 0); } - private void prepareTexturedModel(TexturedModel model) + private void prepareTexturedModel(GLTexturedModel model) { RawModel rawModel = model.getRawModel(); GL30.glBindVertexArray(rawModel.getVaoID()); @@ -66,11 +66,11 @@ public class NormalMappingRenderer extends Renderer GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getNormalMap()); } - public void render(Map> entities, Vector4f clipPlane, List lights, Camera camera) + public void render(Map> entities, Vector4f clipPlane, List lights, Camera camera) { shader.start(); prepare(clipPlane, lights, camera); - for (TexturedModel model : entities.keySet()) + for (GLTexturedModel model : entities.keySet()) { prepareTexturedModel(model); List batch = entities.get(model); diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/ObjectRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/ObjectRenderer.java index 2a81add..b99c01d 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/ObjectRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/ObjectRenderer.java @@ -36,7 +36,7 @@ public class ObjectRenderer extends Renderer shader.loadTransformationMatrix(transformationMatrix); } - private void prepareTexturedModel(TexturedModel model) + private void prepareTexturedModel(GLTexturedModel model) { RawModel rawModel = model.getRawModel(); GL30.glBindVertexArray(rawModel.getVaoID()); @@ -58,9 +58,9 @@ public class ObjectRenderer extends Renderer GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID()); } - public void render(Map> entities) + public void render(Map> entities) { - for (TexturedModel model : entities.keySet()) + for (GLTexturedModel model : entities.keySet()) { prepareTexturedModel(model); List batch = entities.get(model); @@ -94,7 +94,7 @@ public class ObjectRenderer extends Renderer { if (entity != null && entity.getModel() != null) { - TexturedModel model = entity.getModel(); + GLTexturedModel model = entity.getModel(); prepareTexturedModel(model); prepareInstance(entity); GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapEntityRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapEntityRenderer.java index 70e021e..cb7648f 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapEntityRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapEntityRenderer.java @@ -60,9 +60,9 @@ public class ShadowMapEntityRenderer * * @param entities * - the entities to be rendered to the shadow map. */ - protected void render(Map> entities) + protected void render(Map> entities) { - for (TexturedModel model : entities.keySet()) + for (GLTexturedModel model : entities.keySet()) { RawModel rawModel = model.getRawModel(); bindModel(rawModel); diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapMasterRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapMasterRenderer.java index 0e27772..3c542e1 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapMasterRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/shadow/ShadowMapMasterRenderer.java @@ -8,7 +8,7 @@ import org.lwjgl.opengl.GL11; import com.github.hydos.ginger.engine.common.cameras.Camera; import com.github.hydos.ginger.engine.common.elements.objects.*; -import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; +import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; /** This class is in charge of using all of the classes in the shadows package to * carry out the shadow render pass, i.e. rendering the scene to the shadow map @@ -130,11 +130,11 @@ public class ShadowMapMasterRenderer * * @param entities * - the lists of entities to be rendered. Each list is - * associated with the {@link TexturedModel} that all of the + * associated with the {@link GLTexturedModel} that all of the * entities in that list use. * @param sun * - the light acting as the sun in the scene. */ - public void render(Map> entities, Light sun) + public void render(Map> entities, Light sun) { shadowBox.update(); Vector3f sunPosition = sun.getPosition(); diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java deleted file mode 100644 index db55cea..0000000 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/TempMethods.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.github.hydos.ginger.engine.vulkan; - -import static org.lwjgl.system.MemoryUtil.*; - -import java.nio.*; - -import org.lwjgl.PointerBuffer; -import org.lwjgl.vulkan.*; - -import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; -import com.github.hydos.ginger.engine.vulkan.model.VKVertices; -import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; - -public class TempMethods -{ - - public static VKVertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) - { - ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4); - FloatBuffer fb = vertexBuffer.asFloatBuffer(); - // first triangle - fb.put(-0.5f).put(-0.5f).put(0.5f).put(1.0f).put(0.0f).put(0.0f); - fb.put(0.5f).put(-0.5f).put(0.5f).put(0.0f).put(1.0f).put(0.0f); - fb.put(0.0f).put(0.5f).put(0.5f).put(0.0f).put(0.0f).put(1.0f); - // second triangle - fb.put(0.5f).put(-0.5f).put(-0.5f).put(1.0f).put(1.0f).put(0.0f); - fb.put(-0.5f).put(-0.5f).put(-0.5f).put(0.0f).put(1.0f).put(1.0f); - fb.put(0.0f).put(0.5f).put(-0.5f).put(1.0f).put(0.0f).put(1.0f); - VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc() - .sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); - VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); - int err; - // Generate vertex buffer - // Setup - VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc() - .sType(VK12.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) - .size(vertexBuffer.remaining()) - .usage(VK12.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); - LongBuffer pBuffer = memAllocLong(1); - err = VK12.vkCreateBuffer(device, bufInfo, null, pBuffer); - long verticesBuf = pBuffer.get(0); - memFree(pBuffer); - bufInfo.free(); - if (err != VK12.VK_SUCCESS) - { throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); } - VK12.vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); - memAlloc.allocationSize(memReqs.size()); - IntBuffer memoryTypeIndex = memAllocInt(1); - VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); - memAlloc.memoryTypeIndex(memoryTypeIndex.get(0)); - memFree(memoryTypeIndex); - memReqs.free(); - LongBuffer pMemory = memAllocLong(1); - err = VK12.vkAllocateMemory(device, memAlloc, null, pMemory); - long verticesMem = pMemory.get(0); - memFree(pMemory); - if (err != VK12.VK_SUCCESS) - { throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); } - PointerBuffer pData = memAllocPointer(1); - err = VK12.vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData); - memAlloc.free(); - long data = pData.get(0); - memFree(pData); - if (err != VK12.VK_SUCCESS) - { throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); } - memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining()); - memFree(vertexBuffer); - VK12.vkUnmapMemory(device, verticesMem); - err = VK12.vkBindBufferMemory(device, verticesBuf, verticesMem, 0); - if (err != VK12.VK_SUCCESS) - { throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); } - // Binding description - VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1) - .binding(0) // <- we bind our vertex buffer to point 0 - .stride((3 + 3) * 4) - .inputRate(VK12.VK_VERTEX_INPUT_RATE_VERTEX); - // Attribute descriptions - // Describes memory layout and shader attribute locations - VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2); - // Location 0 : Position - attributeDescriptions.get(0) - .binding(0) // <- binding point used in the VkVertexInputBindingDescription - .location(0) // <- location in the shader's attribute layout (inside the shader source) - .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) - .offset(0); - // Location 1 : Color - attributeDescriptions.get(1) - .binding(0) // <- binding point used in the VkVertexInputBindingDescription - .location(1) // <- location in the shader's attribute layout (inside the shader source) - .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) - .offset(3 * 4); - // Assign to vertex buffer - VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc(); - vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); - vi.pVertexBindingDescriptions(bindingDescriptor); - vi.pVertexAttributeDescriptions(attributeDescriptions); - VKVertices ret = new VKVertices(); - ret.createInfo = vi; - ret.vkVerticiesBuffer = verticesBuf; - return ret; - } - -} diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java index 4635436..d3044c4 100644 --- a/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/model/VKModelConverter.java @@ -1,11 +1,98 @@ package com.github.hydos.ginger.engine.vulkan.model; +import static org.lwjgl.system.MemoryUtil.*; + +import java.nio.*; + +import org.lwjgl.PointerBuffer; +import org.lwjgl.vulkan.*; + +import com.github.hydos.ginger.engine.common.obj.Mesh; +import com.github.hydos.ginger.engine.vulkan.memory.VKMemory; +import com.github.hydos.ginger.engine.vulkan.utils.VKUtils; + public class VKModelConverter { - public VKVertices toVKVerticies() + public static VKVertices convertModel(Mesh mesh, VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device) { - return null; + ByteBuffer vertexBuffer = memAlloc(mesh.getVertices().length * 4); + FloatBuffer bufferVertices = vertexBuffer.asFloatBuffer(); + for(float vertex: mesh.getVertices()) { + bufferVertices.put(vertex); + } + VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); + VkMemoryRequirements memReqs = VkMemoryRequirements.calloc(); + int err; + // Generate vertex buffer + // Setup + VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc() + .sType(VK12.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO) + .size(vertexBuffer.remaining()) + .usage(VK12.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + LongBuffer pBuffer = memAllocLong(1); + err = VK12.vkCreateBuffer(device, bufInfo, null, pBuffer); + long verticesBuf = pBuffer.get(0); + memFree(pBuffer); + bufInfo.free(); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); } + VK12.vkGetBufferMemoryRequirements(device, verticesBuf, memReqs); + memAlloc.allocationSize(memReqs.size()); + IntBuffer memoryTypeIndex = memAllocInt(1); + VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex); + memAlloc.memoryTypeIndex(memoryTypeIndex.get(0)); + memFree(memoryTypeIndex); + memReqs.free(); + LongBuffer pMemory = memAllocLong(1); + err = VK12.vkAllocateMemory(device, memAlloc, null, pMemory); + long verticesMem = pMemory.get(0); + memFree(pMemory); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); } + PointerBuffer pData = memAllocPointer(1); + err = VK12.vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData); + memAlloc.free(); + long data = pData.get(0); + memFree(pData); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); } + memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining()); + memFree(vertexBuffer); + VK12.vkUnmapMemory(device, verticesMem); + err = VK12.vkBindBufferMemory(device, verticesBuf, verticesMem, 0); + if (err != VK12.VK_SUCCESS) + { throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); } + // Binding description + VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1) + .binding(0) // <- we bind our vertex buffer to point 0 + .stride((3 + 3) * 4) + .inputRate(VK12.VK_VERTEX_INPUT_RATE_VERTEX); + // Attribute descriptions + // Describes memory layout and shader attribute locations + VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2); + // Location 0 : Position + attributeDescriptions.get(0) + .binding(0) // <- binding point used in the VkVertexInputBindingDescription + .location(0) // <- location in the shader's attribute layout (inside the shader source) + .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) + .offset(0); + // Location 1 : Color + attributeDescriptions.get(1) + .binding(0) // <- binding point used in the VkVertexInputBindingDescription + .location(1) // <- location in the shader's attribute layout (inside the shader source) + .format(VK12.VK_FORMAT_R32G32B32_SFLOAT) + .offset(3 * 4); + // Assign to vertex buffer + VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc(); + vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO); + vi.pVertexBindingDescriptions(bindingDescriptor); + vi.pVertexAttributeDescriptions(attributeDescriptions); + VKVertices ret = new VKVertices(); + ret.createInfo = vi; + ret.vkVerticiesBuffer = verticesBuf; + return ret; } } From 4f4e41d990f7e2ac2a6859d482efd684adcaca85 Mon Sep 17 00:00:00 2001 From: hYdos Date: Tue, 3 Mar 2020 16:16:41 +1000 Subject: [PATCH 8/8] yus --- .../java/com/github/hydos/ginger/VulkanStarter.java | 6 +++--- .../hydos/ginger/engine/vulkan/VKRenderRegister.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/github/hydos/ginger/engine/vulkan/VKRenderRegister.java diff --git a/src/main/java/com/github/hydos/ginger/VulkanStarter.java b/src/main/java/com/github/hydos/ginger/VulkanStarter.java index b739c78..269fb83 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanStarter.java +++ b/src/main/java/com/github/hydos/ginger/VulkanStarter.java @@ -354,12 +354,11 @@ public class VulkanStarter float time = 0.0f; while (!GLFW.glfwWindowShouldClose(Window.getWindow())) { - // Handle window messages. Resize events happen exactly here. - // So it is safe to use the new swapchain images and framebuffers afterwards. GLFW.glfwPollEvents(); if (swapchainRecreator.mustRecreate) swapchainRecreator.recreate(); - // Create a semaphore to wait for the swapchain to acquire the next image + + err = VK12.vkCreateSemaphore(device, semaphoreCreateInfo, null, pImageAcquiredSemaphore); if (err != VK12.VK_SUCCESS) { throw new AssertionError("Failed to create image acquired semaphore: " + VKUtils.translateVulkanResult(err)); } @@ -381,6 +380,7 @@ public class VulkanStarter lastTime = thisTime; ubo.updateUbo(device, time); // Submit to the graphics queue + err = VK12.vkQueueSubmit(queue, submitInfo, VK12.VK_NULL_HANDLE); if (err != VK12.VK_SUCCESS) { throw new AssertionError("Failed to submit render queue: " + VKUtils.translateVulkanResult(err)); } diff --git a/src/main/java/com/github/hydos/ginger/engine/vulkan/VKRenderRegister.java b/src/main/java/com/github/hydos/ginger/engine/vulkan/VKRenderRegister.java new file mode 100644 index 0000000..d7d71cd --- /dev/null +++ b/src/main/java/com/github/hydos/ginger/engine/vulkan/VKRenderRegister.java @@ -0,0 +1,10 @@ +package com.github.hydos.ginger.engine.vulkan; + +import org.lwjgl.vulkan.VkQueue; + +public class VKRenderRegister +{ + public static VkQueue renderQueue;//used for queueing up render updates + + +}