// VulkanTutorial.h: 标准系统包含文件的包含文件 // 或项目特定的包含文件。 #pragma once #define GLFW_INCLUDE_VULKAN #include #include #include #include #include #include #include #include #include #include #include #include #include #include VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,const VkAllocationCallbacks* pAllocator,VkDebugUtilsMessengerEXT* pDebugMessenger); void DestroyDebugUtilsMessengerEXT(VkInstance instance,VkDebugUtilsMessengerEXT debugMessenger,const VkAllocationCallbacks* pAllocator); class HelloTriangleApplication { public: const int MAX_FRAME_IN_FLIGHT = 2; HelloTriangleApplication(); ~HelloTriangleApplication(); void run(int Width, int Height) { initWindow(Width,Height); initVulkan(); mainLoop(window); cleanup(window); } private: GLFWwindow* window; VkInstance instance; VkDebugUtilsMessengerEXT debugMessenger; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkDevice device; VkQueue graphicQueue; VkQueue presentQueue; VkSurfaceKHR surface; VkSwapchainKHR swapChain; std::vector swapChainImages; VkFormat swapChainImageFormat; VkExtent2D swapChainExtent; std::vector swapChainImageViews; VkRenderPass renderPass; VkPipelineLayout pipelineLayout; VkPipeline graphicsPipeline; struct QueueFamilyIndices { std::optional graphicsFamily; std::optional presentFamily; bool isComplete() { return graphicsFamily.has_value() && presentFamily.has_value(); } }; struct SwapChainSupportDetails { VkSurfaceCapabilitiesKHR capabilities; std::vector formats; std::vector presentModes; }; std::vector swapChainFramebuffers; VkCommandPool commandPool; std::vector commandBuffers; std::vector imageAvailableSemaphores; std::vector renderFinishedSemaphores; std::vector inFlightFences; bool framebufferResized = false; struct Vertex { glm::vec2 pos; glm::vec3 color; static VkVertexInputBindingDescription getBindingDescription(); static std::array getAttributeDescriptions(); }; const std::vector vertices = { {{0.0f, -0.5f}, {1.0f, 1.0f, 1.0f}}, {{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}}, {{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}} }; VkBuffer vertexBuffer; VkDeviceMemory vertexBufferMemory; void initWindow(int Width, int Height); void createInstance(); void initVulkan(); void mainLoop(GLFWwindow* window); void cleanup(GLFWwindow* window); bool checkValidationLayerSupport(); std::vector getRequiredExtensions(); void showAvailableExtension(); static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageTypes, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData); void setupDebugMessenger(); void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo); void createSurface(); bool checkDeviceExtensionSupport(VkPhysicalDevice device); void pickPhysicalDevice(); bool isDeviceSuitable(VkPhysicalDevice device); QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device); void createLogicalDevice(); SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device); VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector& availableFormats); VkPresentModeKHR chooseSwapPresentMode(const std::vector& availablePresentModes); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities); void createSwapChain(); void cleanupSwapChain(); void recreateSwapChain(); void createImageView(); void createRenderPass(); void createGraphicPipeline(); static std::vector readFile(const std::string& filename); VkShaderModule createShaderModule(const std::vector code); void creatFramebuffers(); void createCommandPool(); void createVertexBuffer(); uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); void createCommandBuffer(); void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex); void drawFrame(); void createSyncObjects(); static void framebufferResizeCallback(GLFWwindow* window, int width, int height); }; HelloTriangleApplication::HelloTriangleApplication() { } HelloTriangleApplication::~HelloTriangleApplication() { }