2017-01-31 20:40:15 -08:00
|
|
|
#pragma comment(lib, "glfw3.lib")
|
|
|
|
#pragma comment(lib, "vulkan-1.lib")
|
2016-10-30 01:21:33 -07:00
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
2017-01-31 20:40:15 -08:00
|
|
|
#if _WIN32 || _WIN64
|
|
|
|
#include <algorithm>
|
|
|
|
#endif
|
2016-10-30 01:21:33 -07:00
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
|
|
|
#include <set>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
const int WIDTH = 1200;
|
|
|
|
const int HEIGHT = 900;
|
|
|
|
|
|
|
|
const std::vector<const char*> validationLayers = {
|
|
|
|
"VK_LAYER_LUNARG_standard_validation"
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector<const char*> deviceExtensions = {
|
|
|
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
const bool enableValidationLayers = false;
|
|
|
|
#else
|
|
|
|
const bool enableValidationLayers = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class HelloTriangleApplication {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
initWindow();
|
|
|
|
initVulkan();
|
|
|
|
mainLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// our debug callback function, just prints the message from the Validation layer to stderr
|
|
|
|
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( VkDebugReportFlagsEXT flags,
|
|
|
|
VkDebugReportObjectTypeEXT objType, uint64_t obj, size_t location, int32_t code,
|
|
|
|
const char* layerPrefix, const char* msg, void* userData) {
|
|
|
|
std::cerr << "[Validation Layer]: " << msg << std::endl;
|
|
|
|
|
|
|
|
return VK_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:29:34 -07:00
|
|
|
static void onWindowResized(GLFWwindow* window, int width, int height) {
|
|
|
|
if(width == 0 || height == 0) return;
|
|
|
|
|
|
|
|
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
|
|
|
|
app->recreateSwapChain();
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
private:
|
|
|
|
// Instance variables:
|
|
|
|
GLFWwindow* window;
|
2018-04-05 21:28:49 -07:00
|
|
|
VkInstance instance;
|
|
|
|
VkDebugReportCallbackEXT callback;
|
2016-10-30 01:21:33 -07:00
|
|
|
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|
|
|
VkQueue graphicsQueue;
|
2018-04-05 21:28:49 -07:00
|
|
|
VkDevice device;
|
|
|
|
VkSurfaceKHR surface;
|
2016-10-30 01:21:33 -07:00
|
|
|
VkQueue presentQueue;
|
2018-04-05 21:28:49 -07:00
|
|
|
VkSwapchainKHR swapChain;
|
2016-10-30 01:21:33 -07:00
|
|
|
std::vector<VkImage> swapChainImages;
|
|
|
|
VkFormat swapChainImageFormat;
|
|
|
|
VkExtent2D swapChainExtent;
|
2018-04-05 21:28:49 -07:00
|
|
|
std::vector<VkImageView> swapChainImageViews;
|
|
|
|
VkRenderPass renderPass;
|
|
|
|
VkPipelineLayout pipelineLayout;
|
|
|
|
VkPipeline graphicsPipeline;
|
|
|
|
std::vector<VkFramebuffer> swapChainFramebuffers;
|
|
|
|
VkCommandPool commandPool;
|
2016-10-31 21:29:34 -07:00
|
|
|
std::vector<VkCommandBuffer> commandBuffers;
|
2018-04-05 21:28:49 -07:00
|
|
|
VkSemaphore imageAvailableSemaphore;
|
|
|
|
VkSemaphore renderFinishedSemaphore;
|
2016-10-31 21:29:34 -07:00
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
|
|
|
|
// Initialize our GLFW window.
|
|
|
|
void initWindow() {
|
|
|
|
glfwInit();
|
2016-10-31 21:29:34 -07:00
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
2016-10-31 21:29:34 -07:00
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
|
2016-10-31 21:29:34 -07:00
|
|
|
|
|
|
|
glfwSetWindowUserPointer(window, this);
|
|
|
|
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
|
2016-10-30 01:21:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void initVulkan() {
|
|
|
|
createInstance();
|
|
|
|
setupDebugCallback();
|
|
|
|
createSurface();
|
|
|
|
pickPhysicalDevice();
|
|
|
|
createLogicalDevice();
|
|
|
|
createSwapChain();
|
|
|
|
createImageViews();
|
|
|
|
createRenderPass();
|
|
|
|
createGraphicsPipeline();
|
|
|
|
createFramebuffers();
|
2016-10-31 21:29:34 -07:00
|
|
|
createCommandPool();
|
|
|
|
createCommandBuffers();
|
|
|
|
createSemaphores();
|
|
|
|
}
|
|
|
|
|
|
|
|
void recreateSwapChain() {
|
|
|
|
vkDeviceWaitIdle(device);
|
|
|
|
|
|
|
|
createSwapChain();
|
|
|
|
createImageViews();
|
|
|
|
createRenderPass();
|
|
|
|
createGraphicsPipeline();
|
|
|
|
createFramebuffers();
|
|
|
|
createCommandBuffers();
|
2016-10-30 01:21:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out what extensions GLFW requires to make a Vulkan surface.
|
|
|
|
// Also add the debug report extension, so we can add our debug report callback function.
|
|
|
|
std::vector<const char*> getRequiredExtensions() {
|
|
|
|
std::vector<const char*> extensions;
|
|
|
|
|
|
|
|
unsigned int glfwExtensionCount = 0;
|
|
|
|
const char** glfwExtensions;
|
|
|
|
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < glfwExtensionCount; i++) {
|
|
|
|
extensions.push_back(glfwExtensions[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(enableValidationLayers) {
|
|
|
|
extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create our VkInstance with the validation layers enabled and the extensions needed for
|
|
|
|
// presentation on our surface GLFW will create.
|
|
|
|
void createInstance() {
|
|
|
|
if(enableValidationLayers && !checkValidationLayerSupport()) {
|
|
|
|
throw std::runtime_error("validation layers requested, but not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create VkApplicationInfo
|
|
|
|
VkApplicationInfo appInfo = {};
|
|
|
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
|
|
|
appInfo.pApplicationName = "Hello Triangle";
|
|
|
|
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
|
|
appInfo.pEngineName = "No Engine";
|
|
|
|
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
|
|
appInfo.apiVersion = VK_API_VERSION_1_0;
|
|
|
|
|
|
|
|
// Create VkInstanceCreateInfo and have it point to VkApplicationInfo
|
|
|
|
VkInstanceCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
createInfo.pApplicationInfo = &appInfo;
|
|
|
|
|
|
|
|
// Enable the validation layers if they are requested
|
|
|
|
if(enableValidationLayers) {
|
|
|
|
createInfo.enabledLayerCount = validationLayers.size();
|
|
|
|
createInfo.ppEnabledLayerNames = validationLayers.data();
|
|
|
|
} else {
|
|
|
|
createInfo.enabledLayerCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto reqExtensions = getRequiredExtensions();
|
|
|
|
createInfo.enabledExtensionCount = reqExtensions.size();
|
|
|
|
createInfo.ppEnabledExtensionNames = reqExtensions.data();
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create Vulkan instance!");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t extensionCount = 0;
|
|
|
|
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
|
|
|
std::vector<VkExtensionProperties> extensions(extensionCount);
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
|
|
|
|
|
|
|
std::cout << "available extensions:" << std::endl;
|
|
|
|
|
|
|
|
for(const auto& extension : extensions) {
|
|
|
|
std::cout << "\t" << extension.extensionName << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroyInstance() {
|
|
|
|
vkDestroyInstance(instance, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
// Check if the validation layers are supported.s
|
|
|
|
bool checkValidationLayerSupport() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up our debug report callback to get information back from the validation layers.
|
|
|
|
void setupDebugCallback() {
|
|
|
|
if (!enableValidationLayers) return;
|
|
|
|
|
|
|
|
VkDebugReportCallbackCreateInfoEXT createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
|
|
|
|
createInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
|
|
|
|
createInfo.pfnCallback = debugCallback;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to setup debug callback!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void cleanupDebugCallback() {
|
|
|
|
if (!enableValidationLayers) return;
|
|
|
|
|
|
|
|
DestroyDebugReportCallbackEXT(instance, callback, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
// Pick the first suitable physical device, using isDeviceSuitable(VkPhysicalDevice)
|
|
|
|
void pickPhysicalDevice() {
|
|
|
|
uint32_t deviceCount = 0;
|
|
|
|
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
|
|
|
|
|
|
|
if(deviceCount == 0) {
|
|
|
|
throw std::runtime_error("Failed to find a device with Vulkan support!");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<VkPhysicalDevice> devices(deviceCount);
|
|
|
|
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
|
|
|
|
|
|
|
|
for(const auto& device : devices) {
|
|
|
|
if(isDeviceSuitable(device)) {
|
|
|
|
physicalDevice = device;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(physicalDevice == VK_NULL_HANDLE) {
|
|
|
|
throw std::runtime_error("Failed to find a suitable device!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the chosen physical device is suitable for our application.
|
|
|
|
bool isDeviceSuitable(VkPhysicalDevice device) {
|
|
|
|
/* example: device must be a discrete GPU and support geometry shaders
|
|
|
|
VkPhysicalDeviceProperties deviceProperties;
|
|
|
|
VkPhysicalDeviceFeatures deviceFeatures;
|
|
|
|
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
|
|
|
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
|
|
|
|
|
|
|
|
return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
|
|
|
|
deviceFeatures.geometryShader;
|
|
|
|
*/
|
|
|
|
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(device);
|
|
|
|
|
|
|
|
bool extensionsSupported = checkDeviceExtensionSupport(device);
|
|
|
|
|
|
|
|
bool swapChainAdequate = false;
|
|
|
|
|
|
|
|
if(extensionsSupported) {
|
|
|
|
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
|
|
|
|
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices.isComplete() && extensionsSupported && swapChainAdequate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check in the given physical device supports the extensions we require.
|
|
|
|
bool 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the queue families that support graphics and present. These could be
|
|
|
|
// two different queue families, or the same one.
|
|
|
|
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
|
|
|
|
QueueFamilyIndices indices;
|
|
|
|
|
|
|
|
uint32_t queueFamilyCount;
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for(const auto& queueFamily : queueFamilies) {
|
|
|
|
if(queueFamily.queueCount > 0 && (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)) {
|
|
|
|
indices.graphicsFamily = i;
|
|
|
|
}
|
|
|
|
VkBool32 presentSupport = false;
|
|
|
|
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
|
|
|
|
|
|
|
if(queueFamily.queueCount > 0 && presentSupport) {
|
|
|
|
indices.presentFamily = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(indices.isComplete()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the logical device with our required extensions, and the validation layers,
|
|
|
|
// then create the graphics and present queues.
|
|
|
|
void createLogicalDevice() {
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
|
|
|
|
|
|
|
|
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
|
|
|
|
std::set<int> uniqueQueueFamilies = {indices.graphicsFamily, indices.presentFamily};
|
|
|
|
|
|
|
|
float queuePriority = 1.0f;
|
|
|
|
for(int 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.pQueueCreateInfos = queueCreateInfos.data();
|
|
|
|
createInfo.queueCreateInfoCount = (uint32_t) queueCreateInfos.size();
|
|
|
|
|
|
|
|
createInfo.pEnabledFeatures = &deviceFeatures;
|
|
|
|
createInfo.enabledExtensionCount = deviceExtensions.size();
|
|
|
|
createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
|
|
|
|
|
|
|
if(enableValidationLayers) {
|
|
|
|
createInfo.enabledLayerCount = validationLayers.size();
|
|
|
|
createInfo.ppEnabledLayerNames= validationLayers.data();
|
|
|
|
} else {
|
|
|
|
createInfo.enabledLayerCount = 0;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create logical device!");
|
|
|
|
}
|
|
|
|
|
|
|
|
vkGetDeviceQueue(device, indices.graphicsFamily, 0, &graphicsQueue);
|
|
|
|
vkGetDeviceQueue(device, indices.presentFamily, 0, &presentQueue);
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroyLogicalDevice() {
|
|
|
|
vkDestroyDevice(device, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
// Have GLFW create a surface for us, this lets us not be concerned with the platform-specifics
|
|
|
|
// involved in surfaces.
|
|
|
|
void createSurface() {
|
2018-04-05 21:28:49 -07:00
|
|
|
if(glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create window surface!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroySurface() {
|
|
|
|
vkDestroySurfaceKHR(instance, surface, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
// Find out what formats and present modes the physical device supports.
|
|
|
|
SwapChainSupportDetails 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats) {
|
|
|
|
// If the surface has no preference of format, use 24-bit BGR and the SRGB color space
|
|
|
|
if(availableFormats.size() == 1 && availableFormats[0].format == VK_FORMAT_UNDEFINED) {
|
|
|
|
return {VK_FORMAT_B8G8R8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if this same 24-bit BGR and SRGB combo is available
|
|
|
|
for(const auto& availableFormat : availableFormats) {
|
|
|
|
if(availableFormat.format == VK_FORMAT_B8G8R8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
|
|
|
|
return availableFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a last resort, pick the surface's first preferred format
|
|
|
|
return availableFormats[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes) {
|
|
|
|
// Look if the the surface supports the "mailbox" present mode (similar to triple buffering)
|
|
|
|
for(const auto& availablePresentMode : availablePresentModes) {
|
|
|
|
if(availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
|
|
|
return availablePresentMode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, use the first in first out present mode (similar to basic double buffered vsync)
|
|
|
|
return VK_PRESENT_MODE_FIFO_KHR;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
|
|
|
|
// Swap extent is resolution of the swap chain images we will be drawing to
|
|
|
|
|
|
|
|
// Attempt to use the current extent, if it is valid.
|
|
|
|
if(capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
|
|
|
|
return capabilities.currentExtent;
|
|
|
|
} else {
|
|
|
|
VkExtent2D actualExtent = {WIDTH, HEIGHT};
|
|
|
|
|
|
|
|
// Get the size of extent we can use.
|
|
|
|
actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
|
|
|
|
actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
|
|
|
|
|
|
|
|
return actualExtent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the swap chain that will be used to submit completed frames to
|
|
|
|
void createSwapChain() {
|
2018-04-05 21:28:49 -07:00
|
|
|
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
|
2016-10-30 01:21:33 -07:00
|
|
|
|
|
|
|
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
|
|
|
|
VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
|
|
|
|
VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
|
|
|
|
|
|
|
|
// Attempt to use 1 + minimum image count for this device's swap chain support
|
|
|
|
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
|
|
|
// If this is too many, clamp to the maximum image count.
|
|
|
|
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;
|
|
|
|
// Number of layers each image in swap chain consists of (stereoscopic app might use 2 layers)
|
|
|
|
createInfo.imageArrayLayers = 1;
|
|
|
|
// Tell Vulkan we will be rendering directly to the images in this swap chain.
|
|
|
|
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|
|
|
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
|
|
|
|
// Array of queue family indices we will use. Only used if they are different queue families.
|
|
|
|
uint32_t queueFamilyIndices[] = {(uint32_t) indices.graphicsFamily, (uint32_t) indices.presentFamily};
|
|
|
|
|
|
|
|
// If graphics queue and present queue are on different queue families, we must tell Vulkan both queue
|
|
|
|
// families will be concurrently sharing this swap chain.
|
|
|
|
if(indices.graphicsFamily != indices.presentFamily) {
|
|
|
|
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
|
|
|
createInfo.queueFamilyIndexCount = 2;
|
|
|
|
createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
|
|
|
} else {
|
|
|
|
// Graphics queue and present queue are on same queue family,
|
|
|
|
// so the swap chain does not need to be shared.
|
|
|
|
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
createInfo.queueFamilyIndexCount = 0;
|
|
|
|
createInfo.pQueueFamilyIndices = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not want the swap chain images to be transformed, so use the current transform.
|
|
|
|
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
|
|
|
// Ignore alpha for blending with other windows.
|
|
|
|
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
|
|
|
|
|
|
createInfo.presentMode = presentMode;
|
|
|
|
createInfo.clipped = VK_TRUE; // Clip pixels that are obscured for improved perf.
|
|
|
|
|
|
|
|
// We have no old swap chain for now. Could be used if the window is resized or something
|
|
|
|
// and we want to create a new swap chain to match the new window size.
|
|
|
|
createInfo.oldSwapchain = VK_NULL_HANDLE;
|
|
|
|
|
2016-10-31 21:29:34 -07:00
|
|
|
VkSwapchainKHR oldSwapChain = swapChain;
|
|
|
|
createInfo.oldSwapchain = oldSwapChain;
|
|
|
|
|
|
|
|
VkSwapchainKHR newSwapChain;
|
|
|
|
if(vkCreateSwapchainKHR(device, &createInfo, nullptr, &newSwapChain) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create swap chain!");
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:29:34 -07:00
|
|
|
swapChain = newSwapChain;
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
// Get the swapchain images.
|
|
|
|
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
|
|
|
swapChainImages.resize(imageCount);
|
|
|
|
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
|
|
|
|
|
|
|
// Store image format and extent of the swap chain images.
|
|
|
|
swapChainImageFormat = surfaceFormat.format;
|
|
|
|
swapChainExtent = extent;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroySwapChain() {
|
|
|
|
vkDestroySwapchainKHR(device, swapChain, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
void createImageViews() {
|
2018-04-05 21:28:49 -07:00
|
|
|
swapChainImageViews.resize(swapChainImages.size(), VkImageView{});
|
2016-10-30 01:21:33 -07:00
|
|
|
|
|
|
|
// For each image in the swap chain create a VkImageView
|
|
|
|
for(uint32_t i = 0; i < swapChainImages.size(); i++) {
|
|
|
|
VkImageViewCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
createInfo.image = swapChainImages[i];
|
|
|
|
|
|
|
|
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
createInfo.format = swapChainImageFormat;
|
|
|
|
|
|
|
|
// all our components should only be "swizzled" with the identity matrix.
|
|
|
|
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;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create image views!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroyImageViews() {
|
|
|
|
for (uint32_t i = 0; i < swapChainImages.size(); i++) {
|
|
|
|
vkDestroyImageView(device, swapChainImageViews[i], nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
void createGraphicsPipeline() {
|
|
|
|
auto vertShaderCode = readFile("shaders/vert.spv");
|
|
|
|
auto fragShaderCode = readFile("shaders/frag.spv");
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
VkShaderModule vertShaderModule;
|
|
|
|
VkShaderModule fragShaderModule;
|
2016-10-30 01:21:33 -07:00
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
createShaderModule(vertShaderCode, &vertShaderModule);
|
|
|
|
createShaderModule(fragShaderCode, &fragShaderModule);
|
2016-10-30 01:21:33 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
vertexInputInfo.vertexBindingDescriptionCount = 0;
|
|
|
|
vertexInputInfo.pVertexBindingDescriptions = nullptr;
|
|
|
|
vertexInputInfo.vertexAttributeDescriptionCount = 0;
|
|
|
|
vertexInputInfo.pVertexAttributeDescriptions = nullptr;
|
|
|
|
|
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
|
|
|
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
|
|
|
inputAssembly.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;
|
|
|
|
multisampling.pSampleMask = nullptr;
|
|
|
|
multisampling.alphaToCoverageEnable = VK_FALSE;
|
|
|
|
multisampling.alphaToOneEnable = VK_FALSE;
|
|
|
|
|
|
|
|
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_TRUE;
|
|
|
|
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
|
|
|
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
|
|
|
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
|
|
|
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
|
|
|
|
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.pSetLayouts = nullptr;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create pipeline layout!");
|
|
|
|
}
|
|
|
|
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineInfo = {};
|
|
|
|
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
|
|
|
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;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create graphics pipeline!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroyGraphicsPipeline() {
|
|
|
|
vkDestroyPipeline(device, graphicsPipeline, nullptr);
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
static std::vector<char> readFile(const std::string& filename) {
|
|
|
|
// Start reading at end of the file, as binary.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void createShaderModule(const std::vector<char>& code, VkShaderModule* shaderModule) {
|
2016-10-30 01:21:33 -07:00
|
|
|
VkShaderModuleCreateInfo createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
|
|
createInfo.codeSize = code.size();
|
|
|
|
createInfo.pCode = (uint32_t*) code.data();
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateShaderModule(device, &createInfo, nullptr, shaderModule) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create shader module!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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;
|
|
|
|
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
|
|
renderPassInfo.attachmentCount = 1;
|
|
|
|
renderPassInfo.pAttachments = &colorAttachment;
|
|
|
|
renderPassInfo.subpassCount = 1;
|
|
|
|
renderPassInfo.pSubpasses = &subPass;
|
|
|
|
|
2016-10-31 21:29:34 -07:00
|
|
|
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_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
|
|
|
|
renderPassInfo.dependencyCount = 1;
|
|
|
|
renderPassInfo.pDependencies = &dependency;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create render pass!");
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 21:28:49 -07:00
|
|
|
|
|
|
|
void destroyRenderPass() {
|
|
|
|
vkDestroyRenderPass(device, renderPass, nullptr);
|
|
|
|
}
|
2016-10-30 01:21:33 -07:00
|
|
|
|
|
|
|
void createFramebuffers() {
|
2018-04-05 21:28:49 -07:00
|
|
|
swapChainFramebuffers.resize(swapChainImageViews.size(), VkFramebuffer{});
|
2016-10-30 01:21:33 -07:00
|
|
|
|
|
|
|
for(size_t i = 0; i < swapChainImageViews.size(); i++) {
|
|
|
|
VkImageView attachments[] = { swapChainImageViews[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;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
|
2016-10-30 01:21:33 -07:00
|
|
|
throw std::runtime_error("failed to create framebuffer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroyFramebuffers() {
|
|
|
|
for (size_t i = 0; i < swapChainImageViews.size(); i++) {
|
|
|
|
vkDestroyFramebuffer(device, swapChainFramebuffers[i], nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:29:34 -07:00
|
|
|
void createCommandPool() {
|
|
|
|
QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
|
|
|
|
|
|
|
|
VkCommandPoolCreateInfo poolInfo = {};
|
|
|
|
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
|
|
|
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
|
|
|
|
poolInfo.flags = 0;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
|
2016-10-31 21:29:34 -07:00
|
|
|
throw std::runtime_error("failed to create command pool!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroyCommandPool() {
|
|
|
|
vkDestroyCommandPool(device, commandPool, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:29:34 -07:00
|
|
|
void createCommandBuffers() {
|
|
|
|
if(commandBuffers.size() > 0) {
|
|
|
|
vkFreeCommandBuffers(device, commandPool, commandBuffers.size(), commandBuffers.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
commandBuffers.resize(swapChainFramebuffers.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 std::runtime_error("failed to allocate command buffers!");
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < commandBuffers.size(); i++) {
|
|
|
|
VkCommandBufferBeginInfo beginInfo = {};
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
|
|
|
beginInfo.pInheritanceInfo = nullptr;
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(commandBuffers[i], &beginInfo);
|
|
|
|
|
|
|
|
VkRenderPassBeginInfo renderPassInfo = {};
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
|
|
|
renderPassInfo.renderPass = renderPass;
|
|
|
|
renderPassInfo.framebuffer = swapChainFramebuffers[i];
|
|
|
|
renderPassInfo.renderArea.offset = {0, 0};
|
|
|
|
renderPassInfo.renderArea.extent = swapChainExtent;
|
|
|
|
|
|
|
|
VkClearValue clearColor = {0.0f, 0.0f, 0.0f, 1.0f};
|
|
|
|
renderPassInfo.clearValueCount = 1;
|
|
|
|
renderPassInfo.pClearValues = &clearColor;
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
|
|
vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
|
|
|
|
|
|
|
|
vkCmdDraw(commandBuffers[i], 3, 1, 0, 0);
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(commandBuffers[i]);
|
|
|
|
|
|
|
|
if(vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
|
|
|
|
throw std::runtime_error("failed to record command buffer!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 21:28:49 -07:00
|
|
|
|
|
|
|
void destroyCommandBuffers() {
|
|
|
|
if (commandBuffers.size() > 0) {
|
|
|
|
vkFreeCommandBuffers(device, commandPool, commandBuffers.size(), commandBuffers.data());
|
|
|
|
}
|
|
|
|
}
|
2016-10-31 21:29:34 -07:00
|
|
|
|
|
|
|
void createSemaphores() {
|
|
|
|
VkSemaphoreCreateInfo semaphoreInfo = {};
|
|
|
|
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
if(vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphore) != VK_SUCCESS ||
|
|
|
|
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphore) != VK_SUCCESS) {
|
2016-10-31 21:29:34 -07:00
|
|
|
throw std::runtime_error("failed to create semaphores!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:28:49 -07:00
|
|
|
void destroySemaphores() {
|
|
|
|
vkDestroySemaphore(device, imageAvailableSemaphore, nullptr);
|
|
|
|
vkDestroySemaphore(device, renderFinishedSemaphore, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:21:33 -07:00
|
|
|
void mainLoop() {
|
|
|
|
// While we shouldn't close, have glfw poll for events
|
|
|
|
while(!glfwWindowShouldClose(window)) {
|
|
|
|
glfwPollEvents();
|
2016-10-31 21:29:34 -07:00
|
|
|
drawFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
vkDeviceWaitIdle(device);
|
2018-04-05 21:28:49 -07:00
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup() {
|
|
|
|
destroySemaphores();
|
|
|
|
destroyCommandBuffers();
|
|
|
|
destroyCommandPool();
|
|
|
|
destroyFramebuffers();
|
|
|
|
destroyGraphicsPipeline();
|
|
|
|
destroyRenderPass();
|
|
|
|
destroyImageViews();
|
|
|
|
destroySwapChain();
|
|
|
|
destroyLogicalDevice();
|
|
|
|
destroySurface();
|
|
|
|
cleanupDebugCallback();
|
|
|
|
destroyInstance();
|
2016-10-31 21:29:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawFrame() {
|
|
|
|
uint32_t imageIndex;
|
|
|
|
vkAcquireNextImageKHR(device, swapChain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
|
|
|
|
|
|
|
|
VkSubmitInfo submitInfo = {};
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
|
|
|
|
VkSemaphore waitSemaphores[] = {imageAvailableSemaphore};
|
|
|
|
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[] = {renderFinishedSemaphore};
|
|
|
|
submitInfo.signalSemaphoreCount = 1;
|
|
|
|
submitInfo.pSignalSemaphores = signalSemaphores;
|
|
|
|
|
|
|
|
if(vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS) {
|
|
|
|
throw std::runtime_error("failed to submit draw command buffer!");
|
2016-10-30 01:21:33 -07:00
|
|
|
}
|
2016-10-31 21:29:34 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
presentInfo.pResults = nullptr;
|
|
|
|
|
|
|
|
vkQueuePresentKHR(presentQueue, &presentInfo);
|
2016-10-30 01:21:33 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
HelloTriangleApplication app;
|
|
|
|
|
|
|
|
try {
|
|
|
|
app.run();
|
|
|
|
} catch (const std::runtime_error& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|