From c54f9ff1ebf6b9626e7b3e032e55cde55eaf6a5b Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 29 Aug 2024 09:50:26 +0200 Subject: [PATCH] start of vertex buffer --- CMakeLists.txt | 4 ++- shaders/shader.vert | 17 +++------- main.cpp => src/main.cpp | 69 ++++++++++++++++++++++++++++++++++------ src/pipeline.cpp | 2 ++ src/pipeline.hpp | 0 5 files changed, 68 insertions(+), 24 deletions(-) rename main.cpp => src/main.cpp (93%) create mode 100644 src/pipeline.cpp create mode 100644 src/pipeline.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a9c5c78..6f84f1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,11 @@ project(VulkanSimulation) set(CMAKE_CXX_STANDARD 20) -add_executable(VulkanSimulation main.cpp) +file(GLOB SRC_FILES src/*.cpp) +add_executable(VulkanSimulation ${SRC_FILES}) find_package(Vulkan REQUIRED) find_package(glfw3 REQUIRED) +find_package(glm REQUIRED) target_link_libraries(VulkanSimulation glfw Vulkan::Vulkan) diff --git a/shaders/shader.vert b/shaders/shader.vert index 9ada36e..804b6da 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -1,20 +1,11 @@ #version 450 -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); - -vec3 colors[3] = vec3[]( - vec3(1.0, 0.0, 0.0), - vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) -); +layout (location = 0) in vec2 inPosition; +layout (location = 1) in vec3 inColor; layout (location = 0) out vec3 fragColor; void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); - fragColor = colors[gl_VertexIndex]; + gl_Position = vec4(inPosition, 0.0, 1.0); + fragColor = inColor; } \ No newline at end of file diff --git a/main.cpp b/src/main.cpp similarity index 93% rename from main.cpp rename to src/main.cpp index e6037e9..911fd2e 100644 --- a/main.cpp +++ b/src/main.cpp @@ -1,10 +1,7 @@ #define GLFW_INCLUDE_VULKAN #include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include +#include #include #include @@ -13,6 +10,8 @@ #include #include #include +#include +#include const std::vector deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME @@ -55,6 +54,19 @@ bool checkValidationLayerSupport(){ constexpr int MAX_FRAMES_IN_FLIGHT = 2; +class Timer { +public: + explicit Timer(){ + start = std::chrono::system_clock::now(); + } + ~Timer(){ + size_t nanoseconds = (std::chrono::system_clock::now() - start).count(); + printf("Timer: %zu mus\n", nanoseconds / 1000); + } +private: + std::chrono::time_point start; +}; + std::vector readFile(const std::string& fileName){ std::ifstream file(fileName, std::ios::ate | std::ios::binary); @@ -72,6 +84,41 @@ std::vector readFile(const std::string& fileName){ return buffer; } +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 getAttributeDescriptions(){ + std::array attributeDescriptions {}; + + attributeDescriptions[0].binding = 0; + attributeDescriptions[0].location = 0; + attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; + attributeDescriptions[0].offset = offsetof(Vertex, pos); + + attributeDescriptions[1].binding = 0; + attributeDescriptions[1].location = 1; + attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[1].offset = offsetof(Vertex, color); + + return attributeDescriptions; + } +}; + +const std::vector vertices = { + {{0.0, -0.5}, {1, 0, 0}}, + {{0.5, 0.5}, {0, 1, 0}}, + {{-0.5, 0.5}, {0, 0, 1}} +}; + class MyApp { public: void run(){ @@ -83,14 +130,11 @@ public: private: GLFWwindow *window = nullptr; - const uint32_t WIDTH = 800; - const uint32_t HEIGHT = 600; - void initWindow(){ glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); - window = glfwCreateWindow((int)WIDTH, (int)HEIGHT, "Vulkan Simulation", nullptr, nullptr); + window = glfwCreateWindow(800, 600, "Vulkan Simulation", nullptr, nullptr); } void initVulkan(){ createInstance(); @@ -532,10 +576,15 @@ private: VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo}; + auto bindingDescription = Vertex::getBindingDescription(); + auto attributeDescriptions = Vertex::getAttributeDescriptions(); + VkPipelineVertexInputStateCreateInfo vertexInputInfo {}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - vertexInputInfo.vertexBindingDescriptionCount = 0; - vertexInputInfo.vertexAttributeDescriptionCount = 0; + vertexInputInfo.vertexBindingDescriptionCount = 1; + vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; + vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.size(); + vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); VkPipelineInputAssemblyStateCreateInfo inputAssembly {}; inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; diff --git a/src/pipeline.cpp b/src/pipeline.cpp new file mode 100644 index 0000000..2069174 --- /dev/null +++ b/src/pipeline.cpp @@ -0,0 +1,2 @@ +#include "pipeline.hpp" + diff --git a/src/pipeline.hpp b/src/pipeline.hpp new file mode 100644 index 0000000..e69de29