diff --git a/projects/particle_simulation/CMakeLists.txt b/projects/particle_simulation/CMakeLists.txt index 2e62c66e72f4eea9434acf4b1c4f26f739b5d989..35af9b32870ccafce13397c75c503197eb453c36 100644 --- a/projects/particle_simulation/CMakeLists.txt +++ b/projects/particle_simulation/CMakeLists.txt @@ -24,7 +24,7 @@ if(MSVC) 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}) +target_include_directories(particle_simulation SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include}) # linking with libraries from all dependencies and the VkCV framework -target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera) +target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera vkcv_shader_compiler) diff --git a/projects/particle_simulation/shaders/comp1.spv b/projects/particle_simulation/shaders/comp1.spv deleted file mode 100644 index b07868be1fc5595cda5ebc37604ba185c6ac29a3..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/comp1.spv and /dev/null differ diff --git a/projects/particle_simulation/shaders/comp_space.spv b/projects/particle_simulation/shaders/comp_space.spv deleted file mode 100644 index 1064be63178218c2bae7ccb62ade34a281518a83..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/comp_space.spv and /dev/null differ diff --git a/projects/particle_simulation/shaders/comp_water.spv b/projects/particle_simulation/shaders/comp_water.spv deleted file mode 100644 index b07868be1fc5595cda5ebc37604ba185c6ac29a3..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/comp_water.spv and /dev/null differ diff --git a/projects/particle_simulation/shaders/compile.bat b/projects/particle_simulation/shaders/compile.bat deleted file mode 100644 index db5496e208e398ffed3a4cb6e072f089c6f9cafc..0000000000000000000000000000000000000000 --- a/projects/particle_simulation/shaders/compile.bat +++ /dev/null @@ -1,6 +0,0 @@ -%VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv -%VULKAN_SDK%\Bin32\glslc.exe shader_water.frag -o frag_water.spv -%VULKAN_SDK%\Bin32\glslc.exe shader_water.comp -o comp_water.spv -%VULKAN_SDK%\Bin32\glslc.exe shader_space.frag -o frag_space.spv -%VULKAN_SDK%\Bin32\glslc.exe shader_space.comp -o comp_space.spv -pause \ No newline at end of file diff --git a/projects/particle_simulation/shaders/frag.spv b/projects/particle_simulation/shaders/frag.spv deleted file mode 100644 index 0ce71df61bc127c7d057d290b78871593ee4f78e..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/frag.spv and /dev/null differ diff --git a/projects/particle_simulation/shaders/frag_space.spv b/projects/particle_simulation/shaders/frag_space.spv deleted file mode 100644 index 8acf8a4295ba661f1b7a893d3c8dd508a4b168b6..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/frag_space.spv and /dev/null differ diff --git a/projects/particle_simulation/shaders/frag_water.spv b/projects/particle_simulation/shaders/frag_water.spv deleted file mode 100644 index 406e44df35757621d0688d37bd93cb83f5f04c24..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/frag_water.spv and /dev/null differ diff --git a/projects/particle_simulation/shaders/tonemapping.comp b/projects/particle_simulation/shaders/tonemapping.comp new file mode 100644 index 0000000000000000000000000000000000000000..4ba7f2c661a0549e9852e30b9140811852ddbdbd --- /dev/null +++ b/projects/particle_simulation/shaders/tonemapping.comp @@ -0,0 +1,19 @@ +#version 440 + +layout(set=0, binding=0, rgba16f) uniform image2D inImage; +layout(set=0, binding=1, rgba8) uniform image2D outImage; + + +layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + +void main(){ + + if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(inImage)))){ + return; + } + ivec2 uv = ivec2(gl_GlobalInvocationID.xy); + vec3 linearColor = imageLoad(inImage, uv).rgb; + vec3 tonemapped = linearColor / (linearColor + 1); // reinhard tonemapping + vec3 gammaCorrected = pow(tonemapped, vec3(1.f / 2.2f)); + imageStore(outImage, uv, vec4(gammaCorrected, 0.f)); +} \ No newline at end of file diff --git a/projects/particle_simulation/shaders/vert.spv b/projects/particle_simulation/shaders/vert.spv deleted file mode 100644 index e5a23d663612f70bbbafca156f9a49215dc2fd72..0000000000000000000000000000000000000000 Binary files a/projects/particle_simulation/shaders/vert.spv and /dev/null differ diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp index 262aa32d11e098b6371552afd547459c5682384b..b6fad8d76fedc9dfa473e4db3261f12aa51e0ea5 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -7,6 +7,7 @@ #include <random> #include <glm/gtc/matrix_access.hpp> #include <time.h> +#include <vkcv/shader/GLSLCompiler.hpp> int main(int argc, const char **argv) { const char *applicationName = "Particlesystem"; @@ -36,12 +37,12 @@ int main(int argc, const char **argv) { uint16_t indices[3] = {0, 1, 2}; particleIndexBuffer.fill(&indices[0], sizeof(indices)); - + vk::Format colorFormat = vk::Format::eR16G16B16A16Sfloat; // an example attachment for passes that output to the window const vkcv::AttachmentDescription present_color_attachment( vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::CLEAR, - core.getSwapchain().getFormat()); + colorFormat); vkcv::PassConfig particlePassDefinition({present_color_attachment}); @@ -59,8 +60,11 @@ int main(int argc, const char **argv) { // use space or use water bool useSpace = true; + vkcv::shader::GLSLCompiler compiler; vkcv::ShaderProgram computeShaderProgram{}; - computeShaderProgram.addShader(vkcv::ShaderStage::COMPUTE, std::filesystem::path(useSpace? "shaders/comp_space.spv" : "shaders/comp_water.spv")); + compiler.compile(vkcv::ShaderStage::COMPUTE, useSpace ? "shaders/shader_space.comp" : "shaders/shader_water.comp", [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + computeShaderProgram.addShader(shaderStage, path); + }); vkcv::DescriptorSetHandle computeDescriptorSet = core.createDescriptorSet(computeShaderProgram.getReflectedDescriptors()[0]); @@ -73,8 +77,12 @@ int main(int argc, const char **argv) { const vkcv::VertexLayout computeLayout(computeBindings); vkcv::ShaderProgram particleShaderProgram{}; - particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv")); - particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path( useSpace? "shaders/frag_space.spv" : "shaders/frag_water.spv")); + compiler.compile(vkcv::ShaderStage::VERTEX, "shaders/shader.vert", [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + particleShaderProgram.addShader(shaderStage, path); + }); + compiler.compile(vkcv::ShaderStage::FRAGMENT, useSpace ? "shaders/shader_space.frag" : "shaders/shader_water.frag", [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + particleShaderProgram.addShader(shaderStage, path); + }); vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet( particleShaderProgram.getReflectedDescriptors()[0]); @@ -208,6 +216,23 @@ int main(int argc, const char **argv) { cameraManager.getCamera(camIndex1).setPosition(glm::vec3(0.0f, 0.0f, -2.0f)); cameraManager.getCamera(camIndex1).setCenter(glm::vec3(0.0f, 0.0f, 0.0f)); + vkcv::ImageHandle colorBuffer = core.createImage(colorFormat, windowWidth, windowHeight, 1, false, true, true).getHandle(); + window.e_resize.add([&](int width, int height) { + windowWidth = width; + windowHeight = height; + colorBuffer = core.createImage(colorFormat, windowWidth, windowHeight, 1, false, true, true).getHandle(); + }); + + vkcv::ShaderProgram tonemappingShader; + compiler.compile(vkcv::ShaderStage::COMPUTE, "shaders/tonemapping.comp", [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + tonemappingShader.addShader(shaderStage, path); + }); + + vkcv::DescriptorSetHandle tonemappingDescriptor = core.createDescriptorSet(tonemappingShader.getReflectedDescriptors()[0]); + vkcv::PipelineHandle tonemappingPipe = core.createComputePipeline( + tonemappingShader, + { core.getDescriptorSet(tonemappingDescriptor).layout }); + std::uniform_real_distribution<float> rdm = std::uniform_real_distribution<float>(0.95f, 1.05f); std::default_random_engine rdmEngine; while (window.isWindowOpen()) { @@ -254,7 +279,30 @@ int main(int argc, const char **argv) { particlePipeline, pushConstantDataDraw, {drawcalls}, - {swapchainInput}); + { colorBuffer }); + + core.prepareImageForStorage(cmdStream, colorBuffer); + core.prepareImageForStorage(cmdStream, swapchainInput); + + vkcv::DescriptorWrites tonemappingDescriptorWrites; + tonemappingDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(0, colorBuffer), + vkcv::StorageImageDescriptorWrite(1, swapchainInput) + }; + core.writeDescriptorSet(tonemappingDescriptor, tonemappingDescriptorWrites); + + uint32_t tonemappingDispatchCount[3]; + tonemappingDispatchCount[0] = std::ceil(windowWidth / 8.f); + tonemappingDispatchCount[1] = std::ceil(windowHeight / 8.f); + tonemappingDispatchCount[2] = 1; + + core.recordComputeDispatchToCmdStream( + cmdStream, + tonemappingPipe, + tonemappingDispatchCount, + {vkcv::DescriptorSetUsage(0, core.getDescriptorSet(tonemappingDescriptor).vulkanHandle) }, + vkcv::PushConstantData(nullptr, 0)); + core.prepareSwapchainImageForPresent(cmdStream); core.submitCommandStream(cmdStream); core.endFrame();