start of vertex buffer

main
Benjamin Kraft 3 weeks ago
parent 3e43d096fc
commit c54f9ff1eb
  1. 4
      CMakeLists.txt
  2. 17
      shaders/shader.vert
  3. 69
      src/main.cpp
  4. 2
      src/pipeline.cpp
  5. 0
      src/pipeline.hpp

@ -3,9 +3,11 @@ project(VulkanSimulation)
set(CMAKE_CXX_STANDARD 20) 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(Vulkan REQUIRED)
find_package(glfw3 REQUIRED) find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
target_link_libraries(VulkanSimulation glfw Vulkan::Vulkan) target_link_libraries(VulkanSimulation glfw Vulkan::Vulkan)

@ -1,20 +1,11 @@
#version 450 #version 450
vec2 positions[3] = vec2[]( layout (location = 0) in vec2 inPosition;
vec2(0.0, -0.5), layout (location = 1) in vec3 inColor;
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) out vec3 fragColor; layout (location = 0) out vec3 fragColor;
void main() { void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); gl_Position = vec4(inPosition, 0.0, 1.0);
fragColor = colors[gl_VertexIndex]; fragColor = inColor;
} }

@ -1,10 +1,7 @@
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS #include <glm/glm.hpp>
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -13,6 +10,8 @@
#include <set> #include <set>
#include <fstream> #include <fstream>
#include <numeric> #include <numeric>
#include <chrono>
#include <array>
const std::vector<const char*> deviceExtensions = { const std::vector<const char*> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME VK_KHR_SWAPCHAIN_EXTENSION_NAME
@ -55,6 +54,19 @@ bool checkValidationLayerSupport(){
constexpr int MAX_FRAMES_IN_FLIGHT = 2; 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<std::chrono::system_clock> start;
};
std::vector<char> readFile(const std::string& fileName){ std::vector<char> readFile(const std::string& fileName){
std::ifstream file(fileName, std::ios::ate | std::ios::binary); std::ifstream file(fileName, std::ios::ate | std::ios::binary);
@ -72,6 +84,41 @@ std::vector<char> readFile(const std::string& fileName){
return buffer; 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<VkVertexInputAttributeDescription, 2> getAttributeDescriptions(){
std::array<VkVertexInputAttributeDescription, 2> 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<Vertex> vertices = {
{{0.0, -0.5}, {1, 0, 0}},
{{0.5, 0.5}, {0, 1, 0}},
{{-0.5, 0.5}, {0, 0, 1}}
};
class MyApp { class MyApp {
public: public:
void run(){ void run(){
@ -83,14 +130,11 @@ public:
private: private:
GLFWwindow *window = nullptr; GLFWwindow *window = nullptr;
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;
void initWindow(){ void initWindow(){
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); 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(){ void initVulkan(){
createInstance(); createInstance();
@ -532,10 +576,15 @@ private:
VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo}; VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo};
auto bindingDescription = Vertex::getBindingDescription();
auto attributeDescriptions = Vertex::getAttributeDescriptions();
VkPipelineVertexInputStateCreateInfo vertexInputInfo {}; VkPipelineVertexInputStateCreateInfo vertexInputInfo {};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = 0; vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.vertexAttributeDescriptionCount = 0; vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.size();
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
VkPipelineInputAssemblyStateCreateInfo inputAssembly {}; VkPipelineInputAssemblyStateCreateInfo inputAssembly {};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;

@ -0,0 +1,2 @@
#include "pipeline.hpp"
Loading…
Cancel
Save