From ec66e754d234fa09f6d5647c499e7b547b9d9024 Mon Sep 17 00:00:00 2001 From: Lars Hoerttrich <larshoerttrich@uni-koblenz.de> Date: Mon, 12 Jul 2021 16:25:07 +0200 Subject: [PATCH] [#91] very basic main.cpp, still has errors --- .../resources/shaders/simpleShader.comp | 18 +++++ projects/neural_network/src/main.cpp | 73 ++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 projects/neural_network/resources/shaders/simpleShader.comp diff --git a/projects/neural_network/resources/shaders/simpleShader.comp b/projects/neural_network/resources/shaders/simpleShader.comp new file mode 100644 index 00000000..f53d0e95 --- /dev/null +++ b/projects/neural_network/resources/shaders/simpleShader.comp @@ -0,0 +1,18 @@ +#version 450 core +#extension GL_ARB_separate_shader_objects : enable + +layout(local_size_x = 1) in; + +layout(std430, binding = 0) coherent buffer buffer_input +{ + int bufferInput[]; +}; + +layout( push_constant ) uniform constants{ + int increment; +}; + +void main() { + uint id = gl_GlobalInvocationID.x; + bufferInput[id] += increment; +} diff --git a/projects/neural_network/src/main.cpp b/projects/neural_network/src/main.cpp index 3383bcdb..0387185c 100644 --- a/projects/neural_network/src/main.cpp +++ b/projects/neural_network/src/main.cpp @@ -1,14 +1,81 @@ #include <iostream> #include <vkcv/Core.hpp> #include <GLFW/glfw3.h> -#include <vkcv/camera/CameraManager.hpp> #include <chrono> -#include <vkcv/asset/asset_loader.hpp> #include <vkcv/shader/GLSLCompiler.hpp> -#include <vkcv/scene/Scene.hpp> int main(int argc, const char** argv) { + const char* applicationName = "Neural-Network"; + + uint32_t windowWidth = 800; + uint32_t windowHeight = 600; + vkcv::Window window = vkcv::Window::create( + applicationName, + windowWidth, + windowHeight, + true + ); + + 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" } + ); + + int input[64] = { 0 }; + vkcv::Buffer<int> inputBuffer = core.createBuffer<int>(vkcv::BufferType::STORAGE, 64); + inputBuffer.fill(input); + + vkcv::PassConfig computePassDefinition({}); + vkcv::PassHandle computePass = core.createPass(computePassDefinition); + + if (!computePass) + { + std::cout << "Error. Could not create renderpass. Exiting." << std::endl; + return EXIT_FAILURE; + } + + // shader path + std::string shaderPathCompute = "resources/shaders/simpleShader.comp"; + + vkcv::shader::GLSLCompiler compiler; + vkcv::ShaderProgram computeShaderProgram{}; + compiler.compile(vkcv::ShaderStage::COMPUTE, shaderPathCompute, [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + computeShaderProgram.addShader(shaderStage, path); + }); + + + vkcv::DescriptorSetHandle computeDescriptorSet = core.createDescriptorSet(computeShaderProgram.getReflectedDescriptors()[0]); + + vkcv::PipelineHandle computePipeline = core.createComputePipeline(computeShaderProgram, { core.getDescriptorSet(computeDescriptorSet).layout }); + + vkcv::DescriptorWrites computeWrites; + computeWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(0,inputBuffer.getHandle()) }; + core.writeDescriptorSet(computeDescriptorSet, computeWrites); + + if (!computePipeline) + { + std::cout << "Error. Could not create compute pipeline. Exiting." << std::endl; + return EXIT_FAILURE; + } + + auto cmdStream = core.createCommandStream(vkcv::QueueType::Compute); + uint32_t computeDispatchCount[3] = {inputBuffer.getSize(),1,1 }; + + vkcv::PushConstants pushConstantsCompute(sizeof(int)); + pushConstantsCompute.appendDrawcall(1); + + core.recordComputeDispatchToCmdStream(cmdStream, + computePipeline, + computeDispatchCount, + { vkcv::DescriptorSetUsage(0,core.getDescriptorSet(computeDescriptorSet).vulkanHandle) }, + pushConstantsCompute); + + core.recordBufferMemoryBarrier(cmdStream, inputBuffer.getHandle()); return 0; -- GitLab