123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959 |
- #include "Graphics.h"
- #include "Buffer.h"
- #include "SDL_vulkan.h"
- #include "window/Window.h"
- #include "common/Exception.h"
- #include "Shader.h"
- #include <vector>
- #include <cstring>
- #include <set>
- #include <fstream>
- #include <iostream>
- namespace love {
- namespace graphics {
- namespace vulkan {
- const std::vector<const char*> validationLayers = {
- "VK_LAYER_KHRONOS_validation"
- };
- const std::vector<const char*> deviceExtensions = {
- VK_KHR_SWAPCHAIN_EXTENSION_NAME
- };
- #ifdef NDEBUG
- const bool enableValidationLayers = false;
- #else
- const bool enableValidationLayers = true;
- #endif
- const int MAX_FRAMES_IN_FLIGHT = 2;
- static std::vector<char> readFile(const std::string& filename) {
- std::ifstream file(filename, std::ios::ate | std::ios::binary);
- if (!file.is_open()) {
- throw std::runtime_error("failed to open file!");
- }
- size_t fileSize = (size_t)file.tellg();
- std::vector<char> buffer(fileSize);
- file.seekg(0);
- file.read(buffer.data(), fileSize);
- file.close();
- return buffer;
- }
- const char* Graphics::getName() const {
- return "love.graphics.vulkan";
- }
- Graphics::Graphics() {
- }
- void Graphics::initVulkan() {
- if (!init) {
- init = true;
- createVulkanInstance();
- createSurface();
- pickPhysicalDevice();
- createLogicalDevice();
- createSwapChain();
- createImageViews();
- createRenderPass();
- createGraphicsPipeline();
- createFramebuffers();
- createCommandPool();
- createCommandBuffers();
- createSyncObjects();
- startRecordingGraphicsCommands();
- }
- }
- Graphics::~Graphics() {
- cleanup();
- }
- // START OVERRIDEN FUNCTIONS
- love::graphics::Buffer* Graphics::newBuffer(const love::graphics::Buffer::Settings& settings, const std::vector<love::graphics::Buffer::DataDeclaration>& format, const void* data, size_t size, size_t arraylength) {
- std::cout << "newBuffer ";
- return nullptr;
- }
- void Graphics::startRecordingGraphicsCommands() {
- vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
-
- while (true) {
- VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
- if (result == VK_ERROR_OUT_OF_DATE_KHR) {
- recreateSwapChain();
- continue;
- }
- else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
- throw love::Exception("failed to acquire swap chain image");
- }
- break;
- }
- VkCommandBufferBeginInfo beginInfo{};
- beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
- beginInfo.flags = 0;
- beginInfo.pInheritanceInfo = nullptr;
- std::cout << "beginCommandBuffer(imageIndex=" << imageIndex << ") ";
- if (vkBeginCommandBuffer(commandBuffers.at(imageIndex), &beginInfo) != VK_SUCCESS) {
- throw love::Exception("failed to begin recording command buffer");
- }
- VkRenderPassBeginInfo renderPassInfo{};
- renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
- renderPassInfo.renderPass = renderPass;
- renderPassInfo.framebuffer = swapChainFramBuffers.at(imageIndex);
- renderPassInfo.renderArea.offset = { 0, 0 };
- renderPassInfo.renderArea.extent = swapChainExtent;
- renderPassInfo.clearValueCount = 1;
- renderPassInfo.pClearValues = &clearColor;
- const auto& commandBuffer = commandBuffers.at(imageIndex);
- vkCmdBeginRenderPass(commandBuffers.at(imageIndex), &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
- vkCmdBindPipeline(commandBuffers.at(imageIndex), VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
- }
- void Graphics::endRecordingGraphicsCommands() {
- const auto& commandBuffer = commandBuffers.at(imageIndex);
- std::cout << "endCommandBuffer(imageIndex=" << imageIndex << ") ";
- vkCmdEndRenderPass(commandBuffers.at(imageIndex));
- if (vkEndCommandBuffer(commandBuffers.at(imageIndex)) != VK_SUCCESS) {
- throw love::Exception("failed to record command buffer");
- }
- }
- void Graphics::present(void* screenshotCallbackdata) {
- flushBatchedDraws();
- endRecordingGraphicsCommands();
- if (imagesInFlight[imageIndex] != VK_NULL_HANDLE) {
- vkWaitForFences(device, 1, &imagesInFlight.at(imageIndex), VK_TRUE, UINT64_MAX);
- }
- imagesInFlight[imageIndex] = inFlightFences[currentFrame];
- VkSubmitInfo submitInfo{};
- submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
- VkSemaphore waitSemaphores[] = { imageAvailableSemaphores.at(currentFrame) };
- VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
- submitInfo.waitSemaphoreCount = 1;
- submitInfo.pWaitSemaphores = waitSemaphores;
- submitInfo.pWaitDstStageMask = waitStages;
- submitInfo.commandBufferCount = 1;
- submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
- VkSemaphore signalSemaphores[] = { renderFinishedSemaphores.at(currentFrame) };
- submitInfo.signalSemaphoreCount = 1;
- submitInfo.pSignalSemaphores = signalSemaphores;
- vkResetFences(device, 1, &inFlightFences[currentFrame]);
- if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences.at(currentFrame)) != VK_SUCCESS) {
- throw love::Exception("failed to submit draw command buffer");
- }
- 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;
- VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo);
- if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
- framebufferResized = false;
- recreateSwapChain();
- }
- else if (result != VK_SUCCESS) {
- throw love::Exception("failed to present swap chain image");
- }
- currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
- std::cout << "present" << std::endl;
- startRecordingGraphicsCommands();
- }
- void Graphics::setViewportSize(int width, int height, int pixelwidth, int pixelheight) {
- std::cout << "setViewPortSize";
- recreateSwapChain();
- }
- bool Graphics::setMode(void* context, int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil, int msaa) {
- std::cout << "setMode ";
- if (batchedDrawState.vb[0] == nullptr)
- {
- // Initial sizes that should be good enough for most cases. It will
- // resize to fit if needed, later.
- batchedDrawState.vb[0] = new StreamBuffer(this, device, physicalDevice, BUFFERUSAGE_VERTEX, 1024 * 1024 * 1);
- batchedDrawState.vb[1] = new StreamBuffer(this, device, physicalDevice, BUFFERUSAGE_VERTEX, 256 * 1024 * 1);
- batchedDrawState.indexBuffer = new StreamBuffer(this, device, physicalDevice, BUFFERUSAGE_INDEX, sizeof(uint16) * LOVE_UINT16_MAX);
- }
- return true;
- }
- void Graphics::draw(const DrawIndexedCommand& cmd) {
- std::cout << "drawIndexed ";
- std::vector<VkBuffer> buffers;
- std::vector<VkDeviceSize> offsets;
- buffers.push_back((VkBuffer)cmd.buffers->info[0].buffer->getHandle());
- offsets.push_back((VkDeviceSize)cmd.buffers->info[0].offset);
- buffers.push_back((VkBuffer)cmd.buffers->info[1].buffer->getHandle());
- offsets.push_back((VkDeviceSize)cmd.buffers->info[1].offset);
- vkCmdDraw(commandBuffers.at(imageIndex), 3, 1, 0, 0); // todo adjust
- }
- graphics::StreamBuffer* Graphics::newStreamBuffer(BufferUsage type, size_t size) {
- std::cout << "newStreamBuffer ";
- return new StreamBuffer(this, device, physicalDevice, type, size);
- }
- // END IMPLEMENTATION OVERRIDDEN FUNCTIONS
- void Graphics::createVulkanInstance() {
- if (enableValidationLayers && !checkValidationSupport()) {
- throw love::Exception("validation layers requested, but not available");
- }
- VkApplicationInfo appInfo{};
- appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
- appInfo.pApplicationName = "LOVE";
- appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); //todo, get this version from somewhere else?
- appInfo.pEngineName = "LOVE Engine";
- appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); //todo, same as above
- appInfo.apiVersion = VK_API_VERSION_1_0;
- VkInstanceCreateInfo createInfo{};
- createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
- createInfo.pApplicationInfo = &appInfo;
- createInfo.pNext = nullptr;
- auto window = Module::getInstance<love::window::Window>(M_WINDOW);
- const void* handle = window->getHandle();
- unsigned int count;
- if (SDL_Vulkan_GetInstanceExtensions((SDL_Window*)handle, &count, nullptr) != SDL_TRUE) {
- throw love::Exception("couldn't retrieve sdl vulkan extensions");
- }
- std::vector<const char*> extensions = {}; // can add more here
- size_t addition_extension_count = extensions.size();
- extensions.resize(addition_extension_count + count);
- if (SDL_Vulkan_GetInstanceExtensions((SDL_Window*)handle, &count, extensions.data() + addition_extension_count) != SDL_TRUE) {
- throw love::Exception("couldn't retrieve sdl vulkan extensions");
- }
- createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
- createInfo.ppEnabledExtensionNames = extensions.data();
- if (enableValidationLayers) {
- createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
- createInfo.ppEnabledLayerNames = validationLayers.data();
- }
- else {
- createInfo.enabledLayerCount = 0;
- createInfo.ppEnabledLayerNames = nullptr;
- }
- if (vkCreateInstance(
- &createInfo,
- nullptr,
- &instance) != VK_SUCCESS) {
- throw love::Exception("couldn't create vulkan instance");
- }
- }
- bool Graphics::checkValidationSupport() {
- uint32_t layerCount;
- vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
- std::vector<VkLayerProperties> availableLayers(layerCount);
- vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
- for (const char* layerName : validationLayers) {
- bool layerFound = false;
- for (const auto& layerProperties : availableLayers) {
- if (strcmp(layerName, layerProperties.layerName) == 0) {
- layerFound = true;
- break;
- }
- }
- if (!layerFound) {
- return false;
- }
- }
- return true;
- }
- void Graphics::pickPhysicalDevice() {
- uint32_t deviceCount = 0;
- vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
- if (deviceCount == 0) {
- throw love::Exception("failed to find GPUs with Vulkan support");
- }
- std::vector<VkPhysicalDevice> devices(deviceCount);
- vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
- std::multimap<int, VkPhysicalDevice> candidates;
- for (const auto& device : devices) {
- int score = rateDeviceSuitability(device);
- candidates.insert(std::make_pair(score, device));
- }
- if (candidates.rbegin()->first > 0) {
- physicalDevice = candidates.rbegin()->second;
- }
- else {
- throw love::Exception("failed to find a suitable gpu");
- }
- }
- bool Graphics::checkDeviceExtensionSupport(VkPhysicalDevice device) {
- uint32_t extensionCount;
- vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
- std::vector<VkExtensionProperties> availableExtensions(extensionCount);
- vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
- std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
- for (const auto& extension : availableExtensions) {
- requiredExtensions.erase(extension.extensionName);
- }
- return requiredExtensions.empty();
- }
- int Graphics::rateDeviceSuitability(VkPhysicalDevice device) {
- VkPhysicalDeviceProperties deviceProperties;
- VkPhysicalDeviceFeatures deviceFeatures;
- vkGetPhysicalDeviceProperties(device, &deviceProperties);
- vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
- int score = 1;
- // optional
- if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
- score += 1000;
- }
- // definitely needed
- QueueFamilyIndices indices = findQueueFamilies(device);
- if (!indices.isComplete()) {
- score = 0;
- }
- bool extensionsSupported = checkDeviceExtensionSupport(device);
- if (!extensionsSupported) {
- score = 0;
- }
- if (extensionsSupported) {
- auto swapChainSupport = querySwapChainSupport(device);
- bool swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
- if (!swapChainAdequate) {
- score = 0;
- }
- }
- return score;
- }
- Graphics::QueueFamilyIndices Graphics::findQueueFamilies(VkPhysicalDevice device) {
- QueueFamilyIndices indices;
- uint32_t queueFamilyCount = 0;
- vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
- std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
- vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
- int i = 0;
- for (const auto& queueFamily : queueFamilies) {
- if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
- indices.graphicsFamily = i;
- }
- VkBool32 presentSupport = false;
- vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
- if (presentSupport) {
- indices.presentFamily = i;
- }
- if (indices.isComplete()) {
- break;
- }
- i++;
- }
- return indices;
- }
- void Graphics::createLogicalDevice() {
- QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
- std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
- std::set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() };
- float queuePriority = 1.0f;
- for (uint32_t queueFamily : uniqueQueueFamilies) {
- VkDeviceQueueCreateInfo queueCreateInfo{};
- queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
- queueCreateInfo.queueFamilyIndex = queueFamily;
- queueCreateInfo.queueCount = 1;
- queueCreateInfo.pQueuePriorities = &queuePriority;
- queueCreateInfos.push_back(queueCreateInfo);
- }
- VkPhysicalDeviceFeatures deviceFeatures{};
- VkDeviceCreateInfo createInfo{};
- createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
- createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
- createInfo.pQueueCreateInfos = queueCreateInfos.data();
- createInfo.pEnabledFeatures = &deviceFeatures;
- createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
- createInfo.ppEnabledExtensionNames = deviceExtensions.data();
- // can this be removed?
- if (enableValidationLayers) {
- createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
- createInfo.ppEnabledLayerNames = validationLayers.data();
- }
- else {
- createInfo.enabledLayerCount = 0;
- }
- if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
- throw love::Exception("failed to create logical device");
- }
- vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
- vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
- }
- void Graphics::createSurface() {
- auto window = Module::getInstance<love::window::Window>(M_WINDOW);
- const void* handle = window->getHandle();
- if (SDL_Vulkan_CreateSurface((SDL_Window*)handle, instance, &surface) != SDL_TRUE) {
- throw love::Exception("failed to create window surface");
- }
- }
- Graphics::SwapChainSupportDetails Graphics::querySwapChainSupport(VkPhysicalDevice device) {
- SwapChainSupportDetails details;
- vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
- uint32_t formatCount;
- vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
- if (formatCount != 0) {
- details.formats.resize(formatCount);
- vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
- }
- uint32_t presentModeCount;
- vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
- if (presentModeCount != 0) {
- details.presentModes.resize(presentModeCount);
- vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
- }
- return details;
- }
- void Graphics::createSwapChain() {
- SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
- VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
- VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
- VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
- uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
- if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
- imageCount = swapChainSupport.capabilities.maxImageCount;
- }
- VkSwapchainCreateInfoKHR createInfo{};
- createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
- createInfo.surface = surface;
- createInfo.minImageCount = imageCount;
- createInfo.imageFormat = surfaceFormat.format;
- createInfo.imageColorSpace = surfaceFormat.colorSpace;
- createInfo.imageExtent = extent;
- createInfo.imageArrayLayers = 1;
- createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
- QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
- uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
- if (indices.graphicsFamily != indices.presentFamily) {
- createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
- createInfo.queueFamilyIndexCount = 2;
- createInfo.pQueueFamilyIndices = queueFamilyIndices;
- }
- else {
- createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
- createInfo.queueFamilyIndexCount = 0;
- createInfo.pQueueFamilyIndices = nullptr;
- }
- createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
- createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
- createInfo.presentMode = presentMode;
- createInfo.clipped = VK_TRUE;
- createInfo.oldSwapchain = VK_NULL_HANDLE;
- if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
- throw love::Exception("failed to create swap chain");
- }
- vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
- swapChainImages.resize(imageCount);
- vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
- swapChainImageFormat = surfaceFormat.format;
- swapChainExtent = extent;
- }
- VkSurfaceFormatKHR Graphics::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats) {
- for (const auto& availableFormat : availableFormats) {
- if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
- return availableFormat;
- }
- }
- return availableFormats[0];
- }
- VkPresentModeKHR Graphics::chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes) {
- // needed ?
- for (const auto& availablePresentMode : availablePresentModes) {
- if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
- return availablePresentMode;
- }
- }
- return VK_PRESENT_MODE_FIFO_KHR;
- }
- VkExtent2D Graphics::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
- if (capabilities.currentExtent.width != UINT32_MAX) {
- return capabilities.currentExtent;
- }
- else {
- auto window = Module::getInstance<love::window::Window>(M_WINDOW);
- const void* handle = window->getHandle();
- int width, height;
- // is this the equivalent of glfwGetFramebufferSize ?
- SDL_Vulkan_GetDrawableSize((SDL_Window*)handle, &width, &height);
- VkExtent2D actualExtent = {
- static_cast<uint32_t>(width),
- static_cast<uint32_t>(height)
- };
- actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
- actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
- return actualExtent;
- }
- }
- void Graphics::createImageViews() {
- swapChainImageViews.resize(swapChainImages.size());
- for (size_t i = 0; i < swapChainImages.size(); i++) {
- VkImageViewCreateInfo createInfo{};
- createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
- createInfo.image = swapChainImages.at(i);
- createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
- createInfo.format = swapChainImageFormat;
- createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
- createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
- createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
- createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
- createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- createInfo.subresourceRange.baseMipLevel = 0;
- createInfo.subresourceRange.levelCount = 1;
- createInfo.subresourceRange.baseArrayLayer = 0;
- createInfo.subresourceRange.layerCount = 1;
- if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews.at(i)) != VK_SUCCESS) {
- throw love::Exception("failed to create image views");
- }
- }
- }
- void Graphics::createRenderPass() {
- VkAttachmentDescription colorAttachment{};
- colorAttachment.format = swapChainImageFormat;
- 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;
- VkSubpassDescription subpass{};
- subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
- subpass.colorAttachmentCount = 1;
- subpass.pColorAttachments = &colorAttachmentRef;
- VkSubpassDependency dependency{};
- dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
- dependency.dstSubpass = 0;
- dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- dependency.srcAccessMask = 0;
- dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
- VkRenderPassCreateInfo renderPassInfo{};
- renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
- renderPassInfo.attachmentCount = 1;
- renderPassInfo.pAttachments = &colorAttachment;
- renderPassInfo.subpassCount = 1;
- renderPassInfo.pSubpasses = &subpass;
- renderPassInfo.dependencyCount = 1;
- renderPassInfo.pDependencies = &dependency;
- if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
- throw love::Exception("failed to create render pass");
- }
- }
- static VkShaderModule createShaderModule(VkDevice device, const std::vector<char>& code) {
- VkShaderModuleCreateInfo createInfo{};
- createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
- createInfo.codeSize = code.size();
- createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
- VkShaderModule shaderModule;
- if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
- throw love::Exception("failed to create shader module");
- }
- return shaderModule;
- }
- void Graphics::createGraphicsPipeline() {
- // love::graphics::vulkan::Shader* shader = dynamic_cast<love::graphics::vulkan::Shader*>(getShader());
- // auto shaderStages = shader->getShaderStages();
- auto vertShaderCode = readFile("vert.spv");
- auto fragShaderCode = readFile("frag.spv");
- VkShaderModule vertShaderModule = createShaderModule(device, vertShaderCode);
- VkShaderModule fragShaderModule = createShaderModule(device, fragShaderCode);
- VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
- vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
- vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
- vertShaderStageInfo.module = vertShaderModule;
- vertShaderStageInfo.pName = "main";
- VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
- fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
- fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
- fragShaderStageInfo.module = fragShaderModule;
- fragShaderStageInfo.pName = "main";
- VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
- VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
- vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
- VkVertexInputBindingDescription vertexBindingDescription;
- vertexBindingDescription.binding = 0;
- vertexBindingDescription.stride = 2 * sizeof(float); // just position for now
- vertexBindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
- // todo later
- VkVertexInputAttributeDescription positionInputAttributeDescription;
- positionInputAttributeDescription.binding = 0;
- positionInputAttributeDescription.location = 0;
- positionInputAttributeDescription.format = VK_FORMAT_R32G32_SFLOAT;
- positionInputAttributeDescription.offset = 0;
- vertexInputInfo.vertexBindingDescriptionCount = 0;
- vertexInputInfo.pVertexBindingDescriptions = &vertexBindingDescription;
- vertexInputInfo.vertexAttributeDescriptionCount = 0;
- vertexInputInfo.pVertexAttributeDescriptions = &positionInputAttributeDescription;
- VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
- inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
- inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
- inputAssembly.primitiveRestartEnable = VK_FALSE;
-
- VkViewport viewport{};
- viewport.x = 0.0f;
- viewport.y = 0.0f;
- viewport.width = (float)swapChainExtent.width;
- viewport.height = (float)swapChainExtent.height;
- viewport.minDepth = 0.0f;
- viewport.maxDepth = 1.0f;
- VkRect2D scissor{};
- scissor.offset = { 0, 0 };
- scissor.extent = swapChainExtent;
- 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.depthClampEnable = VK_FALSE;
- rasterizer.rasterizerDiscardEnable = VK_FALSE;
- rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
- rasterizer.lineWidth = 1.0f;
- 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;
- VkPipelineMultisampleStateCreateInfo multisampling{};
- multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
- multisampling.sampleShadingEnable = VK_FALSE;
- multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
- multisampling.minSampleShading = 1.0f; // Optional
- multisampling.pSampleMask = nullptr; // Optional
- multisampling.alphaToCoverageEnable = VK_FALSE; // Optional
- multisampling.alphaToOneEnable = VK_FALSE; // Optional
- VkPipelineColorBlendAttachmentState colorBlendAttachment{};
- colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
- colorBlendAttachment.blendEnable = VK_FALSE;
- VkPipelineColorBlendStateCreateInfo colorBlending{};
- colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
- 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;
- VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
- pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
- pipelineLayoutInfo.setLayoutCount = 0;
- pipelineLayoutInfo.pushConstantRangeCount = 0;
- if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
- throw love::Exception("failed to create pipeline layout");
- }
- VkGraphicsPipelineCreateInfo pipelineInfo{};
- pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
- // pipelineInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
- // pipelineInfo.pStages = shaderStages.data();
- pipelineInfo.stageCount = 2;
- pipelineInfo.pStages = shaderStages;
- pipelineInfo.pVertexInputState = &vertexInputInfo;
- pipelineInfo.pInputAssemblyState = &inputAssembly;
- pipelineInfo.pViewportState = &viewportState;
- pipelineInfo.pRasterizationState = &rasterizer;
- pipelineInfo.pMultisampleState = &multisampling;
- pipelineInfo.pDepthStencilState = nullptr;
- pipelineInfo.pColorBlendState = &colorBlending;
- pipelineInfo.pDynamicState = nullptr;
- pipelineInfo.layout = pipelineLayout;
- pipelineInfo.renderPass = renderPass;
- pipelineInfo.subpass = 0;
- pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
- pipelineInfo.basePipelineIndex = -1;
- if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
- throw love::Exception("failed to create graphics pipeline");
- }
- vkDestroyShaderModule(device, vertShaderModule, nullptr);
- vkDestroyShaderModule(device, fragShaderModule, nullptr);
- }
- void Graphics::createFramebuffers() {
- swapChainFramBuffers.resize(swapChainImageViews.size());
- for (size_t i = 0; i < swapChainImageViews.size(); i++) {
- VkImageView attachments[] = {
- swapChainImageViews.at(i)
- };
- VkFramebufferCreateInfo framebufferInfo{};
- framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
- framebufferInfo.renderPass = renderPass;
- framebufferInfo.attachmentCount = 1;
- framebufferInfo.pAttachments = attachments;
- framebufferInfo.width = swapChainExtent.width;
- framebufferInfo.height = swapChainExtent.height;
- framebufferInfo.layers = 1;
- if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramBuffers.at(i)) != VK_SUCCESS) {
- throw love::Exception("failed to create framebuffers");
- }
- }
- }
- void Graphics::createCommandPool() {
- QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
- VkCommandPoolCreateInfo poolInfo{};
- poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
- poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
- poolInfo.flags = 0;
- if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
- throw love::Exception("failed to create command pool");
- }
- }
- void Graphics::createCommandBuffers() {
- commandBuffers.resize(swapChainFramBuffers.size());
- VkCommandBufferAllocateInfo allocInfo{};
- allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
- allocInfo.commandPool = commandPool;
- allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
- allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
- if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
- throw love::Exception("failed to allocate command buffers");
- }
- }
- void Graphics::createSyncObjects() {
- imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
- renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
- inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
- imagesInFlight.resize(swapChainImages.size(), VK_NULL_HANDLE);
- VkSemaphoreCreateInfo semaphoreInfo{};
- semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
- VkFenceCreateInfo fenceInfo{};
- fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
- fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
- for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
- if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores.at(i)) != VK_SUCCESS ||
- vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores.at(i)) != VK_SUCCESS ||
- vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences.at(i)) != VK_SUCCESS) {
- throw love::Exception("failed to create synchronization objects for a frame!");
- }
- }
- }
- void Graphics::cleanup() {
- vkDeviceWaitIdle(device);
- cleanupSwapChain();
- for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
- vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
- vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
- vkDestroyFence(device, inFlightFences[i], nullptr);
- }
- vkDestroyCommandPool(device, commandPool, nullptr);
- vkDestroyDevice(device, nullptr);
- vkDestroySurfaceKHR(instance, surface, nullptr);
- vkDestroyInstance(instance, nullptr);
- }
- void Graphics::cleanupSwapChain() {
- for (size_t i = 0; i < swapChainFramBuffers.size(); i++) {
- vkDestroyFramebuffer(device, swapChainFramBuffers[i], nullptr);
- }
- vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
- vkDestroyPipeline(device, graphicsPipeline, nullptr);
- vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
- vkDestroyRenderPass(device, renderPass, nullptr);
- for (size_t i = 0; i < swapChainImageViews.size(); i++) {
- vkDestroyImageView(device, swapChainImageViews[i], nullptr);
- }
- vkDestroySwapchainKHR(device, swapChain, nullptr);
- }
- void Graphics::recreateSwapChain() {
- vkDeviceWaitIdle(device);
- cleanupSwapChain();
- createSwapChain();
- createImageViews();
- createRenderPass();
- createGraphicsPipeline();
- createFramebuffers();
- createCommandBuffers();
- startRecordingGraphicsCommands();
- }
- love::graphics::Graphics* createInstance() {
- love::graphics::Graphics* instance = nullptr;
- try {
- instance = new Graphics();
- }
- catch (love::Exception& e) {
- printf("Cannot create Vulkan renderer: %s\n", e.what());
- }
- return instance;
- }
- }
- }
- }
|