vulkanTutorial/VulkanTutorial.h

245 lines
5.7 KiB
C
Raw Normal View History

2024-02-18 23:22:05 +08:00
// VulkanTutorial.h: 标准系统包含文件的包含文件
// 或项目特定的包含文件。
#pragma once
2024-03-18 23:56:23 +08:00
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
2024-02-21 20:23:59 +08:00
2024-02-18 23:22:05 +08:00
#define GLFW_INCLUDE_VULKAN
#include<GLFW/glfw3.h>
2024-02-20 22:04:01 +08:00
#include<vulkan/vulkan.h>
#include<glm/glm.hpp>
2024-02-18 23:22:05 +08:00
#include <iostream>
2024-02-21 20:23:59 +08:00
#include <optional>
2024-02-18 23:22:05 +08:00
#include <stdexcept>
#include <functional>
#include <cstdlib>
2024-02-21 20:23:59 +08:00
#include <set>
2024-02-29 23:05:44 +08:00
#include <cstdint>
#include <limits>
#include <algorithm>
#include <array>
2024-03-18 23:56:23 +08:00
#include <chrono>
#include <fstream>
2024-02-29 23:05:44 +08:00
2024-02-20 22:04:01 +08:00
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,const VkAllocationCallbacks* pAllocator,VkDebugUtilsMessengerEXT* pDebugMessenger);
void DestroyDebugUtilsMessengerEXT(VkInstance instance,VkDebugUtilsMessengerEXT debugMessenger,const VkAllocationCallbacks* pAllocator);
2024-02-18 23:22:05 +08:00
class HelloTriangleApplication
{
public:
const int MAX_FRAME_IN_FLIGHT = 2;
2024-02-18 23:22:05 +08:00
HelloTriangleApplication();
~HelloTriangleApplication();
void run(int Width, int Height) {
initWindow(Width,Height);
2024-02-18 23:22:05 +08:00
initVulkan();
mainLoop(window);
cleanup(window);
}
private:
2024-02-19 16:30:49 +08:00
2024-02-20 22:04:01 +08:00
GLFWwindow* window;
2024-02-19 16:30:49 +08:00
VkInstance instance;
2024-02-20 22:04:01 +08:00
VkDebugUtilsMessengerEXT debugMessenger;
2024-02-21 20:23:59 +08:00
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkDevice device;
VkQueue graphicQueue;
VkQueue presentQueue;
VkSurfaceKHR surface;
2024-02-29 23:05:44 +08:00
VkSwapchainKHR swapChain;
std::vector<VkImage> swapChainImages;
VkFormat swapChainImageFormat;
VkExtent2D swapChainExtent;
2024-02-29 23:30:33 +08:00
std::vector<VkImageView> swapChainImageViews;
2024-03-04 00:08:40 +08:00
VkRenderPass renderPass;
2024-03-18 23:56:23 +08:00
VkDescriptorSetLayout descriptorSetLayout;
2024-03-03 23:41:06 +08:00
VkPipelineLayout pipelineLayout;
2024-03-05 00:09:31 +08:00
VkPipeline graphicsPipeline;
2024-03-10 16:21:28 +08:00
2024-02-21 20:23:59 +08:00
struct QueueFamilyIndices
{
std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
bool isComplete() {
return graphicsFamily.has_value() && presentFamily.has_value();
}
};
2024-02-29 23:05:44 +08:00
struct SwapChainSupportDetails
{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
};
std::vector<VkFramebuffer> swapChainFramebuffers;
2024-03-09 22:42:37 +08:00
VkCommandPool commandPool;
2024-02-21 20:23:59 +08:00
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkSemaphore> imageAvailableSemaphores;
std::vector<VkSemaphore> renderFinishedSemaphores;
std::vector<VkFence> inFlightFences;
2024-02-21 20:23:59 +08:00
bool framebufferResized = false;
struct Vertex
{
glm::vec2 pos;
glm::vec3 color;
static VkVertexInputBindingDescription getBindingDescription();
static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions();
};
const std::vector<Vertex> vertices = {
2024-03-14 22:09:11 +08:00
{{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
{{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
{{-0.5f,0.5f}, {1.0f,1.0f,1.0f}}
};
const std::vector<uint16_t> indices = {
0,1,2,2,3,0
};
2024-03-18 23:56:23 +08:00
struct UniformBufferObject
{
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
std::vector<VkBuffer> uniformBuffers;
std::vector<VkDeviceMemory> uniformBuffersMemory;
std::vector<void*> uniformBuffersMapped;
2024-03-14 14:58:45 +08:00
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;
2024-03-14 22:09:11 +08:00
VkBuffer indexBuffer;
VkDeviceMemory indexBufferMemory;
2024-03-14 14:58:45 +08:00
2024-03-18 23:56:23 +08:00
VkDescriptorPool descriptorPool;
std::vector<VkDescriptorSet> descriptorSets;
void initWindow(int Width, int Height);
2024-02-18 23:22:05 +08:00
2024-02-19 16:30:49 +08:00
void createInstance();
void initVulkan();
2024-02-18 23:22:05 +08:00
void mainLoop(GLFWwindow* window);
void cleanup(GLFWwindow* window);
2024-02-20 22:04:01 +08:00
bool checkValidationLayerSupport();
std::vector<const char*> getRequiredExtensions();
2024-02-21 20:23:59 +08:00
void showAvailableExtension();
2024-02-20 22:04:01 +08:00
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData);
void setupDebugMessenger();
void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
2024-02-21 20:23:59 +08:00
void createSurface();
2024-02-29 23:05:44 +08:00
bool checkDeviceExtensionSupport(VkPhysicalDevice device);
2024-02-21 20:23:59 +08:00
void pickPhysicalDevice();
bool isDeviceSuitable(VkPhysicalDevice device);
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
void createLogicalDevice();
2024-02-29 23:05:44 +08:00
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
void createSwapChain();
void cleanupSwapChain();
void recreateSwapChain();
2024-02-29 23:05:44 +08:00
void createImageView();
2024-02-29 23:30:33 +08:00
2024-03-04 00:08:40 +08:00
void createRenderPass();
2024-03-18 23:56:23 +08:00
void createDescriptorSetLayout();
void createGraphicPipeline();
static std::vector<char> readFile(const std::string& filename);
2024-03-01 23:29:16 +08:00
VkShaderModule createShaderModule(const std::vector<char> code);
void creatFramebuffers();
2024-03-09 22:42:37 +08:00
void createCommandPool();
2024-03-14 14:58:45 +08:00
void createVertexBuffer();
2024-03-14 22:09:11 +08:00
void createIndexBuffer();
2024-03-18 23:56:23 +08:00
void createUniformBuffers();
void createDescriptorPool();
void createDescriptorSets();
void updateUniformBuffer(uint32_t currentImage);
2024-03-14 16:39:19 +08:00
void createBuffer(VkDeviceSize size,VkBufferUsageFlags usage,VkMemoryPropertyFlags properties,VkBuffer& buffer,VkDeviceMemory& bufferMemory);
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
2024-03-14 14:58:45 +08:00
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
2024-03-09 22:42:37 +08:00
void createCommandBuffer();
2024-03-18 23:56:23 +08:00
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex,uint32_t currentFrame);
2024-03-09 22:42:37 +08:00
void drawFrame();
2024-03-10 16:21:28 +08:00
void createSyncObjects();
static void framebufferResizeCallback(GLFWwindow* window, int width, int height);
2024-02-18 23:22:05 +08:00
};
HelloTriangleApplication::HelloTriangleApplication()
{
}
HelloTriangleApplication::~HelloTriangleApplication()
{
}