#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_VULKAN #include "backends/imgui_impl_glfw.h" #include "backends/imgui_impl_vulkan.h" #include "window.hpp" #include "asset-manager.hpp" #include "camera.hpp" #include "ecs.hpp" #include "font.hpp" #include "game-settings.hpp" #include "math-helpers.hpp" #include "pk.h" #include "plugins.hpp" #include "static-cube.hpp" #include "static-missing-texture.hpp" #include "window-types.hpp" #include "glm/ext/matrix_transform.hpp" #include struct pk_membucket *MemBkt_Vulkan = nullptr; struct pke_vkAllocData { void *data; std::size_t size; }; DynArray *vulkanAllocs = nullptr; #define NELEMS(x) (sizeof(x) / sizeof((x)[0])) const bool ENABLE_VALIDATION_LAYERS = true; const bool VULKAN_DEBUG_REPORT = true; bool pke_window_did_init_imgui = false; /* * Initialization */ GLFWwindow *window = nullptr; GLFWmonitor *monitor = nullptr; GLFWvidmode vidMode; VkInstance vkInstance = nullptr; VkPhysicalDevice vkPhysicalDevice = nullptr; VkPhysicalDeviceProperties vkPhysicalDeviceProperties; VkPhysicalDeviceMemoryProperties vkPhysicalDeviceMemoryProperties; VkDevice vkDevice = nullptr; VkQueue presentQueue = nullptr; VkSurfaceKHR vkSurfaceKHR = nullptr; VkDebugReportCallbackEXT vkDebugReport = nullptr; VkAllocationCallbacks vkAllocatorStruct = {}; VkAllocationCallbacks *vkAllocator = nullptr; unsigned int graphicsFamilyIndex; unsigned int presentFamilyIndex; unsigned int transferFamilyIndex; /* * Instantiation */ bool shouldRecreateSwapchain = false; unsigned int CURRENT_FRAME = 0; unsigned int selectedSurfaceIndex = -1u; unsigned int prevSwapchainLength = 0u; unsigned int swapchainLength = 0u; VkSwapchainKHR vkSwapchainKHR = VK_NULL_HANDLE; VkSurfaceFormatKHR vkSurfaceFormatKHR; VkFormat depthFormat; VkPresentModeKHR vkPresentModeKHR = VK_PRESENT_MODE_FIFO_KHR; VkExtent2D Extent; VkSampler global_sampler; VkSampleCountFlagBits global_sample_count; // pkvk_shared.descr_set_layout.single_sampler // the pke default, works for: // 2025-01-28 - present // 2025-01-28 - standard 3d struct PKVK_Shared { struct PKVK_Shared_Command_Pool { VkCommandPool transfer; VkCommandPool graphics; } command_pool; struct PKVK_Shared_Command_Buffer { VkCommandBuffer transfer; VkCommandBuffer graphics; } command_buffer; struct PKVK_Shared_Queue { VkQueue transfer; VkQueue graphics; } queue; } pkvk_shared; struct PKVK_Present { VkDescriptorSetLayout descriptor_set_layout; VkPipelineLayout pipeline_layout; VkPipeline pipeline; VkRenderPass render_pass; VkDescriptorPool descriptor_pool; VkDescriptorSet *descriptor_sets; VkCommandBuffer *command_buffers; VkSemaphore *semaphores_image_available; VkSemaphore *semaphores_render_finished; VkFence *fences_in_flight; VkImage *images; VkImageView *image_views; VkFramebuffer *framebuffers; } pkvk_present; // TODO consider creating a depth pre-pass // 2025-01-29 JCB // I know this is a popular thing to do, but I want to avoid it until we // actually need it. // Unsure how we would want to handle 2d - we could either intentionally // go for painter's method, or maybe there's a good way to declare depth // in 2d that I am not thinking of at the moment. VkImage *depthImages = nullptr; VkImageView *depthImageViews = nullptr; VkDeviceMemory depthImagesMemory; struct PKVK_3D { VkRenderPass render_pass; VkFramebuffer *framebuffers = nullptr; VkDeviceMemory images_memory; VkImageView *image_views = nullptr; VkImage *render_images = nullptr; VkImage *images_color_resolve = nullptr; VkImageView *image_views_color_resolve = nullptr; VkDeviceMemory images_memory_color_resolve; } pkvk_3d; struct PKVK_2D { VkRenderPass render_pass; VkFramebuffer *framebuffers = nullptr; VkImage *images = nullptr; VkImageView *image_views = nullptr; VkDeviceMemory images_memory; VkImage *images_color_resolve = nullptr; VkImageView *image_views_color_resolve = nullptr; VkDeviceMemory images_memory_color_resolve; } pkvk_2d; UniformBufferObject UBO{ .model = glm::mat4(1), .view = glm::lookAt(glm::vec3(0), glm::vec3(2, 2, 0), glm::vec3(0, 1, 0)), .proj = glm::mat4(1), }; VkDeviceMemory uniformBufferMemory; VkDeviceSize paddedUboBufferSize; // public VkBuffer *UniformBuffers; DebugHitbox pkeDebugHitbox{}; ImplementedPKVK pkePipelines{}; /* * ImGui */ VkDescriptorPool imGuiDescriptorPool; const std::vector REQUIRED_EXTENSIONS = std::vector { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; static void ImGuiCheckVkResult(VkResult err) { if (err == 0) { return; } fprintf(stderr, "[imgui][vulkan] Error: VkResult = %d\n", err); if (err < 0) { abort(); } } VkBool32 UserDebugCallback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData) { (void)messageSeverity; (void)messageType; (void)pUserData; printf("Validation Layer: %s\n", pCallbackData->pMessage); return VK_FALSE; } static VKAPI_ATTR VkBool32 VKAPI_CALL DebugReport(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, unsigned long object, size_t location, int messageCode, const char *pLayerPrefix, const char *pMessage, void *pUserData) { (void)flags; (void)object; (void)location; (void)messageCode; (void)pUserData; (void)pLayerPrefix; // Unused arguments fprintf(stderr, "[vulkan] Debug report from ObjectType: %i\nMessage: %s\n\n", objectType, pMessage); return VK_FALSE; } unsigned int FindMemoryTypeIndex(uint32_t typeFilter, VkMemoryPropertyFlags memPropertyFlags) { for (uint32_t i = 0; i < vkPhysicalDeviceMemoryProperties.memoryTypeCount; i++) { if ((typeFilter & (1 << i)) && (vkPhysicalDeviceMemoryProperties.memoryTypes[i].propertyFlags & memPropertyFlags) == memPropertyFlags) { return i; } } fprintf(stdout, "[vulkan] Failed to find appropriate memory type index: typeFilter: %u - memPropertyFlags: %u\n", typeFilter, memPropertyFlags); return 0; } VkFormat FindSupportedFormat(long candidateCount, const VkFormat *candidates, VkImageTiling tiling, VkFormatFeatureFlags flags) { for (long i = 0; i < candidateCount; ++i) { VkFormatProperties props; vkGetPhysicalDeviceFormatProperties(vkPhysicalDevice, candidates[i], &props); if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & flags) == flags) { return candidates[i]; } else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & flags) == flags) { return candidates[i]; } } fprintf(stdout, "[vulkan] Failed to find appropriate format: tiling: %u - flags: %u\n", tiling, flags); return VkFormat(0); } VkCommandBuffer GetCommandBuffer_Graphics() { return pkvk_shared.command_buffer.graphics; } VkCommandBuffer GetCommandBuffer_Transfer() { return pkvk_shared.command_buffer.transfer; } // TODO fence void PKVK_BeginBuffer(unsigned int family_index, VkDeviceSize requestedMemorySize, PKVK_TmpBufferDetails &tmpBufferDetails, PKVK_TmpBufferFlags flags) { if (family_index == graphicsFamilyIndex) { tmpBufferDetails.queue = pkvk_shared.queue.graphics; tmpBufferDetails.cmdBuffer = pkvk_shared.command_buffer.graphics; } else if (family_index == transferFamilyIndex) { tmpBufferDetails.queue = pkvk_shared.queue.transfer; tmpBufferDetails.cmdBuffer = pkvk_shared.command_buffer.transfer; } else { throw "Unhandled queue family"; } if (requestedMemorySize == 0) return; VkBufferCreateInfo transferBufferCI; transferBufferCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; transferBufferCI.pNext = nullptr; transferBufferCI.flags = 0; transferBufferCI.size = requestedMemorySize; transferBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; transferBufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE; transferBufferCI.queueFamilyIndexCount = 1; transferBufferCI.pQueueFamilyIndices = &family_index; VkResult vkResult = vkCreateBuffer(vkDevice, &transferBufferCI, vkAllocator, &tmpBufferDetails.buffer); assert(vkResult == VK_SUCCESS); VkMemoryRequirements memoryRequirements; vkGetBufferMemoryRequirements(vkDevice, tmpBufferDetails.buffer, &memoryRequirements); VkMemoryAllocateInfo transferMemAllocInfo; transferMemAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; transferMemAllocInfo.pNext = nullptr; transferMemAllocInfo.allocationSize = memoryRequirements.size; transferMemAllocInfo.memoryTypeIndex = FindMemoryTypeIndex(memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); vkResult = vkAllocateMemory(vkDevice, &transferMemAllocInfo, vkAllocator, &tmpBufferDetails.deviceMemory); assert(vkResult == VK_SUCCESS); vkResult = vkBindBufferMemory(vkDevice, tmpBufferDetails.buffer, tmpBufferDetails.deviceMemory, 0); assert(vkResult == VK_SUCCESS); if (!PK_HAS_FLAG(flags, PKVK_TmpBufferFlags_MAP_MEMORY)) return; vkResult = vkMapMemory(vkDevice, tmpBufferDetails.deviceMemory, 0, transferMemAllocInfo.allocationSize, 0, &tmpBufferDetails.deviceData); assert(vkResult == VK_SUCCESS); } void PKVK_EndBuffer(PKVK_TmpBufferDetails &tmpBufferDetails, PKVK_TmpBufferFlags flags) { if (PK_HAS_FLAG(flags, PKVK_TmpBufferFlags_MAP_MEMORY)) { if (tmpBufferDetails.deviceMemory != VK_NULL_HANDLE) vkUnmapMemory(vkDevice, tmpBufferDetails.deviceMemory); } if (tmpBufferDetails.buffer != VK_NULL_HANDLE) vkDestroyBuffer(vkDevice, tmpBufferDetails.buffer, vkAllocator); if (tmpBufferDetails.deviceMemory != VK_NULL_HANDLE) vkFreeMemory(vkDevice, tmpBufferDetails.deviceMemory, vkAllocator); tmpBufferDetails = {}; } unsigned int FindQueueFamilyIndex(VkPhysicalDevice device, char hasPresentSupport = -1, VkQueueFlagBits includeBits = (VkQueueFlagBits)0U, VkQueueFlagBits excludeBits = (VkQueueFlagBits)0U) { if (hasPresentSupport == -1 && includeBits == 0 && excludeBits == 0) { return 0U; } unsigned int queueFamilyPropertyCount = 0U; vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyPropertyCount, nullptr); auto *queueFamilyProperties = pk_new(queueFamilyPropertyCount); vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyPropertyCount, queueFamilyProperties); for (unsigned int i = 0; i < queueFamilyPropertyCount; i++) { if (includeBits != 0 && (queueFamilyProperties[i].queueFlags & includeBits) == 0) { continue; } if (excludeBits != 0 && (queueFamilyProperties[i].queueFlags & excludeBits) != 0) { continue; } if (hasPresentSupport != -1) { VkBool32 presentSupport; VkResult vkResult = vkGetPhysicalDeviceSurfaceSupportKHR(device, i, vkSurfaceKHR, &presentSupport); assert(vkResult == VK_SUCCESS); if (presentSupport != (VkBool32)hasPresentSupport) { continue; } } pk_delete(queueFamilyProperties, queueFamilyPropertyCount); return i; } pk_delete(queueFamilyProperties, queueFamilyPropertyCount); return 0xFFFFFFFF; } void PKE_vkFreeFunction(void *pUserData, void *pMemory) { (void)pUserData; if (pMemory == nullptr) return; pke_vkAllocData *chunk = nullptr; size_t index = -1; int64_t count = vulkanAllocs->Count(); for (int64_t i = 0; i < count; ++i) { if ((*vulkanAllocs)[i].data == pMemory) { chunk = &(*vulkanAllocs)[i]; index = i; break; } } if (chunk != nullptr) { assert(chunk != nullptr); pk_delete_bkt(chunk->data, chunk->size, MemBkt_Vulkan); vulkanAllocs->Remove(index); } else { fprintf(stderr, "%s\n", "PKE_vkFreeFunction - untracked pointer"); } } void *PKE_vkAllocateFunction(void *pUserData, size_t size, size_t alignment, VkSystemAllocationScope allocScope) { (void)pUserData; (void)allocScope; if (size == 0) { return nullptr; } void *ptr = pk_new_bkt(size, alignment, MemBkt_Vulkan); vulkanAllocs->Push({ .data = ptr, .size = size, }); return ptr; } void *PKE_vkReallocationFunction(void *pUserData, void *pOriginal, size_t size, size_t alignment, VkSystemAllocationScope allocScope) { (void)pUserData; if (pOriginal == nullptr) { return PKE_vkAllocateFunction(pUserData, size, alignment, allocScope); } if (size == 0) { PKE_vkFreeFunction(pUserData, pOriginal); return nullptr; } pke_vkAllocData *chunk = nullptr; size_t index = -1; int64_t count = vulkanAllocs->Count(); for (int64_t i = 0; i < count; ++i) { if ((*vulkanAllocs)[i].data == pOriginal) { chunk = &((*vulkanAllocs)[i]); index = i; break; } } if (chunk != nullptr) { if (chunk->size >= size) { return pOriginal; } } void *newPtr = PKE_vkAllocateFunction(pUserData, size, alignment, allocScope); if (chunk != nullptr) { memcpy(newPtr, chunk->data, (chunk->size < size ? chunk->size : size) - 1); pk_delete_bkt(chunk->data, chunk->size, MemBkt_Vulkan); vulkanAllocs->Remove(index); } else { } return newPtr; } void PKE_vkInternalAllocationNotification(void *pUserData, size_t size, VkInternalAllocationType allocationType, VkSystemAllocationScope allocationScope) { (void)pUserData; (void)size; (void)allocationType; (void)allocationScope; return; } void PKE_vkInternalFreeNotification(void *pUserData, size_t size, VkInternalAllocationType allocationType, VkSystemAllocationScope allocationScope) { (void)pUserData; (void)size; (void)allocationType; (void)allocationScope; return; } void InitVulkan() { vkAllocator = pk_new(MemBkt_Vulkan); vkAllocator->pUserData = nullptr; vkAllocator->pfnAllocation = PKE_vkAllocateFunction; vkAllocator->pfnReallocation = PKE_vkReallocationFunction; vkAllocator->pfnFree = PKE_vkFreeFunction; vkAllocator->pfnInternalAllocation = PKE_vkInternalAllocationNotification; vkAllocator->pfnInternalFree = PKE_vkInternalFreeNotification; VkResult vkResult; if (ENABLE_VALIDATION_LAYERS) { unsigned int layerCount; vkResult = vkEnumerateInstanceLayerProperties(&layerCount, nullptr); assert(vkResult == VK_SUCCESS); VkLayerProperties *availableLayerProperties = pk_new(layerCount); vkResult = vkEnumerateInstanceLayerProperties(&layerCount, availableLayerProperties); assert(vkResult == VK_SUCCESS); printf("Available Layers:\n"); for (long i = 0; i < layerCount; ++i) { printf("\t%s\n", availableLayerProperties[i].layerName); } pk_delete(availableLayerProperties, layerCount); } VkApplicationInfo appInfo; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.pNext = nullptr; appInfo.pApplicationName = "pikul"; appInfo.applicationVersion = 1; appInfo.pEngineName = "pikul"; appInfo.engineVersion = 1; appInfo.apiVersion = VK_API_VERSION_1_3; VkInstanceCreateInfo createInfo; createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; createInfo.pNext = nullptr; createInfo.flags = {}; createInfo.pApplicationInfo = &appInfo; createInfo.enabledLayerCount = 0; std::vector enabledLayerNames(1); enabledLayerNames[0] = "VK_LAYER_KHRONOS_validation"; printf("Requested Layers:\n"); for (const char *s : enabledLayerNames) { printf("\t%s\n", s); } VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo; debugCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; debugCreateInfo.pNext = nullptr; debugCreateInfo.flags = {}; if (ENABLE_VALIDATION_LAYERS) { createInfo.enabledLayerCount = enabledLayerNames.size(); createInfo.ppEnabledLayerNames = enabledLayerNames.data(); debugCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; debugCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; debugCreateInfo.pfnUserCallback = UserDebugCallback; debugCreateInfo.pUserData = nullptr; createInfo.pNext = &debugCreateInfo; } std::vector allGlfwExtensions; { unsigned int extensionCount = 0; const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&extensionCount); allGlfwExtensions.reserve(extensionCount + 2); copy(&glfwExtensions[0], &glfwExtensions[extensionCount], back_inserter(allGlfwExtensions)); if (ENABLE_VALIDATION_LAYERS) { allGlfwExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); allGlfwExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); } createInfo.enabledExtensionCount = allGlfwExtensions.size(); createInfo.ppEnabledExtensionNames = allGlfwExtensions.data(); } printf("Required Extensions:\n"); for (const auto &ext : allGlfwExtensions) { printf("\t%s\n", ext); } if (ENABLE_VALIDATION_LAYERS) { unsigned int extensionCount = 0; vkResult = vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); assert(vkResult == VK_SUCCESS); auto *extensions = pk_new(extensionCount); vkResult = vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions); assert(vkResult == VK_SUCCESS); printf("Available Extensions:\n"); for (long i = 0; i < extensionCount; ++i) { printf("\t%s\n", extensions[i].extensionName); } pk_delete(extensions, extensionCount); } vkResult = vkCreateInstance(&createInfo, vkAllocator, &vkInstance); if (vkResult != VK_SUCCESS) { printf("Failed to create VkInstance! : %d\n", vkResult); throw vkResult; } if (VULKAN_DEBUG_REPORT) { PFN_vkCreateDebugReportCallbackEXT func = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(vkInstance, "vkCreateDebugReportCallbackEXT"); assert(func != nullptr && "vkCreateDebugReportCallbackEXT ProcAddr was nullptr"); VkDebugReportCallbackCreateInfoEXT debugReportCallbackCreateInfo; debugReportCallbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT; debugReportCallbackCreateInfo.pNext = nullptr; debugReportCallbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; debugReportCallbackCreateInfo.pfnCallback = DebugReport; debugReportCallbackCreateInfo.pUserData = nullptr; vkResult = func(vkInstance, &debugReportCallbackCreateInfo, vkAllocator, &vkDebugReport); if (vkResult != VK_SUCCESS) { fprintf(stderr, "%s\n", "Failed to create debug report!"); } } // create surface if (glfwCreateWindowSurface(vkInstance, window, vkAllocator, &vkSurfaceKHR) != VK_SUCCESS) { throw "Failed to create window surface"; } // pick physical device unsigned int physicalDeviceCount = 0; vkResult = vkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, nullptr); assert(vkResult == VK_SUCCESS); assert(physicalDeviceCount > 0); auto *physicalDevices = pk_new(physicalDeviceCount); vkResult = vkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, physicalDevices); assert(vkResult == VK_SUCCESS); graphicsFamilyIndex = 0; presentFamilyIndex = 0; for (long i = 0; i < physicalDeviceCount; ++i) { const auto &device = physicalDevices[i]; // check queue families graphicsFamilyIndex = FindQueueFamilyIndex(device, -1, VK_QUEUE_GRAPHICS_BIT); presentFamilyIndex = FindQueueFamilyIndex(device, 1); if (graphicsFamilyIndex == 0xFFFFFFFF || presentFamilyIndex == 0xFFFFFFFF) { continue; } // check device extension support std::vector requiredExtensions(REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end()); unsigned int extensionCount = 0; vkResult = vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr); assert(vkResult == VK_SUCCESS); auto *extensionProperties = pk_new(extensionCount); vkResult = vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, extensionProperties); assert(vkResult == VK_SUCCESS); for (long k = 0; k < extensionCount; ++k) { const auto &property = extensionProperties[k]; auto it = requiredExtensions.begin(); while (it != requiredExtensions.end()) { if (strcmp(*it.base(), property.extensionName)) { it = requiredExtensions.erase(it); } else { it++; } } } pk_delete(extensionProperties, extensionCount); if (requiredExtensions.empty() == false) { continue; } // surface formats unsigned int surfaceCount = 0; vkResult = vkGetPhysicalDeviceSurfaceFormatsKHR(device, vkSurfaceKHR, &surfaceCount, nullptr); assert(vkResult == VK_SUCCESS); if (surfaceCount == 0) { continue; } // check present modes unsigned int presentModeCount = 0; vkResult = vkGetPhysicalDeviceSurfacePresentModesKHR(device, vkSurfaceKHR, &presentModeCount, nullptr); assert(vkResult == VK_SUCCESS); if (presentModeCount == 0) { continue; } vkPhysicalDevice = device; vkGetPhysicalDeviceProperties(vkPhysicalDevice, &vkPhysicalDeviceProperties); printf("Selected VkPhysicalDevice: %s\n", vkPhysicalDeviceProperties.deviceName); vkGetPhysicalDeviceMemoryProperties(vkPhysicalDevice, &vkPhysicalDeviceMemoryProperties); break; } assert(vkPhysicalDevice != nullptr && "Failed to find suitable physical device"); pk_delete(physicalDevices, physicalDeviceCount); // Create logical device { transferFamilyIndex = FindQueueFamilyIndex(vkPhysicalDevice, 0, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); if (transferFamilyIndex == 0xFFFFFFFF) { transferFamilyIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); } if (transferFamilyIndex == 0xFFFFFFFF) { transferFamilyIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT); } if (transferFamilyIndex == 0xFFFFFFFF) { printf("Failed to find transfer queue index\n"); throw("Failed to find transfer queue index"); } VkDeviceQueueCreateInfo deviceQueueCreateInfos[2]; float graphicsPriorities[1] = { 1.0f }; deviceQueueCreateInfos[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; deviceQueueCreateInfos[0].pNext = nullptr; deviceQueueCreateInfos[0].flags = 0; deviceQueueCreateInfos[0].queueFamilyIndex = graphicsFamilyIndex; deviceQueueCreateInfos[0].queueCount = 1l; deviceQueueCreateInfos[0].pQueuePriorities = graphicsPriorities; float transferPriorities[1] = { 0.5f }; deviceQueueCreateInfos[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; deviceQueueCreateInfos[1].pNext = nullptr; deviceQueueCreateInfos[1].flags = 0; deviceQueueCreateInfos[1].queueFamilyIndex = transferFamilyIndex; deviceQueueCreateInfos[1].queueCount = 1l; deviceQueueCreateInfos[1].pQueuePriorities = transferPriorities; VkPhysicalDeviceFeatures vkPhysicalDeviceFeatures; vkGetPhysicalDeviceFeatures(vkPhysicalDevice, &vkPhysicalDeviceFeatures); VkPhysicalDeviceFeatures requestedFeatures{}; // {} to initialize everything to 0 requestedFeatures.samplerAnisotropy = VK_TRUE; requestedFeatures.sampleRateShading = VK_TRUE; requestedFeatures.wideLines = VK_TRUE; requestedFeatures.fillModeNonSolid = vkPhysicalDeviceFeatures.fillModeNonSolid; VkDeviceCreateInfo vkDeviceCreateInfo; vkDeviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; vkDeviceCreateInfo.pNext = nullptr; vkDeviceCreateInfo.flags = 0; vkDeviceCreateInfo.queueCreateInfoCount = 2; vkDeviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfos; if (ENABLE_VALIDATION_LAYERS) { vkDeviceCreateInfo.enabledLayerCount = enabledLayerNames.size(); vkDeviceCreateInfo.ppEnabledLayerNames = enabledLayerNames.data(); } vkDeviceCreateInfo.enabledExtensionCount = REQUIRED_EXTENSIONS.size(); vkDeviceCreateInfo.ppEnabledExtensionNames = REQUIRED_EXTENSIONS.data(); vkDeviceCreateInfo.pEnabledFeatures = &requestedFeatures; vkResult = vkCreateDevice(vkPhysicalDevice, &vkDeviceCreateInfo, vkAllocator, &vkDevice); if (vkResult != VK_SUCCESS) { printf("Failed to create VkInstance! : %d\n", vkResult); throw vkResult; } } // queues { vkGetDeviceQueue(vkDevice, graphicsFamilyIndex, 0, &pkvk_shared.queue.graphics); vkGetDeviceQueue(vkDevice, presentFamilyIndex, 0, &presentQueue); vkGetDeviceQueue(vkDevice, transferFamilyIndex, 0, &pkvk_shared.queue.transfer); } // surface formats { const VkFormat acceptableDepthFormats[3] = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT, }; depthFormat = FindSupportedFormat(3, acceptableDepthFormats, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); } // sampler { VkSampleCountFlags counts = vkPhysicalDeviceProperties.limits.framebufferColorSampleCounts & vkPhysicalDeviceProperties.limits.framebufferDepthSampleCounts; if (counts & VK_SAMPLE_COUNT_64_BIT) { global_sample_count = VK_SAMPLE_COUNT_64_BIT; } else if (counts & VK_SAMPLE_COUNT_32_BIT) { global_sample_count = VK_SAMPLE_COUNT_32_BIT; } else if (counts & VK_SAMPLE_COUNT_16_BIT) { global_sample_count = VK_SAMPLE_COUNT_16_BIT; } else if (counts & VK_SAMPLE_COUNT_8_BIT) { global_sample_count = VK_SAMPLE_COUNT_8_BIT; } else if (counts & VK_SAMPLE_COUNT_4_BIT) { global_sample_count = VK_SAMPLE_COUNT_4_BIT; } else if (counts & VK_SAMPLE_COUNT_2_BIT) { global_sample_count = VK_SAMPLE_COUNT_2_BIT; } } // generic sampler VkSamplerCreateInfo samplerCreateInfo; samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerCreateInfo.pNext = nullptr; samplerCreateInfo.flags = 0U; samplerCreateInfo.magFilter = VK_FILTER_NEAREST; samplerCreateInfo.minFilter = VK_FILTER_NEAREST; samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.mipLodBias = 0.0f; samplerCreateInfo.anisotropyEnable = VK_TRUE; samplerCreateInfo.maxAnisotropy = 1.0f; samplerCreateInfo.compareEnable = VK_TRUE; samplerCreateInfo.compareOp = VK_COMPARE_OP_LESS; samplerCreateInfo.minLod = 0.0f; samplerCreateInfo.maxLod = 1.0f; samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; samplerCreateInfo.unnormalizedCoordinates = {}; vkResult = vkCreateSampler(vkDevice, &samplerCreateInfo, vkAllocator, &global_sampler); assert(vkResult == VK_SUCCESS); } void CreateImageResources_Inner(VkImageCreateInfo *imageCreateInfo, VkImageViewCreateInfo *imageViewCreateInfo, VkBufferUsageFlagBits bufferUsageFlagBits, VkBuffer *imagesBuffer, VkImage *images, VkImageView *imageViews, VkDeviceMemory *deviceMemory) { (void)bufferUsageFlagBits; (void)imagesBuffer; VkImage tmpImage; VkResult vkResult; vkResult = vkCreateImage(vkDevice, imageCreateInfo, vkAllocator, &tmpImage); assert(vkResult == VK_SUCCESS); VkMemoryRequirements imageMemoryRequirements; vkGetImageMemoryRequirements(vkDevice, tmpImage, &imageMemoryRequirements); VkDeviceSize paddedImageSize = imageMemoryRequirements.size + (imageMemoryRequirements.alignment - (imageMemoryRequirements.size % imageMemoryRequirements.alignment)); assert(paddedImageSize % imageMemoryRequirements.alignment == 0); vkDestroyImage(vkDevice, tmpImage, vkAllocator); VkMemoryAllocateInfo vkMemoryAllocateInfo{}; vkMemoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; vkMemoryAllocateInfo.pNext = nullptr; vkMemoryAllocateInfo.allocationSize = paddedImageSize * swapchainLength; vkMemoryAllocateInfo.memoryTypeIndex = FindMemoryTypeIndex(imageMemoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); vkResult = vkAllocateMemory(vkDevice, &vkMemoryAllocateInfo, vkAllocator, deviceMemory); assert(vkResult == VK_SUCCESS); for (long i = 0; i < swapchainLength; ++i) { vkResult = vkCreateImage(vkDevice, imageCreateInfo, vkAllocator, &images[i]); assert(vkResult == VK_SUCCESS); vkResult = vkBindImageMemory(vkDevice, images[i], *deviceMemory, paddedImageSize * i); assert(vkResult == VK_SUCCESS); imageViewCreateInfo->image = images[i]; vkResult = vkCreateImageView(vkDevice, imageViewCreateInfo, vkAllocator, &imageViews[i]); assert(vkResult == VK_SUCCESS); } } void CreateSwapchain() { VkResult vkResult; VkSurfaceCapabilitiesKHR surfaceCapabilities; vkResult = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vkPhysicalDevice, vkSurfaceKHR, &surfaceCapabilities); swapchainLength = surfaceCapabilities.minImageCount; assert(vkResult == VK_SUCCESS); assert(swapchainLength >= surfaceCapabilities.minImageCount); assert(surfaceCapabilities.maxImageCount == 0 || swapchainLength <= surfaceCapabilities.maxImageCount); if (selectedSurfaceIndex == -1u) { unsigned int surfaceFormatCounts; vkResult = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, vkSurfaceKHR, &surfaceFormatCounts, nullptr); assert(vkResult == VK_SUCCESS); VkSurfaceFormatKHR *surfaceFormats = pk_new(surfaceFormatCounts); vkResult = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, vkSurfaceKHR, &surfaceFormatCounts, surfaceFormats); assert(vkResult == VK_SUCCESS); selectedSurfaceIndex = 0; for (long i = 0; i < surfaceFormatCounts; ++i) { if (surfaceFormats[i].format != VkFormat::VK_FORMAT_B8G8R8A8_SRGB) continue; if (surfaceFormats[i].colorSpace != VkColorSpaceKHR::VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) continue; selectedSurfaceIndex = i; vkSurfaceFormatKHR.colorSpace = surfaceFormats[i].colorSpace; vkSurfaceFormatKHR.format = surfaceFormats[i].format; break; } pk_delete(surfaceFormats, surfaceFormatCounts); } int width, height; glfwGetFramebufferSize(window, &width, &height); Extent.width = width; Extent.height = height; // clamp Extent.width = PK_CLAMP(Extent.width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width); Extent.height = PK_CLAMP(Extent.height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height); vkPresentModeKHR = VK_PRESENT_MODE_FIFO_KHR; if (pkeSettings.graphicsSettings.isWaitingForVsync == false || pkeSettings.graphicsSettings.isFramerateUnlocked == true) { unsigned int presentModeCount = 0; vkResult = vkGetPhysicalDeviceSurfacePresentModesKHR(vkPhysicalDevice, vkSurfaceKHR, &presentModeCount, nullptr); assert(vkResult == VK_SUCCESS); VkPresentModeKHR *presentModes = pk_new(presentModeCount); vkResult = vkGetPhysicalDeviceSurfacePresentModesKHR(vkPhysicalDevice, vkSurfaceKHR, &presentModeCount, presentModes); assert(vkResult == VK_SUCCESS); int32_t immediateIndex = -1; int32_t fifoRelaxedIndex = -1; /* uint32_t mailboxIndex = -1; uint32_t fifoIndex = -1; */ for (long i = 0; i < presentModeCount; ++i) { if (presentModes[i] == VkPresentModeKHR::VK_PRESENT_MODE_IMMEDIATE_KHR) { immediateIndex = i; } else if (presentModes[i] == VkPresentModeKHR::VK_PRESENT_MODE_FIFO_RELAXED_KHR) { fifoRelaxedIndex = i; } /* // TODO returns 5 swapchain images, which causes other unhandled issues else if (presentModes[i] == VkPresentModeKHR::VK_PRESENT_MODE_MAILBOX_KHR) { mailboxIndex = i; } else if (presentModes[i] == VkPresentModeKHR::VK_PRESENT_MODE_FIFO_KHR) { fifoIndex = i; } */ } if (fifoRelaxedIndex != -1) { vkPresentModeKHR = VK_PRESENT_MODE_FIFO_RELAXED_KHR; } /* if (mailboxIndex != -1) { vkPresentModeKHR = VK_PRESENT_MODE_MAILBOX_KHR; } else */ if (immediateIndex != -1) { vkPresentModeKHR = VK_PRESENT_MODE_IMMEDIATE_KHR; } pk_delete(presentModes, presentModeCount); } VkSwapchainCreateInfoKHR vkSwapchainCreateInfo; vkSwapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; vkSwapchainCreateInfo.pNext = nullptr; vkSwapchainCreateInfo.flags = 0; vkSwapchainCreateInfo.surface = vkSurfaceKHR; vkSwapchainCreateInfo.minImageCount = swapchainLength; vkSwapchainCreateInfo.imageFormat = vkSurfaceFormatKHR.format; vkSwapchainCreateInfo.imageColorSpace = vkSurfaceFormatKHR.colorSpace; vkSwapchainCreateInfo.imageExtent = Extent; vkSwapchainCreateInfo.imageArrayLayers = 1; vkSwapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // vkSwapchainCreateInfo.imageSharingMode = {}; vkSwapchainCreateInfo.queueFamilyIndexCount = 0; vkSwapchainCreateInfo.pQueueFamilyIndices = nullptr; vkSwapchainCreateInfo.preTransform = surfaceCapabilities.currentTransform; vkSwapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; vkSwapchainCreateInfo.presentMode = vkPresentModeKHR; vkSwapchainCreateInfo.clipped = VK_TRUE; // vkSwapchainCreateInfo.oldSwapchain = vkSwapchainKHR; vkSwapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE; unsigned int qfi[2] = { graphicsFamilyIndex, presentFamilyIndex }; if (graphicsFamilyIndex != presentFamilyIndex) { vkSwapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; vkSwapchainCreateInfo.queueFamilyIndexCount = 2; vkSwapchainCreateInfo.pQueueFamilyIndices = qfi; } else { vkSwapchainCreateInfo.imageSharingMode = VkSharingMode::VK_SHARING_MODE_EXCLUSIVE; vkSwapchainCreateInfo.queueFamilyIndexCount = 0; } vkResult = vkCreateSwapchainKHR(vkDevice, &vkSwapchainCreateInfo, vkAllocator, &vkSwapchainKHR); assert(vkResult == VK_SUCCESS); assert(pkvk_present.images == nullptr || pkvk_present.images == CAFE_BABE(VkImage)); assert(pkvk_present.image_views == nullptr || pkvk_present.image_views == CAFE_BABE(VkImageView)); assert(pkvk_3d.render_images == nullptr || pkvk_3d.render_images == CAFE_BABE(VkImage)); assert(pkvk_3d.image_views == nullptr || pkvk_3d.image_views == CAFE_BABE(VkImageView)); assert(pkvk_3d.images_color_resolve == nullptr || pkvk_3d.images_color_resolve == CAFE_BABE(VkImage)); assert(pkvk_3d.image_views_color_resolve == nullptr || pkvk_3d.image_views_color_resolve == CAFE_BABE(VkImageView)); assert(depthImages == nullptr || depthImages == CAFE_BABE(VkImage)); assert(depthImageViews == nullptr || depthImageViews == CAFE_BABE(VkImageView)); assert(pkvk_2d.images == nullptr || pkvk_2d.images == CAFE_BABE(VkImage)); assert(pkvk_2d.image_views == nullptr || pkvk_2d.image_views == CAFE_BABE(VkImageView)); assert(pkvk_2d.images_color_resolve == nullptr || pkvk_2d.images_color_resolve == CAFE_BABE(VkImage)); assert(pkvk_2d.image_views_color_resolve == nullptr || pkvk_2d.image_views_color_resolve == CAFE_BABE(VkImageView)); prevSwapchainLength = swapchainLength; vkResult = vkGetSwapchainImagesKHR(vkDevice, vkSwapchainKHR, &swapchainLength, nullptr); assert(vkResult == VK_SUCCESS); pkvk_present.images = pk_new(swapchainLength); vkResult = vkGetSwapchainImagesKHR(vkDevice, vkSwapchainKHR, &swapchainLength, pkvk_present.images); assert(vkResult == VK_SUCCESS); pkvk_present.image_views = pk_new(swapchainLength); VkImageSubresourceRange vkImageSubresourceRange; vkImageSubresourceRange.aspectMask = VkImageAspectFlagBits::VK_IMAGE_ASPECT_COLOR_BIT; vkImageSubresourceRange.baseMipLevel = 0u; vkImageSubresourceRange.levelCount = 1u; vkImageSubresourceRange.baseArrayLayer = 0u; vkImageSubresourceRange.layerCount = 1u; VkImageViewCreateInfo vkImageViewCreateInfo; vkImageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; vkImageViewCreateInfo.pNext = nullptr; vkImageViewCreateInfo.flags = 0; vkImageViewCreateInfo.viewType = VkImageViewType::VK_IMAGE_VIEW_TYPE_2D; vkImageViewCreateInfo.format = vkSurfaceFormatKHR.format; vkImageViewCreateInfo.components = { VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY, VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY, VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY, VkComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY, }; vkImageViewCreateInfo.subresourceRange = vkImageSubresourceRange; for (long i = 0; i < swapchainLength; ++i) { vkImageViewCreateInfo.image = pkvk_present.images[i]; vkResult = vkCreateImageView(vkDevice, &vkImageViewCreateInfo, vkAllocator, &pkvk_present.image_views[i]); assert(vkResult == VK_SUCCESS); } // render target items VkImageCreateInfo renderTargetImageCI{}; renderTargetImageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; renderTargetImageCI.pNext = nullptr; renderTargetImageCI.flags = 0; renderTargetImageCI.imageType = VK_IMAGE_TYPE_2D; renderTargetImageCI.format = vkSurfaceFormatKHR.format; renderTargetImageCI.extent.width = Extent.width; renderTargetImageCI.extent.height = Extent.height; renderTargetImageCI.extent.depth = 1; renderTargetImageCI.mipLevels = 1; renderTargetImageCI.arrayLayers = 1; renderTargetImageCI.samples = global_sample_count; renderTargetImageCI.tiling = VK_IMAGE_TILING_OPTIMAL; renderTargetImageCI.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; renderTargetImageCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE; renderTargetImageCI.queueFamilyIndexCount = 0; renderTargetImageCI.pQueueFamilyIndices = nullptr; renderTargetImageCI.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; // 3d color (fragment) images pkvk_3d.render_images = pk_new(swapchainLength); pkvk_3d.image_views = pk_new(swapchainLength); CreateImageResources_Inner(&renderTargetImageCI, &vkImageViewCreateInfo, VkBufferUsageFlagBits(0), nullptr, pkvk_3d.render_images, pkvk_3d.image_views, &pkvk_3d.images_memory); // 2d color (fragment) images pkvk_2d.images = pk_new(swapchainLength); pkvk_2d.image_views = pk_new(swapchainLength); CreateImageResources_Inner(&renderTargetImageCI, &vkImageViewCreateInfo, VkBufferUsageFlagBits(0), nullptr, pkvk_2d.images, pkvk_2d.image_views, &pkvk_2d.images_memory); // resolve settings renderTargetImageCI.samples = VK_SAMPLE_COUNT_1_BIT; renderTargetImageCI.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // 3d color (resolve) pkvk_3d.images_color_resolve = pk_new(swapchainLength); pkvk_3d.image_views_color_resolve = pk_new(swapchainLength); CreateImageResources_Inner(&renderTargetImageCI, &vkImageViewCreateInfo, VkBufferUsageFlagBits(0), nullptr, pkvk_3d.images_color_resolve, pkvk_3d.image_views_color_resolve, &pkvk_3d.images_memory_color_resolve); // 2d color (resolve) pkvk_2d.images_color_resolve = pk_new(swapchainLength); pkvk_2d.image_views_color_resolve = pk_new(swapchainLength); CreateImageResources_Inner(&renderTargetImageCI, &vkImageViewCreateInfo, VkBufferUsageFlagBits(0), nullptr, pkvk_2d.images_color_resolve, pkvk_2d.image_views_color_resolve, &pkvk_2d.images_memory_color_resolve); // depth settings renderTargetImageCI.samples = global_sample_count; renderTargetImageCI.format = depthFormat; renderTargetImageCI.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; vkImageViewCreateInfo.format = depthFormat; vkImageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; // depth images depthImages = pk_new(swapchainLength); depthImageViews = pk_new(swapchainLength); CreateImageResources_Inner(&renderTargetImageCI, &vkImageViewCreateInfo, VkBufferUsageFlagBits(0), nullptr, depthImages, depthImageViews, &depthImagesMemory); } void UpdatePresentDescriptorSets() { VkWriteDescriptorSet *writeDescriptorSets = pk_new(swapchainLength * 2); VkDescriptorImageInfo *descriptorImageInfo = pk_new(swapchainLength * 2); for (long i = 0; i < swapchainLength * 2; ++i) { writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[i].pNext = nullptr; writeDescriptorSets[i].dstBinding = 0U; writeDescriptorSets[i].dstArrayElement = 0U; writeDescriptorSets[i].descriptorCount = 1; writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[i].pBufferInfo = nullptr; writeDescriptorSets[i].pTexelBufferView = nullptr; descriptorImageInfo[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; descriptorImageInfo[i].sampler = global_sampler; writeDescriptorSets[i].dstSet = pkvk_present.descriptor_sets[i]; writeDescriptorSets[i].pImageInfo = &descriptorImageInfo[i]; } for (long i = 0; i < swapchainLength; ++i) { descriptorImageInfo[(i * 2)].imageView = pkvk_3d.image_views_color_resolve[i]; descriptorImageInfo[(i * 2) + 1].imageView = pkvk_2d.image_views_color_resolve[i]; } vkUpdateDescriptorSets(vkDevice, swapchainLength * 2, writeDescriptorSets, 0, nullptr); pk_delete(writeDescriptorSets, swapchainLength * 2); pk_delete(descriptorImageInfo, swapchainLength * 2); } void UpdateCamera() { if (ActiveCamera->stale == PkeCameraStaleFlags{0}) return; CompInstance *inst = nullptr; CompInstance *targetInst = nullptr; glm::vec3 gPos; glm::quat gRot; btTransform trfm; if (ActiveCamera == &NullCamera) { inst = &NullCameraInstance; } else { inst = ECS_GetInstance(ActiveCamera->phys.instHandle); } inst->bt.motionState->getWorldTransform(trfm); BulletToGlm(trfm.getOrigin(), gPos); BulletToGlm(trfm.getRotation(), gRot); if (bool(ActiveCamera->stale & PKE_CAMERA_STALE_POS)) { UBO.model = glm::translate(glm::mat4(1.f), gPos); } if (bool(ActiveCamera->stale & PKE_CAMERA_STALE_ROT)) { if (bool(ActiveCamera->view == PKE_CAMERA_VIEW_FREE)) { UBO.view = glm::mat4_cast(gRot); } else if (bool(ActiveCamera->view == PKE_CAMERA_VIEW_TARGET)) { glm::vec3 gTargetPos{0.f, 0.f, 0.f}; targetInst = ECS_GetInstance(ActiveCamera->phys.targetInstHandle); if (targetInst != nullptr && targetInst != CAFE_BABE(CompInstance)) { btTransform targetTrfm; targetInst->bt.motionState->getWorldTransform(targetTrfm); BulletToGlm(targetTrfm.getOrigin(), gTargetPos); } UBO.view = glm::lookAt(glm::vec3(0), gPos + gTargetPos, glm::vec3(0.f, 1.f, 0.f)); } } if (bool(ActiveCamera->stale & PKE_CAMERA_STALE_PERSPECTIVE)) { if (bool(ActiveCamera->type == PKE_CAMERA_TYPE_PERSPECTIVE)) { UBO.proj = glm::perspective(glm::radians(45.f), Extent.width / (float)Extent.height, 0.1f, 1000.f); UBO.proj[1][1] *= -1; } else if (bool(ActiveCamera->type == PKE_CAMERA_TYPE_ORTHOGONAL)) { UBO.proj = glm::ortho(0.f, (float)Extent.width, 0.f, (float)Extent.height, 0.f, 1000.f); UBO.proj[1][1] *= -1; } } ActiveCamera->stale = PkeCameraStaleFlags{0}; } void CreateRenderPass() { /* 2025-01-29 JCB * I refactored this so that VkAttachmentDescription was an array and that * wasn't totally necessary. It does have the advantage of 1:1-ing with the * VkAttachmentReference, but it means you have to just know what each index * is. Here's a legend since the code is no longer self-documenting. * 0: render target (fragment) * 1: color resolve (multisampling) * 2: depth * See also: CreateFramebuffers(): the same ordering can be observed. */ VkAttachmentDescription attachment_descriptions[3]; memset(attachment_descriptions, 0, sizeof(VkAttachmentDescription) * 3); VkAttachmentReference attachment_refs[3]; memset(attachment_refs, 0, sizeof(VkAttachmentReference) * 3); VkSubpassDependency subpass_dependencies[2]; memset(subpass_dependencies, 0, sizeof(VkSubpassDependency) * 2); for (long i = 0; i < 3; ++i) { attachment_refs[i].attachment = i; } for (long i = 0; i < 2; ++i) { subpass_dependencies[i].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; } // color (fragment) attachment_descriptions[0].flags = {}; attachment_descriptions[0].format = vkSurfaceFormatKHR.format; attachment_descriptions[0].samples = VK_SAMPLE_COUNT_1_BIT; attachment_descriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attachment_descriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachment_descriptions[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment_descriptions[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachment_descriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attachment_descriptions[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; // color (fragment) attachment_refs[0].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; VkSubpassDescription subpass; subpass.flags = {}; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.inputAttachmentCount = 0; subpass.pInputAttachments = nullptr; subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &attachment_refs[0]; subpass.pResolveAttachments = nullptr; subpass.pDepthStencilAttachment = nullptr; subpass.preserveAttachmentCount = 0; subpass.pPreserveAttachments = nullptr; subpass_dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; subpass_dependencies[0].dstSubpass = 0U; subpass_dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; subpass_dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; subpass_dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; subpass_dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; subpass_dependencies[1].srcSubpass = 0U; subpass_dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL; subpass_dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; subpass_dependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; subpass_dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; subpass_dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; VkRenderPassCreateInfo renderPassInfo; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassInfo.pNext = nullptr; renderPassInfo.flags = {}; renderPassInfo.attachmentCount = 1; renderPassInfo.pAttachments = &attachment_descriptions[0]; renderPassInfo.subpassCount = 1; renderPassInfo.pSubpasses = &subpass; renderPassInfo.dependencyCount = 2; renderPassInfo.pDependencies = subpass_dependencies; if (vkCreateRenderPass(vkDevice, &renderPassInfo, vkAllocator, &pkvk_present.render_pass) != VK_SUCCESS) { throw "failed to create present render pass!"; } // color resolve attachment_refs[1].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; subpass.pResolveAttachments = &attachment_refs[1]; // color attachment_descriptions[0].samples = global_sample_count; attachment_descriptions[0].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // color resolve attachment_descriptions[1] = attachment_descriptions[0]; attachment_descriptions[1].samples = VK_SAMPLE_COUNT_1_BIT; attachment_descriptions[1].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment_descriptions[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachment_descriptions[1].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; subpass_dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; subpass_dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; subpass_dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT; subpass_dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; subpass_dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; subpass_dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; subpass_dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; subpass_dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; renderPassInfo.attachmentCount = 2; if (vkCreateRenderPass(vkDevice, &renderPassInfo, vkAllocator, &pkvk_2d.render_pass) != VK_SUCCESS) { throw "failed to create 2d render pass!"; } // color attachment_descriptions[0].samples = global_sample_count; attachment_descriptions[0].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // depth attachment_descriptions[2] = attachment_descriptions[0]; attachment_descriptions[2].format = depthFormat; attachment_descriptions[2].samples = global_sample_count; attachment_descriptions[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attachment_descriptions[2].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachment_descriptions[2].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment_descriptions[2].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE; attachment_descriptions[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attachment_descriptions[2].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; // depth attachment_refs[2].layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; subpass.pDepthStencilAttachment = &attachment_refs[2]; subpass_dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; subpass_dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; subpass_dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT; subpass_dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; subpass_dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; subpass_dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; subpass_dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; subpass_dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; renderPassInfo.attachmentCount = 3; if (vkCreateRenderPass(vkDevice, &renderPassInfo, vkAllocator, &pkvk_3d.render_pass) != VK_SUCCESS) { throw "failed to create 3d render pass!"; } } void CreatePresentDescriptorSetLayout() { VkDescriptorSetLayoutBinding imageSamplerLayoutBinding; imageSamplerLayoutBinding.binding = 0; imageSamplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; imageSamplerLayoutBinding.descriptorCount = 1; imageSamplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; imageSamplerLayoutBinding.pImmutableSamplers = nullptr; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo; descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCreateInfo.pNext = nullptr; descriptorSetLayoutCreateInfo.flags = 0; descriptorSetLayoutCreateInfo.bindingCount = 1; descriptorSetLayoutCreateInfo.pBindings = &imageSamplerLayoutBinding; auto vkResult = vkCreateDescriptorSetLayout(vkDevice, &descriptorSetLayoutCreateInfo, vkAllocator, &pkvk_present.descriptor_set_layout); if (vkResult != VK_SUCCESS) { throw "failed to create descriptor set layout"; } } void CreatePresentPipeline() { VkResult vkResult; // enqueue asset loading AssetHandle vertShaderAsset{AM_GetHandle(AssetKey{"pke_prsnt_vrt\0\0"})}; AssetHandle fragShaderAsset{AM_GetHandle(AssetKey{"pke_prsnt_frg\0\0"})}; VkPipelineShaderStageCreateInfo shaderStages[2]; for (long i = 0; i < 2; ++i) { shaderStages[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; shaderStages[i].pNext = nullptr; shaderStages[i].flags = 0; shaderStages[i].pName = "main"; shaderStages[i].pSpecializationInfo = nullptr; } shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT; shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; // shaderStages[0].module = // see below // shaderStages[1].module = // see below VkDynamicState dynamicStates[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo dynamicState; dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; dynamicState.pNext = nullptr; dynamicState.flags = {}; dynamicState.dynamicStateCount = 2; dynamicState.pDynamicStates = dynamicStates; VkPipelineVertexInputStateCreateInfo vertexInputInfo; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputInfo.pNext = nullptr; vertexInputInfo.flags = {}; vertexInputInfo.vertexBindingDescriptionCount = 0; vertexInputInfo.pVertexBindingDescriptions = nullptr; vertexInputInfo.vertexAttributeDescriptionCount = 0; vertexInputInfo.pVertexAttributeDescriptions = nullptr; VkPipelineInputAssemblyStateCreateInfo inputAssembly; inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssembly.pNext = nullptr; inputAssembly.flags = {}; inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; inputAssembly.primitiveRestartEnable = VK_FALSE; // dynamic VkViewport viewport; viewport.x = 0.0f; viewport.y = 0.0f; viewport.width = 0.0f; viewport.height = 0.0f; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; // dynamic VkRect2D scissor; scissor.offset = {0, 0}; scissor.extent = Extent; VkPipelineViewportStateCreateInfo viewportState; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState.pNext = nullptr; viewportState.flags = {}; viewportState.viewportCount = 1; viewportState.pViewports = &viewport; viewportState.scissorCount = 1; viewportState.pScissors = &scissor; VkPipelineRasterizationStateCreateInfo rasterizer; rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizer.pNext = nullptr; rasterizer.flags = {}; rasterizer.depthClampEnable = VK_FALSE; rasterizer.rasterizerDiscardEnable = VK_FALSE; rasterizer.polygonMode = VK_POLYGON_MODE_FILL; rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE; rasterizer.depthBiasEnable = VK_FALSE; rasterizer.depthBiasConstantFactor = 0.0f; rasterizer.depthBiasClamp = 0.0f; rasterizer.depthBiasSlopeFactor = 0.0f; rasterizer.lineWidth = 1.0f; VkPipelineMultisampleStateCreateInfo multisampling; multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; multisampling.pNext = nullptr; multisampling.flags = {}; multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; multisampling.sampleShadingEnable = VK_FALSE; multisampling.minSampleShading = 1.0f; multisampling.pSampleMask = nullptr; multisampling.alphaToCoverageEnable = VK_FALSE; multisampling.alphaToOneEnable = VK_FALSE; // TODO VkPipelineDepthStencilStateCreateInfo depthStencilInfo{}; depthStencilInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; depthStencilInfo.pNext = nullptr; depthStencilInfo.flags = 0; depthStencilInfo.depthTestEnable = VK_FALSE; depthStencilInfo.depthWriteEnable = VK_FALSE; depthStencilInfo.depthCompareOp = VK_COMPARE_OP_LESS; depthStencilInfo.depthBoundsTestEnable = VK_FALSE; depthStencilInfo.stencilTestEnable = VK_FALSE; depthStencilInfo.front = {}; depthStencilInfo.back = {}; depthStencilInfo.minDepthBounds = {}; depthStencilInfo.maxDepthBounds = {}; VkPipelineColorBlendAttachmentState colorBlendAttachment; colorBlendAttachment.blendEnable = VK_TRUE; colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; VkPipelineColorBlendStateCreateInfo colorBlending; colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; colorBlending.pNext = nullptr; colorBlending.flags = {}; colorBlending.logicOpEnable = VK_FALSE; colorBlending.logicOp = VK_LOGIC_OP_COPY; colorBlending.attachmentCount = 1; colorBlending.pAttachments = &colorBlendAttachment; colorBlending.blendConstants[0] = 0.0f; colorBlending.blendConstants[1] = 0.0f; colorBlending.blendConstants[2] = 0.0f; colorBlending.blendConstants[3] = 0.0f; /* * PipelineLayout + DescriptorSets */ VkDescriptorPoolSize vkDescriptorPoolSize; vkDescriptorPoolSize.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; vkDescriptorPoolSize.descriptorCount = swapchainLength * 2; VkDescriptorPoolCreateInfo descriptorPoolCreateInfo; descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; descriptorPoolCreateInfo.pNext = nullptr; descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; descriptorPoolCreateInfo.maxSets = swapchainLength * 2; descriptorPoolCreateInfo.poolSizeCount = 1; descriptorPoolCreateInfo.pPoolSizes = &vkDescriptorPoolSize; vkResult = vkCreateDescriptorPool(vkDevice, &descriptorPoolCreateInfo, vkAllocator, &pkvk_present.descriptor_pool); assert(vkResult == VK_SUCCESS); VkDescriptorSetLayout *setLayouts = pk_new(swapchainLength * 2); for (long i = 0; i < swapchainLength * 2; ++i) { setLayouts[i] = pkvk_present.descriptor_set_layout; } VkDescriptorSetAllocateInfo allocInfo; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocInfo.pNext = nullptr; allocInfo.descriptorPool = pkvk_present.descriptor_pool; allocInfo.descriptorSetCount = swapchainLength * 2; allocInfo.pSetLayouts = setLayouts; pkvk_present.descriptor_sets = pk_new(swapchainLength * 2, MemBkt_Vulkan); vkResult = vkAllocateDescriptorSets(vkDevice, &allocInfo, pkvk_present.descriptor_sets); pk_delete(setLayouts, swapchainLength * 2); // setLayouts = CAFE_BABE(VkDescriptorSetLayout); if (vkResult != VK_SUCCESS) { throw "failed to allocate present descriptor sets"; } VkPipelineLayoutCreateInfo pipelineLayoutInfo; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.pNext = nullptr; pipelineLayoutInfo.flags = {}; pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &pkvk_present.descriptor_set_layout; pipelineLayoutInfo.pushConstantRangeCount = 0; pipelineLayoutInfo.pPushConstantRanges = nullptr; vkResult = vkCreatePipelineLayout(vkDevice, &pipelineLayoutInfo, vkAllocator, &pkvk_present.pipeline_layout); if (vkResult != VK_SUCCESS) { throw "failed to create pipeline layout"; } VkGraphicsPipelineCreateInfo pipelineInfo; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.pNext = nullptr; pipelineInfo.flags = {}; pipelineInfo.stageCount = 2; pipelineInfo.pStages = shaderStages; pipelineInfo.pVertexInputState = &vertexInputInfo; pipelineInfo.pInputAssemblyState = &inputAssembly; pipelineInfo.pTessellationState = nullptr; pipelineInfo.pViewportState = &viewportState; pipelineInfo.pRasterizationState = &rasterizer; pipelineInfo.pMultisampleState = &multisampling; pipelineInfo.pDepthStencilState = &depthStencilInfo; pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = &dynamicState; pipelineInfo.layout = pkvk_present.pipeline_layout; pipelineInfo.renderPass = pkvk_present.render_pass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = {}; /* 2023-12-22 JCB * This happens just before needed because we want to let this * thread run while background threads load the assets. */ auto vertShader = UploadShader(vertShaderAsset); auto fragShader = UploadShader(fragShaderAsset); shaderStages[0].module = vertShader; shaderStages[1].module = fragShader; vkResult = vkCreateGraphicsPipelines(vkDevice, VK_NULL_HANDLE, 1, &pipelineInfo, vkAllocator, &pkvk_present.pipeline); if (vkResult != VK_SUCCESS) { throw "failed to create graphics pipeline."; } vkDestroyShaderModule(vkDevice, fragShader, vkAllocator); vkDestroyShaderModule(vkDevice, vertShader, vkAllocator); AM_Release(fragShaderAsset); AM_Release(vertShaderAsset); } void CreateFramebuffers() { assert(pkvk_present.framebuffers == nullptr || pkvk_present.framebuffers == CAFE_BABE(VkFramebuffer)); assert(pkvk_3d.framebuffers == nullptr || pkvk_3d.framebuffers == CAFE_BABE(VkFramebuffer)); assert(pkvk_2d.framebuffers == nullptr || pkvk_2d.framebuffers == CAFE_BABE(VkFramebuffer)); pkvk_present.framebuffers = pk_new(swapchainLength); pkvk_3d.framebuffers = pk_new(swapchainLength); pkvk_2d.framebuffers = pk_new(swapchainLength); VkFramebufferCreateInfo framebufferInfo; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.pNext = nullptr; framebufferInfo.flags = {}; framebufferInfo.renderPass = pkvk_present.render_pass; framebufferInfo.attachmentCount = 1; framebufferInfo.pAttachments = nullptr; framebufferInfo.width = Extent.width; framebufferInfo.height = Extent.height; framebufferInfo.layers = 1; for (long i = 0; i < swapchainLength; ++i) { VkImageView attachments[] = { pkvk_present.image_views[i] }; framebufferInfo.pAttachments = attachments; auto result = vkCreateFramebuffer(vkDevice, &framebufferInfo, vkAllocator, &pkvk_present.framebuffers[i]); if (result != VK_SUCCESS) { throw "failed to create present framebuffer"; } } framebufferInfo.attachmentCount = 2; framebufferInfo.renderPass = pkvk_2d.render_pass; for (long i = 0; i < swapchainLength; ++i) { VkImageView attachments[] = { pkvk_2d.image_views[i], pkvk_2d.image_views_color_resolve[i], }; framebufferInfo.pAttachments = attachments; auto result = vkCreateFramebuffer(vkDevice, &framebufferInfo, vkAllocator, &pkvk_2d.framebuffers[i]); if (result != VK_SUCCESS) { throw "failed to create 2d overlay framebuffer"; } } framebufferInfo.attachmentCount = 3; framebufferInfo.renderPass = pkvk_3d.render_pass; for (long i = 0; i < swapchainLength; ++i) { VkImageView attachments[] = { pkvk_3d.image_views[i], pkvk_3d.image_views_color_resolve[i], depthImageViews[i], }; framebufferInfo.pAttachments = attachments; auto result = vkCreateFramebuffer(vkDevice, &framebufferInfo, vkAllocator, &pkvk_3d.framebuffers[i]); if (result != VK_SUCCESS) { throw "failed to create render framebuffer"; } } } void CreateCommandPool() { VkCommandPoolCreateInfo poolInfo; poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; poolInfo.pNext = nullptr; poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; poolInfo.queueFamilyIndex = graphicsFamilyIndex; auto result = vkCreateCommandPool(vkDevice, &poolInfo, vkAllocator, &pkvk_shared.command_pool.graphics); if (result != VK_SUCCESS) { throw "failed to create command pool"; } poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; poolInfo.queueFamilyIndex = transferFamilyIndex; result = vkCreateCommandPool(vkDevice, &poolInfo, vkAllocator, &pkvk_shared.command_pool.transfer); if (result != VK_SUCCESS) { throw "failed to create transfer command pool"; } } void CreateCommandBuffer() { VkCommandBufferAllocateInfo allocInfo; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.pNext = nullptr; allocInfo.commandPool = pkvk_shared.command_pool.graphics; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.commandBufferCount = swapchainLength; pkvk_present.command_buffers = pk_new(swapchainLength, MemBkt_Vulkan); auto result = vkAllocateCommandBuffers(vkDevice, &allocInfo, pkvk_present.command_buffers); if (result != VK_SUCCESS) { throw "failed to allocate command buffer"; } allocInfo.commandBufferCount = 1; result = vkAllocateCommandBuffers(vkDevice, &allocInfo, &pkvk_shared.command_buffer.graphics); if (result != VK_SUCCESS) { throw "failed to allocate command buffer"; } allocInfo.commandPool = pkvk_shared.command_pool.transfer; result = vkAllocateCommandBuffers(vkDevice, &allocInfo, &pkvk_shared.command_buffer.transfer); if (result != VK_SUCCESS) { throw "failed to allocate command buffer"; } } void CreateUniformBuffers() { VkResult result; uint32_t queueFamilyIndexes[2] = {graphicsFamilyIndex, transferFamilyIndex}; VkBufferCreateInfo vkBufferCreateInfo; vkBufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; vkBufferCreateInfo.pNext = nullptr; vkBufferCreateInfo.flags = {}; vkBufferCreateInfo.size = sizeof(UniformBufferObject); vkBufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; vkBufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; vkBufferCreateInfo.queueFamilyIndexCount = graphicsFamilyIndex == transferFamilyIndex ? 1 : 2; vkBufferCreateInfo.pQueueFamilyIndices = queueFamilyIndexes; UniformBuffers = pk_new(swapchainLength, MemBkt_Vulkan); result = vkCreateBuffer(vkDevice, &vkBufferCreateInfo, vkAllocator, &UniformBuffers[0]); assert(result == VK_SUCCESS); VkMemoryRequirements memReqs; vkGetBufferMemoryRequirements(vkDevice, UniformBuffers[0], &memReqs); paddedUboBufferSize = memReqs.size + (memReqs.alignment - (memReqs.size & memReqs.alignment)); VkMemoryAllocateInfo vkMemoryAllocateInfo; vkMemoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; vkMemoryAllocateInfo.pNext = nullptr; vkMemoryAllocateInfo.allocationSize = paddedUboBufferSize * swapchainLength; vkMemoryAllocateInfo.memoryTypeIndex = FindMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); result = vkAllocateMemory(vkDevice, &vkMemoryAllocateInfo, vkAllocator, &uniformBufferMemory); assert(result == VK_SUCCESS); vkDestroyBuffer(vkDevice, UniformBuffers[0], vkAllocator); vkBufferCreateInfo.size = paddedUboBufferSize; for (long i = 0; i < swapchainLength; ++i) { result = vkCreateBuffer(vkDevice, &vkBufferCreateInfo, vkAllocator, &UniformBuffers[i]); assert(result == VK_SUCCESS); result = vkBindBufferMemory(vkDevice, UniformBuffers[i], uniformBufferMemory, paddedUboBufferSize * i); assert(result == VK_SUCCESS); } } void CreateSyncObjects() { VkSemaphoreCreateInfo semaphoreInfo; semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; semaphoreInfo.pNext = nullptr; semaphoreInfo.flags = {}; VkFenceCreateInfo fenceInfo; fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fenceInfo.pNext = nullptr; fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; pkvk_present.semaphores_image_available = pk_new(swapchainLength, MemBkt_Vulkan); pkvk_present.semaphores_render_finished = pk_new(swapchainLength, MemBkt_Vulkan); pkvk_present.fences_in_flight = pk_new(swapchainLength, MemBkt_Vulkan); for (long i = 0; i < swapchainLength; ++i) { auto result1 = vkCreateSemaphore(vkDevice, &semaphoreInfo, vkAllocator, &pkvk_present.semaphores_image_available[i]); auto result2 = vkCreateSemaphore(vkDevice, &semaphoreInfo, vkAllocator, &pkvk_present.semaphores_render_finished[i]); auto result3 = vkCreateFence(vkDevice, &fenceInfo, vkAllocator, &pkvk_present.fences_in_flight[i]); auto result = result1 == result2 && result2 == result3 && result3 == VK_SUCCESS; if (!result) { throw "failed to create sync objects"; } } } void CreateGraphicsPipelines() { VkResult vkResult; PKVK_TmpBufferDetails tmpBufferDetails{}; // layouts & sampler { VkDescriptorSetLayoutBinding vkDescriptorSetLayoutBindings[2]; for (long i = 0; i < 2; ++i) { vkDescriptorSetLayoutBindings[i].pImmutableSamplers = nullptr; } vkDescriptorSetLayoutBindings[0].binding = 0; vkDescriptorSetLayoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; vkDescriptorSetLayoutBindings[0].descriptorCount = 1; vkDescriptorSetLayoutBindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; vkDescriptorSetLayoutBindings[1].binding = 1; vkDescriptorSetLayoutBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; vkDescriptorSetLayoutBindings[1].descriptorCount = 1; vkDescriptorSetLayoutBindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; VkDescriptorSetLayoutCreateInfo vkDescriptorSetLayoutCreateInfo { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .pNext = nullptr, .flags = 0, .bindingCount = 2, .pBindings = vkDescriptorSetLayoutBindings, }; vkResult = vkCreateDescriptorSetLayout(vkDevice, &vkDescriptorSetLayoutCreateInfo, vkAllocator, &pkePipelines.descr_layouts.named.texture); assert(vkResult == VK_SUCCESS); assert(pkePipelines.descr_layouts.named.texture != VK_NULL_HANDLE); // no UBO on glyph vkDescriptorSetLayoutBindings[0] = vkDescriptorSetLayoutBindings[1]; vkDescriptorSetLayoutBindings[0].binding = 0; vkDescriptorSetLayoutCreateInfo.bindingCount = 1; vkResult = vkCreateDescriptorSetLayout(vkDevice, &vkDescriptorSetLayoutCreateInfo, vkAllocator, &pkePipelines.descr_layouts.named.glyph); assert(vkResult == VK_SUCCESS); assert(pkePipelines.descr_layouts.named.glyph != VK_NULL_HANDLE); VkPipelineLayoutCreateInfo vkPipelineLayoutCreateInfo { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .pNext = nullptr, .flags = 0, .setLayoutCount = 1, .pSetLayouts = &pkePipelines.descr_layouts.named.texture, .pushConstantRangeCount = 0, .pPushConstantRanges = nullptr, }; vkCreatePipelineLayout(vkDevice, &vkPipelineLayoutCreateInfo, vkAllocator, &pkePipelines.pipe_layouts.named.texture); assert(pkePipelines.pipe_layouts.named.texture != VK_NULL_HANDLE); vkPipelineLayoutCreateInfo.pSetLayouts = &pkePipelines.descr_layouts.named.glyph; vkCreatePipelineLayout(vkDevice, &vkPipelineLayoutCreateInfo, vkAllocator, &pkePipelines.pipe_layouts.named.glyph); assert(pkePipelines.pipe_layouts.named.glyph != VK_NULL_HANDLE); } // pipelines { // enqueue asset loading AssetHandle vertShaderAssetHandle{AM_GetHandle(AssetKey{"pke_txtr_vrt\0\0\0"})}; AssetHandle textureFragShaderAssetHandle { AM_GetHandle(AssetKey{"pke_txtr_frg\0\0\0"})}; AssetHandle vertGlyphAssetHandle{AM_GetHandle(AssetKey{"pke_glyph_vrt\0\0"})}; AssetHandle fragGlyphAssetHandle { AM_GetHandle(AssetKey{"pke_glyph_frg\0\0"})}; const long vertexBindingCount = 4; long index = 0; VkVertexInputBindingDescription vertInputBD_txtr[vertexBindingCount]; const long vertexAttrDescCount = 7; VkVertexInputAttributeDescription vertAttrDesc_txtr[vertexAttrDescCount]; VkPipelineVertexInputStateCreateInfo vkPipelineVertexInputStateCreateInfo_txtr; { // model vertex vertInputBD_txtr[index].binding = index; vertInputBD_txtr[index].stride = sizeof(glm::vec3); vertInputBD_txtr[index].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; index += 1; // model normals vertInputBD_txtr[index].binding = index; vertInputBD_txtr[index].stride = sizeof(glm::vec3); vertInputBD_txtr[index].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; index += 1; // model UV vertInputBD_txtr[index].binding = index; vertInputBD_txtr[index].stride = sizeof(glm::vec2); vertInputBD_txtr[index].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; index += 1; // instance vertInputBD_txtr[index].binding = index; vertInputBD_txtr[index].stride = sizeof(glm::mat4); vertInputBD_txtr[index].inputRate = VK_VERTEX_INPUT_RATE_INSTANCE; // index += 1; index = 0; for (long i = 0; i < vertexAttrDescCount; ++i) { vertAttrDesc_txtr[i].location = i; } // model vertex vertAttrDesc_txtr[index].binding = 0; vertAttrDesc_txtr[index].format = VK_FORMAT_R32G32B32_SFLOAT; vertAttrDesc_txtr[index].offset = 0; index += 1; // model normals vertAttrDesc_txtr[index].binding = 1; vertAttrDesc_txtr[index].format = VK_FORMAT_R32G32B32_SFLOAT; vertAttrDesc_txtr[index].offset = 0; index += 1; // model UV vertAttrDesc_txtr[index].binding = 2; vertAttrDesc_txtr[index].format = VK_FORMAT_R32G32_SFLOAT; vertAttrDesc_txtr[index].offset = 0; index += 1; // instPosRotScale for (long i = 0; i < 4; ++i) { vertAttrDesc_txtr[index].binding = 3; vertAttrDesc_txtr[index].format = VK_FORMAT_R32G32B32A32_SFLOAT; vertAttrDesc_txtr[index].offset = sizeof(glm::vec4) * i; index += 1; } vkPipelineVertexInputStateCreateInfo_txtr.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vkPipelineVertexInputStateCreateInfo_txtr.pNext = nullptr; vkPipelineVertexInputStateCreateInfo_txtr.flags = {}; vkPipelineVertexInputStateCreateInfo_txtr.vertexBindingDescriptionCount = vertexBindingCount; vkPipelineVertexInputStateCreateInfo_txtr.pVertexBindingDescriptions = vertInputBD_txtr; vkPipelineVertexInputStateCreateInfo_txtr.vertexAttributeDescriptionCount = vertexAttrDescCount; vkPipelineVertexInputStateCreateInfo_txtr.pVertexAttributeDescriptions = vertAttrDesc_txtr; } index = 0; long offset = 0; const long vertexBindingCount_glyph = 4; VkVertexInputBindingDescription vertInputBD_glyph[vertexBindingCount_glyph]; const long vertexAttrDescCount_glyph = 12; VkVertexInputAttributeDescription vertAttrDesc_glyph[vertexAttrDescCount_glyph]; VkPipelineVertexInputStateCreateInfo vkPipelineVertexInputStateCreateInfo_glyph{vkPipelineVertexInputStateCreateInfo_txtr}; { // vertex - vertex vertInputBD_glyph[index].binding = index; vertInputBD_glyph[index].stride = sizeof(glm::vec2); vertInputBD_glyph[index].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; index += 1; // vertex - UV vertInputBD_glyph[index].binding = index; vertInputBD_glyph[index].stride = sizeof(glm::vec2); vertInputBD_glyph[index].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; index += 1; // vertex - atlas_size vertInputBD_glyph[index].binding = index; vertInputBD_glyph[index].stride = sizeof(glm::vec2); vertInputBD_glyph[index].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; index += 1; // instance - total vertInputBD_glyph[index].binding = index; vertInputBD_glyph[index].stride = 0 + sizeof(glm::mat4) + sizeof(glm::vec4) + sizeof(glm::vec4) + sizeof(glm::vec2) + sizeof(glm::vec2) + sizeof(float) + (sizeof(float) * 3) // padding + 0; vertInputBD_glyph[index].inputRate = VK_VERTEX_INPUT_RATE_INSTANCE; // index += 1; index = 0; for (long i = 0; i < vertexAttrDescCount_glyph; ++i) { vertAttrDesc_glyph[i].location = i; } // vertex - vertex vertAttrDesc_glyph[index].binding = 0; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; vertAttrDesc_glyph[index].offset = 0; index += 1; // vertex - UV vertAttrDesc_glyph[index].binding = 1; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; vertAttrDesc_glyph[index].offset = 0; index += 1; // vertex - in_atlas_size vertAttrDesc_glyph[index].binding = 2; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; vertAttrDesc_glyph[index].offset = 0; index += 1; // instance - pos_scale for (long i = 0; i < 4; ++i) { vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32B32A32_SFLOAT; vertAttrDesc_glyph[index].offset = offset; offset += sizeof(glm::vec4); index += 1; } // instance - in_fg_color vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32B32A32_SFLOAT; vertAttrDesc_glyph[index].offset = offset; offset += sizeof(glm::vec4); index += 1; // instance - in_bg_color vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32B32A32_SFLOAT; vertAttrDesc_glyph[index].offset = offset; offset += sizeof(glm::vec4); index += 1; // instance - in_sprite_region_min vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; vertAttrDesc_glyph[index].offset = offset; offset += sizeof(glm::vec2); index += 1; // instance - in_sprite_region_max vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; vertAttrDesc_glyph[index].offset = offset; offset += sizeof(glm::vec2); index += 1; // instance - in_width vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32_SFLOAT; vertAttrDesc_glyph[index].offset = offset; // offset += sizeof(float); // index += 1; vkPipelineVertexInputStateCreateInfo_glyph.vertexBindingDescriptionCount = vertexBindingCount_glyph; vkPipelineVertexInputStateCreateInfo_glyph.pVertexBindingDescriptions = vertInputBD_glyph; vkPipelineVertexInputStateCreateInfo_glyph.vertexAttributeDescriptionCount = vertexAttrDescCount_glyph; vkPipelineVertexInputStateCreateInfo_glyph.pVertexAttributeDescriptions = vertAttrDesc_glyph; } VkPipelineInputAssemblyStateCreateInfo vkPipelineInputAssemblyStateCreateInfo; vkPipelineInputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; vkPipelineInputAssemblyStateCreateInfo.pNext = nullptr; vkPipelineInputAssemblyStateCreateInfo.flags = {}; vkPipelineInputAssemblyStateCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; vkPipelineInputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE; // TODO - is this right? set to dynamic later VkPipelineViewportStateCreateInfo vkPipelineViewportStateCreateInfo; vkPipelineViewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; vkPipelineViewportStateCreateInfo.pNext = nullptr; vkPipelineViewportStateCreateInfo.flags = {}; vkPipelineViewportStateCreateInfo.viewportCount = 1; vkPipelineViewportStateCreateInfo.pViewports = nullptr; vkPipelineViewportStateCreateInfo.scissorCount = 1; vkPipelineViewportStateCreateInfo.pScissors = nullptr; VkPipelineRasterizationStateCreateInfo vkPipelineRasterizationStateCreateInfoFill; vkPipelineRasterizationStateCreateInfoFill.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; vkPipelineRasterizationStateCreateInfoFill.pNext = nullptr; vkPipelineRasterizationStateCreateInfoFill.flags = {}; vkPipelineRasterizationStateCreateInfoFill.depthClampEnable = VK_FALSE; vkPipelineRasterizationStateCreateInfoFill.rasterizerDiscardEnable = VK_FALSE; vkPipelineRasterizationStateCreateInfoFill.polygonMode = VK_POLYGON_MODE_FILL; vkPipelineRasterizationStateCreateInfoFill.cullMode = VK_CULL_MODE_BACK_BIT; vkPipelineRasterizationStateCreateInfoFill.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; vkPipelineRasterizationStateCreateInfoFill.depthBiasEnable = VK_FALSE; vkPipelineRasterizationStateCreateInfoFill.depthBiasConstantFactor = 0.0f; vkPipelineRasterizationStateCreateInfoFill.depthBiasClamp = 0.0f; vkPipelineRasterizationStateCreateInfoFill.depthBiasSlopeFactor = 0.0f; vkPipelineRasterizationStateCreateInfoFill.lineWidth = 1.0f; VkPipelineRasterizationStateCreateInfo vkPipelineRasterizationStateCreateInfoLine{vkPipelineRasterizationStateCreateInfoFill}; vkPipelineRasterizationStateCreateInfoLine.cullMode = VK_CULL_MODE_NONE; vkPipelineRasterizationStateCreateInfoLine.polygonMode = VK_POLYGON_MODE_LINE; vkPipelineRasterizationStateCreateInfoLine.lineWidth = 5.f; VkPipelineMultisampleStateCreateInfo vkPipelineMultisampleStateCreateInfo; vkPipelineMultisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; vkPipelineMultisampleStateCreateInfo.pNext = nullptr; vkPipelineMultisampleStateCreateInfo.flags = 0; vkPipelineMultisampleStateCreateInfo.rasterizationSamples = global_sample_count; vkPipelineMultisampleStateCreateInfo.sampleShadingEnable = VK_FALSE; vkPipelineMultisampleStateCreateInfo.minSampleShading = 0.0f; vkPipelineMultisampleStateCreateInfo.pSampleMask = nullptr; vkPipelineMultisampleStateCreateInfo.alphaToCoverageEnable = VK_FALSE; vkPipelineMultisampleStateCreateInfo.alphaToOneEnable = VK_FALSE; // TODO Transparency VkPipelineColorBlendAttachmentState vkPipelineColorBlendAttachmentState[1]; vkPipelineColorBlendAttachmentState[0].blendEnable = VK_FALSE; vkPipelineColorBlendAttachmentState[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; vkPipelineColorBlendAttachmentState[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; vkPipelineColorBlendAttachmentState[0].colorBlendOp = VK_BLEND_OP_ADD; vkPipelineColorBlendAttachmentState[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; vkPipelineColorBlendAttachmentState[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; vkPipelineColorBlendAttachmentState[0].alphaBlendOp = VK_BLEND_OP_SUBTRACT; vkPipelineColorBlendAttachmentState[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; VkPipelineColorBlendStateCreateInfo vkPipelineColorBlendStateCreateInfo; vkPipelineColorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; vkPipelineColorBlendStateCreateInfo.pNext = nullptr; vkPipelineColorBlendStateCreateInfo.flags = {}; vkPipelineColorBlendStateCreateInfo.logicOpEnable = VK_FALSE; vkPipelineColorBlendStateCreateInfo.logicOp = VK_LOGIC_OP_COPY; vkPipelineColorBlendStateCreateInfo.attachmentCount = 1; vkPipelineColorBlendStateCreateInfo.pAttachments = vkPipelineColorBlendAttachmentState; vkPipelineColorBlendStateCreateInfo.blendConstants[0] = 0.0f; vkPipelineColorBlendStateCreateInfo.blendConstants[1] = 0.0f; vkPipelineColorBlendStateCreateInfo.blendConstants[2] = 0.0f; vkPipelineColorBlendStateCreateInfo.blendConstants[3] = 0.0f; VkDynamicState dynamicStates[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo vkPipelineDynamicStateCreateInfo; vkPipelineDynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; vkPipelineDynamicStateCreateInfo.pNext = nullptr; vkPipelineDynamicStateCreateInfo.flags = {}; vkPipelineDynamicStateCreateInfo.dynamicStateCount = 2; vkPipelineDynamicStateCreateInfo.pDynamicStates = dynamicStates; VkPipelineDepthStencilStateCreateInfo vkPipelineDepthStencilStateCreateInfo; vkPipelineDepthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; vkPipelineDepthStencilStateCreateInfo.pNext = nullptr; vkPipelineDepthStencilStateCreateInfo.flags = {}; vkPipelineDepthStencilStateCreateInfo.depthTestEnable = VK_TRUE; vkPipelineDepthStencilStateCreateInfo.depthWriteEnable = VK_TRUE; vkPipelineDepthStencilStateCreateInfo.depthCompareOp = VK_COMPARE_OP_LESS; vkPipelineDepthStencilStateCreateInfo.depthBoundsTestEnable = VK_FALSE; vkPipelineDepthStencilStateCreateInfo.stencilTestEnable = VK_FALSE; vkPipelineDepthStencilStateCreateInfo.front = {}; vkPipelineDepthStencilStateCreateInfo.back = {}; vkPipelineDepthStencilStateCreateInfo.minDepthBounds = {}; vkPipelineDepthStencilStateCreateInfo.maxDepthBounds = {}; const Asset *textureVertShaderAsset = AM_Get(vertShaderAssetHandle); const Asset *textureFragShaderAsset = AM_Get(textureFragShaderAssetHandle); const Asset *glyphVertShaderAsset = AM_Get(vertGlyphAssetHandle); const Asset *glyphFragShaderAsset = AM_Get(fragGlyphAssetHandle); assert(textureVertShaderAsset != nullptr && textureVertShaderAsset->state == PKE_ASSET_LOADING_STATE_LOADED); assert(textureFragShaderAsset != nullptr && textureFragShaderAsset->state == PKE_ASSET_LOADING_STATE_LOADED); assert(glyphVertShaderAsset != nullptr && glyphVertShaderAsset->state == PKE_ASSET_LOADING_STATE_LOADED); assert(glyphFragShaderAsset != nullptr && glyphFragShaderAsset->state == PKE_ASSET_LOADING_STATE_LOADED); VkPipelineShaderStageCreateInfo vkPipelineShaderStageCreateInfo_txtr[2]; for (long i = 0; i < 2; ++i) { vkPipelineShaderStageCreateInfo_txtr[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; vkPipelineShaderStageCreateInfo_txtr[i].pNext = nullptr; vkPipelineShaderStageCreateInfo_txtr[i].flags = {}; vkPipelineShaderStageCreateInfo_txtr[i].pName = "main"; vkPipelineShaderStageCreateInfo_txtr[i].pSpecializationInfo = nullptr; } vkPipelineShaderStageCreateInfo_txtr[0].stage = VK_SHADER_STAGE_VERTEX_BIT; vkPipelineShaderStageCreateInfo_txtr[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; VkPipelineShaderStageCreateInfo vkPipelineShaderStageCreateInfo_glyph[2]; for (long i = 0; i < 2; ++i) { vkPipelineShaderStageCreateInfo_glyph[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; vkPipelineShaderStageCreateInfo_glyph[i].pNext = nullptr; vkPipelineShaderStageCreateInfo_glyph[i].flags = {}; vkPipelineShaderStageCreateInfo_glyph[i].pName = "main"; vkPipelineShaderStageCreateInfo_glyph[i].pSpecializationInfo = nullptr; } vkPipelineShaderStageCreateInfo_glyph[0].stage = VK_SHADER_STAGE_VERTEX_BIT; vkPipelineShaderStageCreateInfo_glyph[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; VkGraphicsPipelineCreateInfo vkGraphicsPipelineCreateInfo[3]; for (long i = 0; i < 3; ++i) { vkGraphicsPipelineCreateInfo[i].sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; vkGraphicsPipelineCreateInfo[i].pNext = nullptr; vkGraphicsPipelineCreateInfo[i].flags = {}; vkGraphicsPipelineCreateInfo[i].stageCount = 2; vkGraphicsPipelineCreateInfo[i].pInputAssemblyState = &vkPipelineInputAssemblyStateCreateInfo; vkGraphicsPipelineCreateInfo[i].pTessellationState = nullptr; vkGraphicsPipelineCreateInfo[i].pViewportState = &vkPipelineViewportStateCreateInfo; vkGraphicsPipelineCreateInfo[i].pMultisampleState = &vkPipelineMultisampleStateCreateInfo; vkGraphicsPipelineCreateInfo[i].pDepthStencilState = &vkPipelineDepthStencilStateCreateInfo; vkGraphicsPipelineCreateInfo[i].pColorBlendState = &vkPipelineColorBlendStateCreateInfo; vkGraphicsPipelineCreateInfo[i].pDynamicState = &vkPipelineDynamicStateCreateInfo; vkGraphicsPipelineCreateInfo[i].subpass = 0; vkGraphicsPipelineCreateInfo[i].basePipelineHandle = VK_NULL_HANDLE; vkGraphicsPipelineCreateInfo[i].basePipelineIndex = {}; } vkGraphicsPipelineCreateInfo[0].layout = pkePipelines.pipe_layouts.named.texture; vkGraphicsPipelineCreateInfo[1].layout = pkePipelines.pipe_layouts.named.texture; vkGraphicsPipelineCreateInfo[2].layout = pkePipelines.pipe_layouts.named.glyph; vkGraphicsPipelineCreateInfo[0].renderPass = pkvk_3d.render_pass; vkGraphicsPipelineCreateInfo[1].renderPass = pkvk_3d.render_pass; vkGraphicsPipelineCreateInfo[2].renderPass = pkvk_2d.render_pass; vkGraphicsPipelineCreateInfo[0].pVertexInputState = &vkPipelineVertexInputStateCreateInfo_txtr; vkGraphicsPipelineCreateInfo[1].pVertexInputState = &vkPipelineVertexInputStateCreateInfo_txtr; vkGraphicsPipelineCreateInfo[2].pVertexInputState = &vkPipelineVertexInputStateCreateInfo_glyph; vkGraphicsPipelineCreateInfo[0].pRasterizationState = &vkPipelineRasterizationStateCreateInfoFill; vkGraphicsPipelineCreateInfo[1].pRasterizationState = &vkPipelineRasterizationStateCreateInfoLine; vkGraphicsPipelineCreateInfo[2].pRasterizationState = &vkPipelineRasterizationStateCreateInfoFill; vkGraphicsPipelineCreateInfo[0].pStages = vkPipelineShaderStageCreateInfo_txtr; vkGraphicsPipelineCreateInfo[1].pStages = vkPipelineShaderStageCreateInfo_txtr; vkGraphicsPipelineCreateInfo[2].pStages = vkPipelineShaderStageCreateInfo_glyph; // deffered shader creation vkPipelineShaderStageCreateInfo_txtr[0].module = UploadShader(vertShaderAssetHandle); vkPipelineShaderStageCreateInfo_txtr[1].module = UploadShader(textureFragShaderAssetHandle); vkPipelineShaderStageCreateInfo_glyph[0].module = UploadShader(vertGlyphAssetHandle); vkPipelineShaderStageCreateInfo_glyph[1].module = UploadShader(fragGlyphAssetHandle); vkResult = vkCreateGraphicsPipelines(vkDevice, VK_NULL_HANDLE, 1, &vkGraphicsPipelineCreateInfo[0], vkAllocator, &pkePipelines.pipelines.named.texture); assert(vkResult == VK_SUCCESS); vkResult = vkCreateGraphicsPipelines(vkDevice, VK_NULL_HANDLE, 1, &vkGraphicsPipelineCreateInfo[1], vkAllocator, &pkePipelines.pipelines.named.texture_wireframe); assert(vkResult == VK_SUCCESS); vkResult = vkCreateGraphicsPipelines(vkDevice, VK_NULL_HANDLE, 1, &vkGraphicsPipelineCreateInfo[2], vkAllocator, &pkePipelines.pipelines.named.glyph); assert(vkResult == VK_SUCCESS); assert(pkePipelines.pipelines.named.texture != VK_NULL_HANDLE); assert(pkePipelines.pipelines.named.texture_wireframe != VK_NULL_HANDLE); assert(pkePipelines.pipelines.named.glyph != VK_NULL_HANDLE); for (long i = 0; i < 2; ++i) { vkDestroyShaderModule(vkDevice, vkPipelineShaderStageCreateInfo_txtr[i].module, vkAllocator); vkDestroyShaderModule(vkDevice, vkPipelineShaderStageCreateInfo_glyph[i].module, vkAllocator); } // reverse order AM_Release(fragGlyphAssetHandle); AM_Release(vertGlyphAssetHandle); AM_Release(vertShaderAssetHandle); AM_Release(textureFragShaderAssetHandle); } // debug texture { VkImageCreateInfo dbgTextureCI; dbgTextureCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; dbgTextureCI.pNext = nullptr; dbgTextureCI.flags = 0; dbgTextureCI.imageType = VK_IMAGE_TYPE_2D; dbgTextureCI.format = VK_FORMAT_R8G8B8A8_SRGB; dbgTextureCI.extent = VkExtent3D { .width = 2, .height = 2, .depth = 1, }; dbgTextureCI.mipLevels = 1; dbgTextureCI.arrayLayers = 1; dbgTextureCI.samples = VK_SAMPLE_COUNT_1_BIT; dbgTextureCI.tiling = VK_IMAGE_TILING_OPTIMAL; dbgTextureCI.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; dbgTextureCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE; dbgTextureCI.queueFamilyIndexCount = 0; dbgTextureCI.pQueueFamilyIndices = nullptr; dbgTextureCI.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; VkResult result; result = vkCreateImage(vkDevice, &dbgTextureCI, vkAllocator, &pkeDebugHitbox.vkImage); assert(result == VK_SUCCESS); VkMemoryRequirements imageMemReqs; vkGetImageMemoryRequirements(vkDevice, pkeDebugHitbox.vkImage, &imageMemReqs); VkMemoryAllocateInfo imageAllocInfo; imageAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; imageAllocInfo.pNext = nullptr; imageAllocInfo.allocationSize = imageMemReqs.size; imageAllocInfo.memoryTypeIndex = FindMemoryTypeIndex(imageMemReqs.memoryTypeBits, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT); if (imageAllocInfo.memoryTypeIndex == 0) { imageAllocInfo.memoryTypeIndex = FindMemoryTypeIndex(imageMemReqs.memoryTypeBits, 0); } result = vkAllocateMemory(vkDevice, &imageAllocInfo, vkAllocator, &pkeDebugHitbox.textureMemory); assert(result == VK_SUCCESS); result = vkBindImageMemory(vkDevice, pkeDebugHitbox.vkImage, pkeDebugHitbox.textureMemory, 0); assert(result == VK_SUCCESS); VkImageViewCreateInfo dbgImageViewCI; dbgImageViewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; dbgImageViewCI.pNext = nullptr; dbgImageViewCI.flags = 0; dbgImageViewCI.image = pkeDebugHitbox.vkImage; dbgImageViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D; dbgImageViewCI.format = VK_FORMAT_R8G8B8A8_SRGB; dbgImageViewCI.components = VkComponentMapping { .r = VK_COMPONENT_SWIZZLE_IDENTITY, .g = VK_COMPONENT_SWIZZLE_IDENTITY, .b = VK_COMPONENT_SWIZZLE_IDENTITY, .a = VK_COMPONENT_SWIZZLE_IDENTITY, }; dbgImageViewCI.subresourceRange = VkImageSubresourceRange { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1, }; result = vkCreateImageView(vkDevice, &dbgImageViewCI, vkAllocator, &pkeDebugHitbox.vkImageView); assert(result == VK_SUCCESS); PKVK_BeginBuffer(transferFamilyIndex, 4 * 4, tmpBufferDetails); memcpy(tmpBufferDetails.deviceData, PKE_MISSING_TEXTURE_DATA, 4 * 4); { VkImageMemoryBarrier vkImageMemoryBarrier; vkImageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; vkImageMemoryBarrier.pNext = nullptr; vkImageMemoryBarrier.srcAccessMask = {}; vkImageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; vkImageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; vkImageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; vkImageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; vkImageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; vkImageMemoryBarrier.image = pkeDebugHitbox.vkImage; vkImageMemoryBarrier.subresourceRange = VkImageSubresourceRange { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1, }; VkCommandBufferBeginInfo vkCommandBufferBeginInfo; vkCommandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; vkCommandBufferBeginInfo.pNext = nullptr; vkCommandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; vkCommandBufferBeginInfo.pInheritanceInfo = nullptr; vkBeginCommandBuffer(pkvk_shared.command_buffer.transfer, &vkCommandBufferBeginInfo); vkCmdPipelineBarrier(pkvk_shared.command_buffer.transfer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &vkImageMemoryBarrier); VkBufferImageCopy vkBufferImageCopy; vkBufferImageCopy.bufferOffset = 0; vkBufferImageCopy.bufferRowLength = 2; vkBufferImageCopy.bufferImageHeight = 2; vkBufferImageCopy.imageSubresource = VkImageSubresourceLayers { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .mipLevel = 0, .baseArrayLayer = 0, .layerCount = 1, }; vkBufferImageCopy.imageOffset = VkOffset3D { .x = 0, .y = 0, .z = 0, }; vkBufferImageCopy.imageExtent = VkExtent3D { .width = 2, .height = 2, .depth = 1, }; vkCmdCopyBufferToImage(pkvk_shared.command_buffer.transfer, tmpBufferDetails.buffer, pkeDebugHitbox.vkImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &vkBufferImageCopy); vkEndCommandBuffer(pkvk_shared.command_buffer.transfer); VkSubmitInfo submitInfo; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.pNext = nullptr; submitInfo.waitSemaphoreCount = 0; submitInfo.pWaitSemaphores = nullptr; submitInfo.pWaitDstStageMask = nullptr; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &pkvk_shared.command_buffer.transfer; submitInfo.signalSemaphoreCount = 0; submitInfo.pSignalSemaphores = nullptr; vkQueueSubmit(tmpBufferDetails.queue, 1, &submitInfo, nullptr); vkQueueWaitIdle(tmpBufferDetails.queue); vkResetCommandBuffer(pkvk_shared.command_buffer.transfer, 0); } PKVK_EndBuffer(tmpBufferDetails); { VkImageMemoryBarrier vkImageMemoryBarrier; vkImageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; vkImageMemoryBarrier.pNext = nullptr; vkImageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; vkImageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; vkImageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; vkImageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; vkImageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; vkImageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; vkImageMemoryBarrier.image = pkeDebugHitbox.vkImage; vkImageMemoryBarrier.subresourceRange = VkImageSubresourceRange { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1, }; VkCommandBufferBeginInfo vkCommandBufferBeginInfo; vkCommandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; vkCommandBufferBeginInfo.pNext = nullptr; vkCommandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; vkCommandBufferBeginInfo.pInheritanceInfo = nullptr; vkBeginCommandBuffer(pkvk_shared.command_buffer.graphics, &vkCommandBufferBeginInfo); vkCmdPipelineBarrier(pkvk_shared.command_buffer.graphics, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &vkImageMemoryBarrier); vkEndCommandBuffer(pkvk_shared.command_buffer.graphics); VkSubmitInfo submitInfo; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.pNext = nullptr; submitInfo.waitSemaphoreCount = 0; submitInfo.pWaitSemaphores = nullptr; submitInfo.pWaitDstStageMask = nullptr; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &pkvk_shared.command_buffer.graphics; submitInfo.signalSemaphoreCount = 0; submitInfo.pSignalSemaphores = nullptr; vkQueueSubmit(pkvk_shared.queue.graphics, 1, &submitInfo, nullptr); vkQueueWaitIdle(pkvk_shared.queue.graphics); } } // debug model { // ((sizeof(glm::vec3) + sizeof(glm::vec3) + sizeof(glm::vec2)) * 8) // + (sizeof(uint16_t) * 36); long index = 0; VkMemoryRequirements vkMemoryRequirements[4]; VkBufferCreateInfo dbgModelBufferCI{}; dbgModelBufferCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; dbgModelBufferCI.pNext = nullptr; dbgModelBufferCI.flags = 0; dbgModelBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; dbgModelBufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE; dbgModelBufferCI.queueFamilyIndexCount = 1; dbgModelBufferCI.pQueueFamilyIndices = &graphicsFamilyIndex; // vertex dbgModelBufferCI.size = sizeof(glm::vec3) * 8; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.vertexBuffer); vkGetBufferMemoryRequirements(vkDevice, pkeDebugHitbox.vertexBuffer, &vkMemoryRequirements[index]); index++; // normals dbgModelBufferCI.size = sizeof(glm::vec3) * 8; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.normalsBuffer); vkGetBufferMemoryRequirements(vkDevice, pkeDebugHitbox.normalsBuffer, &vkMemoryRequirements[index]); index++; // uv dbgModelBufferCI.size = sizeof(glm::vec2) * 8; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.uvBuffer); vkGetBufferMemoryRequirements(vkDevice, pkeDebugHitbox.uvBuffer, &vkMemoryRequirements[index]); index++; // index dbgModelBufferCI.size = sizeof(uint16_t) * 36; dbgModelBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.indexBuffer); vkGetBufferMemoryRequirements(vkDevice, pkeDebugHitbox.indexBuffer, &vkMemoryRequirements[index]); pkeDebugHitbox.indexCount = 36; // index++; VkMemoryRequirements combinedMemReqs{}; combinedMemReqs.alignment = vkMemoryRequirements[0].alignment; combinedMemReqs.memoryTypeBits = 0; combinedMemReqs.size = 0; for (long i = 1; i < 4; ++i) { if (combinedMemReqs.alignment == vkMemoryRequirements[i].alignment) { continue; } int larger, smaller; if (combinedMemReqs.alignment > vkMemoryRequirements[i].alignment) { larger = combinedMemReqs.alignment; smaller = vkMemoryRequirements[i].alignment; } else { larger = vkMemoryRequirements[i].alignment; smaller = combinedMemReqs.alignment; } if (larger % smaller == 0) { combinedMemReqs.alignment = larger; continue; } int combined = larger * smaller; while ((combined / 2) % 2 == 0 && (combined / 2) % larger == 0) { combined /= 2; } combinedMemReqs.alignment = combined; } for (long i = 0; i < 4; ++i) { uint32_t alignmentPadding = vkMemoryRequirements[i].size % combinedMemReqs.alignment; combinedMemReqs.size += vkMemoryRequirements[i].size + (alignmentPadding == 0 ? 0 : combinedMemReqs.alignment - alignmentPadding); combinedMemReqs.memoryTypeBits |= vkMemoryRequirements[i].memoryTypeBits; } VkMemoryAllocateInfo vkMemoryAllocateInfo; vkMemoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; vkMemoryAllocateInfo.pNext = nullptr; vkMemoryAllocateInfo.allocationSize = combinedMemReqs.size; vkMemoryAllocateInfo.memoryTypeIndex = FindMemoryTypeIndex(combinedMemReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); vkAllocateMemory(vkDevice, &vkMemoryAllocateInfo, vkAllocator, &pkeDebugHitbox.vertBufferMemory); vkDestroyBuffer(vkDevice, pkeDebugHitbox.indexBuffer, vkAllocator); vkDestroyBuffer(vkDevice, pkeDebugHitbox.uvBuffer, vkAllocator); vkDestroyBuffer(vkDevice, pkeDebugHitbox.normalsBuffer, vkAllocator); vkDestroyBuffer(vkDevice, pkeDebugHitbox.vertexBuffer, vkAllocator); // bind buffers uint32_t runningOffset = 0; uint32_t alignmentPadding; // vertex uint32_t offsetVert = runningOffset; uint32_t sizeVert = sizeof(glm::vec3) * 8; alignmentPadding = sizeVert % combinedMemReqs.alignment; alignmentPadding = alignmentPadding == 0 ? 0 : combinedMemReqs.alignment - alignmentPadding; sizeVert += alignmentPadding; dbgModelBufferCI.size = sizeVert; dbgModelBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.vertexBuffer); vkBindBufferMemory(vkDevice, pkeDebugHitbox.vertexBuffer, pkeDebugHitbox.vertBufferMemory, offsetVert); runningOffset += sizeVert; // norm uint32_t offsetNorm = runningOffset; uint32_t sizeNorm = sizeof(glm::vec3) * 8; alignmentPadding = sizeNorm % combinedMemReqs.alignment; alignmentPadding = alignmentPadding == 0 ? 0 : combinedMemReqs.alignment - alignmentPadding; sizeNorm += alignmentPadding; dbgModelBufferCI.size = sizeNorm; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.normalsBuffer); vkBindBufferMemory(vkDevice, pkeDebugHitbox.normalsBuffer, pkeDebugHitbox.vertBufferMemory, offsetNorm); runningOffset += sizeNorm; // uv uint32_t offsetUV = runningOffset; uint32_t sizeUV = sizeof(glm::vec2) * 8; alignmentPadding = sizeUV % combinedMemReqs.alignment; alignmentPadding = alignmentPadding == 0 ? 0 : combinedMemReqs.alignment - alignmentPadding; sizeUV += alignmentPadding; dbgModelBufferCI.size = sizeUV; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.uvBuffer); vkBindBufferMemory(vkDevice, pkeDebugHitbox.uvBuffer , pkeDebugHitbox.vertBufferMemory, offsetUV); runningOffset += sizeUV; // index uint32_t offsetIndex = runningOffset; uint32_t sizeIndex = sizeof(uint16_t) * 36; alignmentPadding = sizeIndex % combinedMemReqs.alignment; alignmentPadding = alignmentPadding == 0 ? 0 : combinedMemReqs.alignment - alignmentPadding; sizeIndex += alignmentPadding; dbgModelBufferCI.size = sizeIndex; dbgModelBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT; vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.indexBuffer); vkBindBufferMemory(vkDevice, pkeDebugHitbox.indexBuffer, pkeDebugHitbox.vertBufferMemory, offsetIndex); runningOffset += sizeIndex; assert(runningOffset == combinedMemReqs.size); // create transfer items && transfer { PKVK_BeginBuffer(transferFamilyIndex, combinedMemReqs.size, tmpBufferDetails); memset(tmpBufferDetails.deviceData, '\0', combinedMemReqs.size); char *dstPtr = nullptr; char *srcPtr = nullptr; dstPtr = static_cast(tmpBufferDetails.deviceData) + offsetVert; srcPtr = reinterpret_cast(pkeIntrinsicsCube.vert); memcpy(dstPtr, srcPtr, sizeVert); dstPtr = static_cast(tmpBufferDetails.deviceData) + offsetNorm; srcPtr = reinterpret_cast(pkeIntrinsicsCube.norm); memcpy(dstPtr, srcPtr, sizeNorm); dstPtr = static_cast(tmpBufferDetails.deviceData) + offsetUV; srcPtr = reinterpret_cast(pkeIntrinsicsCube.uv); memcpy(dstPtr, srcPtr, sizeUV); dstPtr = static_cast(tmpBufferDetails.deviceData) + offsetIndex; srcPtr = reinterpret_cast(pkeIntrinsicsCube.index); memcpy(dstPtr, srcPtr, sizeIndex); VkCommandBufferBeginInfo vkCommandBufferBeginInfo; vkCommandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; vkCommandBufferBeginInfo.pNext = nullptr; // TODO consider single-use? vkCommandBufferBeginInfo.flags = 0; vkCommandBufferBeginInfo.pInheritanceInfo = nullptr; vkBeginCommandBuffer(pkvk_shared.command_buffer.transfer, &vkCommandBufferBeginInfo); VkBufferCopy bufferCopys[4]; for (long i = 0; i < 4; ++i) { bufferCopys[i].dstOffset = 0; } index = 0; bufferCopys[index].srcOffset = offsetVert; bufferCopys[index].size = sizeVert; vkCmdCopyBuffer(pkvk_shared.command_buffer.transfer, tmpBufferDetails.buffer, pkeDebugHitbox.vertexBuffer, 1, &bufferCopys[index]); index+=1; bufferCopys[index].srcOffset = offsetNorm; bufferCopys[index].size = sizeNorm; vkCmdCopyBuffer(pkvk_shared.command_buffer.transfer, tmpBufferDetails.buffer, pkeDebugHitbox.normalsBuffer, 1, &bufferCopys[index]); index+=1; bufferCopys[index].srcOffset = offsetUV; bufferCopys[index].size = sizeUV; vkCmdCopyBuffer(pkvk_shared.command_buffer.transfer, tmpBufferDetails.buffer, pkeDebugHitbox.uvBuffer, 1, &bufferCopys[index]); index+=1; bufferCopys[index].srcOffset = offsetIndex; bufferCopys[index].size = sizeIndex; vkCmdCopyBuffer(pkvk_shared.command_buffer.transfer, tmpBufferDetails.buffer, pkeDebugHitbox.indexBuffer, 1, &bufferCopys[index]); // index+=1; vkEndCommandBuffer(pkvk_shared.command_buffer.transfer); VkSubmitInfo submitInfo; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.pNext = nullptr; submitInfo.waitSemaphoreCount = 0; submitInfo.pWaitSemaphores = nullptr; submitInfo.pWaitDstStageMask = nullptr; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &pkvk_shared.command_buffer.transfer; submitInfo.signalSemaphoreCount = 0; submitInfo.pSignalSemaphores = nullptr; vkQueueSubmit(tmpBufferDetails.queue, 1, &submitInfo, nullptr); vkQueueWaitIdle(tmpBufferDetails.queue); PKVK_EndBuffer(tmpBufferDetails); } } } void UpdateDebugGraphicsPipeline() { // descriptor pool & sets VkDescriptorPoolSize descriptorPoolSizes[2]; descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptorPoolSizes[0].descriptorCount = swapchainLength; descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; descriptorPoolSizes[1].descriptorCount = swapchainLength; VkDescriptorPoolCreateInfo vkDescriptorPoolCreateInfo; vkDescriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; vkDescriptorPoolCreateInfo.pNext = nullptr; vkDescriptorPoolCreateInfo.flags = 0; vkDescriptorPoolCreateInfo.maxSets = swapchainLength; vkDescriptorPoolCreateInfo.poolSizeCount = (uint32_t)2; vkDescriptorPoolCreateInfo.pPoolSizes = descriptorPoolSizes; // consider making me a global pool auto vkResult = vkCreateDescriptorPool(vkDevice, &vkDescriptorPoolCreateInfo, vkAllocator, &pkeDebugHitbox.vkDescriptorPool); assert(vkResult == VK_SUCCESS); VkDescriptorSetLayout *descriptorSets = pk_new(swapchainLength); for (long i = 0; i < swapchainLength; ++i) { descriptorSets[i] = pkePipelines.descr_layouts.named.texture; } VkDescriptorSetAllocateInfo vkDescriptorSetAllocateInfo; vkDescriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; vkDescriptorSetAllocateInfo.pNext = nullptr; vkDescriptorSetAllocateInfo.descriptorPool = pkeDebugHitbox.vkDescriptorPool; vkDescriptorSetAllocateInfo.descriptorSetCount = swapchainLength; vkDescriptorSetAllocateInfo.pSetLayouts = descriptorSets; pkeDebugHitbox.vkDescriptorSets = pk_new(swapchainLength); for (long i = 0; i < swapchainLength; ++i) { pkeDebugHitbox.vkDescriptorSets[i] = VkDescriptorSet{}; } vkResult = vkAllocateDescriptorSets(vkDevice, &vkDescriptorSetAllocateInfo, pkeDebugHitbox.vkDescriptorSets); pk_delete(descriptorSets, swapchainLength); // descriptorSets = CAFE_BABE(VkDescriptorSetLayout); assert(vkResult == VK_SUCCESS); VkWriteDescriptorSet *writeDescriptorSets = pk_new(2 * swapchainLength); for (long i = 0; i < 2 * swapchainLength; ++i) { writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[i].pNext = nullptr; writeDescriptorSets[i].dstSet = nullptr; writeDescriptorSets[i].dstBinding = i % 2; writeDescriptorSets[i].dstArrayElement = 0; writeDescriptorSets[i].descriptorCount = 1; writeDescriptorSets[i].descriptorType = (i % 2) == 0 ? VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER : VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[i].pImageInfo = nullptr; writeDescriptorSets[i].pBufferInfo = nullptr; writeDescriptorSets[i].pTexelBufferView = nullptr; } VkDescriptorImageInfo textureDescriptorInfo; textureDescriptorInfo.sampler = global_sampler; textureDescriptorInfo.imageView = pkeDebugHitbox.vkImageView; textureDescriptorInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; VkDescriptorBufferInfo *vkDescriptorBufferInfo = pk_new(swapchainLength); for (long i = 0; i < swapchainLength; ++i) { vkDescriptorBufferInfo[i].buffer = UniformBuffers[i]; vkDescriptorBufferInfo[i].offset = 0; vkDescriptorBufferInfo[i].range = sizeof(UniformBufferObject); long uboIndex = i * 2; long samplerIndex = uboIndex + 1; writeDescriptorSets[uboIndex].pBufferInfo = &vkDescriptorBufferInfo[i]; writeDescriptorSets[uboIndex].dstSet = pkeDebugHitbox.vkDescriptorSets[i]; writeDescriptorSets[samplerIndex].pImageInfo = &textureDescriptorInfo; writeDescriptorSets[samplerIndex].dstSet = pkeDebugHitbox.vkDescriptorSets[i]; } vkUpdateDescriptorSets(vkDevice, 2 * swapchainLength, writeDescriptorSets, 0, nullptr); pk_delete(writeDescriptorSets, 2 * swapchainLength); pk_delete(vkDescriptorBufferInfo, swapchainLength); } void CreateImGui() { IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; ImGui::StyleColorsDark(); VkDescriptorPoolSize poolSizes[] = { {VK_DESCRIPTOR_TYPE_SAMPLER, 1000}, {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000}, {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000}, {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000}, {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000}, {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000}, {VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000}, }; VkDescriptorPoolCreateInfo descriptorPoolCreateInfo; descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; descriptorPoolCreateInfo.pNext = nullptr; descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; descriptorPoolCreateInfo.maxSets = 1000 * IM_ARRAYSIZE(poolSizes), descriptorPoolCreateInfo.poolSizeCount = static_cast(IM_ARRAYSIZE(poolSizes)); descriptorPoolCreateInfo.pPoolSizes = poolSizes; vkCreateDescriptorPool(vkDevice, &descriptorPoolCreateInfo, vkAllocator, &imGuiDescriptorPool); ImGui_ImplGlfw_InitForVulkan(window, true); ImGui_ImplVulkan_InitInfo initInfo = {}; initInfo.Allocator = vkAllocator; initInfo.CheckVkResultFn = ImGuiCheckVkResult; // initInfo.ColorAttachmentFormat = VkFormat::VK_FORMAT_B8G8R8A8_SRGB; initInfo.DescriptorPool = imGuiDescriptorPool; initInfo.Device = vkDevice; initInfo.ImageCount = swapchainLength; initInfo.Instance = vkInstance; initInfo.MSAASamples = VK_SAMPLE_COUNT_1_BIT; initInfo.MinImageCount = swapchainLength; initInfo.PhysicalDevice = vkPhysicalDevice; initInfo.PipelineCache = {}; initInfo.Queue = pkvk_shared.queue.graphics; initInfo.QueueFamily = graphicsFamilyIndex; initInfo.RenderPass = pkvk_present.render_pass; initInfo.Subpass = 0; initInfo.UseDynamicRendering = false; ImGui_ImplVulkan_Init(&initInfo); pke_window_did_init_imgui = true; // font // { // VkCommandBufferBeginInfo begInfo; // begInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; // begInfo.pNext = nullptr; // begInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; // begInfo.pInheritanceInfo = {}; // vkBeginCommandBuffer(pkvk_present.command_buffers[0], &begInfo); // ImGui_ImplVulkan_CreateFontsTexture(); // ImGui_ImplVulkan_CreateFontsTexture(pkvk_present.command_buffers[0]); // vkEndCommandBuffer(pkvk_present.command_buffers[0]); // VkSubmitInfo submitInfo; // submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; // submitInfo.pNext = nullptr; // submitInfo.waitSemaphoreCount = 0; // submitInfo.pWaitSemaphores = nullptr; // submitInfo.pWaitDstStageMask = nullptr; // submitInfo.commandBufferCount = 1; // submitInfo.pCommandBuffers = pkvk_present.command_buffers; // submitInfo.signalSemaphoreCount = 0; // submitInfo.pSignalSemaphores = nullptr; // vkQueueSubmit(graphicsQueue, 1, &submitInfo, nullptr); // vkQueueWaitIdle(graphicsQueue); // } // ImGui_ImplVulkan_DestroyFontsTexture(); // ImGui_ImplVulkan_DestroyFontUploadObjects(); } void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { vkResetCommandBuffer(commandBuffer, 0); VkCommandBufferBeginInfo beginInfo; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.pNext = nullptr; beginInfo.flags = 0; beginInfo.pInheritanceInfo = nullptr; auto result = vkBeginCommandBuffer(commandBuffer, &beginInfo); if (result != VK_SUCCESS) { throw "failed to begin recording command buffer"; } VkClearColorValue clearColorTransparent = {{0.0f, 0.0f, 0.0f, 0.0f}}; VkClearColorValue clearColorBlack = {{0.0f, 0.0f, 0.0f, 1.0f}}; VkClearValue clearValues[3]; clearValues[0].color = clearColorBlack; clearValues[1].color = clearColorBlack; clearValues[2].depthStencil.depth = 1.0; clearValues[2].depthStencil.stencil = 0; VkRenderPassBeginInfo renderPassInfo; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassInfo.renderPass = pkvk_3d.render_pass; renderPassInfo.framebuffer = pkvk_3d.framebuffers[imageIndex]; renderPassInfo.renderArea.offset = {0, 0}; renderPassInfo.renderArea.extent = Extent; renderPassInfo.clearValueCount = 3; renderPassInfo.pClearValues = clearValues; renderPassInfo.pNext = VK_NULL_HANDLE; vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); VkViewport viewport; viewport.x = 0.0f; viewport.y = 0.0f; viewport.width = (float)Extent.width; viewport.height = (float)Extent.height; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; VkRect2D scissor; scissor.offset = {0, 0}; scissor.extent = Extent; vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetScissor(commandBuffer, 0, 1, &scissor); VkDeviceSize offsets[1] = {0U}; pk_handle_bucket_index_T bindBucketCount = ECS_GetGrBinds_BucketCount(); for (pk_handle_bucket_index_T b = 0; b < bindBucketCount; ++b) { pk_handle_item_index_T itemCount; CompGrBinds *items = ECS_GetGrBinds(b, itemCount); for (pk_handle_item_index_T i = 0; i < itemCount; ++i) { CompGrBinds *binder = &items[i]; if (binder->grBindsHandle == GrBindsHandle_MAX) continue; if (!binder->vkPipelineLayout) continue; if (binder->instanceBD.bindingCount < 1) { continue; } vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, binder->graphicsPipeline); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, binder->vkPipelineLayout, 0, 1, &binder->vkDescriptorSets[imageIndex], 0, {}); vkCmdBindVertexBuffers(commandBuffer, 0, 1, &UniformBuffers[imageIndex], offsets); vkCmdBindIndexBuffer(commandBuffer, binder->indexBD.buffer, binder->indexBD.offsets[0], VK_INDEX_TYPE_UINT16); vkCmdBindVertexBuffers(commandBuffer, binder->vertexBD.firstBinding, binder->vertexBD.bindingCount, &binder->vertexBD.buffer, binder->vertexBD.offsets); vkCmdBindVertexBuffers(commandBuffer, binder->normalsBD.firstBinding, binder->normalsBD.bindingCount, &binder->normalsBD.buffer, binder->normalsBD.offsets); vkCmdBindVertexBuffers(commandBuffer, binder->uvBD.firstBinding, binder->uvBD.bindingCount, &binder->uvBD.buffer, binder->uvBD.offsets); vkCmdBindVertexBuffers(commandBuffer, binder->instanceBD.firstBinding, binder->instanceBD.bindingCount, &binder->instanceBD.buffer, binder->instanceBD.offsets); vkCmdDrawIndexed(commandBuffer, binder->indexCount, binder->instanceCounter, 0, 0, 0); if (pkeSettings.isRenderingDebug) { vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipelines.named.texture_wireframe); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipe_layouts.named.texture, 0, 1, &pkeDebugHitbox.vkDescriptorSets[imageIndex], 0, {}); vkCmdBindVertexBuffers(commandBuffer, binder->physVertBD.firstBinding, 1, &binder->physVertBD.buffer, binder->physVertBD.offsets); vkCmdBindVertexBuffers(commandBuffer, binder->physNormBD.firstBinding, 1, &binder->physNormBD.buffer, binder->physVertBD.offsets); vkCmdBindVertexBuffers(commandBuffer, binder->physUvBD.firstBinding, 1, &binder->physUvBD.buffer, binder->physVertBD.offsets); vkCmdBindIndexBuffer(commandBuffer, binder->physIndxBD.buffer, binder->physIndxBD.offsets[0], VK_INDEX_TYPE_UINT16); vkCmdDrawIndexed(commandBuffer, binder->physIndxBD.bindingCount, binder->instanceCounter, 0, 0, 0); } } } if (pkeDebugHitbox.instanceBuffer != VK_NULL_HANDLE) { vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipelines.named.texture_wireframe); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipe_layouts.named.texture, 0, 1, &pkeDebugHitbox.vkDescriptorSets[imageIndex], 0, {}); vkCmdBindVertexBuffers(commandBuffer, 0, 1, &UniformBuffers[imageIndex], offsets); // TODO don't hardcode firstBinding vkCmdBindVertexBuffers(commandBuffer, 0, 1, &pkeDebugHitbox.vertexBuffer, offsets); vkCmdBindVertexBuffers(commandBuffer, 1, 1, &pkeDebugHitbox.normalsBuffer, offsets); vkCmdBindVertexBuffers(commandBuffer, 2, 1, &pkeDebugHitbox.uvBuffer, offsets); vkCmdBindIndexBuffer(commandBuffer, pkeDebugHitbox.indexBuffer, offsets[0], VK_INDEX_TYPE_UINT16); vkCmdBindVertexBuffers(commandBuffer, 3, 1, &pkeDebugHitbox.instanceBuffer, offsets); vkCmdDrawIndexed(commandBuffer, pkeDebugHitbox.indexCount, pkeDebugHitbox.instanceCount, 0, 0, pkeDebugHitbox.instanceStartingIndex); } vkCmdEndRenderPass(commandBuffer); // 2d overlay pass renderPassInfo.renderPass = pkvk_2d.render_pass; renderPassInfo.framebuffer = pkvk_2d.framebuffers[imageIndex]; renderPassInfo.clearValueCount = 1; clearValues[0].color = clearColorTransparent; vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetScissor(commandBuffer, 0, 1, &scissor); // 2d overlay grbinds FontTypeIndex count; FontType *fts = FontType_GetFonts(count); for (FontTypeIndex i = FontTypeIndex{0}; i < count; ++i) { FontType *ft = &fts[(FontTypeIndex_T)i]; if (ft->bindings.instanceCounter == 0) continue; vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipelines.named.glyph); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipe_layouts.named.glyph, 0, 1, &ft->gr.vkDescriptorSets[imageIndex], 0, {}); vkCmdBindIndexBuffer(commandBuffer, ft->bindings.indexBD.buffer, ft->bindings.indexBD.offsets[0], VK_INDEX_TYPE_UINT16); vkCmdBindVertexBuffers(commandBuffer, ft->bindings.vertexBD.firstBinding, ft->bindings.vertexBD.bindingCount, &ft->bindings.vertexBD.buffer, ft->bindings.vertexBD.offsets); vkCmdBindVertexBuffers(commandBuffer, ft->bindings.uvBD.firstBinding, ft->bindings.uvBD.bindingCount, &ft->bindings.uvBD.buffer, ft->bindings.uvBD.offsets); vkCmdBindVertexBuffers(commandBuffer, ft->bindings.atlasSizeBD.firstBinding, ft->bindings.atlasSizeBD.bindingCount, &ft->bindings.atlasSizeBD.buffer, ft->bindings.atlasSizeBD.offsets); vkCmdBindVertexBuffers(commandBuffer, ft->bindings.instanceBD.firstBinding, ft->bindings.instanceBD.bindingCount, &ft->bindings.instanceBD.buffer, ft->bindings.instanceBD.offsets); vkCmdDrawIndexed(commandBuffer, ft->bindings.indexCount, ft->bindings.instanceCounter, 0, 0, 0); } vkCmdEndRenderPass(commandBuffer); // present pass renderPassInfo.renderPass = pkvk_present.render_pass; renderPassInfo.framebuffer = pkvk_present.framebuffers[imageIndex]; renderPassInfo.clearValueCount = 1; clearValues[0].color = clearColorBlack; vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetScissor(commandBuffer, 0, 1, &scissor); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkvk_present.pipeline); // reminder that present.vert is a triangle // 3d vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkvk_present.pipeline_layout, 0, 1, &pkvk_present.descriptor_sets[(imageIndex * 2)], 0, nullptr); vkCmdDraw(commandBuffer, 3, 1, 0, 0); // 2d vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkvk_present.pipeline_layout, 0, 1, &pkvk_present.descriptor_sets[(imageIndex * 2) + 1], 0, nullptr); vkCmdDraw(commandBuffer, 3, 1, 0, 0); // ImGui const auto plugin_count = LoadedPkePlugins.Count(); bool any = false; for (long i = 0; i < plugin_count; ++i) { if (LoadedPkePlugins[i].OnImGuiRender != nullptr) { any = true; break; } } if (any) { ImGui_ImplVulkan_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); for (long i = 0; i < plugin_count; ++i) { if (LoadedPkePlugins[i].OnImGuiRender != nullptr) { LoadedPkePlugins[i].OnImGuiRender(); } } ImGui::Render(); auto drawData = ImGui::GetDrawData(); const bool isMinimized = drawData->DisplaySize.x <= 0.0f || drawData->DisplaySize.y <= 0.0f; if (isMinimized) return; ImGui_ImplVulkan_RenderDrawData(drawData, commandBuffer); } vkCmdEndRenderPass(commandBuffer); result = vkEndCommandBuffer(commandBuffer); if (result != VK_SUCCESS) { throw "failed to record command buffer!"; } } void DestroySyncObjects() { for (long i = 0; i < prevSwapchainLength; ++i) { if (pkvk_present.semaphores_image_available[i] != VK_NULL_HANDLE) { vkDestroySemaphore(vkDevice, pkvk_present.semaphores_image_available[i], vkAllocator); pkvk_present.semaphores_image_available[i] = VK_NULL_HANDLE; } if (pkvk_present.semaphores_render_finished[i] != VK_NULL_HANDLE) { vkDestroySemaphore(vkDevice, pkvk_present.semaphores_render_finished[i], vkAllocator); pkvk_present.semaphores_render_finished[i] = VK_NULL_HANDLE; } if (pkvk_present.fences_in_flight[i] != VK_NULL_HANDLE) { vkDestroyFence(vkDevice, pkvk_present.fences_in_flight[i], vkAllocator); pkvk_present.fences_in_flight[i] = VK_NULL_HANDLE; } } pk_delete(pkvk_present.semaphores_image_available, prevSwapchainLength, MemBkt_Vulkan); pkvk_present.semaphores_image_available = CAFE_BABE(VkSemaphore); pk_delete(pkvk_present.semaphores_render_finished, prevSwapchainLength, MemBkt_Vulkan); pkvk_present.semaphores_render_finished = CAFE_BABE(VkSemaphore); pk_delete(pkvk_present.fences_in_flight, prevSwapchainLength, MemBkt_Vulkan); pkvk_present.fences_in_flight = CAFE_BABE(VkFence); } void DestroyPresentPipeline() { if (pkvk_present.pipeline != VK_NULL_HANDLE) { vkDestroyPipeline(vkDevice, pkvk_present.pipeline, vkAllocator); pkvk_present.pipeline = VK_NULL_HANDLE; } if (pkvk_present.pipeline_layout != VK_NULL_HANDLE) { vkDestroyPipelineLayout(vkDevice, pkvk_present.pipeline_layout, vkAllocator); pkvk_present.pipeline_layout = VK_NULL_HANDLE; } if (pkvk_present.descriptor_sets != nullptr && pkvk_present.descriptor_sets != CAFE_BABE(VkDescriptorSet)) { vkFreeDescriptorSets(vkDevice, pkvk_present.descriptor_pool, prevSwapchainLength * 2, pkvk_present.descriptor_sets); pk_delete(pkvk_present.descriptor_sets, prevSwapchainLength * 2, MemBkt_Vulkan); pkvk_present.descriptor_sets = CAFE_BABE(VkDescriptorSet); } if (pkvk_present.descriptor_pool != VK_NULL_HANDLE) { vkDestroyDescriptorPool(vkDevice, pkvk_present.descriptor_pool, vkAllocator); pkvk_present.descriptor_pool = VK_NULL_HANDLE; } } void DestroySwapchain() { if (pkvk_2d.framebuffers != nullptr && pkvk_2d.framebuffers != CAFE_BABE(VkFramebuffer)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyFramebuffer(vkDevice, pkvk_2d.framebuffers[i], vkAllocator); } pk_delete(pkvk_2d.framebuffers, swapchainLength); pkvk_2d.framebuffers = CAFE_BABE(VkFramebuffer); } if (pkvk_3d.framebuffers != nullptr && pkvk_3d.framebuffers != CAFE_BABE(VkFramebuffer)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyFramebuffer(vkDevice, pkvk_3d.framebuffers[i], vkAllocator); } pk_delete(pkvk_3d.framebuffers, swapchainLength); pkvk_3d.framebuffers = CAFE_BABE(VkFramebuffer); } if (pkvk_present.framebuffers != nullptr && pkvk_present.framebuffers != CAFE_BABE(VkFramebuffer)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyFramebuffer(vkDevice, pkvk_present.framebuffers[i], vkAllocator); } pk_delete(pkvk_present.framebuffers, swapchainLength); pkvk_present.framebuffers = CAFE_BABE(VkFramebuffer); } if (depthImageViews != nullptr && depthImageViews != CAFE_BABE(VkImageView)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyImageView(vkDevice, depthImageViews[i], vkAllocator); vkDestroyImage(vkDevice, depthImages[i], vkAllocator); } pk_delete(depthImageViews, swapchainLength); depthImageViews = CAFE_BABE(VkImageView); pk_delete(depthImages, swapchainLength); depthImages = CAFE_BABE(VkImage); vkFreeMemory(vkDevice, depthImagesMemory, vkAllocator); } if (pkvk_2d.image_views != nullptr && pkvk_2d.image_views != CAFE_BABE(VkImageView)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyImageView(vkDevice, pkvk_2d.image_views[i], vkAllocator); vkDestroyImage(vkDevice, pkvk_2d.images[i], vkAllocator); } pk_delete(pkvk_2d.image_views, swapchainLength); pkvk_2d.image_views = CAFE_BABE(VkImageView); pk_delete(pkvk_2d.images, swapchainLength); pkvk_2d.images = CAFE_BABE(VkImage); vkFreeMemory(vkDevice, pkvk_2d.images_memory, vkAllocator); } if (pkvk_2d.image_views_color_resolve != nullptr && pkvk_2d.image_views_color_resolve != CAFE_BABE(VkImageView)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyImageView(vkDevice, pkvk_2d.image_views_color_resolve[i], vkAllocator); vkDestroyImage(vkDevice, pkvk_2d.images_color_resolve[i], vkAllocator); } pk_delete(pkvk_2d.image_views_color_resolve, swapchainLength); pkvk_2d.image_views_color_resolve = CAFE_BABE(VkImageView); pk_delete(pkvk_2d.images_color_resolve, swapchainLength); pkvk_2d.images_color_resolve = CAFE_BABE(VkImage); vkFreeMemory(vkDevice, pkvk_2d.images_memory_color_resolve, vkAllocator); } if (pkvk_3d.image_views_color_resolve != nullptr && pkvk_3d.image_views_color_resolve != CAFE_BABE(VkImageView)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyImageView(vkDevice, pkvk_3d.image_views_color_resolve[i], vkAllocator); vkDestroyImage(vkDevice, pkvk_3d.images_color_resolve[i], vkAllocator); } pk_delete(pkvk_3d.image_views_color_resolve, swapchainLength); pkvk_3d.image_views_color_resolve = CAFE_BABE(VkImageView); vkFreeMemory(vkDevice, pkvk_3d.images_memory_color_resolve, vkAllocator); pk_delete(pkvk_3d.images_color_resolve, swapchainLength); pkvk_3d.images_color_resolve = CAFE_BABE(VkImage); } if (pkvk_3d.image_views != nullptr && pkvk_3d.image_views != CAFE_BABE(VkImageView)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyImageView(vkDevice, pkvk_3d.image_views[i], vkAllocator); vkDestroyImage(vkDevice, pkvk_3d.render_images[i], vkAllocator); } pk_delete(pkvk_3d.image_views, swapchainLength); pkvk_3d.image_views = CAFE_BABE(VkImageView); pk_delete(pkvk_3d.render_images, swapchainLength); pkvk_3d.render_images = CAFE_BABE(VkImage); vkFreeMemory(vkDevice, pkvk_3d.images_memory, vkAllocator); } if (pkvk_present.image_views!= nullptr && pkvk_present.image_views != CAFE_BABE(VkImageView)) { for (long i = 0; i < swapchainLength; ++i) { vkDestroyImageView(vkDevice, pkvk_present.image_views[i], vkAllocator); } pk_delete(pkvk_present.image_views, swapchainLength); pkvk_present.image_views = CAFE_BABE(VkImageView); pk_delete(pkvk_present.images, swapchainLength); pkvk_present.images = CAFE_BABE(VkImage); // no memory free for present because they are managed by the swapchain } vkDestroySwapchainKHR(vkDevice, vkSwapchainKHR, vkAllocator); } void DestroyUniformBuffers() { for (long i = 0; i < prevSwapchainLength; ++i) { if (UniformBuffers[i] != VK_NULL_HANDLE) { vkDestroyBuffer(vkDevice, UniformBuffers[i], vkAllocator); UniformBuffers[i] = VK_NULL_HANDLE; } } pk_delete(UniformBuffers, prevSwapchainLength, MemBkt_Vulkan); UniformBuffers = CAFE_BABE(VkBuffer); vkFreeMemory(vkDevice, uniformBufferMemory, vkAllocator); uniformBufferMemory = VK_NULL_HANDLE; } void DetermineMonitor() { int monitorCount; GLFWmonitor **monitors = glfwGetMonitors(&monitorCount); assert(monitors != nullptr); // TODO - actually determine monitor monitor = monitors[0]; const auto *videoMode = glfwGetVideoMode(monitor); vidMode.width = videoMode->width; vidMode.height = videoMode->height; vidMode.redBits = videoMode->redBits; vidMode.greenBits = videoMode->greenBits; vidMode.blueBits = videoMode->blueBits; vidMode.refreshRate = videoMode->refreshRate; } void RecreateSwapchain() { pkeRuntime.graphics.resize_index += 1; int width, height = 0; glfwGetFramebufferSize(window, &width, &height); while (width == 0 || height == 0) { glfwGetFramebufferSize(window, &width, &height); glfwWaitEvents(); } Extent.width = width; Extent.height = height; ActiveCamera->stale = ActiveCamera->stale | PKE_CAMERA_STALE_PERSPECTIVE; DetermineMonitor(); vkDeviceWaitIdle(vkDevice); // reminder: prevSwapchainLength set in CreateSwapchain() DestroySwapchain(); CreateSwapchain(); if (prevSwapchainLength != swapchainLength) { DestroySyncObjects(); DestroyPresentPipeline(); CreatePresentPipeline(); CreateSyncObjects(); } UpdatePresentDescriptorSets(); UpdateCamera(); CreateFramebuffers(); if (prevSwapchainLength != swapchainLength) { DestroyUniformBuffers(); CreateUniformBuffers(); } shouldRecreateSwapchain = false; prevSwapchainLength = swapchainLength; } void FramebufferResizeCallback(GLFWwindow *window, int width, int height) { (void)window; assert(width > -1); assert(height > -1); if (Extent.width == (uint32_t)width && Extent.height != (uint32_t)height) { return; } shouldRecreateSwapchain = true; } void CreateWindow(PKEWindowProperties wp) { if (vkInstance != nullptr) return; MemBkt_Vulkan = pk_bucket_create("vulkan", PK_DEFAULT_BUCKET_SIZE, false); vulkanAllocs = pk_new>(MemBkt_Vulkan); new (vulkanAllocs) DynArray(MemBkt_Vulkan); vulkanAllocs->Reserve(2048); glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); window = glfwCreateWindow(wp.width, wp.height, "Pikul", nullptr, nullptr); InitVulkan(); glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback); CreateSwapchain(); CreateRenderPass(); CreatePresentDescriptorSetLayout(); CreatePresentPipeline(); UpdatePresentDescriptorSets(); UpdateCamera(); CreateFramebuffers(); CreateCommandPool(); CreateCommandBuffer(); CreateUniformBuffers(); CreateSyncObjects(); CreateGraphicsPipelines(); UpdateDebugGraphicsPipeline(); CreateImGui(); DetermineMonitor(); } void DestroyWindow() { if (vkInstance == nullptr) return; if (pke_window_did_init_imgui == true) { ImGui_ImplVulkan_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); } DestroySwapchain(); vkDestroyDescriptorPool(vkDevice, pkeDebugHitbox.vkDescriptorPool, vkAllocator); pk_delete(pkeDebugHitbox.vkDescriptorSets, swapchainLength); vkDestroyBuffer(vkDevice, pkeDebugHitbox.indexBuffer, vkAllocator); vkDestroyBuffer(vkDevice, pkeDebugHitbox.uvBuffer, vkAllocator); vkDestroyBuffer(vkDevice, pkeDebugHitbox.normalsBuffer, vkAllocator); vkDestroyBuffer(vkDevice, pkeDebugHitbox.vertexBuffer, vkAllocator); vkFreeMemory(vkDevice, pkeDebugHitbox.vertBufferMemory, vkAllocator); vkDestroyImageView(vkDevice, pkeDebugHitbox.vkImageView, vkAllocator); vkDestroyImage(vkDevice, pkeDebugHitbox.vkImage, vkAllocator); vkFreeMemory(vkDevice, pkeDebugHitbox.textureMemory, vkAllocator); if (pkePipelines.pipelines.named.texture != VK_NULL_HANDLE) vkDestroyPipeline(vkDevice, pkePipelines.pipelines.named.texture, vkAllocator); if (pkePipelines.pipelines.named.texture_wireframe != VK_NULL_HANDLE) vkDestroyPipeline(vkDevice, pkePipelines.pipelines.named.texture_wireframe, vkAllocator); if (pkePipelines.pipelines.named.glyph != VK_NULL_HANDLE) vkDestroyPipeline(vkDevice, pkePipelines.pipelines.named.glyph, vkAllocator); if (pkePipelines.pipe_layouts.named.texture != VK_NULL_HANDLE) vkDestroyPipelineLayout(vkDevice, pkePipelines.pipe_layouts.named.texture, vkAllocator); if (pkePipelines.pipe_layouts.named.glyph != VK_NULL_HANDLE) vkDestroyPipelineLayout(vkDevice, pkePipelines.pipe_layouts.named.glyph, vkAllocator); if (pkePipelines.descr_layouts.named.texture != VK_NULL_HANDLE) vkDestroyDescriptorSetLayout(vkDevice, pkePipelines.descr_layouts.named.texture, vkAllocator); if (pkePipelines.descr_layouts.named.glyph != VK_NULL_HANDLE) vkDestroyDescriptorSetLayout(vkDevice, pkePipelines.descr_layouts.named.glyph, vkAllocator); DestroySyncObjects(); DestroyUniformBuffers(); vkDestroyCommandPool(vkDevice, pkvk_shared.command_pool.graphics, vkAllocator); vkDestroyCommandPool(vkDevice, pkvk_shared.command_pool.transfer, vkAllocator); DestroyPresentPipeline(); vkDestroyDescriptorPool(vkDevice, imGuiDescriptorPool, vkAllocator); vkDestroyDescriptorSetLayout(vkDevice, pkvk_present.descriptor_set_layout, vkAllocator); vkDestroyRenderPass(vkDevice, pkvk_2d.render_pass, vkAllocator); vkDestroyRenderPass(vkDevice, pkvk_3d.render_pass, vkAllocator); vkDestroyRenderPass(vkDevice, pkvk_present.render_pass, vkAllocator); vkDestroySurfaceKHR(vkInstance, vkSurfaceKHR, vkAllocator); vkDestroySampler(vkDevice, global_sampler, vkAllocator); vkDestroyDevice(vkDevice, vkAllocator); if (VULKAN_DEBUG_REPORT) { auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(vkInstance, "vkDestroyDebugReportCallbackEXT"); vkDestroyDebugReportCallbackEXT(vkInstance, vkDebugReport, vkAllocator); } vkDestroyInstance(vkInstance, vkAllocator); glfwDestroyWindow(window); glfwTerminate(); vulkanAllocs->~DynArray(); pk_bucket_destroy(MemBkt_Vulkan); } VkShaderModule UploadShader(AssetHandle handle) { const Asset *asset = AM_Get(handle); assert(asset != nullptr && asset->state == PKE_ASSET_LOADING_STATE_LOADED); #ifndef NDEBUG fprintf(stdout, "Uploading Shader: '%s'\n", asset->basePath); #endif VkShaderModuleCreateInfo createInfo; createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; createInfo.pNext = nullptr; createInfo.flags = {}; createInfo.codeSize = asset->size; createInfo.pCode = static_cast(asset->ptr); VkShaderModule vkShaderModule; if (vkCreateShaderModule(vkDevice, &createInfo, vkAllocator, &vkShaderModule) != VK_SUCCESS) { throw "failed to create shader module for asset"; } return vkShaderModule; } void CalculateCombinedMemReqs(uint64_t memReqsCount, VkMemoryRequirements *memReqs, VkMemoryRequirements &combinedMemReqs) { combinedMemReqs.size = 0; combinedMemReqs.alignment = memReqs[0].alignment; combinedMemReqs.memoryTypeBits = memReqs[0].memoryTypeBits; for (uint64_t i = 1; i < memReqsCount; ++i) { combinedMemReqs.memoryTypeBits |= memReqs[i].memoryTypeBits; if (combinedMemReqs.alignment == memReqs[i].alignment) { continue; } VkDeviceSize larger, smaller; if (combinedMemReqs.alignment > memReqs[i].alignment) { larger = combinedMemReqs.alignment; smaller = memReqs[i].alignment; } else { larger = memReqs[i].alignment; smaller = combinedMemReqs.alignment; } if (larger % smaller == 0) { combinedMemReqs.alignment = larger; continue; } int combined = larger * smaller; while ((combined / 2) % 2 == 0 && (combined / 2) % larger == 0) { combined /= 2; } combinedMemReqs.alignment = combined; } for (uint64_t i = 0; i < memReqsCount; ++i) { uint32_t alignmentPadding = memReqs[i].size % combinedMemReqs.alignment; memReqs[i].size += (alignmentPadding == 0 ? 0 : combinedMemReqs.alignment - alignmentPadding); combinedMemReqs.size += memReqs[i].size; } } void Render() { vkWaitForFences(vkDevice, 1, &pkvk_present.fences_in_flight[CURRENT_FRAME], VK_TRUE, UINT64_MAX); uint32_t imageIndex; auto result = vkAcquireNextImageKHR(vkDevice, vkSwapchainKHR, UINT64_MAX, pkvk_present.semaphores_image_available[CURRENT_FRAME], VK_NULL_HANDLE, &imageIndex); if (result == VK_ERROR_OUT_OF_DATE_KHR) { RecreateSwapchain(); return; } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { throw "failed to acquire swapchain"; } // update uniform buffer { UpdateCamera(); void *mappedUboMemory; vkMapMemory(vkDevice, uniformBufferMemory, paddedUboBufferSize * imageIndex, sizeof(UniformBufferObject), 0, &mappedUboMemory); memcpy(mappedUboMemory, &UBO, sizeof(UniformBufferObject)); vkUnmapMemory(vkDevice, uniformBufferMemory); } vkResetFences(vkDevice, 1, &pkvk_present.fences_in_flight[CURRENT_FRAME]); vkResetCommandBuffer(pkvk_present.command_buffers[CURRENT_FRAME], 0); RecordCommandBuffer(pkvk_present.command_buffers[CURRENT_FRAME], imageIndex); VkSemaphore waitSemaphores[] = {pkvk_present.semaphores_image_available[CURRENT_FRAME]}; VkSemaphore signalSemaphores[] = {pkvk_present.semaphores_render_finished[CURRENT_FRAME]}; VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT}; VkSubmitInfo submitInfo; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.pNext = nullptr; submitInfo.waitSemaphoreCount = 1; submitInfo.pWaitSemaphores = waitSemaphores; submitInfo.pWaitDstStageMask = waitStages; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &pkvk_present.command_buffers[CURRENT_FRAME]; submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = signalSemaphores; result = vkQueueSubmit(pkvk_shared.queue.graphics, 1, &submitInfo, pkvk_present.fences_in_flight[CURRENT_FRAME]); if (result != VK_SUCCESS) { throw "failed to submit queue"; } VkSwapchainKHR swapchains[] = {vkSwapchainKHR}; VkPresentInfoKHR presentInfo; presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; presentInfo.pNext = nullptr; presentInfo.waitSemaphoreCount = 1; presentInfo.pWaitSemaphores = signalSemaphores; presentInfo.swapchainCount = 1; presentInfo.pSwapchains = swapchains; presentInfo.pImageIndices = &imageIndex; presentInfo.pResults = nullptr; result = vkQueuePresentKHR(presentQueue, &presentInfo); if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || shouldRecreateSwapchain) { RecreateSwapchain(); } else if (result != VK_SUCCESS) { throw "failed to present swapchain image"; } CURRENT_FRAME = (CURRENT_FRAME + 1) % swapchainLength; }