Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- // Definitions
- #define MANUALPHYSICALDEVICE 1
- #define LOGEXTENSIONS 0
- // C++ Includes
- #include <algorithm>
- #include <cstdlib>
- #include <functional>
- #include <iostream>
- #include <optional>
- #include <set>
- #include <stdexcept>
- #include <vector>
- #include <fstream>
- #include <chrono>
- // External Includes
- #define GLFW_INCLUDE_VULKAN
- #define GLM_FORCE_RADIANS
- #include <spdlog/spdlog.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <vulkan/vulkan.h>
- #include <shaderc/shaderc.hpp>
- static std::vector<char> readFile(const std::string& filename)
- {
- std::ifstream file(filename, std::ios::ate | std::ios::binary);
- if (!file.is_open())
- spdlog::info("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;
- }
- static std::string readFile_string(const char* filename)
- {
- std::ifstream t(filename);
- std::string str;
- t.seekg(0, std::ios::end);
- str.reserve(t.tellg());
- t.seekg(0, std::ios::beg);
- str.assign((std::istreambuf_iterator<char>(t)),
- std::istreambuf_iterator<char>());
- return str;
- }
- class Timer {
- public:
- Timer() : beg_(clock_::now()) {}
- void reset() { beg_ = clock_::now(); }
- double elapsed() const {
- return std::chrono::duration_cast<second_> (clock_::now() - beg_).count();
- }
- private:
- typedef std::chrono::high_resolution_clock clock_;
- typedef std::chrono::duration<double, std::ratio<1> > second_;
- std::chrono::time_point<clock_> beg_;
- };
- struct Vertex
- {
- glm::vec2 pos;
- glm::vec3 color;
- static VkVertexInputBindingDescription getBindingDescription()
- {
- VkVertexInputBindingDescription bindingDescription = {};
- bindingDescription.binding = 0;
- bindingDescription.stride = sizeof(Vertex);
- bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
- return bindingDescription;
- }
- static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions()
- {
- std::array<VkVertexInputAttributeDescription, 2> attributeDescription;
- attributeDescription[0].binding = 0;
- attributeDescription[0].location = 0;
- attributeDescription[0].format = VK_FORMAT_R32G32_SFLOAT;
- attributeDescription[0].offset = offsetof(Vertex, pos);
- attributeDescription[1].binding = 0;
- attributeDescription[1].location = 1;
- attributeDescription[1].format = VK_FORMAT_R32G32B32_SFLOAT;
- attributeDescription[1].offset = offsetof(Vertex, color);
- return attributeDescription;
- }
- };
- struct UniformBufferObject
- {
- alignas(16) glm::mat4 model;
- alignas(16) glm::mat4 view;
- alignas(16) glm::mat4 proj;
- };
- struct QueueFamilyIndices
- {
- std::optional<uint32_t> graphicsFamily;
- std::optional<uint32_t> presentFamily;
- bool isComplete()
- {
- return graphicsFamily.has_value() && presentFamily.has_value();
- }
- };
- struct SwapChainSupportDetails
- {
- VkSurfaceCapabilitiesKHR capabilities;
- std::vector<VkSurfaceFormatKHR> formats;
- std::vector<VkPresentModeKHR> presentModes;
- };
- const int WIDTH = 800;
- const int HEIGHT = 600;
- const int MAX_FRAMES_IN_FLIGHT = 2;
- const std::vector<Vertex> vertices
- {
- // Top-Left
- {
- // Position
- { -0.5f, -0.5f },
- // Color
- { 1.0f, 0.0f, 0.0f }
- },
- // Top-Right
- {
- // Position
- { 0.5f, -0.5f },
- // Color
- { 0.0f, 1.0f, 0.0f }
- },
- // Bottom-Right
- {
- // Position
- { 0.5f, 0.5f },
- // Color
- { 0.0f, 0.0f, 1.0f }
- },
- // Bottom-Left
- {
- // Position
- { -0.5f, 0.5f },
- // Color
- { 1.0f, 1.0f, 1.0f }
- }
- };
- const std::vector<uint16_t> indices =
- {
- 0, 1, 2,
- 2, 3, 0
- };
- #ifdef NDEBUG
- const bool enableValidationLayers = false;
- #else
- const bool enableValidationLayers = true;
- #endif
- const std::vector <const char*> validationLayers =
- {
- "VK_LAYER_LUNARG_standard_validation"
- };
- const std::vector<const char*> deviceExtensions =
- {
- VK_KHR_SWAPCHAIN_EXTENSION_NAME
- };
- // Class
- class Application
- {
- // Private members
- private:
- GLFWwindow* m_window;
- int m_framebufferWidth;
- int m_framebufferHeight;
- VkInstance m_instance;
- VkDebugUtilsMessengerEXT m_debugMessenger;
- VkSurfaceKHR m_surface;
- VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
- VkDevice m_device;
- VkQueue m_graphicsQueue;
- VkQueue m_presentQueue;
- QueueFamilyIndices m_indices;
- VkSwapchainKHR m_swapChain;
- std::vector<VkImage> m_swapChainImages;
- VkFormat m_swapChainImageFormat;
- VkExtent2D m_swapChainExtent;
- std::vector<VkImageView> m_swapChainImageViews;
- VkRenderPass m_renderPass;
- VkDescriptorSetLayout m_descriptorSetLayout;
- VkPipelineLayout m_pipelineLayout;
- VkPipeline m_graphicsPipeline;
- std::vector<VkFramebuffer> m_swapChainFramebuffers;
- VkCommandPool m_commandPool;
- VkCommandPool m_tempCommandPool;
- std::vector<VkCommandBuffer> m_commandBuffers;
- std::vector<VkSemaphore> m_imageAvailableSemaphores;
- std::vector<VkSemaphore> m_renderFinishedSemaphores;
- std::vector<VkFence> m_inFlightFences;
- VkBuffer m_vertexBuffer;
- VkDeviceMemory m_vertexBufferMemory;
- VkBuffer m_indexBuffer;
- VkDeviceMemory m_indexBufferMemory;
- std::vector<VkBuffer> m_uniformBuffers;
- std::vector<VkDeviceMemory> m_uniformBuffersMemory;
- VkDescriptorPool m_descriptorPool;
- std::vector<VkDescriptorSet> m_descriptorSets;
- size_t m_currentFrame = 0;
- bool m_framebufferResized = false;
- bool m_recreatingSwapChain = false;
- // Private functions
- private:
- void initWindow();
- void initVulkan();
- void createInstance();
- void setupDebugMessenger();
- void createSurface();
- void pickPhysicalDevice();
- void createLogicalDevice();
- void createSwapChain();
- void createImageViews();
- void createRenderPass();
- void createDescriptorSetLayout();
- void createGraphicsPipeline();
- void createFramebuffers();
- void createCommandPools();
- void createVertexBuffers();
- void createIndexBuffer();
- void createUniformBuffers();
- void createDescriptorPool();
- void createDescriptorSets();
- void createCommandBuffers();
- void createSyncObjects();
- void mainLoop();
- void drawFrame();
- void cleanup();
- void cleanupSwapChain();
- void recreateSwapChain();
- bool checkValidationLayerSupport();
- bool isDeviceSuitable(VkPhysicalDevice device);
- bool checkDeviceExtensionSupport(VkPhysicalDevice device);
- std::vector<const char*> getRequiredExtensions();
- VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger);
- void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator);
- QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
- SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
- VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
- VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes);
- VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
- VkShaderModule createShaderModule(const std::vector<char>& code);
- void compileShaders(const char* vertPath, const char* fragPath);
- void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory);
- void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
- uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
- void updateUniformBuffers(uint32_t currentImage);
- static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
- VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
- VkDebugUtilsMessageTypeFlagsEXT messageType,
- const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
- void* pUserData
- );
- static void framebufferResizeCallback(GLFWwindow* window, int width, int height);
- // Public functions
- public:
- Application();
- ~Application();
- void init();
- void run();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement