2024-02-18 23:22:05 +08:00
|
|
|
|
// VulkanTutorial.cpp: 定义应用程序的入口点。
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "VulkanTutorial.h"
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
const int Width = 800;
|
|
|
|
|
const int Height = 600;
|
|
|
|
|
|
|
|
|
|
const std::vector<const char*> validationLayers = {
|
|
|
|
|
"VK_LAYER_KHRONOS_validation"
|
|
|
|
|
|
|
|
|
|
};
|
2024-02-18 23:22:05 +08:00
|
|
|
|
|
2024-02-29 23:05:44 +08:00
|
|
|
|
const std::vector<const char*> swapchainExtensions = {
|
|
|
|
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
const bool enableValidationLayers = false;
|
|
|
|
|
#else
|
|
|
|
|
const bool enableValidationLayers = true;
|
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
|
|
|
|
//window init
|
2024-02-18 23:22:05 +08:00
|
|
|
|
GLFWwindow* HelloTriangleApplication::initWindow(int Width, int Height) {
|
|
|
|
|
glfwInit();
|
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
2024-02-20 22:04:01 +08:00
|
|
|
|
window = glfwCreateWindow(Width, Height, "vulkan", nullptr, nullptr);
|
2024-02-18 23:22:05 +08:00
|
|
|
|
return window;
|
|
|
|
|
}
|
2024-02-20 22:04:01 +08:00
|
|
|
|
// vulkan instace create
|
2024-02-19 16:30:49 +08:00
|
|
|
|
void HelloTriangleApplication::createInstance() {
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
|
|
|
|
// check validation layers
|
|
|
|
|
if (enableValidationLayers && ! checkValidationLayerSupport())
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("validation layers requsted,but not available");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//setup appliaction info
|
|
|
|
|
VkApplicationInfo appInfo{};
|
2024-02-19 16:30:49 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
// setup createInfo
|
|
|
|
|
VkInstanceCreateInfo createInfo{};
|
2024-02-19 16:30:49 +08:00
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
|
createInfo.pApplicationInfo = &appInfo;
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
auto requiredExtensions = getRequiredExtensions();
|
|
|
|
|
createInfo.enabledExtensionCount = static_cast<uint32_t>(requiredExtensions.size());
|
|
|
|
|
createInfo.ppEnabledExtensionNames = requiredExtensions.data();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* enable glfw extension in creatInfo
|
2024-02-19 16:30:49 +08:00
|
|
|
|
uint32_t glfwExtentionCount = 0;
|
|
|
|
|
const char** glfwExtensions;
|
|
|
|
|
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtentionCount);
|
|
|
|
|
createInfo.enabledExtensionCount = glfwExtentionCount;
|
|
|
|
|
createInfo.ppEnabledExtensionNames = glfwExtensions;
|
2024-02-20 22:04:01 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// enable validation layer if available in createInfo
|
|
|
|
|
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{};
|
|
|
|
|
if (enableValidationLayers)
|
|
|
|
|
{
|
|
|
|
|
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
|
|
|
|
createInfo.ppEnabledLayerNames = validationLayers.data();
|
2024-02-19 16:30:49 +08:00
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
populateDebugMessengerCreateInfo(debugCreateInfo);
|
|
|
|
|
createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*) &debugCreateInfo;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
createInfo.enabledLayerCount = 0;
|
|
|
|
|
|
|
|
|
|
createInfo.pNext = nullptr;
|
|
|
|
|
}
|
2024-02-19 16:30:49 +08:00
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
// throw error in creating instance
|
2024-02-19 16:30:49 +08:00
|
|
|
|
if (vkCreateInstance(&createInfo, nullptr, &instance)!=VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create instance");
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
|
|
|
|
|
2024-02-19 16:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
|
|
|
|
|
2024-02-19 16:30:49 +08:00
|
|
|
|
void HelloTriangleApplication::initVulkan() {
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
2024-02-19 16:30:49 +08:00
|
|
|
|
createInstance();
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
2024-03-03 23:41:06 +08:00
|
|
|
|
//showAvailableExtension();
|
2024-02-21 20:23:59 +08:00
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
setupDebugMessenger();
|
|
|
|
|
|
2024-02-21 20:23:59 +08:00
|
|
|
|
createSurface();
|
|
|
|
|
|
|
|
|
|
pickPhysicalDevice();
|
|
|
|
|
|
|
|
|
|
createLogicalDevice();
|
2024-02-29 23:05:44 +08:00
|
|
|
|
|
|
|
|
|
createSwapChain();
|
2024-02-29 23:30:33 +08:00
|
|
|
|
|
|
|
|
|
creatImageView();
|
2024-03-01 23:28:51 +08:00
|
|
|
|
|
2024-03-04 00:08:40 +08:00
|
|
|
|
createRenderPass();
|
2024-03-01 23:28:51 +08:00
|
|
|
|
createGraphicPipeline();
|
2024-03-08 22:29:35 +08:00
|
|
|
|
|
|
|
|
|
creatFramebuffers();
|
2024-02-19 16:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
|
|
|
|
|
2024-02-18 23:22:05 +08:00
|
|
|
|
void HelloTriangleApplication::mainLoop(GLFWwindow* window){
|
|
|
|
|
|
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
|
{
|
|
|
|
|
glfwPollEvents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
|
|
|
|
|
2024-02-18 23:22:05 +08:00
|
|
|
|
void HelloTriangleApplication::cleanup(GLFWwindow* window) {
|
2024-02-19 16:30:49 +08:00
|
|
|
|
|
2024-03-04 00:08:40 +08:00
|
|
|
|
std::cout << "\nstart to destroy resource\n" << std::endl;
|
2024-03-03 23:41:06 +08:00
|
|
|
|
|
2024-03-08 22:29:35 +08:00
|
|
|
|
|
|
|
|
|
for (auto framebuffer : swapChainFramebuffers)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyFramebuffer(device, framebuffer, nullptr);
|
|
|
|
|
}
|
2024-03-05 00:09:31 +08:00
|
|
|
|
|
|
|
|
|
vkDestroyPipeline(device, graphicsPipeline, nullptr);
|
2024-03-03 23:41:06 +08:00
|
|
|
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
2024-03-04 00:08:40 +08:00
|
|
|
|
vkDestroyRenderPass(device, renderPass, nullptr);
|
2024-03-03 23:41:06 +08:00
|
|
|
|
|
2024-02-29 23:30:33 +08:00
|
|
|
|
for (auto imageView : swapChainImageViews)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyImageView(device, imageView, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 23:05:44 +08:00
|
|
|
|
vkDestroySwapchainKHR(device, swapChain, nullptr);
|
|
|
|
|
|
|
|
|
|
vkDestroySurfaceKHR(instance, surface, nullptr);
|
2024-02-21 20:23:59 +08:00
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
if (enableValidationLayers)
|
|
|
|
|
{
|
|
|
|
|
DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 23:05:44 +08:00
|
|
|
|
vkDestroyDevice(device, nullptr);
|
2024-02-21 20:23:59 +08:00
|
|
|
|
|
2024-02-19 16:30:49 +08:00
|
|
|
|
vkDestroyInstance(instance, nullptr);
|
2024-02-21 20:23:59 +08:00
|
|
|
|
|
2024-02-18 23:22:05 +08:00
|
|
|
|
glfwDestroyWindow(window);
|
|
|
|
|
|
|
|
|
|
glfwTerminate();
|
2024-02-21 20:23:59 +08:00
|
|
|
|
|
|
|
|
|
|
2024-02-18 23:22:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
std::vector<const char*> HelloTriangleApplication::getRequiredExtensions() {
|
|
|
|
|
uint32_t glfwExtensionCount = 0;
|
|
|
|
|
const char** glfwExtensions;
|
|
|
|
|
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
|
|
|
|
|
|
|
|
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
|
|
|
|
|
|
|
|
|
|
if (enableValidationLayers)
|
|
|
|
|
{
|
|
|
|
|
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return extensions;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 20:23:59 +08:00
|
|
|
|
void HelloTriangleApplication::showAvailableExtension()
|
|
|
|
|
{
|
|
|
|
|
// show available extensions
|
|
|
|
|
uint32_t availableExtensionCount = 0;
|
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &availableExtensionCount, nullptr);
|
|
|
|
|
std::vector<VkExtensionProperties> availableExtensions(availableExtensionCount);
|
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &availableExtensionCount, availableExtensions.data());
|
|
|
|
|
std::cout << "available extensions :" << std::endl;
|
|
|
|
|
for (const auto& extension : availableExtensions)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "\t" << extension.extensionName << " ver:" << extension.specVersion << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
VKAPI_ATTR VkBool32 VKAPI_CALL HelloTriangleApplication::debugCallback(
|
|
|
|
|
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|
|
|
|
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
|
|
|
|
|
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|
|
|
|
void* pUserData)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl;
|
|
|
|
|
|
|
|
|
|
return VK_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelloTriangleApplication::setupDebugMessenger()
|
|
|
|
|
{
|
|
|
|
|
if (! enableValidationLayers)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkDebugUtilsMessengerCreateInfoEXT createInfo{};
|
|
|
|
|
populateDebugMessengerCreateInfo(createInfo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CreateDebugUtilsMessengerEXT(instance,&createInfo,nullptr,&debugMessenger)!= VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to set up debug messenger in setupDebugMessenger");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelloTriangleApplication::populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
|
|
|
|
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
|
|
|
|
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
|
|
|
|
createInfo.pfnUserCallback = &debugCallback;
|
|
|
|
|
createInfo.pUserData = nullptr;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 20:23:59 +08:00
|
|
|
|
void HelloTriangleApplication::createSurface()
|
|
|
|
|
{
|
|
|
|
|
if (glfwCreateWindowSurface(instance,window,nullptr,&surface) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create window surface in createSurface()");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 23:05:44 +08:00
|
|
|
|
bool HelloTriangleApplication::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(swapchainExtensions.begin(), swapchainExtensions.end());
|
|
|
|
|
|
|
|
|
|
for (const auto& extension : availableExtensions)
|
|
|
|
|
{
|
|
|
|
|
requiredExtensions.erase(extension.extensionName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return requiredExtensions.empty();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 20:23:59 +08:00
|
|
|
|
void HelloTriangleApplication::pickPhysicalDevice()
|
|
|
|
|
{
|
|
|
|
|
uint32_t deviceCount = 0;
|
|
|
|
|
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
|
|
|
|
if (deviceCount==0)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to find GPUs 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 GPU");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelloTriangleApplication::isDeviceSuitable(VkPhysicalDevice device)
|
|
|
|
|
{
|
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(device);
|
|
|
|
|
|
2024-02-29 23:05:44 +08:00
|
|
|
|
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;
|
2024-02-21 20:23:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HelloTriangleApplication::QueueFamilyIndices HelloTriangleApplication::findQueueFamilies(VkPhysicalDevice device)
|
|
|
|
|
{
|
|
|
|
|
QueueFamilyIndices indices;
|
|
|
|
|
|
|
|
|
|
uint32_t queueFamilyCount = 0;
|
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
|
|
|
|
|
|
|
|
|
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
|
|
|
|
|
|
|
|
|
VkBool32 presentSupport = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
|
|
|
|
for (const auto& queueFamily : queueFamilies)
|
|
|
|
|
{
|
|
|
|
|
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
|
|
|
|
indices.graphicsFamily = i;
|
|
|
|
|
}
|
|
|
|
|
if (indices.isComplete())
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (presentSupport)
|
|
|
|
|
{
|
|
|
|
|
indices.presentFamily = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return indices;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelloTriangleApplication::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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
VkDeviceQueueCreateInfo queueCreateInfo{};
|
|
|
|
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
|
queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value();
|
|
|
|
|
queueCreateInfo.queueCount = 1;
|
|
|
|
|
float queuePriority = 1.0f;
|
|
|
|
|
queueCreateInfo.pQueuePriorities = &queuePriority;
|
|
|
|
|
*/
|
2024-02-29 23:05:44 +08:00
|
|
|
|
VkPhysicalDeviceFeatures deviceFeatures{};
|
2024-02-21 20:23:59 +08:00
|
|
|
|
VkDeviceCreateInfo deviceCreateInfo{};
|
|
|
|
|
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|
|
|
|
deviceCreateInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
|
|
|
|
|
deviceCreateInfo.pQueueCreateInfos = queueCreateInfos.data();
|
|
|
|
|
deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
|
2024-02-29 23:05:44 +08:00
|
|
|
|
deviceCreateInfo.enabledExtensionCount = static_cast<uint32_t>(swapchainExtensions.size());
|
|
|
|
|
deviceCreateInfo.ppEnabledExtensionNames = swapchainExtensions.data();
|
2024-02-21 20:23:59 +08:00
|
|
|
|
//新版本vulkan已不对实例和设备特定验证层做区分,此处保证兼容性
|
2024-02-29 23:05:44 +08:00
|
|
|
|
|
2024-02-21 20:23:59 +08:00
|
|
|
|
if (enableValidationLayers)
|
|
|
|
|
{
|
|
|
|
|
deviceCreateInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
|
|
|
|
deviceCreateInfo.ppEnabledLayerNames = validationLayers.data();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
deviceCreateInfo.enabledLayerCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vkCreateDevice(physicalDevice,&deviceCreateInfo,nullptr,&device) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create logical device");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicQueue);
|
|
|
|
|
vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 23:05:44 +08:00
|
|
|
|
HelloTriangleApplication::SwapChainSupportDetails HelloTriangleApplication::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 HelloTriangleApplication::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
|
|
|
|
|
{
|
|
|
|
|
for (const auto& availableFormat : availableFormats) {
|
|
|
|
|
if (availableFormat.format == VK_FORMAT_B8G8R8_SRGB && availableFormat.colorSpace == VK_COLORSPACE_SRGB_NONLINEAR_KHR)
|
|
|
|
|
{
|
|
|
|
|
return availableFormat;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return availableFormats[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkPresentModeKHR HelloTriangleApplication::chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes)
|
|
|
|
|
{
|
|
|
|
|
for (const auto& availablePresentMode : availablePresentModes) {
|
|
|
|
|
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
|
|
|
|
|
{
|
|
|
|
|
return availablePresentMode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return VK_PRESENT_MODE_FIFO_KHR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkExtent2D HelloTriangleApplication::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities)
|
|
|
|
|
{
|
|
|
|
|
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
|
|
|
|
|
{
|
|
|
|
|
return capabilities.currentExtent;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int width, height;
|
|
|
|
|
glfwGetFramebufferSize(window, &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 HelloTriangleApplication::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 std::runtime_error("failed to create swap chain !");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
|
|
|
|
swapChainImages.resize(imageCount);
|
|
|
|
|
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
|
|
|
|
// store swap format and swap extent to member variable
|
|
|
|
|
swapChainImageFormat = surfaceFormat.format;
|
|
|
|
|
swapChainExtent = extent;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 23:30:33 +08:00
|
|
|
|
void HelloTriangleApplication::creatImageView()
|
|
|
|
|
{
|
|
|
|
|
swapChainImageViews.resize(swapChainImages.size());
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < swapChainImages.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
VkImageViewCreateInfo creatInfo{};
|
|
|
|
|
creatInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
|
creatInfo.image = swapChainImages[i];
|
|
|
|
|
creatInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
|
creatInfo.format = swapChainImageFormat;
|
|
|
|
|
|
|
|
|
|
creatInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
|
creatInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
|
creatInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
|
creatInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
|
|
|
|
|
|
|
|
|
creatInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
|
creatInfo.subresourceRange.baseMipLevel = 0;
|
|
|
|
|
creatInfo.subresourceRange.levelCount = 1;
|
|
|
|
|
creatInfo.subresourceRange.baseArrayLayer = 0;
|
|
|
|
|
creatInfo.subresourceRange.layerCount = 1;
|
|
|
|
|
|
|
|
|
|
if (vkCreateImageView(device,&creatInfo,nullptr,&swapChainImageViews[i]) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to creat image view");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-01 23:28:51 +08:00
|
|
|
|
std::vector<char> HelloTriangleApplication::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!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rsize_t fileSize = (rsize_t)file.tellg();
|
|
|
|
|
std::vector<char> buffer(fileSize);
|
|
|
|
|
|
|
|
|
|
file.seekg(0);
|
|
|
|
|
file.read(buffer.data(), fileSize);
|
|
|
|
|
file.close();
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkShaderModule HelloTriangleApplication::createShaderModule(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 shaderModeul;
|
|
|
|
|
if (vkCreateShaderModule(device,&createInfo,nullptr,&shaderModeul) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create shader module!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shaderModeul;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 22:29:35 +08:00
|
|
|
|
void HelloTriangleApplication::creatFramebuffers()
|
|
|
|
|
{
|
|
|
|
|
swapChainFramebuffers.resize(swapChainImageViews.size());
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (vkCreateFramebuffer(device,&framebufferInfo,nullptr,&swapChainFramebuffers[i]) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create framebuffer in createFramebuffer");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 23:28:51 +08:00
|
|
|
|
|
2024-03-04 00:08:40 +08:00
|
|
|
|
void HelloTriangleApplication::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;// 存储渲染内容到内存
|
|
|
|
|
//stencil数据
|
|
|
|
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;// just dont care
|
|
|
|
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
|
|
|
|
|
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;// render pass 开始前图形具有的布局
|
|
|
|
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;// render pass 完成时自动转换到的布局,此处为准备用交换链进行呈现
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (vkCreateRenderPass(device,&renderPassInfo,nullptr,&renderPass) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create render pass in createRenderPass");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 23:28:51 +08:00
|
|
|
|
void HelloTriangleApplication::createGraphicPipeline()
|
|
|
|
|
{
|
|
|
|
|
auto vertShaderCode = readFile("../../../shaders/triangleVert.spv");
|
|
|
|
|
auto fragShaderCode = readFile("../../..//shaders/triangleFrag.spv");
|
|
|
|
|
|
2024-03-03 23:41:06 +08:00
|
|
|
|
|
|
|
|
|
//std::cout << "vertShader size:" << vertShaderCode.size() << "\nfragShader size:" << fragShaderCode.size() << std::endl;
|
2024-03-01 23:28:51 +08:00
|
|
|
|
|
|
|
|
|
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
|
|
|
|
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
|
|
|
|
|
|
|
|
|
//指定管线vertex阶段
|
|
|
|
|
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";
|
|
|
|
|
//指定管线fragment阶段
|
|
|
|
|
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
|
|
|
|
|
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
2024-03-08 22:29:35 +08:00
|
|
|
|
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
2024-03-01 23:28:51 +08:00
|
|
|
|
fragShaderStageInfo.module = fragShaderModule;
|
|
|
|
|
fragShaderStageInfo.pName = "main";
|
|
|
|
|
|
|
|
|
|
VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo,fragShaderStageInfo };
|
|
|
|
|
|
2024-03-03 23:41:06 +08:00
|
|
|
|
//顶点输入
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//使用动态状态并保留视口和剪刀裁剪状态
|
|
|
|
|
std::vector<VkDynamicState> dynamicStates = {
|
|
|
|
|
VK_DYNAMIC_STATE_VIEWPORT,
|
|
|
|
|
VK_DYNAMIC_STATE_SCISSOR
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState{};
|
|
|
|
|
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
|
|
|
|
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
|
|
|
|
|
dynamicState.pDynamicStates = dynamicStates.data();
|
|
|
|
|
|
|
|
|
|
//描述从顶点绘制哪种几何图形及是否启用primitive restart
|
|
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAsseblyState{};
|
|
|
|
|
inputAsseblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
|
|
|
|
inputAsseblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
|
|
|
inputAsseblyState.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;
|
|
|
|
|
// 创建viewport
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState{};
|
|
|
|
|
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
|
|
|
|
viewportState.viewportCount = 1;
|
|
|
|
|
viewportState.scissorCount = 1;
|
|
|
|
|
|
|
|
|
|
// 光栅化
|
|
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizer{};
|
|
|
|
|
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
|
|
|
|
rasterizer.depthClampEnable = VK_FALSE;// 不丢弃超出近平面和远平面的fragment
|
|
|
|
|
rasterizer.rasterizerDiscardEnable = VK_FALSE;// 启用则几何图形不会通过光栅化,禁用framebuffer的所有输出
|
|
|
|
|
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;// fragment填充多边形区域
|
|
|
|
|
rasterizer.lineWidth = 1.0f;
|
|
|
|
|
|
|
|
|
|
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;// 背面剔除
|
|
|
|
|
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
|
|
|
|
|
|
|
|
|
|
// 根据fragment的斜率偏置深度值
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 深度或模版缓冲区(暂留)
|
|
|
|
|
|
|
|
|
|
// 颜色混合 color blending
|
|
|
|
|
//每个附加帧缓冲区的配置
|
|
|
|
|
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;
|
|
|
|
|
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
|
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
|
|
|
|
|
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
|
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
|
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
|
|
|
|
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
alpha混合
|
|
|
|
|
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 colorBlendState{};
|
|
|
|
|
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
|
|
|
|
colorBlendState.logicOpEnable = VK_FALSE;
|
|
|
|
|
colorBlendState.logicOp = VK_LOGIC_OP_COPY;
|
|
|
|
|
colorBlendState.attachmentCount = 1;
|
|
|
|
|
colorBlendState.pAttachments = &colorBlendAttachment;
|
|
|
|
|
colorBlendState.blendConstants[0] = 0.0f;
|
|
|
|
|
colorBlendState.blendConstants[1] = 0.0f;
|
|
|
|
|
colorBlendState.blendConstants[2] = 0.0f;
|
|
|
|
|
colorBlendState.blendConstants[3] = 0.0f;
|
|
|
|
|
|
|
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
|
|
|
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
|
|
|
|
pipelineLayoutInfo.setLayoutCount = 0;
|
|
|
|
|
pipelineLayoutInfo.pSetLayouts = nullptr;
|
|
|
|
|
pipelineLayoutInfo.pushConstantRangeCount = 0;
|
|
|
|
|
pipelineLayoutInfo.pPushConstantRanges = nullptr;
|
|
|
|
|
|
|
|
|
|
if (vkCreatePipelineLayout(device,&pipelineLayoutInfo,nullptr,&pipelineLayout) != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("failed to create pipeline Layout in createGraphicPipeline");
|
|
|
|
|
}
|
2024-03-01 23:28:51 +08:00
|
|
|
|
|
2024-03-05 00:09:31 +08:00
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineInfo{};
|
|
|
|
|
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
|
|
|
|
pipelineInfo.stageCount = 2;
|
|
|
|
|
pipelineInfo.pStages = shaderStages;
|
|
|
|
|
|
|
|
|
|
pipelineInfo.pVertexInputState = &vertexInputInfo;
|
|
|
|
|
pipelineInfo.pInputAssemblyState = &inputAsseblyState;
|
|
|
|
|
pipelineInfo.pViewportState = &viewportState;
|
|
|
|
|
pipelineInfo.pRasterizationState = &rasterizer;
|
|
|
|
|
pipelineInfo.pMultisampleState = &multisampling;
|
|
|
|
|
pipelineInfo.pDepthStencilState = nullptr;
|
|
|
|
|
pipelineInfo.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineInfo.pDynamicState = &dynamicState;
|
|
|
|
|
//
|
|
|
|
|
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 std::runtime_error("failed to create graphics pipeline in createGraphicPipeline");
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 23:41:06 +08:00
|
|
|
|
// 管道创建完成后销毁着色器模块
|
2024-03-01 23:28:51 +08:00
|
|
|
|
vkDestroyShaderModule(device, fragShaderModule, nullptr);
|
|
|
|
|
vkDestroyShaderModule(device, vertShaderModule, nullptr);
|
|
|
|
|
}
|
2024-02-29 23:30:33 +08:00
|
|
|
|
|
2024-02-21 20:23:59 +08:00
|
|
|
|
|
2024-02-20 22:04:01 +08:00
|
|
|
|
bool HelloTriangleApplication::checkValidationLayerSupport() {
|
|
|
|
|
uint32_t layerCount;
|
|
|
|
|
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
|
|
|
|
|
|
|
|
|
std::vector<VkLayerProperties> availableLayers(layerCount);
|
|
|
|
|
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
|
|
|
|
|
|
|
|
|
// check if validationLayers can be found in availableLayers
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger)
|
|
|
|
|
{
|
|
|
|
|
auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
|
|
|
|
|
|
|
|
|
|
if (func != nullptr)
|
|
|
|
|
{
|
|
|
|
|
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator)
|
|
|
|
|
{
|
|
|
|
|
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
|
|
|
|
|
|
|
|
|
|
if (func!=nullptr)
|
|
|
|
|
{
|
|
|
|
|
func(instance, debugMessenger, pAllocator);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 23:22:05 +08:00
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
HelloTriangleApplication app;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
app.run(Width,Height);
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
2024-02-20 22:04:01 +08:00
|
|
|
|
|
|
|
|
|
|