Added vertex buffer and staging buffers
This commit is contained in:
+125
-79
@@ -19,114 +19,160 @@
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <glm/glm.hpp>
|
||||
#include <array>
|
||||
|
||||
namespace vapp{
|
||||
// Change here for other presentMode
|
||||
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR;
|
||||
|
||||
// Change here for other presentMode
|
||||
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR;
|
||||
struct Vertex{
|
||||
glm::vec2 pos;
|
||||
glm::vec3 color;
|
||||
|
||||
typedef struct _QueueFamilyIndices{
|
||||
std::optional<uint32_t> graphicsFamily;
|
||||
std::optional<uint32_t> presentFamily;
|
||||
static VkVertexInputBindingDescription getBindingDescription(){
|
||||
VkVertexInputBindingDescription bindingDescription{};
|
||||
bindingDescription.binding = 0;
|
||||
bindingDescription.stride = sizeof(Vertex);
|
||||
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||
return bindingDescription;
|
||||
}
|
||||
|
||||
bool isComplete(){
|
||||
return graphicsFamily.has_value();
|
||||
}
|
||||
} QueueFamilyIndices;
|
||||
static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions(){
|
||||
std::array<VkVertexInputAttributeDescription, 2> attributeDescriptions{};
|
||||
|
||||
typedef struct{
|
||||
VkSurfaceCapabilitiesKHR capabilities;
|
||||
std::vector<VkSurfaceFormatKHR> formats;
|
||||
std::vector<VkPresentModeKHR> presentModes;
|
||||
} SwapChainSupportDetails;
|
||||
attributeDescriptions[0].binding = 0;
|
||||
attributeDescriptions[0].location = 0;
|
||||
attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
|
||||
attributeDescriptions[0].offset = offsetof(Vertex, pos);
|
||||
|
||||
static std::vector<char> readFile(const std::string &filename);
|
||||
attributeDescriptions[1].binding = 0;
|
||||
attributeDescriptions[1].location = 1;
|
||||
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
attributeDescriptions[1].offset = offsetof(Vertex, color);
|
||||
|
||||
class Vulkan{
|
||||
private:
|
||||
return attributeDescriptions;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: Remove
|
||||
const std::vector<Vertex> vertices = {
|
||||
{{0.0f, -0.5f}, {0.0f, 1.0f, 0.0f}},
|
||||
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
|
||||
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
|
||||
};
|
||||
|
||||
struct QueueFamilyIndices{
|
||||
std::optional<uint32_t> graphicsFamily;
|
||||
std::optional<uint32_t> presentFamily;
|
||||
|
||||
bool isComplete(){
|
||||
return graphicsFamily.has_value();
|
||||
}
|
||||
};
|
||||
|
||||
struct SwapChainSupportDetails{
|
||||
VkSurfaceCapabilitiesKHR capabilities;
|
||||
std::vector<VkSurfaceFormatKHR> formats;
|
||||
std::vector<VkPresentModeKHR> presentModes;
|
||||
};
|
||||
|
||||
static std::vector<char> readFile(const std::string& filename);
|
||||
static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescription();
|
||||
|
||||
|
||||
class Vulkan{
|
||||
private:
|
||||
#pragma region Fields
|
||||
GLFWwindow *window;
|
||||
uint32_t _width, _height;
|
||||
GLFWwindow *window;
|
||||
uint32_t _width, _height;
|
||||
|
||||
VkInstance instance;
|
||||
VkDebugUtilsMessengerEXT debugMessenger;
|
||||
VkInstance instance;
|
||||
VkDebugUtilsMessengerEXT debugMessenger;
|
||||
|
||||
VkSurfaceKHR surface;
|
||||
VkSurfaceKHR surface;
|
||||
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
VkDevice device;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
VkDevice device;
|
||||
|
||||
VkQueue graphicsQueue;
|
||||
VkQueue presentQueue;
|
||||
VkQueue graphicsQueue;
|
||||
VkQueue presentQueue;
|
||||
|
||||
VkSwapchainKHR swapChain;
|
||||
std::vector<VkImage> swapChainImages;
|
||||
VkFormat swapChainImageFormat;
|
||||
VkExtent2D swapChainExtent;
|
||||
VkSwapchainKHR swapChain;
|
||||
std::vector<VkImage> swapChainImages;
|
||||
VkFormat swapChainImageFormat;
|
||||
VkExtent2D swapChainExtent;
|
||||
|
||||
std::vector<VkImageView> swapChainImageViews;
|
||||
std::vector<VkImageView> swapChainImageViews;
|
||||
|
||||
VkRenderPass renderPass;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
VkRenderPass renderPass;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
|
||||
VkPipeline graphicsPipeline;
|
||||
VkPipeline graphicsPipeline;
|
||||
|
||||
VkCommandPool commandPool;
|
||||
std::vector<VkCommandBuffer> commandBuffers;
|
||||
VkCommandPool commandPool;
|
||||
std::vector<VkCommandBuffer> commandBuffers;
|
||||
|
||||
std::vector<VkSemaphore> imageAvailableSemaphores, renderFinishedSemaphores;
|
||||
std::vector<VkFence> inFlightFences;
|
||||
std::vector<VkSemaphore> imageAvailableSemaphores, renderFinishedSemaphores;
|
||||
std::vector<VkFence> inFlightFences;
|
||||
|
||||
std::vector<VkFramebuffer> swapChainFramebuffers;
|
||||
std::vector<VkFramebuffer> swapChainFramebuffers;
|
||||
|
||||
const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
uint32_t currentFrame = 0;
|
||||
const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
uint32_t currentFrame = 0;
|
||||
|
||||
VkBuffer vertexBuffer;
|
||||
VkDeviceMemory vertexBufferMemory;
|
||||
#pragma endregion
|
||||
|
||||
bool checkValidationLayerSupport();
|
||||
std::vector<const char*> getRequiredExtensions();
|
||||
bool checkValidationLayerSupport();
|
||||
std::vector<const char*> getRequiredExtensions();
|
||||
|
||||
void createInstance();
|
||||
void setupDebugMessenger();
|
||||
void pickPhysicalDevice();
|
||||
void createLogicalDevice();
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
|
||||
bool checkDeviceExtensionSupport(VkPhysicalDevice device);
|
||||
bool isDeviceSuitable(VkPhysicalDevice device);
|
||||
void createSurface();
|
||||
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 createImageViews();
|
||||
void createGraphicsPipeline();
|
||||
VkShaderModule createShaderModule(const std::vector<char> &code);
|
||||
void createRenderPass();
|
||||
void createFrameBuffers();
|
||||
void createCommandPool();
|
||||
void createCommandBuffers();
|
||||
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
||||
void createSyncObjects();
|
||||
void drawFrame();
|
||||
void cleanupSwapChain();
|
||||
void recreateSwapChain();
|
||||
void initWindow(const char* windowName);
|
||||
void initVulkan();
|
||||
void mainLoop();
|
||||
void cleanup();
|
||||
void createInstance();
|
||||
void setupDebugMessenger();
|
||||
void pickPhysicalDevice();
|
||||
void createLogicalDevice();
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
|
||||
bool checkDeviceExtensionSupport(VkPhysicalDevice device);
|
||||
bool isDeviceSuitable(VkPhysicalDevice device);
|
||||
void createSurface();
|
||||
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 createImageViews();
|
||||
void createGraphicsPipeline();
|
||||
VkShaderModule createShaderModule(const std::vector<char>& code);
|
||||
void createRenderPass();
|
||||
void createFrameBuffers();
|
||||
void createCommandPool();
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
||||
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
|
||||
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
|
||||
void createVertexBuffer();
|
||||
void createCommandBuffers();
|
||||
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
||||
void createSyncObjects();
|
||||
void drawFrame();
|
||||
void cleanupSwapChain();
|
||||
void recreateSwapChain();
|
||||
void initWindow(const char *windowName);
|
||||
void initVulkan();
|
||||
void mainLoop();
|
||||
void cleanup();
|
||||
|
||||
public:
|
||||
#ifdef DEBUG
|
||||
public:
|
||||
#ifdef DEBUG
|
||||
const bool enableValidationLayers = true;
|
||||
#else
|
||||
#else
|
||||
const bool enableValidationLayers = false;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bool framebufferResized = false;
|
||||
bool framebufferResized = false;
|
||||
|
||||
void run(const char* windowName, const uint32_t width = 800, const uint32_t height = 600);
|
||||
};
|
||||
void run(const char *windowName, const uint32_t width = 800, const uint32_t height = 600);
|
||||
};
|
||||
} // vapp
|
||||
|
||||
#endif //VULKAN_APP_H
|
||||
|
||||
Reference in New Issue
Block a user