diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index ccbdaf4101c5dabb3e9d43788e255eab85ad5776..6a2a20b755b46a5259b18b50278efc5f55fe1e42 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 0000000000000000000000000000000000000000..4964f89e973f38358aa57f564f56d3d4b0c328a9 --- /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 0000000000000000000000000000000000000000..7fd6399f1d3d6c9aa9c01238c55658c2cbc4600a --- /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 0000000000000000000000000000000000000000..b4521235c40fe5fb163bab874560c2f219b7517f --- /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 Binary files /dev/null and b/projects/particle_simulation/shaders/frag.spv differ diff --git a/projects/particle_simulation/shaders/shader.frag b/projects/particle_simulation/shaders/shader.frag new file mode 100644 index 0000000000000000000000000000000000000000..d9fd4602b338b247d7c8d2cd5eff73acc63abce8 --- /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 0000000000000000000000000000000000000000..3985a850cb4832973d27f61aecbdb91b8d4fa99b --- /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 Binary files /dev/null and b/projects/particle_simulation/shaders/vert.spv differ diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f7ad7f3d398f70a1e21aa03754a907fc0098c8f --- /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; +}