From 4ed3dea81e551bab42adb048cc3a886e7ad38ecf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Katharina=20Kr=C3=A4mer?= <kkraemer4@uni-koblenz.de>
Date: Tue, 8 Jun 2021 12:04:04 +0200
Subject: [PATCH] [#69] first try to set up shaders and uniforms

---
 projects/CMakeLists.txt                       |   3 +-
 projects/particle_simulation/.gitignore       |   1 +
 projects/particle_simulation/CMakeLists.txt   |  28 ++++
 .../particle_simulation/shaders/compile.bat   |   3 +
 projects/particle_simulation/shaders/frag.spv | Bin 0 -> 612 bytes
 .../particle_simulation/shaders/shader.frag   |  11 ++
 .../particle_simulation/shaders/shader.vert   |  13 ++
 projects/particle_simulation/shaders/vert.spv | Bin 0 -> 1184 bytes
 projects/particle_simulation/src/main.cpp     | 154 ++++++++++++++++++
 9 files changed, 212 insertions(+), 1 deletion(-)
 create mode 100644 projects/particle_simulation/.gitignore
 create mode 100644 projects/particle_simulation/CMakeLists.txt
 create mode 100644 projects/particle_simulation/shaders/compile.bat
 create mode 100644 projects/particle_simulation/shaders/frag.spv
 create mode 100644 projects/particle_simulation/shaders/shader.frag
 create mode 100644 projects/particle_simulation/shaders/shader.vert
 create mode 100644 projects/particle_simulation/shaders/vert.spv
 create mode 100644 projects/particle_simulation/src/main.cpp

diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt
index ccbdaf41..6a2a20b7 100644
--- a/projects/CMakeLists.txt
+++ b/projects/CMakeLists.txt
@@ -2,4 +2,5 @@
 # Add new projects/examples here:
 add_subdirectory(first_triangle)
 add_subdirectory(first_mesh)
-add_subdirectory(cmd_sync_test)
\ No newline at end of file
+add_subdirectory(cmd_sync_test)
+add_subdirectory(particle_simulation)
diff --git a/projects/particle_simulation/.gitignore b/projects/particle_simulation/.gitignore
new file mode 100644
index 00000000..4964f89e
--- /dev/null
+++ b/projects/particle_simulation/.gitignore
@@ -0,0 +1 @@
+particle_simulation
\ No newline at end of file
diff --git a/projects/particle_simulation/CMakeLists.txt b/projects/particle_simulation/CMakeLists.txt
new file mode 100644
index 00000000..7fd6399f
--- /dev/null
+++ b/projects/particle_simulation/CMakeLists.txt
@@ -0,0 +1,28 @@
+cmake_minimum_required(VERSION 3.16)
+project(particle_simulation)
+
+# setting c++ standard for the project
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+# this should fix the execution path to load local files from the project
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+
+# adding source files to the project
+add_executable(particle_simulation src/main.cpp)
+
+# this should fix the execution path to load local files from the project (for MSVC)
+if(MSVC)
+	set_target_properties(particle_simulation PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+	set_target_properties(particle_simulation PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+
+	# in addition to setting the output directory, the working directory has to be set
+	# by default visual studio sets the working directory to the build directory, when using the debugger
+	set_target_properties(particle_simulation PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+endif()
+
+# including headers of dependencies and the VkCV framework
+target_include_directories(particle_simulation SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include})
+
+# linking with libraries from all dependencies and the VkCV framework
+target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera)
diff --git a/projects/particle_simulation/shaders/compile.bat b/projects/particle_simulation/shaders/compile.bat
new file mode 100644
index 00000000..b4521235
--- /dev/null
+++ b/projects/particle_simulation/shaders/compile.bat
@@ -0,0 +1,3 @@
+%VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv
+%VULKAN_SDK%\Bin32\glslc.exe shader.frag -o frag.spv
+pause
\ No newline at end of file
diff --git a/projects/particle_simulation/shaders/frag.spv b/projects/particle_simulation/shaders/frag.spv
new file mode 100644
index 0000000000000000000000000000000000000000..df52203662c6efe30f9b311f06075e73074edb6a
GIT binary patch
literal 612
zcmYk2-Acni5QV2rx7ODFDe7H`^in8Zs0gA^Mb?WzK_6g=$%2uXg!~A;o=@eC;5kVy
zvM@V4XTF`8U8ld^6Y0rVrgA9FT1p>^$V7&<*WRnQDavqjd38ZzCQc2KnS&X?(?0zu
zVPYS)07DJMadJ>m<Odf9$ZF4QUmi?hGe7rBYl?S2w7E&&K5S4HWX5FlZl}E`6J(hw
z%J0~kI7+Muqa0N9Wyy^?T$BWH6?OpyqC1xoRnL}I<zpJBIYxq_&`l&rlcMw!w#SPa
zjrpo272a?_0ZXGQ-?*wHk>?J-ZZ!>_b@}ci?DS{#Pc+&I&O>M}>=gC*PmDRQi+u#o
zU#5QB#_UOLg`KsTBRhd-jv86UoR!)!;wiif>%q?3c!o0<@6X}Mvfee`!dW>77yJD}
eQSe*rWEU{jXN^0Mytjl=zsC*MV*H1WH?V*3FFg|g

literal 0
HcmV?d00001

diff --git a/projects/particle_simulation/shaders/shader.frag b/projects/particle_simulation/shaders/shader.frag
new file mode 100644
index 00000000..d9fd4602
--- /dev/null
+++ b/projects/particle_simulation/shaders/shader.frag
@@ -0,0 +1,11 @@
+#version 450
+#extension GL_ARB_separate_shader_objects : enable
+
+layout(location = 0) out vec4 outColor;
+
+layout(set=0, binding=0) uniform vec4 uColor;
+
+void main()
+{
+	outColor = uColor;
+}
\ No newline at end of file
diff --git a/projects/particle_simulation/shaders/shader.vert b/projects/particle_simulation/shaders/shader.vert
new file mode 100644
index 00000000..3985a850
--- /dev/null
+++ b/projects/particle_simulation/shaders/shader.vert
@@ -0,0 +1,13 @@
+#version 450 core
+#extension GL_ARB_separate_shader_objects : enable
+
+layout (location = 0) in vec3 position;
+
+layout( push_constant ) uniform constants{
+    mat4 mvp;
+};
+
+void main()
+{
+	gl_Position = mvp * vec4(position, 1.0);
+}
\ No newline at end of file
diff --git a/projects/particle_simulation/shaders/vert.spv b/projects/particle_simulation/shaders/vert.spv
new file mode 100644
index 0000000000000000000000000000000000000000..1d8d780bcb1a3dff229389902f9d624e83500380
GIT binary patch
literal 1184
zcmYk4TW`}q5QVo%+)GMZO6dhC!KIH#6%T-r5TJ6A`w*#xO1!RIX9bH~$FkiDc;YYc
zm-tn@AaOolLN=Pr%$c*ZXLjB19WEKOV1{PR9GYfrnw}72R?TAVqw)FpsH`SOPo6%8
zv2OY`5zdCucCCTzGWHqqn(U6OuU}98w}=C?VxqA<{`kh0E{}`2a<=>uPh4TA7hhdc
zm2ie|qSMo8{N5&cZp-R>>TK#WXD7Z8%U?Tq%X2)RCF#{<o)CyJuP4wPtJ3i~wP&vQ
z<ci9DQ!2E<iK$IXU-@Y!bgAYuUyb{$I`=<xGJBdDbE)<=_4zwrR&kcNS&iGFc9o{H
zTA;h&URZ&CoASvtV<)3)@1Vx0UtZ^8^vxED7JTkA?*uA75ynic&UaYzcD(9;9(v)S
zt>Y1caaWNl#PCk$v;=kD=v6rTM(Du|EWUu7GJ1f~BT^4Ek7TSZq2LGNa4zwVqDA>o
zwktk;$kDT{|7H15#(LD|D~g$GU-?iTKG;YR`nw*~uu!}k^4+X2+MKrl<DB)H*YxEa
z@(melPdT&E7hNOmVn#4}-;(EiFnW{oZeY}Jw=sNX+?7W&XH(ykUyy;bpPYLGLkrw{
zGGl(ehJT=lz36$SH{c%8#RC5$gf~0ZKWA^t(8D}0#YD%B44j;E@5(sygZ6OVev8S?
zu=_2Bu8zmsvKI|!QiP@lGWNr#j`m;bOD&XfHXQUZ%R?D&fxcIofIO7_)eStC{R3()
BV#NRe

literal 0
HcmV?d00001

diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp
new file mode 100644
index 00000000..4f7ad7f3
--- /dev/null
+++ b/projects/particle_simulation/src/main.cpp
@@ -0,0 +1,154 @@
+#include <iostream>
+#include <vkcv/Core.hpp>
+#include <GLFW/glfw3.h>
+#include <vkcv/camera/CameraManager.hpp>
+#include <chrono>
+
+int main(int argc, const char** argv) {
+    const char* applicationName = "First Triangle";
+
+    const int windowWidth = 800;
+    const int windowHeight = 600;
+    vkcv::Window window = vkcv::Window::create(
+            applicationName,
+            windowWidth,
+            windowHeight,
+            false
+    );
+
+    vkcv::CameraManager cameraManager(window, windowWidth, windowHeight);
+
+    window.initEvents();
+
+    vkcv::Core core = vkcv::Core::create(
+            window,
+            applicationName,
+            VK_MAKE_VERSION(0, 0, 1),
+            { vk::QueueFlagBits::eTransfer,vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute },
+            {},
+            { "VK_KHR_swapchain" }
+    );
+
+    const auto& context = core.getContext();
+    const vk::Instance& instance = context.getInstance();
+    const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice();
+    const vk::Device& device = context.getDevice();
+
+    struct vec3 {
+        float x, y, z;
+    };
+
+    const size_t n = 5027;
+
+    auto testBuffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL);
+    vec3 vec_data[n];
+
+    for (size_t i = 0; i < n; i++) {
+        vec_data[i] = { 42, static_cast<float>(i), 7 };
+    }
+
+    testBuffer.fill(vec_data);
+
+    auto triangleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL);
+    uint16_t indices[3] = { 0, 1, 2 };
+    triangleIndexBuffer.fill(&indices[0], sizeof(indices));
+
+
+    vkcv::SamplerHandle sampler = core.createSampler(
+            vkcv::SamplerFilterType::NEAREST,
+            vkcv::SamplerFilterType::NEAREST,
+            vkcv::SamplerMipmapMode::NEAREST,
+            vkcv::SamplerAddressMode::REPEAT
+    );
+
+    std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl;
+
+    switch (physicalDevice.getProperties().vendorID) {
+        case 0x1002: std::cout << "Running AMD huh? You like underdogs, are you a Linux user?" << std::endl; break;
+        case 0x10DE: std::cout << "An NVidia GPU, how predictable..." << std::endl; break;
+        case 0x8086: std::cout << "Poor child, running on an Intel GPU, probably integrated..."
+                                  "or perhaps you are from the future with a dedicated one?" << std::endl; break;
+        case 0x13B5: std::cout << "ARM? What the hell are you running on, next thing I know you're trying to run Vulkan on a leg..." << std::endl; break;
+        default: std::cout << "Unknown GPU vendor?! Either you're on an exotic system or your driver is broken..." << std::endl;
+    }
+
+    // an example attachment for passes that output to the window
+    const vkcv::AttachmentDescription present_color_attachment(
+            vkcv::AttachmentLayout::UNDEFINED,
+            vkcv::AttachmentLayout::COLOR_ATTACHMENT,
+            vkcv::AttachmentLayout::PRESENTATION,
+            vkcv::AttachmentOperation::STORE,
+            vkcv::AttachmentOperation::CLEAR,
+            core.getSwapchainImageFormat());
+
+    vkcv::PassConfig particlePassDefinition({ present_color_attachment });
+    vkcv::PassHandle particlePass = core.createPass(particlePassDefinition);
+
+    if (!particlePass)
+    {
+        std::cout << "Error. Could not create renderpass. Exiting." << std::endl;
+        return EXIT_FAILURE;
+    }
+
+    vkcv::ShaderProgram particleShaderProgram{};
+    particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv"));
+    particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv"));
+    particleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX);
+    particleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT);
+
+    vkcv::DescriptorSetConfig setConfig({
+        vkcv::DescriptorBinding(vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT),
+    });
+    vkcv::ResourcesHandle set = core.createResourceDescription({setConfig});
+
+    const vkcv::PipelineConfig particlePipelineDefinition(
+            particleShaderProgram,
+            windowWidth,
+            windowHeight,
+            particlePass,
+            {},
+            {core.getDescriptorSetLayout(set, 0)});
+    vkcv::PipelineHandle particlePipeline = core.createGraphicsPipeline(particlePipelineDefinition);
+    vkcv::Buffer<glm_vec4> color = core.createBuffer<glm_vec4>(
+            vkcv::BufferType::UNIFORM,
+            1
+            );
+
+    vkcv::DescriptorWrites setWrites;
+    setWrites.uniformBufferWrites = {vkcv::UniformBufferDescriptorWrite(0,color.getHandle())};
+    core.writeResourceDescription(set,0,setWrites);
+
+    if (!particlePipeline)
+    {
+        std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl;
+        return EXIT_FAILURE;
+    }
+
+    std::vector<vkcv::VertexBufferBinding> vertexBufferBindings;
+
+    auto start = std::chrono::system_clock::now();
+    while (window.isWindowOpen())
+    {
+        core.beginFrame();
+        window.pollEvents();
+        auto end = std::chrono::system_clock::now();
+        auto deltatime = end - start;
+        start = end;
+        cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count());
+        const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView();
+
+        core.renderMesh(
+                particlePass,
+                particlePipeline,
+                sizeof(mvp),
+                &mvp,
+                vertexBufferBindings,
+                triangleIndexBuffer.getHandle(),
+                3,
+                vkcv::ResourcesHandle(),
+                0);
+
+        core.endFrame();
+    }
+    return 0;
+}
-- 
GitLab