|
|
|
@ -1,10 +1,7 @@ |
|
|
|
|
#define GLFW_INCLUDE_VULKAN |
|
|
|
|
#include <GLFW/glfw3.h> |
|
|
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS |
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE |
|
|
|
|
#include <glm/vec4.hpp> |
|
|
|
|
#include <glm/mat4x4.hpp> |
|
|
|
|
#include <glm/glm.hpp> |
|
|
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
#include <vector> |
|
|
|
@ -13,6 +10,8 @@ |
|
|
|
|
#include <set> |
|
|
|
|
#include <fstream> |
|
|
|
|
#include <numeric> |
|
|
|
|
#include <chrono> |
|
|
|
|
#include <array> |
|
|
|
|
|
|
|
|
|
const std::vector<const char*> 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<std::chrono::system_clock> start; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
std::vector<char> readFile(const std::string& fileName){ |
|
|
|
|
std::ifstream file(fileName, std::ios::ate | std::ios::binary); |
|
|
|
|
|
|
|
|
@ -72,6 +84,41 @@ std::vector<char> 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<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 { |
|
|
|
|
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; |