2023-08-06 08:48:11 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
#include "renderer.hpp"
|
|
|
|
|
2023-09-23 14:51:47 -04:00
|
|
|
#include <QDebug>
|
2023-10-10 17:16:11 -04:00
|
|
|
#include <QFile>
|
2022-04-11 23:11:33 -04:00
|
|
|
#include <array>
|
2022-04-12 00:54:11 -04:00
|
|
|
#include <fstream>
|
2022-04-12 02:06:16 -04:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtx/transform.hpp>
|
2023-08-06 08:48:11 -04:00
|
|
|
#include <valarray>
|
|
|
|
#include <vector>
|
|
|
|
#include <vulkan/vulkan.h>
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
#include "imgui.h"
|
|
|
|
#include "imguipass.h"
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
|
|
|
|
const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
|
|
|
|
const VkAllocationCallbacks *pAllocator,
|
|
|
|
VkDebugUtilsMessengerEXT *pCallback)
|
|
|
|
{
|
2022-08-11 17:53:56 -04:00
|
|
|
// Note: It seems that static_cast<...> doesn't work. Use the C-style forced
|
|
|
|
// cast.
|
2023-10-12 23:45:45 -04:00
|
|
|
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
2022-08-11 17:53:56 -04:00
|
|
|
if (func != nullptr) {
|
|
|
|
return func(instance, pCreateInfo, pAllocator, pCallback);
|
|
|
|
} else {
|
|
|
|
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|
|
|
VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|
|
|
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
|
|
|
|
void *pUserData)
|
|
|
|
{
|
2023-12-09 22:35:59 -05:00
|
|
|
Q_UNUSED(messageSeverity)
|
|
|
|
Q_UNUSED(messageType)
|
|
|
|
Q_UNUSED(pUserData)
|
|
|
|
|
2023-09-23 14:51:47 -04:00
|
|
|
qInfo() << pCallbackData->pMessage;
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
return VK_FALSE;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
Renderer::Renderer()
|
|
|
|
{
|
2023-10-10 17:16:11 -04:00
|
|
|
Q_INIT_RESOURCE(shaders);
|
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
ctx = ImGui::CreateContext();
|
|
|
|
ImGui::SetCurrentContext(ctx);
|
|
|
|
|
2023-10-10 18:37:08 -04:00
|
|
|
ImGui::GetIO().IniFilename = "";
|
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
std::vector<const char *> instanceExtensions = {"VK_EXT_debug_utils"};
|
2022-04-12 16:19:06 -04:00
|
|
|
|
|
|
|
uint32_t extensionCount = 0;
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkExtensionProperties> extensions(extensionCount);
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
for (auto &extension : extensions) {
|
2022-04-12 16:19:06 -04:00
|
|
|
if (strstr(extension.extensionName, "surface") != nullptr) {
|
|
|
|
instanceExtensions.push_back(extension.extensionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(extension.extensionName, "VK_KHR_get_physical_device_properties2") != nullptr) {
|
|
|
|
instanceExtensions.push_back(extension.extensionName);
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = {};
|
2023-10-12 23:45:45 -04:00
|
|
|
debugCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
|
|
|
debugCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
|
|
|
debugCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
|
2022-08-11 17:53:56 -04:00
|
|
|
debugCreateInfo.pfnUserCallback = DebugCallback;
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
VkInstanceCreateInfo createInfo = {};
|
2022-08-11 17:53:56 -04:00
|
|
|
createInfo.pNext = &debugCreateInfo;
|
2022-04-11 23:11:33 -04:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
createInfo.ppEnabledExtensionNames = instanceExtensions.data();
|
|
|
|
createInfo.enabledExtensionCount = instanceExtensions.size();
|
|
|
|
|
|
|
|
vkCreateInstance(&createInfo, nullptr, &instance);
|
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
VkDebugUtilsMessengerEXT callback;
|
2023-10-12 23:45:45 -04:00
|
|
|
CreateDebugUtilsMessengerEXT(instance, &debugCreateInfo, nullptr, &callback);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
// pick physical device
|
|
|
|
uint32_t deviceCount = 0;
|
|
|
|
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkPhysicalDevice> devices(deviceCount);
|
|
|
|
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
|
|
|
|
2023-09-26 00:37:55 -04:00
|
|
|
int preferredDevice = 0;
|
|
|
|
int deviceIndex = 0;
|
2022-04-11 23:11:33 -04:00
|
|
|
for (auto device : devices) {
|
|
|
|
VkPhysicalDeviceProperties deviceProperties;
|
|
|
|
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
2023-09-26 00:37:55 -04:00
|
|
|
|
|
|
|
if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) {
|
|
|
|
preferredDevice = deviceIndex;
|
|
|
|
}
|
|
|
|
deviceIndex++;
|
2022-04-11 23:11:33 -04:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:37:55 -04:00
|
|
|
physicalDevice = devices[preferredDevice];
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2022-04-12 16:19:06 -04:00
|
|
|
extensionCount = 0;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, nullptr);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
std::vector<VkExtensionProperties> extensionProperties(extensionCount);
|
2023-10-12 23:45:45 -04:00
|
|
|
vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, extensionProperties.data());
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
// we want to choose the portability subset on platforms that
|
|
|
|
// support it, this is a requirement of the portability spec
|
2023-10-12 23:45:45 -04:00
|
|
|
std::vector<const char *> deviceExtensions = {"VK_KHR_swapchain"};
|
|
|
|
for (auto extension : extensionProperties) {
|
2022-04-11 23:11:33 -04:00
|
|
|
if (!strcmp(extension.extensionName, "VK_KHR_portability_subset"))
|
|
|
|
deviceExtensions.push_back("VK_KHR_portability_subset");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t graphicsFamilyIndex = 0, presentFamilyIndex = 0;
|
|
|
|
|
|
|
|
// create logical device
|
|
|
|
uint32_t queueFamilyCount = 0;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilies.data());
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
int i = 0;
|
2023-10-12 23:45:45 -04:00
|
|
|
for (const auto &queueFamily : queueFamilies) {
|
|
|
|
if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
2022-04-11 23:11:33 -04:00
|
|
|
graphicsFamilyIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
|
|
|
|
|
|
|
|
if (graphicsFamilyIndex == presentFamilyIndex) {
|
|
|
|
VkDeviceQueueCreateInfo queueCreateInfo = {};
|
|
|
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
queueCreateInfo.queueFamilyIndex = graphicsFamilyIndex;
|
|
|
|
queueCreateInfo.queueCount = 1;
|
|
|
|
|
|
|
|
float queuePriority = 1.0f;
|
|
|
|
queueCreateInfo.pQueuePriorities = &queuePriority;
|
|
|
|
|
|
|
|
queueCreateInfos.push_back(queueCreateInfo);
|
|
|
|
} else {
|
|
|
|
// graphics
|
|
|
|
{
|
|
|
|
VkDeviceQueueCreateInfo queueCreateInfo = {};
|
|
|
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
queueCreateInfo.queueFamilyIndex = graphicsFamilyIndex;
|
|
|
|
queueCreateInfo.queueCount = 1;
|
|
|
|
|
|
|
|
float queuePriority = 1.0f;
|
|
|
|
queueCreateInfo.pQueuePriorities = &queuePriority;
|
|
|
|
|
|
|
|
queueCreateInfos.push_back(queueCreateInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// present
|
|
|
|
{
|
|
|
|
VkDeviceQueueCreateInfo queueCreateInfo = {};
|
|
|
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
queueCreateInfo.queueFamilyIndex = presentFamilyIndex;
|
|
|
|
queueCreateInfo.queueCount = 1;
|
|
|
|
|
|
|
|
float queuePriority = 1.0f;
|
|
|
|
queueCreateInfo.pQueuePriorities = &queuePriority;
|
|
|
|
|
|
|
|
queueCreateInfos.push_back(queueCreateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VkDeviceCreateInfo deviceCeateInfo = {};
|
|
|
|
deviceCeateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|
|
|
deviceCeateInfo.pQueueCreateInfos = queueCreateInfos.data();
|
2023-10-12 23:45:45 -04:00
|
|
|
deviceCeateInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
|
2022-04-11 23:11:33 -04:00
|
|
|
deviceCeateInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
|
|
|
deviceCeateInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
|
|
|
|
|
|
|
|
vkCreateDevice(physicalDevice, &deviceCeateInfo, nullptr, &device);
|
|
|
|
|
|
|
|
// get queues
|
|
|
|
vkGetDeviceQueue(device, graphicsFamilyIndex, 0, &graphicsQueue);
|
|
|
|
vkGetDeviceQueue(device, presentFamilyIndex, 0, &presentQueue);
|
|
|
|
|
|
|
|
// command pool
|
|
|
|
VkCommandPoolCreateInfo poolInfo = {};
|
|
|
|
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
|
|
|
poolInfo.queueFamilyIndex = graphicsFamilyIndex;
|
|
|
|
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
|
|
|
|
|
|
vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool);
|
|
|
|
|
2023-07-09 11:53:12 -04:00
|
|
|
createDummyTexture();
|
|
|
|
|
2023-09-23 14:51:47 -04:00
|
|
|
qInfo() << "Initialized renderer!";
|
2022-04-11 23:11:33 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
bool Renderer::initSwapchain(VkSurfaceKHR surface, int width, int height)
|
|
|
|
{
|
2022-04-12 00:30:17 -04:00
|
|
|
vkQueueWaitIdle(presentQueue);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
if (width == 0 || height == 0)
|
2022-04-12 12:19:46 -04:00
|
|
|
return false;
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
// TODO: fix this pls
|
|
|
|
VkBool32 supported;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, 0, surface, &supported);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
// query swapchain support
|
|
|
|
VkSurfaceCapabilitiesKHR capabilities;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
std::vector<VkSurfaceFormatKHR> formats;
|
|
|
|
|
|
|
|
uint32_t formatCount;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
formats.resize(formatCount);
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, formats.data());
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
std::vector<VkPresentModeKHR> presentModes;
|
|
|
|
uint32_t presentModeCount;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
presentModes.resize(presentModeCount);
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data());
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
// choosing swapchain features
|
|
|
|
VkSurfaceFormatKHR swapchainSurfaceFormat = formats[0];
|
2023-10-12 23:45:45 -04:00
|
|
|
for (const auto &availableFormat : formats) {
|
|
|
|
if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
|
2022-04-11 23:11:33 -04:00
|
|
|
swapchainSurfaceFormat = availableFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
2023-10-12 23:45:45 -04:00
|
|
|
for (const auto &availablePresentMode : presentModes) {
|
2022-04-11 23:11:33 -04:00
|
|
|
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
|
|
|
swapchainPresentMode = availablePresentMode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t imageCount = capabilities.minImageCount + 1;
|
2023-10-12 23:45:45 -04:00
|
|
|
if (capabilities.maxImageCount > 0 && imageCount > capabilities.maxImageCount) {
|
2022-04-11 23:11:33 -04:00
|
|
|
imageCount = capabilities.maxImageCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create swapchain
|
|
|
|
VkSwapchainCreateInfoKHR createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|
|
|
createInfo.surface = surface;
|
|
|
|
createInfo.minImageCount = imageCount;
|
|
|
|
createInfo.imageFormat = swapchainSurfaceFormat.format;
|
|
|
|
createInfo.imageColorSpace = swapchainSurfaceFormat.colorSpace;
|
2022-04-12 00:30:17 -04:00
|
|
|
createInfo.imageExtent.width = width;
|
|
|
|
createInfo.imageExtent.height = height;
|
2022-04-11 23:11:33 -04:00
|
|
|
createInfo.imageArrayLayers = 1;
|
|
|
|
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|
|
|
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
createInfo.preTransform = capabilities.currentTransform;
|
|
|
|
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
|
|
createInfo.presentMode = swapchainPresentMode;
|
|
|
|
createInfo.clipped = VK_TRUE;
|
|
|
|
|
2022-04-12 00:30:17 -04:00
|
|
|
VkSwapchainKHR oldSwapchain = swapchain;
|
|
|
|
createInfo.oldSwapchain = oldSwapchain;
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapchain);
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
if (oldSwapchain != VK_NULL_HANDLE)
|
2022-04-12 00:30:17 -04:00
|
|
|
vkDestroySwapchainKHR(device, oldSwapchain, nullptr);
|
|
|
|
|
|
|
|
swapchainExtent.width = width;
|
|
|
|
swapchainExtent.height = height;
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkGetSwapchainImagesKHR(device, swapchain, &imageCount, nullptr);
|
2022-04-11 23:11:33 -04:00
|
|
|
swapchainImages.resize(imageCount);
|
|
|
|
vkGetSwapchainImagesKHR(device, swapchain, &imageCount, swapchainImages.data());
|
|
|
|
|
|
|
|
swapchainViews.resize(swapchainImages.size());
|
|
|
|
|
2022-05-03 12:14:50 -04:00
|
|
|
initDepth(width, height);
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
for (size_t i = 0; i < swapchainImages.size(); i++) {
|
|
|
|
VkImageViewCreateInfo view_create_info = {};
|
|
|
|
view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
view_create_info.image = swapchainImages[i];
|
|
|
|
view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
view_create_info.format = swapchainSurfaceFormat.format;
|
|
|
|
view_create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
view_create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
view_create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
view_create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
view_create_info.subresourceRange.baseMipLevel = 0;
|
|
|
|
view_create_info.subresourceRange.levelCount = 1;
|
|
|
|
view_create_info.subresourceRange.baseArrayLayer = 0;
|
|
|
|
view_create_info.subresourceRange.layerCount = 1;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCreateImageView(device, &view_create_info, nullptr, &swapchainViews[i]);
|
2022-04-11 23:11:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VkAttachmentDescription colorAttachment = {};
|
|
|
|
colorAttachment.format = swapchainSurfaceFormat.format;
|
|
|
|
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
|
|
|
|
|
|
|
VkAttachmentReference colorAttachmentRef = {};
|
|
|
|
colorAttachmentRef.attachment = 0;
|
|
|
|
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
|
|
|
2022-05-03 12:14:50 -04:00
|
|
|
VkAttachmentDescription depthAttachment = {};
|
|
|
|
depthAttachment.format = VK_FORMAT_D32_SFLOAT;
|
|
|
|
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
|
|
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
|
|
|
|
VkAttachmentReference depthAttachmentRef = {};
|
|
|
|
depthAttachmentRef.attachment = 1;
|
|
|
|
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
VkSubpassDependency dependency = {};
|
|
|
|
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
|
|
|
dependency.dstSubpass = 0;
|
2022-05-03 12:14:50 -04:00
|
|
|
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
|
|
|
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
2022-04-11 23:11:33 -04:00
|
|
|
dependency.srcAccessMask = 0;
|
2022-05-03 12:14:50 -04:00
|
|
|
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
2022-04-11 23:11:33 -04:00
|
|
|
dependency.dependencyFlags = 0;
|
|
|
|
|
|
|
|
VkSubpassDescription subpass = {};
|
|
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
|
|
subpass.colorAttachmentCount = 1;
|
|
|
|
subpass.pColorAttachments = &colorAttachmentRef;
|
2022-05-03 12:14:50 -04:00
|
|
|
subpass.pDepthStencilAttachment = &depthAttachmentRef;
|
|
|
|
|
|
|
|
std::array<VkAttachmentDescription, 2> attachments = {colorAttachment, depthAttachment};
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
2022-05-03 12:14:50 -04:00
|
|
|
renderPassInfo.attachmentCount = attachments.size();
|
|
|
|
renderPassInfo.pAttachments = attachments.data();
|
2022-04-11 23:11:33 -04:00
|
|
|
renderPassInfo.subpassCount = 1;
|
|
|
|
renderPassInfo.pSubpasses = &subpass;
|
|
|
|
renderPassInfo.dependencyCount = 1;
|
|
|
|
renderPassInfo.pDependencies = &dependency;
|
|
|
|
|
|
|
|
vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass);
|
|
|
|
|
2022-04-28 17:50:05 -04:00
|
|
|
initDescriptors();
|
2022-04-12 01:57:37 -04:00
|
|
|
initPipeline();
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
swapchainFramebuffers.resize(swapchainViews.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < swapchainViews.size(); i++) {
|
2022-05-03 12:14:50 -04:00
|
|
|
std::array<VkImageView, 2> attachments = {swapchainViews[i], depthView};
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
VkFramebufferCreateInfo framebufferInfo = {};
|
|
|
|
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
|
|
|
framebufferInfo.renderPass = renderPass;
|
2022-05-03 12:14:50 -04:00
|
|
|
framebufferInfo.attachmentCount = attachments.size();
|
|
|
|
framebufferInfo.pAttachments = attachments.data();
|
2022-04-11 23:11:33 -04:00
|
|
|
framebufferInfo.width = swapchainExtent.width;
|
|
|
|
framebufferInfo.height = swapchainExtent.height;
|
|
|
|
framebufferInfo.layers = 1;
|
|
|
|
|
|
|
|
vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapchainFramebuffers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate command buffers
|
2023-10-12 23:45:45 -04:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2022-04-11 23:11:33 -04:00
|
|
|
VkCommandBufferAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
|
|
allocInfo.commandPool = commandPool;
|
|
|
|
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
|
|
allocInfo.commandBufferCount = 1;
|
|
|
|
|
|
|
|
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VkSemaphoreCreateInfo semaphoreInfo = {};
|
|
|
|
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
|
|
|
|
|
|
VkFenceCreateInfo fenceCreateInfo = {};
|
|
|
|
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
|
|
|
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]);
|
|
|
|
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]);
|
|
|
|
vkCreateFence(device, &fenceCreateInfo, nullptr, &inFlightFences[i]);
|
|
|
|
}
|
2022-04-12 12:19:46 -04:00
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
ImGui::SetCurrentContext(ctx);
|
|
|
|
imGuiPass = new ImGuiPass(*this);
|
|
|
|
|
2022-04-12 12:19:46 -04:00
|
|
|
return true;
|
2022-04-11 23:11:33 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::resize(VkSurfaceKHR surface, int width, int height)
|
|
|
|
{
|
2022-04-12 00:30:17 -04:00
|
|
|
initSwapchain(surface, width, height);
|
|
|
|
}
|
|
|
|
|
2023-12-10 07:13:42 -05:00
|
|
|
void Renderer::destroySwapchain()
|
|
|
|
{
|
|
|
|
if (swapchain != VK_NULL_HANDLE) {
|
|
|
|
vkDestroySwapchainKHR(device, swapchain, nullptr);
|
|
|
|
swapchain = VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 08:39:45 -05:00
|
|
|
void Renderer::render(const std::vector<RenderModel> &models)
|
2023-10-12 23:45:45 -04:00
|
|
|
{
|
|
|
|
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, std::numeric_limits<uint64_t>::max());
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
uint32_t imageIndex = 0;
|
2023-10-12 23:45:45 -04:00
|
|
|
VkResult result =
|
|
|
|
vkAcquireNextImageKHR(device, swapchain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkCommandBuffer commandBuffer = commandBuffers[currentFrame];
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo = {};
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
|
|
|
|
|
|
|
VkRenderPassBeginInfo renderPassInfo = {};
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
|
|
|
renderPassInfo.renderPass = renderPass;
|
|
|
|
renderPassInfo.framebuffer = swapchainFramebuffers[imageIndex];
|
|
|
|
|
2022-05-03 12:14:50 -04:00
|
|
|
std::array<VkClearValue, 2> clearValues = {};
|
|
|
|
clearValues[0].color.float32[0] = 0.8;
|
|
|
|
clearValues[0].color.float32[1] = 0.8;
|
|
|
|
clearValues[0].color.float32[2] = 0.8;
|
|
|
|
clearValues[0].color.float32[3] = 1.0;
|
|
|
|
clearValues[1].depthStencil = {1.0f, 0};
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2022-05-03 12:14:50 -04:00
|
|
|
renderPassInfo.clearValueCount = clearValues.size();
|
|
|
|
renderPassInfo.pClearValues = clearValues.data();
|
2022-04-11 23:11:33 -04:00
|
|
|
renderPassInfo.renderArea.extent = swapchainExtent;
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
2022-04-12 01:57:37 -04:00
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
for (auto model : models) {
|
2022-04-28 17:50:05 -04:00
|
|
|
// copy bone data
|
|
|
|
{
|
|
|
|
const size_t bufferSize = sizeof(glm::mat4) * 128;
|
|
|
|
void *mapped_data = nullptr;
|
2023-04-09 15:26:27 -04:00
|
|
|
vkMapMemory(device, model.boneInfoMemory, 0, bufferSize, 0, &mapped_data);
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
memcpy(mapped_data, model.boneData.data(), bufferSize);
|
|
|
|
|
|
|
|
VkMappedMemoryRange range = {};
|
|
|
|
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
2023-04-09 15:26:27 -04:00
|
|
|
range.memory = model.boneInfoMemory;
|
2022-04-28 17:50:05 -04:00
|
|
|
range.size = bufferSize;
|
|
|
|
vkFlushMappedMemoryRanges(device, 1, &range);
|
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
vkUnmapMemory(device, model.boneInfoMemory);
|
2022-04-28 17:50:05 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
if (model.materials.empty())
|
2022-08-11 17:53:56 -04:00
|
|
|
continue;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
for (const auto &part : model.parts) {
|
2023-12-09 22:35:59 -05:00
|
|
|
if (static_cast<size_t>(part.materialIndex) >= model.materials.size()) {
|
2023-09-23 14:07:47 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
RenderMaterial &material = model.materials[part.materialIndex];
|
2023-04-09 15:26:27 -04:00
|
|
|
|
2023-09-23 14:07:47 -04:00
|
|
|
const auto h = hash(model, material);
|
2023-10-12 23:45:45 -04:00
|
|
|
if (!cachedDescriptors.count(h)) {
|
2023-09-23 14:07:47 -04:00
|
|
|
if (auto descriptor = createDescriptorFor(model, material); descriptor != VK_NULL_HANDLE) {
|
|
|
|
cachedDescriptors[h] = descriptor;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-09 15:26:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &cachedDescriptors[h], 0, nullptr);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
VkDeviceSize offsets[] = {0};
|
|
|
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &part.vertexBuffer, offsets);
|
|
|
|
vkCmdBindIndexBuffer(commandBuffer, part.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
2022-04-12 01:57:37 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
glm::mat4 p = glm::perspective(glm::radians(45.0f), swapchainExtent.width / (float)swapchainExtent.height, 0.1f, 100.0f);
|
2023-07-06 17:35:26 -04:00
|
|
|
glm::mat4 v = view;
|
2022-04-28 17:50:05 -04:00
|
|
|
glm::mat4 vp = p * v;
|
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
vkCmdPushConstants(commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(glm::mat4), &vp);
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
glm::mat4 m = glm::mat4(1.0f);
|
2022-04-12 02:06:16 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCmdPushConstants(commandBuffer,
|
|
|
|
pipelineLayout,
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
sizeof(glm::mat4),
|
|
|
|
sizeof(glm::mat4),
|
|
|
|
&m);
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
int test = 0;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCmdPushConstants(commandBuffer,
|
|
|
|
pipelineLayout,
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
sizeof(glm::mat4) * 2,
|
|
|
|
sizeof(int),
|
|
|
|
&test);
|
2023-04-09 15:26:27 -04:00
|
|
|
|
|
|
|
int type = (int)material.type;
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCmdPushConstants(commandBuffer,
|
|
|
|
pipelineLayout,
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
sizeof(glm::mat4) * 2 + sizeof(int),
|
|
|
|
sizeof(int),
|
|
|
|
&type);
|
2022-04-12 02:06:16 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
vkCmdDrawIndexed(commandBuffer, part.numIndices, 1, 0, 0, 0);
|
|
|
|
}
|
2022-04-12 01:57:37 -04:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
if (imGuiPass != nullptr) {
|
|
|
|
ImGui::SetCurrentContext(ctx);
|
|
|
|
imGuiPass->render(commandBuffer);
|
|
|
|
}
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
vkCmdEndRenderPass(commandBuffer);
|
2022-04-11 23:11:33 -04:00
|
|
|
vkEndCommandBuffer(commandBuffer);
|
|
|
|
|
|
|
|
VkSubmitInfo submitInfo = {};
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
|
|
|
|
VkSemaphore waitSemaphores[] = {imageAvailableSemaphores[currentFrame]};
|
2023-10-12 23:45:45 -04:00
|
|
|
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
|
2022-04-11 23:11:33 -04:00
|
|
|
submitInfo.waitSemaphoreCount = 1;
|
|
|
|
submitInfo.pWaitSemaphores = waitSemaphores;
|
|
|
|
submitInfo.pWaitDstStageMask = waitStages;
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
submitInfo.pCommandBuffers = &commandBuffer;
|
|
|
|
|
|
|
|
VkSemaphore signalSemaphores[] = {renderFinishedSemaphores[currentFrame]};
|
|
|
|
submitInfo.signalSemaphoreCount = 1;
|
|
|
|
submitInfo.pSignalSemaphores = signalSemaphores;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkResetFences(device, 1, &inFlightFences[currentFrame]);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// present
|
|
|
|
VkPresentInfoKHR presentInfo = {};
|
|
|
|
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
|
|
|
|
|
|
presentInfo.waitSemaphoreCount = 1;
|
|
|
|
presentInfo.pWaitSemaphores = signalSemaphores;
|
|
|
|
VkSwapchainKHR swapChains[] = {swapchain};
|
|
|
|
presentInfo.swapchainCount = 1;
|
|
|
|
presentInfo.pSwapchains = swapChains;
|
|
|
|
presentInfo.pImageIndices = &imageIndex;
|
|
|
|
|
|
|
|
vkQueuePresentKHR(presentQueue, &presentInfo);
|
|
|
|
|
|
|
|
currentFrame = (currentFrame + 1) % 3;
|
2022-04-12 00:54:11 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
std::tuple<VkBuffer, VkDeviceMemory> Renderer::createBuffer(size_t size, VkBufferUsageFlags usageFlags)
|
|
|
|
{
|
2022-04-12 00:54:11 -04:00
|
|
|
vkDeviceWaitIdle(device);
|
|
|
|
|
|
|
|
// create buffer
|
|
|
|
VkBufferCreateInfo bufferInfo = {};
|
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
|
|
bufferInfo.size = size;
|
|
|
|
bufferInfo.usage = usageFlags;
|
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
|
|
|
|
VkBuffer handle;
|
|
|
|
vkCreateBuffer(device, &bufferInfo, nullptr, &handle);
|
|
|
|
|
|
|
|
// allocate memory
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
|
|
vkGetBufferMemoryRequirements(device, handle, &memRequirements);
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
2023-10-12 23:45:45 -04:00
|
|
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
vkAllocateMemory(device, &allocInfo, nullptr, &memory);
|
|
|
|
|
|
|
|
vkBindBufferMemory(device, handle, memory, 0);
|
|
|
|
|
|
|
|
return {handle, memory};
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
uint32_t Renderer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
|
|
|
|
{
|
2022-04-12 00:54:11 -04:00
|
|
|
VkPhysicalDeviceMemoryProperties memProperties;
|
|
|
|
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
|
2023-10-12 23:45:45 -04:00
|
|
|
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
|
2022-04-12 00:54:11 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
RenderModel Renderer::addModel(const physis_MDL &model, int lod)
|
|
|
|
{
|
2022-04-12 00:54:11 -04:00
|
|
|
RenderModel renderModel;
|
2022-04-12 20:18:22 -04:00
|
|
|
renderModel.model = model;
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
reloadModel(renderModel, lod);
|
2022-04-17 20:02:06 -04:00
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
return renderModel;
|
|
|
|
}
|
|
|
|
|
2023-12-09 22:35:59 -05:00
|
|
|
void Renderer::reloadModel(RenderModel &renderModel, uint32_t lod)
|
2023-12-09 14:49:31 -05:00
|
|
|
{
|
2023-12-09 22:35:59 -05:00
|
|
|
if (lod > renderModel.model.num_lod)
|
2023-12-09 14:49:31 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
renderModel.parts.clear();
|
|
|
|
|
2023-12-09 22:35:59 -05:00
|
|
|
for (uint32_t i = 0; i < renderModel.model.lods[lod].num_parts; i++) {
|
2022-04-12 08:55:38 -04:00
|
|
|
RenderPart renderPart;
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
const physis_Part part = renderModel.model.lods[lod].parts[i];
|
2023-04-09 15:26:27 -04:00
|
|
|
|
|
|
|
renderPart.materialIndex = part.material_index;
|
2022-08-10 14:52:28 -04:00
|
|
|
|
|
|
|
size_t vertexSize = part.num_vertices * sizeof(Vertex);
|
2023-10-12 23:45:45 -04:00
|
|
|
auto [vertexBuffer, vertexMemory] = createBuffer(vertexSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-08-10 14:52:28 -04:00
|
|
|
size_t indexSize = part.num_indices * sizeof(uint16_t);
|
2023-10-12 23:45:45 -04:00
|
|
|
auto [indexBuffer, indexMemory] = createBuffer(indexSize, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
// copy vertex data
|
|
|
|
{
|
2023-10-12 23:45:45 -04:00
|
|
|
void *mapped_data = nullptr;
|
2022-04-12 08:55:38 -04:00
|
|
|
vkMapMemory(device, vertexMemory, 0, vertexSize, 0, &mapped_data);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-08-10 14:52:28 -04:00
|
|
|
memcpy(mapped_data, part.vertices, vertexSize);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
VkMappedMemoryRange range = {};
|
|
|
|
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
|
|
|
range.memory = vertexMemory;
|
|
|
|
range.size = vertexSize;
|
|
|
|
vkFlushMappedMemoryRanges(device, 1, &range);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
vkUnmapMemory(device, vertexMemory);
|
|
|
|
}
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
// copy index data
|
|
|
|
{
|
2023-10-12 23:45:45 -04:00
|
|
|
void *mapped_data = nullptr;
|
2022-04-12 08:55:38 -04:00
|
|
|
vkMapMemory(device, indexMemory, 0, indexSize, 0, &mapped_data);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-08-10 14:52:28 -04:00
|
|
|
memcpy(mapped_data, part.indices, indexSize);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
VkMappedMemoryRange range = {};
|
|
|
|
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
|
|
|
range.memory = indexMemory;
|
|
|
|
range.size = indexSize;
|
|
|
|
vkFlushMappedMemoryRanges(device, 1, &range);
|
|
|
|
|
|
|
|
vkUnmapMemory(device, indexMemory);
|
|
|
|
}
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-08-10 14:52:28 -04:00
|
|
|
renderPart.numIndices = part.num_indices;
|
2022-04-12 01:57:37 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
renderPart.vertexBuffer = vertexBuffer;
|
|
|
|
renderPart.vertexMemory = vertexMemory;
|
2022-04-12 01:57:37 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
renderPart.indexBuffer = indexBuffer;
|
|
|
|
renderPart.indexMemory = indexMemory;
|
|
|
|
|
|
|
|
renderModel.parts.push_back(renderPart);
|
|
|
|
}
|
2022-04-12 01:57:37 -04:00
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
const size_t bufferSize = sizeof(glm::mat4) * 128;
|
|
|
|
auto [buffer, memory] = createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
|
|
|
|
|
|
|
|
renderModel.boneInfoBuffer = buffer;
|
|
|
|
renderModel.boneInfoMemory = memory;
|
2022-04-12 00:54:11 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::initPipeline()
|
|
|
|
{
|
2022-04-12 01:57:37 -04:00
|
|
|
VkPipelineShaderStageCreateInfo vertexShaderStageInfo = {};
|
|
|
|
vertexShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
|
|
vertexShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
2023-10-10 17:16:11 -04:00
|
|
|
vertexShaderStageInfo.module = loadShaderFromDisk(":/shaders/mesh.vert.spv");
|
2022-04-12 01:57:37 -04:00
|
|
|
vertexShaderStageInfo.pName = "main";
|
|
|
|
|
|
|
|
VkPipelineShaderStageCreateInfo fragmentShaderStageInfo = {};
|
|
|
|
fragmentShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
|
|
fragmentShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
2023-10-10 17:16:11 -04:00
|
|
|
fragmentShaderStageInfo.module = loadShaderFromDisk(":/shaders/mesh.frag.spv");
|
2022-04-12 01:57:37 -04:00
|
|
|
fragmentShaderStageInfo.pName = "main";
|
|
|
|
|
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages = {vertexShaderStageInfo, fragmentShaderStageInfo};
|
|
|
|
|
|
|
|
VkVertexInputBindingDescription binding = {};
|
2022-04-12 09:11:31 -04:00
|
|
|
binding.stride = sizeof(Vertex);
|
2022-04-12 01:57:37 -04:00
|
|
|
|
|
|
|
VkVertexInputAttributeDescription positionAttribute = {};
|
|
|
|
positionAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
|
2022-04-12 09:11:31 -04:00
|
|
|
positionAttribute.offset = offsetof(Vertex, position);
|
|
|
|
|
2023-12-09 14:49:31 -05:00
|
|
|
VkVertexInputAttributeDescription uv0Attribute = {};
|
|
|
|
uv0Attribute.format = VK_FORMAT_R32G32_SFLOAT;
|
|
|
|
uv0Attribute.location = 1;
|
|
|
|
uv0Attribute.offset = offsetof(Vertex, uv0);
|
|
|
|
|
|
|
|
VkVertexInputAttributeDescription uv1Attribute = {};
|
|
|
|
uv1Attribute.format = VK_FORMAT_R32G32_SFLOAT;
|
|
|
|
uv1Attribute.location = 2;
|
|
|
|
uv1Attribute.offset = offsetof(Vertex, uv1);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2023-07-06 17:35:42 -04:00
|
|
|
VkVertexInputAttributeDescription normalAttribute = {};
|
|
|
|
normalAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
|
2023-12-09 14:49:31 -05:00
|
|
|
normalAttribute.location = 3;
|
2023-07-06 17:35:42 -04:00
|
|
|
normalAttribute.offset = offsetof(Vertex, normal);
|
|
|
|
|
2023-12-17 19:00:43 -05:00
|
|
|
VkVertexInputAttributeDescription bitangentAttribute = {};
|
|
|
|
bitangentAttribute.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
|
|
|
bitangentAttribute.location = 4;
|
|
|
|
bitangentAttribute.offset = offsetof(Vertex, bitangent);
|
2023-12-09 14:49:31 -05:00
|
|
|
|
|
|
|
VkVertexInputAttributeDescription colorAttribute = {};
|
|
|
|
colorAttribute.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
2023-12-17 19:00:43 -05:00
|
|
|
colorAttribute.location = 5;
|
2023-12-09 14:49:31 -05:00
|
|
|
colorAttribute.offset = offsetof(Vertex, color);
|
|
|
|
|
2022-04-28 17:50:05 -04:00
|
|
|
VkVertexInputAttributeDescription boneWeightAttribute = {};
|
2023-07-06 17:35:42 -04:00
|
|
|
boneWeightAttribute.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
2023-12-17 19:00:43 -05:00
|
|
|
boneWeightAttribute.location = 6;
|
2022-08-10 14:52:28 -04:00
|
|
|
boneWeightAttribute.offset = offsetof(Vertex, bone_weight);
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
VkVertexInputAttributeDescription boneIdAttribute = {};
|
|
|
|
boneIdAttribute.format = VK_FORMAT_R8G8B8A8_UINT;
|
2023-12-17 19:00:43 -05:00
|
|
|
boneIdAttribute.location = 7;
|
2022-08-10 14:52:28 -04:00
|
|
|
boneIdAttribute.offset = offsetof(Vertex, bone_id);
|
2022-04-28 17:50:05 -04:00
|
|
|
|
2023-12-17 19:00:43 -05:00
|
|
|
const std::array attributes =
|
|
|
|
{positionAttribute, uv0Attribute, uv1Attribute, normalAttribute, bitangentAttribute, colorAttribute, boneWeightAttribute, boneIdAttribute};
|
2022-04-12 09:47:36 -04:00
|
|
|
|
2022-04-12 01:57:37 -04:00
|
|
|
VkPipelineVertexInputStateCreateInfo vertexInputState = {};
|
|
|
|
vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
|
|
|
vertexInputState.vertexBindingDescriptionCount = 1;
|
|
|
|
vertexInputState.pVertexBindingDescriptions = &binding;
|
2022-04-12 09:47:36 -04:00
|
|
|
vertexInputState.vertexAttributeDescriptionCount = attributes.size();
|
|
|
|
vertexInputState.pVertexAttributeDescriptions = attributes.data();
|
2022-04-12 01:57:37 -04:00
|
|
|
|
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
|
|
|
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
|
|
|
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
|
|
|
|
|
|
VkViewport viewport = {};
|
2022-04-12 02:06:16 -04:00
|
|
|
viewport.width = swapchainExtent.width;
|
|
|
|
viewport.height = swapchainExtent.height;
|
2022-04-12 01:57:37 -04:00
|
|
|
viewport.maxDepth = 1.0f;
|
|
|
|
|
|
|
|
VkRect2D scissor = {};
|
2022-04-12 02:06:16 -04:00
|
|
|
scissor.extent = swapchainExtent;
|
2022-04-12 01:57:37 -04:00
|
|
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState = {};
|
|
|
|
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
|
|
|
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.lineWidth = 1.0f;
|
|
|
|
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
|
|
|
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
|
|
|
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampling = {};
|
|
|
|
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
|
|
|
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
|
|
|
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
|
|
|
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.attachmentCount = 1;
|
|
|
|
colorBlending.pAttachments = &colorBlendAttachment;
|
|
|
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = {};
|
|
|
|
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
2022-04-12 02:06:16 -04:00
|
|
|
|
|
|
|
VkPushConstantRange pushConstantRange = {};
|
2023-04-09 15:26:27 -04:00
|
|
|
pushConstantRange.size = (sizeof(glm::mat4) * 2) + sizeof(int) * 2;
|
2023-10-12 23:45:45 -04:00
|
|
|
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
2022-04-12 01:57:37 -04:00
|
|
|
|
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
|
|
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
2022-04-12 02:06:16 -04:00
|
|
|
pipelineLayoutInfo.pushConstantRangeCount = 1;
|
|
|
|
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
|
2022-04-28 17:50:05 -04:00
|
|
|
pipelineLayoutInfo.setLayoutCount = 1;
|
|
|
|
pipelineLayoutInfo.pSetLayouts = &setLayout;
|
2022-04-12 01:57:37 -04:00
|
|
|
|
|
|
|
vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout);
|
|
|
|
|
2022-05-03 12:14:50 -04:00
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencil = {};
|
|
|
|
depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
|
|
|
depthStencil.depthTestEnable = VK_TRUE;
|
|
|
|
depthStencil.depthWriteEnable = VK_TRUE;
|
|
|
|
depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
|
|
|
|
depthStencil.maxDepthBounds = 1.0f;
|
|
|
|
|
2022-04-12 01:57:37 -04:00
|
|
|
VkGraphicsPipelineCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
|
|
|
createInfo.stageCount = shaderStages.size();
|
|
|
|
createInfo.pStages = shaderStages.data();
|
|
|
|
createInfo.pVertexInputState = &vertexInputState;
|
|
|
|
createInfo.pInputAssemblyState = &inputAssembly;
|
|
|
|
createInfo.pViewportState = &viewportState;
|
|
|
|
createInfo.pRasterizationState = &rasterizer;
|
|
|
|
createInfo.pMultisampleState = &multisampling;
|
|
|
|
createInfo.pColorBlendState = &colorBlending;
|
|
|
|
createInfo.pDynamicState = &dynamicState;
|
2022-05-03 12:14:50 -04:00
|
|
|
createInfo.pDepthStencilState = &depthStencil;
|
2022-04-12 01:57:37 -04:00
|
|
|
createInfo.layout = pipelineLayout;
|
|
|
|
createInfo.renderPass = renderPass;
|
|
|
|
|
|
|
|
vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &createInfo, nullptr, &pipeline);
|
2022-04-12 00:54:11 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VkShaderModule Renderer::createShaderModule(const uint32_t *code, const int length)
|
|
|
|
{
|
2022-04-12 00:54:11 -04:00
|
|
|
VkShaderModuleCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
|
|
createInfo.codeSize = length;
|
2023-10-12 23:45:45 -04:00
|
|
|
createInfo.pCode = reinterpret_cast<const uint32_t *>(code);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
|
|
|
VkShaderModule shaderModule;
|
|
|
|
vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule);
|
|
|
|
|
|
|
|
return shaderModule;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VkShaderModule Renderer::loadShaderFromDisk(const std::string_view path)
|
|
|
|
{
|
2023-10-10 17:16:11 -04:00
|
|
|
QFile file((QLatin1String(path)));
|
|
|
|
file.open(QFile::ReadOnly);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2023-10-10 17:16:11 -04:00
|
|
|
if (!file.isOpen()) {
|
2023-09-23 14:51:47 -04:00
|
|
|
qFatal("Failed to open shader file: %s", path.data());
|
2022-04-12 00:54:11 -04:00
|
|
|
}
|
|
|
|
|
2023-10-10 17:16:11 -04:00
|
|
|
auto contents = file.readAll();
|
|
|
|
return createShaderModule(reinterpret_cast<const uint32_t *>(contents.data()), contents.size());
|
2022-04-28 17:50:05 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::initDescriptors()
|
|
|
|
{
|
2022-04-28 17:50:05 -04:00
|
|
|
VkDescriptorPoolSize poolSize = {};
|
|
|
|
poolSize.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
2023-07-07 15:51:00 -04:00
|
|
|
poolSize.descriptorCount = 150;
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
VkDescriptorPoolSize poolSize2 = {};
|
|
|
|
poolSize2.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
2023-07-07 15:51:00 -04:00
|
|
|
poolSize2.descriptorCount = 150;
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
const std::array poolSizes = {poolSize, poolSize2};
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo poolCreateInfo = {};
|
|
|
|
poolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
2022-08-11 17:53:56 -04:00
|
|
|
poolCreateInfo.poolSizeCount = poolSizes.size();
|
|
|
|
poolCreateInfo.pPoolSizes = poolSizes.data();
|
2023-07-07 15:51:00 -04:00
|
|
|
poolCreateInfo.maxSets = 150;
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
vkCreateDescriptorPool(device, &poolCreateInfo, nullptr, &descriptorPool);
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutBinding boneInfoBufferBinding = {};
|
|
|
|
boneInfoBufferBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
|
|
|
boneInfoBufferBinding.descriptorCount = 1;
|
|
|
|
boneInfoBufferBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
|
|
|
boneInfoBufferBinding.binding = 2;
|
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
VkDescriptorSetLayoutBinding textureBinding = {};
|
|
|
|
textureBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
textureBinding.descriptorCount = 1;
|
2023-04-09 15:26:27 -04:00
|
|
|
textureBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
2022-08-11 17:53:56 -04:00
|
|
|
textureBinding.binding = 3;
|
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
VkDescriptorSetLayoutBinding normalBinding = {};
|
|
|
|
normalBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
normalBinding.descriptorCount = 1;
|
|
|
|
normalBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
|
|
normalBinding.binding = 4;
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutBinding specularBinding = {};
|
|
|
|
specularBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
specularBinding.descriptorCount = 1;
|
|
|
|
specularBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
|
|
specularBinding.binding = 5;
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutBinding multiBinding = {};
|
|
|
|
multiBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
multiBinding.descriptorCount = 1;
|
|
|
|
multiBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
|
|
multiBinding.binding = 6;
|
|
|
|
|
|
|
|
const std::array bindings = {boneInfoBufferBinding, textureBinding, normalBinding, specularBinding, multiBinding};
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo layoutInfo = {};
|
2022-04-28 17:50:05 -04:00
|
|
|
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
2022-08-11 17:53:56 -04:00
|
|
|
layoutInfo.bindingCount = bindings.size();
|
|
|
|
layoutInfo.pBindings = bindings.data();
|
2022-04-28 17:50:05 -04:00
|
|
|
|
|
|
|
vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &setLayout);
|
|
|
|
}
|
2022-05-03 12:14:50 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::initDepth(int width, int height)
|
|
|
|
{
|
2022-05-03 12:14:50 -04:00
|
|
|
VkImageCreateInfo imageCreateInfo = {};
|
|
|
|
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
imageCreateInfo.extent.width = width;
|
|
|
|
imageCreateInfo.extent.height = height;
|
|
|
|
imageCreateInfo.extent.depth = 1;
|
|
|
|
imageCreateInfo.mipLevels = 1;
|
|
|
|
imageCreateInfo.arrayLayers = 1;
|
|
|
|
imageCreateInfo.format = VK_FORMAT_D32_SFLOAT;
|
|
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
|
|
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
|
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
|
|
|
|
vkCreateImage(device, &imageCreateInfo, nullptr, &depthImage);
|
|
|
|
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
|
|
vkGetImageMemoryRequirements(device, depthImage, &memRequirements);
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo allocateInfo = {};
|
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocateInfo.allocationSize = memRequirements.size;
|
|
|
|
allocateInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
|
|
|
|
|
|
vkAllocateMemory(device, &allocateInfo, nullptr, &depthMemory);
|
|
|
|
|
|
|
|
vkBindImageMemory(device, depthImage, depthMemory, 0);
|
|
|
|
|
|
|
|
VkImageViewCreateInfo viewCreateInfo = {};
|
|
|
|
viewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
viewCreateInfo.image = depthImage;
|
|
|
|
viewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
viewCreateInfo.format = VK_FORMAT_D32_SFLOAT;
|
|
|
|
viewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
|
|
viewCreateInfo.subresourceRange.levelCount = 1;
|
|
|
|
viewCreateInfo.subresourceRange.layerCount = 1;
|
|
|
|
|
|
|
|
vkCreateImageView(device, &viewCreateInfo, nullptr, &depthView);
|
|
|
|
}
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
RenderTexture Renderer::addTexture(const uint32_t width, const uint32_t height, const uint8_t *data, const uint32_t data_size)
|
|
|
|
{
|
2022-08-11 17:53:56 -04:00
|
|
|
RenderTexture newTexture = {};
|
|
|
|
|
|
|
|
VkImageCreateInfo imageInfo = {};
|
|
|
|
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
|
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
imageInfo.extent.width = width;
|
|
|
|
imageInfo.extent.height = height;
|
|
|
|
imageInfo.extent.depth = 1;
|
|
|
|
imageInfo.mipLevels = 1;
|
|
|
|
imageInfo.arrayLayers = 1;
|
|
|
|
imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
|
|
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
|
|
|
vkCreateImage(device, &imageInfo, nullptr, &newTexture.handle);
|
|
|
|
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
|
|
vkGetImageMemoryRequirements(device, newTexture.handle, &memRequirements);
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
2023-10-12 23:45:45 -04:00
|
|
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
vkAllocateMemory(device, &allocInfo, nullptr, &newTexture.memory);
|
|
|
|
|
|
|
|
vkBindImageMemory(device, newTexture.handle, newTexture.memory, 0);
|
|
|
|
|
|
|
|
// copy image data
|
|
|
|
VkBuffer stagingBuffer;
|
|
|
|
VkDeviceMemory stagingBufferMemory;
|
|
|
|
|
|
|
|
VkBufferCreateInfo bufferInfo = {};
|
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
|
|
bufferInfo.size = data_size;
|
|
|
|
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
|
|
|
|
vkCreateBuffer(device, &bufferInfo, nullptr, &stagingBuffer);
|
|
|
|
|
|
|
|
// allocate staging memory
|
|
|
|
vkGetBufferMemoryRequirements(device, stagingBuffer, &memRequirements);
|
|
|
|
|
|
|
|
allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
2023-10-12 23:45:45 -04:00
|
|
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
vkAllocateMemory(device, &allocInfo, nullptr, &stagingBufferMemory);
|
|
|
|
|
|
|
|
vkBindBufferMemory(device, stagingBuffer, stagingBufferMemory, 0);
|
|
|
|
|
|
|
|
// copy to staging buffer
|
2023-10-12 23:45:45 -04:00
|
|
|
void *mapped_data;
|
2022-08-11 17:53:56 -04:00
|
|
|
vkMapMemory(device, stagingBufferMemory, 0, data_size, 0, &mapped_data);
|
|
|
|
memcpy(mapped_data, data, data_size);
|
|
|
|
vkUnmapMemory(device, stagingBufferMemory);
|
|
|
|
|
|
|
|
// copy staging buffer to image
|
|
|
|
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
|
|
|
|
|
|
|
|
VkImageSubresourceRange range = {};
|
|
|
|
range.baseMipLevel = 0;
|
|
|
|
range.levelCount = 1;
|
|
|
|
range.baseArrayLayer = 0;
|
|
|
|
range.layerCount = 1;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
inlineTransitionImageLayout(commandBuffer,
|
|
|
|
newTexture.handle,
|
|
|
|
imageInfo.format,
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
range,
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
2022-08-11 17:53:56 -04:00
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
|
|
|
|
|
|
|
VkBufferImageCopy region = {};
|
|
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
region.imageSubresource.mipLevel = 0;
|
|
|
|
region.imageSubresource.baseArrayLayer = 0;
|
|
|
|
region.imageSubresource.layerCount = 1;
|
2023-10-12 23:45:45 -04:00
|
|
|
region.imageExtent = {(uint32_t)width, (uint32_t)height, 1};
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, newTexture.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
inlineTransitionImageLayout(commandBuffer,
|
|
|
|
newTexture.handle,
|
|
|
|
imageInfo.format,
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
range,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
2022-08-11 17:53:56 -04:00
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
|
|
|
|
|
|
endSingleTimeCommands(commandBuffer);
|
|
|
|
|
|
|
|
range = {};
|
|
|
|
range.levelCount = 1;
|
|
|
|
range.layerCount = 1;
|
|
|
|
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
|
|
|
VkImageViewCreateInfo viewInfo = {};
|
|
|
|
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
viewInfo.image = newTexture.handle;
|
|
|
|
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
viewInfo.format = imageInfo.format;
|
|
|
|
viewInfo.subresourceRange = range;
|
|
|
|
|
|
|
|
vkCreateImageView(device, &viewInfo, nullptr, &newTexture.view);
|
|
|
|
|
|
|
|
VkSamplerCreateInfo samplerInfo = {};
|
|
|
|
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
|
|
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
|
|
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
|
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
samplerInfo.maxLod = 1.0f;
|
|
|
|
|
|
|
|
vkCreateSampler(device, &samplerInfo, nullptr, &newTexture.sampler);
|
|
|
|
|
|
|
|
return newTexture;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VkCommandBuffer Renderer::beginSingleTimeCommands()
|
|
|
|
{
|
2022-08-11 17:53:56 -04:00
|
|
|
VkCommandBufferAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
|
|
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
|
|
allocInfo.commandPool = commandPool;
|
|
|
|
allocInfo.commandBufferCount = 1;
|
|
|
|
|
|
|
|
VkCommandBuffer commandBuffer;
|
|
|
|
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo = {};
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
|
|
|
|
|
|
|
return commandBuffer;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::endSingleTimeCommands(VkCommandBuffer commandBuffer)
|
|
|
|
{
|
2022-08-11 17:53:56 -04:00
|
|
|
vkEndCommandBuffer(commandBuffer);
|
|
|
|
|
|
|
|
VkSubmitInfo submitInfo = {};
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
submitInfo.pCommandBuffers = &commandBuffer;
|
|
|
|
|
|
|
|
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
|
|
|
|
vkQueueWaitIdle(graphicsQueue);
|
|
|
|
|
|
|
|
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::inlineTransitionImageLayout(VkCommandBuffer commandBuffer,
|
|
|
|
VkImage image,
|
|
|
|
VkFormat format,
|
|
|
|
VkImageAspectFlags aspect,
|
|
|
|
VkImageSubresourceRange range,
|
|
|
|
VkImageLayout oldLayout,
|
|
|
|
VkImageLayout newLayout,
|
|
|
|
VkPipelineStageFlags src_stage_mask,
|
|
|
|
VkPipelineStageFlags dst_stage_mask)
|
|
|
|
{
|
2023-12-09 22:35:59 -05:00
|
|
|
Q_UNUSED(format)
|
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
VkImageMemoryBarrier barrier = {};
|
|
|
|
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
|
|
barrier.oldLayout = oldLayout;
|
|
|
|
barrier.newLayout = newLayout;
|
|
|
|
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
barrier.image = image;
|
|
|
|
barrier.subresourceRange = range;
|
|
|
|
barrier.subresourceRange.aspectMask = aspect;
|
|
|
|
|
|
|
|
switch (oldLayout) {
|
2023-10-12 23:45:45 -04:00
|
|
|
case VK_IMAGE_LAYOUT_UNDEFINED:
|
|
|
|
barrier.srcAccessMask = 0;
|
|
|
|
break;
|
|
|
|
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
|
|
|
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
break;
|
|
|
|
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
|
|
|
|
barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
break;
|
|
|
|
case VK_IMAGE_LAYOUT_GENERAL:
|
|
|
|
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-08-11 17:53:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (newLayout) {
|
2023-10-12 23:45:45 -04:00
|
|
|
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
|
|
|
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
break;
|
|
|
|
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
|
|
|
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
break;
|
|
|
|
case VK_IMAGE_LAYOUT_GENERAL:
|
|
|
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-08-11 17:53:56 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCmdPipelineBarrier(commandBuffer, src_stage_mask, dst_stage_mask, 0, 0, nullptr, 0, nullptr, 1, &barrier);
|
2022-08-11 17:53:56 -04:00
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
uint64_t Renderer::hash(const RenderModel &model, const RenderMaterial &material)
|
|
|
|
{
|
2023-09-23 14:07:47 -04:00
|
|
|
uint64_t hash = 0;
|
2023-10-12 23:45:45 -04:00
|
|
|
hash += reinterpret_cast<intptr_t>((void *)&model);
|
2023-04-09 15:26:27 -04:00
|
|
|
if (material.diffuseTexture)
|
2023-10-12 23:45:45 -04:00
|
|
|
hash += reinterpret_cast<intptr_t>((void *)material.diffuseTexture);
|
2023-04-09 15:26:27 -04:00
|
|
|
if (material.normalTexture)
|
2023-10-12 23:45:45 -04:00
|
|
|
hash += reinterpret_cast<intptr_t>((void *)material.normalTexture);
|
2023-04-09 15:26:27 -04:00
|
|
|
if (material.specularTexture)
|
2023-10-12 23:45:45 -04:00
|
|
|
hash += reinterpret_cast<intptr_t>((void *)material.specularTexture);
|
2023-04-09 15:26:27 -04:00
|
|
|
if (material.multiTexture)
|
2023-10-12 23:45:45 -04:00
|
|
|
hash += reinterpret_cast<intptr_t>((void *)material.multiTexture);
|
2023-04-09 15:26:27 -04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VkDescriptorSet Renderer::createDescriptorFor(const RenderModel &model, const RenderMaterial &material)
|
|
|
|
{
|
2022-08-11 17:53:56 -04:00
|
|
|
VkDescriptorSet set;
|
|
|
|
|
|
|
|
VkDescriptorSetAllocateInfo allocateInfo = {};
|
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
|
|
|
allocateInfo.descriptorPool = descriptorPool;
|
|
|
|
allocateInfo.descriptorSetCount = 1;
|
|
|
|
allocateInfo.pSetLayouts = &setLayout;
|
|
|
|
|
|
|
|
vkAllocateDescriptorSets(device, &allocateInfo, &set);
|
2023-09-23 14:07:47 -04:00
|
|
|
if (set == VK_NULL_HANDLE) {
|
2023-09-26 17:09:12 -04:00
|
|
|
// qFatal("Failed to create descriptor set!");
|
2023-09-23 14:07:47 -04:00
|
|
|
return VK_NULL_HANDLE;
|
|
|
|
}
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
const size_t bufferSize = sizeof(glm::mat4) * 128;
|
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
std::vector<VkWriteDescriptorSet> writes;
|
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
VkDescriptorBufferInfo bufferInfo = {};
|
2023-04-09 15:26:27 -04:00
|
|
|
bufferInfo.buffer = model.boneInfoBuffer;
|
2022-08-11 17:53:56 -04:00
|
|
|
bufferInfo.range = bufferSize;
|
|
|
|
|
|
|
|
VkWriteDescriptorSet descriptorWrite = {};
|
|
|
|
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
descriptorWrite.dstSet = set;
|
|
|
|
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
|
|
|
descriptorWrite.descriptorCount = 1;
|
|
|
|
descriptorWrite.pBufferInfo = &bufferInfo;
|
|
|
|
descriptorWrite.dstBinding = 2;
|
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
writes.push_back(descriptorWrite);
|
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
VkDescriptorImageInfo imageInfo = {};
|
|
|
|
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
2023-04-09 15:26:27 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
if (material.diffuseTexture) {
|
2023-04-09 15:26:27 -04:00
|
|
|
imageInfo.imageView = material.diffuseTexture->view;
|
|
|
|
imageInfo.sampler = material.diffuseTexture->sampler;
|
2023-07-09 11:53:12 -04:00
|
|
|
} else {
|
|
|
|
imageInfo.imageView = dummyView;
|
|
|
|
imageInfo.sampler = dummySampler;
|
|
|
|
}
|
2023-04-09 15:26:27 -04:00
|
|
|
|
2023-07-09 11:53:12 -04:00
|
|
|
VkWriteDescriptorSet descriptorWrite2 = {};
|
|
|
|
descriptorWrite2.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
descriptorWrite2.dstSet = set;
|
|
|
|
descriptorWrite2.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
descriptorWrite2.descriptorCount = 1;
|
|
|
|
descriptorWrite2.pImageInfo = &imageInfo;
|
|
|
|
descriptorWrite2.dstBinding = 3;
|
2023-04-09 15:26:27 -04:00
|
|
|
|
2023-07-09 11:53:12 -04:00
|
|
|
writes.push_back(descriptorWrite2);
|
2023-04-09 15:26:27 -04:00
|
|
|
|
|
|
|
VkDescriptorImageInfo normalImageInfo = {};
|
|
|
|
normalImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
if (material.normalTexture) {
|
2023-04-09 15:26:27 -04:00
|
|
|
normalImageInfo.imageView = material.normalTexture->view;
|
|
|
|
normalImageInfo.sampler = material.normalTexture->sampler;
|
|
|
|
|
|
|
|
VkWriteDescriptorSet normalDescriptorWrite2 = {};
|
|
|
|
normalDescriptorWrite2.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
normalDescriptorWrite2.dstSet = set;
|
|
|
|
normalDescriptorWrite2.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
normalDescriptorWrite2.descriptorCount = 1;
|
|
|
|
normalDescriptorWrite2.pImageInfo = &normalImageInfo;
|
|
|
|
normalDescriptorWrite2.dstBinding = 4;
|
|
|
|
|
|
|
|
writes.push_back(normalDescriptorWrite2);
|
|
|
|
}
|
|
|
|
|
|
|
|
VkDescriptorImageInfo specularImageInfo = {};
|
|
|
|
specularImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
if (material.specularTexture) {
|
2023-04-09 15:26:27 -04:00
|
|
|
specularImageInfo.imageView = material.specularTexture->view;
|
|
|
|
specularImageInfo.sampler = material.specularTexture->sampler;
|
|
|
|
|
|
|
|
VkWriteDescriptorSet specularDescriptorWrite2 = {};
|
|
|
|
specularDescriptorWrite2.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
specularDescriptorWrite2.dstSet = set;
|
|
|
|
specularDescriptorWrite2.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
specularDescriptorWrite2.descriptorCount = 1;
|
|
|
|
specularDescriptorWrite2.pImageInfo = &specularImageInfo;
|
|
|
|
specularDescriptorWrite2.dstBinding = 5;
|
|
|
|
|
|
|
|
writes.push_back(specularDescriptorWrite2);
|
|
|
|
}
|
|
|
|
|
|
|
|
VkDescriptorImageInfo multiImageInfo = {};
|
|
|
|
multiImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
|
|
|
if (material.multiTexture) {
|
|
|
|
multiImageInfo.imageView = material.multiTexture->view;
|
|
|
|
multiImageInfo.sampler = material.multiTexture->sampler;
|
|
|
|
|
|
|
|
VkWriteDescriptorSet multiDescriptorWrite2 = {};
|
|
|
|
multiDescriptorWrite2.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
|
|
multiDescriptorWrite2.dstSet = set;
|
|
|
|
multiDescriptorWrite2.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
multiDescriptorWrite2.descriptorCount = 1;
|
|
|
|
multiDescriptorWrite2.pImageInfo = &multiImageInfo;
|
|
|
|
multiDescriptorWrite2.dstBinding = 6;
|
|
|
|
|
|
|
|
writes.push_back(multiDescriptorWrite2);
|
|
|
|
}
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
vkUpdateDescriptorSets(device, writes.size(), writes.data(), 0, nullptr);
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
2023-07-09 11:53:12 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void Renderer::createDummyTexture()
|
|
|
|
{
|
2023-07-09 11:53:12 -04:00
|
|
|
VkImageCreateInfo imageInfo = {};
|
|
|
|
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
|
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
imageInfo.extent.width = 1;
|
|
|
|
imageInfo.extent.height = 1;
|
|
|
|
imageInfo.extent.depth = 1;
|
|
|
|
imageInfo.mipLevels = 1;
|
|
|
|
imageInfo.arrayLayers = 1;
|
|
|
|
imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
|
|
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
|
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
|
|
|
vkCreateImage(device, &imageInfo, nullptr, &dummyImage);
|
|
|
|
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
|
|
vkGetImageMemoryRequirements(device, dummyImage, &memRequirements);
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
2023-10-12 23:45:45 -04:00
|
|
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2023-07-09 11:53:12 -04:00
|
|
|
|
|
|
|
vkAllocateMemory(device, &allocInfo, nullptr, &dummyMemory);
|
|
|
|
|
|
|
|
vkBindImageMemory(device, dummyImage, dummyMemory, 0);
|
|
|
|
|
|
|
|
// copy image data
|
|
|
|
VkBuffer stagingBuffer;
|
|
|
|
VkDeviceMemory stagingBufferMemory;
|
|
|
|
|
|
|
|
VkBufferCreateInfo bufferInfo = {};
|
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
|
|
bufferInfo.size = 1;
|
|
|
|
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
|
|
|
|
vkCreateBuffer(device, &bufferInfo, nullptr, &stagingBuffer);
|
|
|
|
|
|
|
|
// allocate staging memory
|
|
|
|
vkGetBufferMemoryRequirements(device, stagingBuffer, &memRequirements);
|
|
|
|
|
|
|
|
allocInfo = {};
|
|
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
allocInfo.allocationSize = memRequirements.size;
|
2023-10-12 23:45:45 -04:00
|
|
|
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
2023-07-09 11:53:12 -04:00
|
|
|
|
|
|
|
vkAllocateMemory(device, &allocInfo, nullptr, &stagingBufferMemory);
|
|
|
|
|
|
|
|
vkBindBufferMemory(device, stagingBuffer, stagingBufferMemory, 0);
|
|
|
|
|
|
|
|
int dummydata[4] = {1, 1, 1, 1};
|
|
|
|
|
|
|
|
// copy to staging buffer
|
2023-10-12 23:45:45 -04:00
|
|
|
void *mapped_data;
|
2023-07-09 11:53:12 -04:00
|
|
|
vkMapMemory(device, stagingBufferMemory, 0, 4, 0, &mapped_data);
|
|
|
|
memcpy(mapped_data, dummydata, 1);
|
|
|
|
vkUnmapMemory(device, stagingBufferMemory);
|
|
|
|
|
|
|
|
// copy staging buffer to image
|
|
|
|
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
|
|
|
|
|
|
|
|
VkImageSubresourceRange range = {};
|
|
|
|
range.baseMipLevel = 0;
|
|
|
|
range.levelCount = 1;
|
|
|
|
range.baseArrayLayer = 0;
|
|
|
|
range.layerCount = 1;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
inlineTransitionImageLayout(commandBuffer,
|
|
|
|
dummyImage,
|
|
|
|
imageInfo.format,
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
range,
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
2023-07-09 11:53:12 -04:00
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
|
|
|
|
|
|
|
VkBufferImageCopy region = {};
|
|
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
region.imageSubresource.mipLevel = 0;
|
|
|
|
region.imageSubresource.baseArrayLayer = 0;
|
|
|
|
region.imageSubresource.layerCount = 1;
|
2023-10-12 23:45:45 -04:00
|
|
|
region.imageExtent = {(uint32_t)1, (uint32_t)1, 1};
|
2023-07-09 11:53:12 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, dummyImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
2023-07-09 11:53:12 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
inlineTransitionImageLayout(commandBuffer,
|
|
|
|
dummyImage,
|
|
|
|
imageInfo.format,
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
range,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
2023-07-09 11:53:12 -04:00
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
|
|
|
|
|
|
endSingleTimeCommands(commandBuffer);
|
|
|
|
|
|
|
|
range = {};
|
|
|
|
range.levelCount = 1;
|
|
|
|
range.layerCount = 1;
|
|
|
|
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
|
|
|
VkImageViewCreateInfo viewInfo = {};
|
|
|
|
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
viewInfo.image = dummyImage;
|
|
|
|
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
viewInfo.format = imageInfo.format;
|
|
|
|
viewInfo.subresourceRange = range;
|
|
|
|
|
|
|
|
vkCreateImageView(device, &viewInfo, nullptr, &dummyView);
|
|
|
|
|
|
|
|
VkSamplerCreateInfo samplerInfo = {};
|
|
|
|
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
|
|
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
|
|
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
|
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
samplerInfo.maxLod = 1.0f;
|
|
|
|
|
|
|
|
vkCreateSampler(device, &samplerInfo, nullptr, &dummySampler);
|
|
|
|
}
|