diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 84a1e902ace668fbee40346cf71e3c4c7a519f2e..b31345be95891932fa8d1ad2d5b1334d156f30e1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -66,7 +66,7 @@ build_win10_mingw: - mkdir debug - cd debug - cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\msys64\mingw64\bin\x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\msys64\mingw64\bin\x86_64-w64-mingw32-g++.exe .. -G "Unix Makefiles" - - cmake --build . -j 8 + - cmake --build . -j 4 build_mac_clang: only: diff --git a/include/vkcv/Buffer.hpp b/include/vkcv/Buffer.hpp index ae935ba9501d4d7776cad7e3ba190a2dd02e5e38..f5cd183d21c4ae9a5849ff09fc54af70667c12c6 100644 --- a/include/vkcv/Buffer.hpp +++ b/include/vkcv/Buffer.hpp @@ -76,8 +76,8 @@ namespace vkcv { {} [[nodiscard]] - static Buffer<T> create(BufferManager* manager, BufferType type, size_t count, BufferMemoryType memoryType) { - return Buffer<T>(manager, manager->createBuffer(type, count * sizeof(T), memoryType), type, count, memoryType); + static Buffer<T> create(BufferManager* manager, BufferType type, size_t count, BufferMemoryType memoryType, bool supportIndirect) { + return Buffer<T>(manager, manager->createBuffer(type, count * sizeof(T), memoryType, supportIndirect), type, count, memoryType); } }; diff --git a/include/vkcv/BufferManager.hpp b/include/vkcv/BufferManager.hpp index c7f32d9f134108bafa87ff493bca4e113d53003a..7bec33d8c4fa752be2487a849c16eaeeea0e6237 100644 --- a/include/vkcv/BufferManager.hpp +++ b/include/vkcv/BufferManager.hpp @@ -70,7 +70,7 @@ namespace vkcv * @param memoryType Type of buffers memory * @return New buffer handle */ - BufferHandle createBuffer(BufferType type, size_t size, BufferMemoryType memoryType); + BufferHandle createBuffer(BufferType type, size_t size, BufferMemoryType memoryType, bool supportIndirect); /** * Returns the Vulkan buffer handle of a buffer diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index 5677dbf6569a182eddba494852d39320f8154711..7b5c1d94a6519e626249d55c65da19b1e8f95044 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -185,8 +185,8 @@ namespace vkcv * return Buffer-Object */ template<typename T> - Buffer<T> createBuffer(vkcv::BufferType type, size_t count, BufferMemoryType memoryType = BufferMemoryType::DEVICE_LOCAL) { - return Buffer<T>::create(m_BufferManager.get(), type, count, memoryType); + Buffer<T> createBuffer(vkcv::BufferType type, size_t count, BufferMemoryType memoryType = BufferMemoryType::DEVICE_LOCAL, bool supportIndirect = false) { + return Buffer<T>::create(m_BufferManager.get(), type, count, memoryType, supportIndirect); } /** @@ -271,6 +271,14 @@ namespace vkcv const std::vector<DescriptorSetUsage> &descriptorSetUsages, const PushConstants& pushConstants); + void recordComputeIndirectDispatchToCmdStream( + const CommandStreamHandle cmdStream, + const PipelineHandle computePipeline, + const vkcv::BufferHandle buffer, + const size_t bufferArgOffset, + const std::vector<DescriptorSetUsage>& descriptorSetUsages, + const PushConstants& pushConstants); + /** * @brief end recording and present image */ diff --git a/include/vkcv/DrawcallRecording.hpp b/include/vkcv/DrawcallRecording.hpp index 260fbbc6a2a577d0d333656a1eff4f7f3f88cd69..37cf02d9102fcab5abd10ada711f67b721bcb52b 100644 --- a/include/vkcv/DrawcallRecording.hpp +++ b/include/vkcv/DrawcallRecording.hpp @@ -29,8 +29,19 @@ namespace vkcv { }; struct Mesh { - inline Mesh(std::vector<VertexBufferBinding> vertexBufferBindings, vk::Buffer indexBuffer, size_t indexCount, IndexBitCount indexBitCount = IndexBitCount::Bit16) noexcept - : vertexBufferBindings(vertexBufferBindings), indexBuffer(indexBuffer), indexCount(indexCount), indexBitCount(indexBitCount){} + + inline Mesh(){} + + inline Mesh( + std::vector<VertexBufferBinding> vertexBufferBindings, + vk::Buffer indexBuffer, + size_t indexCount, + IndexBitCount indexBitCount = IndexBitCount::Bit16) noexcept + : + vertexBufferBindings(vertexBufferBindings), + indexBuffer(indexBuffer), + indexCount(indexCount), + indexBitCount(indexBitCount) {} std::vector<VertexBufferBinding> vertexBufferBindings; vk::Buffer indexBuffer; diff --git a/modules/camera/include/vkcv/camera/PilotCameraController.hpp b/modules/camera/include/vkcv/camera/PilotCameraController.hpp index 2b64cdc0dd3045714aba7b3b7c6241af2337c706..67388818a59b66775598e9d4257fa4c36646332a 100644 --- a/modules/camera/include/vkcv/camera/PilotCameraController.hpp +++ b/modules/camera/include/vkcv/camera/PilotCameraController.hpp @@ -29,42 +29,6 @@ namespace vkcv::camera { float m_fov_min; float m_fov_max; - /** - * @brief Indicates forward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveForward(int action); - - /** - * @brief Indicates backward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveBackward(int action); - - /** - * @brief Indicates left movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveLeft(int action); - - /** - * @brief Indicates right movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveRight(int action); - - /** - * @brief Indicates upward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveUpward(int action); - - /** - * @brief Indicates downward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveDownward(int action); - public: /** diff --git a/modules/camera/src/vkcv/camera/CameraManager.cpp b/modules/camera/src/vkcv/camera/CameraManager.cpp index f129f3a248325957cb56470e2547a0146bc7c971..c8aa4f7e0e493a2aaf5bfd6d93768e169cd255b9 100644 --- a/modules/camera/src/vkcv/camera/CameraManager.cpp +++ b/modules/camera/src/vkcv/camera/CameraManager.cpp @@ -52,8 +52,8 @@ namespace vkcv::camera { } void CameraManager::mouseMoveCallback(double x, double y){ - auto xoffset = static_cast<float>(x - m_lastX); - auto yoffset = static_cast<float>(y - m_lastY); + auto xoffset = static_cast<float>(x - m_lastX) / m_window.getWidth(); + auto yoffset = static_cast<float>(y - m_lastY) / m_window.getHeight(); m_lastX = x; m_lastY = y; getActiveController().mouseMoveCallback(xoffset, yoffset, getActiveCamera()); diff --git a/modules/camera/src/vkcv/camera/PilotCameraController.cpp b/modules/camera/src/vkcv/camera/PilotCameraController.cpp index 28ef7c6943428078589047497fc2d3b44fde5fd7..1c7bb12679e57c9221465452f2fc41a539b6b2a0 100644 --- a/modules/camera/src/vkcv/camera/PilotCameraController.cpp +++ b/modules/camera/src/vkcv/camera/PilotCameraController.cpp @@ -50,11 +50,11 @@ namespace vkcv::camera { } // handle yaw rotation - float yaw = camera.getYaw() + static_cast<float>(xOffset) * m_cameraSpeed; + float yaw = camera.getYaw() + static_cast<float>(xOffset) * 90.0f * m_cameraSpeed; camera.setYaw(yaw); // handle pitch rotation - float pitch = camera.getPitch() - static_cast<float>(yOffset) * m_cameraSpeed; + float pitch = camera.getPitch() - static_cast<float>(yOffset) * 90.0f * m_cameraSpeed; pitch = glm::clamp(pitch, -89.0f, 89.0f); camera.setPitch(pitch); } @@ -82,22 +82,22 @@ namespace vkcv::camera { void PilotCameraController::keyCallback(int key, int scancode, int action, int mods, Camera &camera) { switch (key) { case GLFW_KEY_W: - moveForward(action); + m_forward = static_cast<bool>(action); break; case GLFW_KEY_S: - moveBackward(action); + m_backward = static_cast<bool>(action); break; case GLFW_KEY_A: - moveLeft(action); + m_left = static_cast<bool>(action); break; case GLFW_KEY_D: - moveRight(action); + m_right = static_cast<bool>(action); break; case GLFW_KEY_E: - moveUpward(action); + m_upward = static_cast<bool>(action); break; case GLFW_KEY_Q: - moveDownward(action); + m_downward = static_cast<bool>(action); break; default: break; @@ -109,31 +109,25 @@ namespace vkcv::camera { } void PilotCameraController::mouseMoveCallback(double xoffset, double yoffset, Camera &camera) { - if(!m_rotationActive){ - return; - } - - float sensitivity = 0.05f; - xoffset *= sensitivity; - yoffset *= sensitivity; + xoffset *= static_cast<float>(m_rotationActive); + yoffset *= static_cast<float>(m_rotationActive); panView(xoffset , yoffset, camera); } void PilotCameraController::mouseButtonCallback(int button, int action, int mods, Camera &camera) { - if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == false && action == GLFW_PRESS){ - m_rotationActive = true; - } - else if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == true && action == GLFW_RELEASE){ - m_rotationActive = false; - } + if (button == GLFW_MOUSE_BUTTON_2) { + if (m_rotationActive != (action == GLFW_PRESS)) { + m_rotationActive = (action == GLFW_PRESS); + } + } } void PilotCameraController::gamepadCallback(int gamepadIndex, Camera &camera, double frametime) { GLFWgamepadstate gamepadState; glfwGetGamepadState(gamepadIndex, &gamepadState); - float sensitivity = 100.0f; + float sensitivity = 1.0f; double threshold = 0.1; // handle rotations @@ -162,29 +156,4 @@ namespace vkcv::camera { * -copysign(1.0, stickLeftX); } - - void PilotCameraController::moveForward(int action){ - m_forward = static_cast<bool>(action); - } - - void PilotCameraController::moveBackward(int action){ - m_backward = static_cast<bool>(action); - } - - void PilotCameraController::moveLeft(int action){ - m_left = static_cast<bool>(action); - } - - void PilotCameraController::moveRight(int action){ - m_right = static_cast<bool>(action); - } - - void PilotCameraController::moveUpward(int action){ - m_upward = static_cast<bool>(action); - } - - void PilotCameraController::moveDownward(int action){ - m_downward = static_cast<bool>(action); - } - } \ No newline at end of file diff --git a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp index b149a168f061125c08103ba63fcd7a97fa13ccc3..8de2beb87d8f29415db611bfe0d17c5efd57a2a3 100644 --- a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp +++ b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp @@ -23,10 +23,10 @@ namespace vkcv::camera { } // handle yaw rotation - m_yaw = m_yaw + static_cast<float>(xOffset) * m_cameraSpeed; + m_yaw = m_yaw + static_cast<float>(xOffset) * 90.0f * m_cameraSpeed; // handle pitch rotation - m_pitch = m_pitch + static_cast<float>(yOffset) * m_cameraSpeed; + m_pitch = m_pitch + static_cast<float>(yOffset) * 90.0f * m_cameraSpeed; } void TrackballCameraController::updateRadius(double offset, Camera &camera) { @@ -67,15 +67,10 @@ namespace vkcv::camera { } void TrackballCameraController::mouseMoveCallback(double xoffset, double yoffset, Camera &camera) { - if(!m_rotationActive){ - return; - } - - float sensitivity = 0.025f; - xoffset *= sensitivity; - yoffset *= sensitivity; + xoffset *= static_cast<float>(m_rotationActive); + yoffset *= static_cast<float>(m_rotationActive); - panView(xoffset , yoffset, camera); + panView(xoffset, yoffset, camera); } void TrackballCameraController::mouseButtonCallback(int button, int action, int mods, Camera &camera) { @@ -91,7 +86,7 @@ namespace vkcv::camera { GLFWgamepadstate gamepadState; glfwGetGamepadState(gamepadIndex, &gamepadState); - float sensitivity = 100.0f; + float sensitivity = 1.0f; double threshold = 0.1; // handle rotations diff --git a/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp b/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp index c8878513bf99054e357f1b076dfe12664be763b3..16067aebedfda8793a0096803ba5344275bcbbcd 100644 --- a/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp +++ b/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp @@ -2,7 +2,7 @@ #include "vkcv/shader/GLSLCompiler.hpp" #include <fstream> -#include <strstream> +#include <sstream> #include <glslang/SPIRV/GlslangToSpv.h> #include <glslang/StandAlone/DirStackFileIncluder.h> @@ -219,12 +219,10 @@ namespace vkcv::shader { std::string source (shaderSource); if (!m_defines.empty()) { - std::strstream defines; + std::ostringstream defines; for (const auto& define : m_defines) { defines << "#define " << define.first << " " << define.second << std::endl; } - - defines << '\0'; size_t pos = source.find("#version") + 8; if (pos >= source.length()) { @@ -236,8 +234,10 @@ namespace vkcv::shader { pos = epos; } + const auto defines_str = defines.str(); + pos = source.find('\n', pos) + 1; - source = source.insert(pos, defines.str()); + source = source.insert(pos, defines_str); } const char *shaderStrings [1]; diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index c4fde45438e5c8446c6a1d50ceec16f830e8ebfd..8010718447b8e72aed8eab42c8eac3e9591986ee 100644 --- a/projects/CMakeLists.txt +++ b/projects/CMakeLists.txt @@ -6,3 +6,4 @@ add_subdirectory(first_scene) add_subdirectory(particle_simulation) add_subdirectory(voxelization) add_subdirectory(mesh_shader) +add_subdirectory(indirect_dispatch) diff --git a/projects/first_mesh/src/main.cpp b/projects/first_mesh/src/main.cpp index 731d3e56975ff0cd2d8e6d503a19d56de1b922fe..fc682ae1f8b3d1a174ff230c274b89093bc3325c 100644 --- a/projects/first_mesh/src/main.cpp +++ b/projects/first_mesh/src/main.cpp @@ -166,7 +166,6 @@ int main(int argc, const char** argv) { vkcv::camera::CameraManager cameraManager(window); uint32_t camIndex0 = cameraManager.addCamera(vkcv::camera::ControllerType::PILOT); - uint32_t camIndex1 = cameraManager.addCamera(vkcv::camera::ControllerType::TRACKBALL); cameraManager.getCamera(camIndex0).setPosition(glm::vec3(0, 0, -3)); diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp index 253efad491e6e320ba5e5e8b270b187e2e79da82..3598da5f579b608d2c29f1f6fea0b0e25a560336 100644 --- a/projects/first_triangle/src/main.cpp +++ b/projects/first_triangle/src/main.cpp @@ -26,8 +26,6 @@ int main(int argc, const char** argv) { { "VK_KHR_swapchain" } ); - const auto& context = core.getContext(); - auto triangleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 3, vkcv::BufferMemoryType::DEVICE_LOCAL); uint16_t indices[3] = { 0, 1, 2 }; triangleIndexBuffer.fill(&indices[0], sizeof(indices)); diff --git a/projects/indirect_dispatch/.gitignore b/projects/indirect_dispatch/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5f18d9c205e538dabeb0282640bede0359edc33d --- /dev/null +++ b/projects/indirect_dispatch/.gitignore @@ -0,0 +1 @@ +indirect_dispatch diff --git a/projects/indirect_dispatch/CMakeLists.txt b/projects/indirect_dispatch/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..7bc86cbc8470352f13bbfcc62f793b0a99d92884 --- /dev/null +++ b/projects/indirect_dispatch/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.16) +project(indirect_dispatch) + +# 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(indirect_dispatch src/main.cpp) + +target_sources(indirect_dispatch PRIVATE + src/App.hpp + src/App.cpp + + src/AppConfig.hpp + src/MotionBlurConfig.hpp + + src/AppSetup.hpp + src/AppSetup.cpp + + src/MotionBlur.hpp + src/MotionBlur.cpp + + src/MotionBlurSetup.hpp + src/MotionBlurSetup.cpp) + +# this should fix the execution path to load local files from the project (for MSVC) +if(MSVC) + set_target_properties(indirect_dispatch PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) + set_target_properties(indirect_dispatch 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(indirect_dispatch PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) +endif() + +# including headers of dependencies and the VkCV framework +target_include_directories(indirect_dispatch SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include} ${vkcv_gui_include}) + +# linking with libraries from all dependencies and the VkCV framework +target_link_libraries(indirect_dispatch vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_testing vkcv_camera vkcv_shader_compiler vkcv_gui) \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/models/cube.bin b/projects/indirect_dispatch/resources/models/cube.bin new file mode 100644 index 0000000000000000000000000000000000000000..728d38cd39cd10c30a93c15eef021cb0cf7dda74 --- /dev/null +++ b/projects/indirect_dispatch/resources/models/cube.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ccc59e0be3552b4347457bc935d8e548b52f12ca91716a0f0dc37d5bac65f123 +size 840 diff --git a/projects/indirect_dispatch/resources/models/cube.gltf b/projects/indirect_dispatch/resources/models/cube.gltf new file mode 100644 index 0000000000000000000000000000000000000000..ef975c326c71ec1a2fa650a422989534f1c32191 --- /dev/null +++ b/projects/indirect_dispatch/resources/models/cube.gltf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0072448af64bdebffe8eec5a7f32f110579b1a256cd97438bf227e4cc4a87328 +size 2571 diff --git a/projects/indirect_dispatch/resources/models/grid.png b/projects/indirect_dispatch/resources/models/grid.png new file mode 100644 index 0000000000000000000000000000000000000000..5f40eee62f7f9dba3dc156ff6a3653ea2e7f5391 --- /dev/null +++ b/projects/indirect_dispatch/resources/models/grid.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a11c33e4935d93723ab11f597f2aca1ca1ff84af66f2e2d10a01580eb0b7831a +size 40135 diff --git a/projects/indirect_dispatch/resources/models/ground.bin b/projects/indirect_dispatch/resources/models/ground.bin new file mode 100644 index 0000000000000000000000000000000000000000..e29e4f18552def1ac64c167d994be959f82e35c7 --- /dev/null +++ b/projects/indirect_dispatch/resources/models/ground.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8e20cd1c62da3111536283517b63a149f258ea82b1dff8ddafdb79020065b7c +size 140 diff --git a/projects/indirect_dispatch/resources/models/ground.gltf b/projects/indirect_dispatch/resources/models/ground.gltf new file mode 100644 index 0000000000000000000000000000000000000000..6935d3e21a06da1629087c9b0b7f957c57feaf6e --- /dev/null +++ b/projects/indirect_dispatch/resources/models/ground.gltf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a12b8d7cca8110d4ffa9bc4a2223286d1ccfd9c087739a75294e0a3fbfb65c5 +size 2840 diff --git a/projects/indirect_dispatch/resources/shaders/gammaCorrection.comp b/projects/indirect_dispatch/resources/shaders/gammaCorrection.comp new file mode 100644 index 0000000000000000000000000000000000000000..7a6e129d7f8658d3ea424d35b809a3384d12bccc --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/gammaCorrection.comp @@ -0,0 +1,24 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +layout(set=0, binding=0) uniform texture2D inTexture; +layout(set=0, binding=1) uniform sampler textureSampler; +layout(set=0, binding=2, rgba8) uniform image2D outImage; + +layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + +void main(){ + + ivec2 outImageRes = imageSize(outImage); + ivec2 coord = ivec2(gl_GlobalInvocationID.xy); + + if(any(greaterThanEqual(coord, outImageRes))) + return; + + vec2 uv = vec2(coord) / outImageRes; + vec3 linearColor = texture(sampler2D(inTexture, textureSampler), uv).rgb; + + vec3 gammaCorrected = pow(linearColor, vec3(1 / 2.2)); + + imageStore(outImage, coord, vec4(gammaCorrected, 0.f)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/mesh.frag b/projects/indirect_dispatch/resources/shaders/mesh.frag new file mode 100644 index 0000000000000000000000000000000000000000..531c9cbf8b5e097af618d2ca639821a62a30611d --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/mesh.frag @@ -0,0 +1,17 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 passNormal; +layout(location = 1) in vec2 passUV; + +layout(location = 0) out vec3 outColor; + +layout(set=0, binding=0) uniform texture2D albedoTexture; +layout(set=0, binding=1) uniform sampler textureSampler; + +void main() { + vec3 albedo = texture(sampler2D(albedoTexture, textureSampler), passUV).rgb; + vec3 N = normalize(passNormal); + float light = max(N.y * 0.5 + 0.5, 0); + outColor = light * albedo; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/mesh.vert b/projects/indirect_dispatch/resources/shaders/mesh.vert new file mode 100644 index 0000000000000000000000000000000000000000..734fd63cdee66e5fbf61cc427ca21fae18a31d82 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/mesh.vert @@ -0,0 +1,20 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 inPosition; +layout(location = 1) in vec3 inNormal; +layout(location = 2) in vec2 inUV; + +layout(location = 0) out vec3 passNormal; +layout(location = 1) out vec2 passUV; + +layout( push_constant ) uniform constants{ + mat4 mvp; + mat4 model; +}; + +void main() { + gl_Position = mvp * vec4(inPosition, 1.0); + passNormal = (model * vec4(inNormal, 0)).xyz; + passUV = inUV; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlur.comp b/projects/indirect_dispatch/resources/shaders/motionBlur.comp new file mode 100644 index 0000000000000000000000000000000000000000..091c21aa7ddfe9db1780aa64adc77fd5457a3843 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlur.comp @@ -0,0 +1,194 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlur.inc" +#include "motionBlurConfig.inc" +#include "motionBlurWorkTile.inc" + +layout(set=0, binding=0) uniform texture2D inColor; +layout(set=0, binding=1) uniform texture2D inDepth; +layout(set=0, binding=2) uniform texture2D inMotionFullRes; +layout(set=0, binding=3) uniform texture2D inMotionNeighbourhoodMax; +layout(set=0, binding=4) uniform sampler nearestSampler; +layout(set=0, binding=5, r11f_g11f_b10f) uniform image2D outImage; + +layout(set=0, binding=6) buffer WorkTileBuffer { + WorkTiles workTiles; +}; + +layout(local_size_x = motionTileSize, local_size_y = motionTileSize, local_size_z = 1) in; + +layout( push_constant ) uniform constants{ + // computed from delta time and shutter speed + float motionScaleFactor; + // camera planes are needed to linearize depth + float cameraNearPlane; + float cameraFarPlane; + float motionTileOffsetLength; +}; + +float linearizeDepth(float depth, float near, float far){ + return near * far / (far + depth * (near - far)); +} + +struct SampleData{ + vec3 color; + float depthLinear; + vec2 coordinate; + vec2 motion; + float velocityPixels; +}; + +struct PointSpreadCompare{ + float foreground; + float background; +}; + +// results in range [0, 1] +// computes if the sample pixel in the foreground would blur over the main pixel and if the sample pixel in the background would be part of the main pixel background +// contribution depends on if the distance between pixels is smaller than it's velocity +// note that compared to the constant falloff used in McGuire's papers this function from Jimenez is constant until the last pixel +// this is important for the later gradient computation +PointSpreadCompare samplePointSpreadCompare(SampleData mainPixel, SampleData samplePixel){ + + float sampleOffset = distance(mainPixel.coordinate, samplePixel.coordinate); + + PointSpreadCompare pointSpread; + pointSpread.foreground = clamp(1 - sampleOffset + samplePixel.velocityPixels, 0, 1); + pointSpread.background = clamp(1 - sampleOffset + mainPixel.velocityPixels, 0, 1); + + return pointSpread; +} + +struct DepthClassification{ + float foreground; + float background; +}; + +// classifies depthSample compared to depthMain in regards to being in the fore- or background +// the range is [0, 1] and sums to 1 +DepthClassification sampleDepthClassification(SampleData mainPixel, SampleData samplePixel){ + + const float softDepthExtent = 0.1; + + DepthClassification classification; + // only the sign is different, so the latter term will cancel out on addition, so only two times 0.5 remains which sums to one + classification.foreground = clamp(0.5 + (mainPixel.depthLinear - samplePixel.depthLinear) / softDepthExtent, 0, 1); + classification.background = clamp(0.5 - (mainPixel.depthLinear - samplePixel.depthLinear) / softDepthExtent, 0, 1); + return classification; +} + +// reconstruction filter and helper functions from "Next Generation Post Processing in Call of Duty Advanced Warfare", Jimenez +// returns value in range [0, 1] +float computeSampleWeigth(SampleData mainPixel, SampleData samplePixel){ + + PointSpreadCompare pointSpread = samplePointSpreadCompare( mainPixel, samplePixel); + DepthClassification depthClassification = sampleDepthClassification(mainPixel, samplePixel); + + return + depthClassification.foreground * pointSpread.foreground + + depthClassification.background * pointSpread.background; +} + +SampleData loadSampleData(vec2 uv){ + + SampleData data; + data.color = texture(sampler2D(inColor, nearestSampler), uv).rgb; + data.coordinate = ivec2(uv * imageSize(outImage)); + data.motion = processMotionVector(texture(sampler2D(inMotionFullRes, nearestSampler), uv).rg, motionScaleFactor, imageSize(outImage)); + data.velocityPixels = length(data.motion * imageSize(outImage)); + data.depthLinear = texture(sampler2D(inDepth, nearestSampler), uv).r; + data.depthLinear = linearizeDepth(data.depthLinear, cameraNearPlane, cameraFarPlane); + + return data; +} + +void main(){ + + uint tileIndex = gl_WorkGroupID.x; + ivec2 tileCoordinates = workTiles.tileXY[tileIndex]; + ivec2 coord = ivec2(tileCoordinates * motionTileSize + gl_LocalInvocationID.xy); + + if(any(greaterThanEqual(coord, imageSize(outImage)))) + return; + + ivec2 textureRes = textureSize(sampler2D(inColor, nearestSampler), 0); + vec2 uv = vec2(coord + 0.5) / textureRes; // + 0.5 to shift uv into pixel center + + // the motion tile lookup is jittered, so the hard edges in the blur are replaced by noise + // dither is shifted, so it does not line up with motion tiles + float motionOffset = motionTileOffsetLength * (dither(coord + ivec2(ditherSize / 2)) * 2 - 1); + vec2 motionNeighbourhoodMax = processMotionVector(texelFetch(sampler2D(inMotionNeighbourhoodMax, nearestSampler), ivec2(coord + motionOffset) / motionTileSize, 0).rg, motionScaleFactor, imageSize(outImage)); + + SampleData mainPixel = loadSampleData(uv); + + // early out on movement less than half a pixel + if(length(motionNeighbourhoodMax * imageSize(outImage)) <= 0.5){ + imageStore(outImage, coord, vec4(mainPixel.color, 0.f)); + return; + } + + vec3 color = vec3(0); + float weightSum = 0; + + // clamping start and end points avoids artifacts at image borders + // the sampler clamps the sample uvs anyways, but without clamping here, many samples can be stuck at the border + vec2 uvStart = clamp(uv - motionNeighbourhoodMax, 0, 1); + vec2 uvEnd = clamp(uv + motionNeighbourhoodMax, 0, 1); + + // samples are placed evenly, but the entire filter is jittered + // dither returns either 0 or 1 + // the sampleUV code expects an offset in range [-0.5, 0.5], so the dither is rescaled to a binary -0.25/0.25 + float random = dither(coord) * 0.5 - 0.25; + + const int sampleCountHalf = 8; + + // two samples are processed at a time to allow for mirrored background reconstruction + for(int i = 0; i < sampleCountHalf; i++){ + + vec2 sampleUV1 = mix(uv, uvEnd, (i + random + 1) / float(sampleCountHalf + 1)); + vec2 sampleUV2 = mix(uv, uvStart, (i + random + 1) / float(sampleCountHalf + 1)); + + SampleData sample1 = loadSampleData(sampleUV1); + SampleData sample2 = loadSampleData(sampleUV2); + + float weight1 = computeSampleWeigth(mainPixel, sample1); + float weight2 = computeSampleWeigth(mainPixel, sample2); + + bool mirroredBackgroundReconstruction = true; + if(mirroredBackgroundReconstruction){ + // see Jimenez paper for details and comparison + // problem is that in the foreground the background is reconstructed, which is blurry + // in the background the background is obviously known, so it is sharper + // at the border between fore- and background this causes a discontinuity + // to fix this the weights are mirrored on this border, effectively reconstructing the background, even though it is known + + // these bools check if sample1 is an affected background pixel (further away and slower moving than sample2) + bool inBackground = sample1.depthLinear > sample2.depthLinear; + bool blurredOver = sample1.velocityPixels < sample2.velocityPixels; + + // this mirrors the weights depending on the results: + // if both conditions are true, then weight2 is mirrored to weight1 + // if both conditions are false, then weight1 is mirrored to weight2, as sample2 is an affected background pixel + // if only one condition is true, then the weights are kept as is + weight1 = inBackground && blurredOver ? weight2 : weight1; + weight2 = inBackground || blurredOver ? weight2 : weight1; + } + + weightSum += weight1; + weightSum += weight2; + + color += sample1.color * weight1; + color += sample2.color * weight2; + } + + // normalize color and weight + weightSum /= sampleCountHalf * 2; + color /= sampleCountHalf * 2; + + // the main color is considered the background + // the weight sum can be interpreted as the alpha of the combined samples, see Jimenez paper + color += (1 - weightSum) * mainPixel.color; + + imageStore(outImage, coord, vec4(color, 0.f)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlur.inc b/projects/indirect_dispatch/resources/shaders/motionBlur.inc new file mode 100644 index 0000000000000000000000000000000000000000..6fdaf4c5f5e4b07a3111946b0732137f42f295ef --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlur.inc @@ -0,0 +1,35 @@ +#ifndef MOTION_BLUR +#define MOTION_BLUR + +#include "motionBlurConfig.inc" + +// see "A Reconstruction Filter for Plausible Motion Blur", section 2.2 +vec2 processMotionVector(vec2 motion, float motionScaleFactor, ivec2 imageResolution){ + // every frame a pixel should blur over the distance it moves + // as we blur in two directions (where it was and where it will be) we must half the motion + vec2 motionHalf = motion * 0.5; + vec2 motionScaled = motionHalf * motionScaleFactor; // scale factor contains shutter speed and delta time + + // pixels are anisotropic, so the ratio for clamping the velocity is computed in pixels instead of uv coordinates + vec2 motionPixel = motionScaled * imageResolution; + float velocityPixels = length(motionPixel); + + float epsilon = 0.0001; + + // this clamps the motion to not exceed the radius given by the motion tile size + return motionScaled * max(0.5, min(velocityPixels, motionTileSize)) / (velocityPixels + epsilon); +} + +const int ditherSize = 4; + +// simple binary dither pattern +// could be optimized to avoid modulo and branch +float dither(ivec2 coord){ + + bool x = coord.x % ditherSize < (ditherSize / 2); + bool y = coord.y % ditherSize < (ditherSize / 2); + + return x ^^ y ? 1 : 0; +} + +#endif // #ifndef MOTION_BLUR \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurColorCopy.comp b/projects/indirect_dispatch/resources/shaders/motionBlurColorCopy.comp new file mode 100644 index 0000000000000000000000000000000000000000..1d8f210c86c2670241fa1d011835b120a39eddc0 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurColorCopy.comp @@ -0,0 +1,29 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlurConfig.inc" +#include "motionBlurWorkTile.inc" + +layout(set=0, binding=0) uniform texture2D inColor; +layout(set=0, binding=1) uniform sampler nearestSampler; +layout(set=0, binding=2, r11f_g11f_b10f) uniform image2D outImage; + +layout(set=0, binding=3) buffer WorkTileBuffer { + WorkTiles workTiles; +}; + +layout(local_size_x = motionTileSize, local_size_y = motionTileSize, local_size_z = 1) in; + +void main(){ + + uint tileIndex = gl_WorkGroupID.x; + ivec2 tileCoordinates = workTiles.tileXY[tileIndex]; + ivec2 coordinate = ivec2(tileCoordinates * motionTileSize + gl_LocalInvocationID.xy); + + if(any(greaterThanEqual(coordinate, imageSize(outImage)))) + return; + + vec3 color = texelFetch(sampler2D(inColor, nearestSampler), coordinate, 0).rgb; + + imageStore(outImage, coordinate, vec4(color, 0.f)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurConfig.inc b/projects/indirect_dispatch/resources/shaders/motionBlurConfig.inc new file mode 100644 index 0000000000000000000000000000000000000000..5b8679da119d84242c55d7d89de80ed8b64e5cc9 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurConfig.inc @@ -0,0 +1,8 @@ +#ifndef MOTION_BLUR_CONFIG +#define MOTION_BLUR_CONFIG + +const int motionTileSize = 16; +const int maxMotionBlurWidth = 3840; +const int maxMotionBlurHeight = 2160; + +#endif // #ifndef MOTION_BLUR_CONFIG \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurFastPath.comp b/projects/indirect_dispatch/resources/shaders/motionBlurFastPath.comp new file mode 100644 index 0000000000000000000000000000000000000000..2e27ebedcc4be1da93ff89a187fe1d3e992e8d22 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurFastPath.comp @@ -0,0 +1,68 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlur.inc" +#include "motionBlurConfig.inc" +#include "motionBlurWorkTile.inc" + +layout(set=0, binding=0) uniform texture2D inColor; +layout(set=0, binding=1) uniform texture2D inMotionNeighbourhoodMax; +layout(set=0, binding=2) uniform sampler nearestSampler; +layout(set=0, binding=3, r11f_g11f_b10f) uniform image2D outImage; + +layout(set=0, binding=4) buffer WorkTileBuffer { + WorkTiles workTiles; +}; + +layout(local_size_x = motionTileSize, local_size_y = motionTileSize, local_size_z = 1) in; + +layout( push_constant ) uniform constants{ + // computed from delta time and shutter speed + float motionScaleFactor; +}; + +void main(){ + + uint tileIndex = gl_WorkGroupID.x; + ivec2 tileCoordinates = workTiles.tileXY[tileIndex]; + ivec2 coord = ivec2(tileCoordinates * motionTileSize + gl_LocalInvocationID.xy); + + if(any(greaterThanEqual(coord, imageSize(outImage)))) + return; + + ivec2 textureRes = textureSize(sampler2D(inColor, nearestSampler), 0); + vec2 uv = vec2(coord + 0.5) / textureRes; // + 0.5 to shift uv into pixel center + + vec2 motionNeighbourhoodMax = processMotionVector(texelFetch(sampler2D(inMotionNeighbourhoodMax, nearestSampler), coord / motionTileSize, 0).rg, motionScaleFactor, imageSize(outImage)); + + // early out on movement less than half a pixel + if(length(motionNeighbourhoodMax * imageSize(outImage)) <= 0.5){ + vec3 color = texture(sampler2D(inColor, nearestSampler), uv).rgb; + imageStore(outImage, coord, vec4(color, 0.f)); + return; + } + + vec3 color = vec3(0); + + // clamping start and end points avoids artifacts at image borders + // the sampler clamps the sample uvs anyways, but without clamping here, many samples can be stuck at the border + vec2 uvStart = clamp(uv - motionNeighbourhoodMax, 0, 1); + vec2 uvEnd = clamp(uv + motionNeighbourhoodMax, 0, 1); + + // samples are placed evenly, but the entire filter is jittered + // dither returns either 0 or 1 + // the sampleUV code expects an offset in range [-0.5, 0.5], so the dither is rescaled to a binary -0.25/0.25 + float random = dither(coord) * 0.5 - 0.25; + + const int sampleCount = 16; + + for(int i = 0; i < sampleCount; i++){ + + vec2 sampleUV = mix(uvStart, uvEnd, (i + random + 1) / float(sampleCount + 1)); + color += texture(sampler2D(inColor, nearestSampler), sampleUV).rgb; + } + + color /= sampleCount; + + imageStore(outImage, coord, vec4(color, 0.f)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurTileClassification.comp b/projects/indirect_dispatch/resources/shaders/motionBlurTileClassification.comp new file mode 100644 index 0000000000000000000000000000000000000000..3c6f9e3715951ac4fe6770725c3314590cbbff47 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurTileClassification.comp @@ -0,0 +1,58 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlurWorkTile.inc" + +layout(set=0, binding=0) uniform texture2D inMotionMax; +layout(set=0, binding=1) uniform texture2D inMotionMin; +layout(set=0, binding=2) uniform sampler nearestSampler; + +layout(set=0, binding=3) buffer FullPathTileBuffer { + WorkTiles fullPathTiles; +}; + +layout(set=0, binding=4) buffer CopyPathTileBuffer { + WorkTiles copyPathTiles; +}; + +layout(set=0, binding=5) buffer FastPathTileBuffer { + WorkTiles fastPathTiles; +}; + +layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + +layout( push_constant ) uniform constants{ + uint width; + uint height; + float fastPathThreshold; +}; + +void main(){ + + ivec2 tileCoord = ivec2(gl_GlobalInvocationID.xy); + + if(any(greaterThanEqual(gl_GlobalInvocationID.xy, textureSize(sampler2D(inMotionMax, nearestSampler), 0)))) + return; + + vec2 motionMax = texelFetch(sampler2D(inMotionMax, nearestSampler), tileCoord, 0).rg; + vec2 motionMin = texelFetch(sampler2D(inMotionMin, nearestSampler), tileCoord, 0).rg; + + vec2 motionPixelMax = motionMax * vec2(width, height); + vec2 motionPixelMin = motionMin * vec2(width, height); + + float velocityPixelMax = length(motionPixelMax); + float minMaxDistance = distance(motionPixelMin, motionPixelMax); + + if(velocityPixelMax <= 0.5){ + uint index = atomicAdd(copyPathTiles.tileCount, 1); + copyPathTiles.tileXY[index] = tileCoord; + } + else if(minMaxDistance <= fastPathThreshold){ + uint index = atomicAdd(fastPathTiles.tileCount, 1); + fastPathTiles.tileXY[index] = tileCoord; + } + else{ + uint index = atomicAdd(fullPathTiles.tileCount, 1); + fullPathTiles.tileXY[index] = tileCoord; + } +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurTileClassificationVis.comp b/projects/indirect_dispatch/resources/shaders/motionBlurTileClassificationVis.comp new file mode 100644 index 0000000000000000000000000000000000000000..3382ff5ef0b407b9a3a7785eda0d19efe5a8f96e --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurTileClassificationVis.comp @@ -0,0 +1,56 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlurConfig.inc" +#include "motionBlurWorkTile.inc" + +layout(set=0, binding=0) uniform texture2D inColor; +layout(set=0, binding=1) uniform sampler nearestSampler; +layout(set=0, binding=2, r11f_g11f_b10f) uniform image2D outImage; + +layout(set=0, binding=3) buffer FullPathTileBuffer { + WorkTiles fullPathTiles; +}; + +layout(set=0, binding=4) buffer CopyPathTileBuffer { + WorkTiles copyPathTiles; +}; + +layout(set=0, binding=5) buffer FastPathTileBuffer { + WorkTiles fastPathTiles; +}; + +layout(local_size_x = motionTileSize, local_size_y = motionTileSize, local_size_z = 1) in; + +void main(){ + + uint tileIndexFullPath = gl_WorkGroupID.x; + uint tileIndexCopyPath = gl_WorkGroupID.x - fullPathTiles.tileCount; + uint tileIndexFastPath = gl_WorkGroupID.x - fullPathTiles.tileCount - copyPathTiles.tileCount; + + vec3 debugColor; + ivec2 tileCoordinates; + + if(tileIndexFullPath < fullPathTiles.tileCount){ + debugColor = vec3(1, 0, 0); + tileCoordinates = fullPathTiles.tileXY[tileIndexFullPath]; + } + else if(tileIndexCopyPath < copyPathTiles.tileCount){ + debugColor = vec3(0, 1, 0); + tileCoordinates = copyPathTiles.tileXY[tileIndexCopyPath]; + } + else if(tileIndexFastPath < fastPathTiles.tileCount){ + debugColor = vec3(0, 0, 1); + tileCoordinates = fastPathTiles.tileXY[tileIndexFastPath]; + } + else{ + return; + } + + ivec2 coordinate = ivec2(tileCoordinates * motionTileSize + gl_LocalInvocationID.xy); + vec3 color = texelFetch(sampler2D(inColor, nearestSampler), coordinate, 0).rgb; + + color = mix(color, debugColor, 0.5); + + imageStore(outImage, coordinate, vec4(color, 0)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurWorkTile.inc b/projects/indirect_dispatch/resources/shaders/motionBlurWorkTile.inc new file mode 100644 index 0000000000000000000000000000000000000000..8577f100aac524b93eecac406606a962bc52d222 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurWorkTile.inc @@ -0,0 +1,19 @@ +#ifndef MOTION_BLUR_WORK_TILE +#define MOTION_BLUR_WORK_TILE + +#include "motionBlurConfig.inc" + +const int maxTileCount = + (maxMotionBlurWidth + motionTileSize - 1) / motionTileSize * + (maxMotionBlurHeight + motionTileSize - 1) / motionTileSize; + +struct WorkTiles{ + uint tileCount; + // dispatch Y/Z are here so the buffer can be used directly as an indirect dispatch argument buffer + uint dispatchY; + uint dispatchZ; + + ivec2 tileXY[maxTileCount]; +}; + +#endif // #ifndef MOTION_BLUR_WORK_TILE \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionBlurWorkTileReset.comp b/projects/indirect_dispatch/resources/shaders/motionBlurWorkTileReset.comp new file mode 100644 index 0000000000000000000000000000000000000000..d4b55582a0a18c0c6a3fecf1dd6ce69ed49ca2c1 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionBlurWorkTileReset.comp @@ -0,0 +1,32 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlurWorkTile.inc" + +layout(set=0, binding=0) buffer FullPathTileBuffer { + WorkTiles fullPathTiles; +}; + +layout(set=0, binding=1) buffer CopyPathTileBuffer { + WorkTiles copyPathTiles; +}; + +layout(set=0, binding=2) buffer FastPathTileBuffer { + WorkTiles fastPathTiles; +}; + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + +void main(){ + fullPathTiles.tileCount = 0; + fullPathTiles.dispatchY = 1; + fullPathTiles.dispatchZ = 1; + + copyPathTiles.tileCount = 0; + copyPathTiles.dispatchY = 1; + copyPathTiles.dispatchZ = 1; + + fastPathTiles.tileCount = 0; + fastPathTiles.dispatchY = 1; + fastPathTiles.dispatchZ = 1; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionVector.inc b/projects/indirect_dispatch/resources/shaders/motionVector.inc new file mode 100644 index 0000000000000000000000000000000000000000..498478cbc38b9666366eaa3d3e1a715dfc30236b --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionVector.inc @@ -0,0 +1,9 @@ +vec2 computeMotionVector(vec4 NDC, vec4 NDCPrevious){ + vec2 ndc = NDC.xy / NDC.w; + vec2 ndcPrevious = NDCPrevious.xy / NDCPrevious.w; + + vec2 uv = ndc * 0.5 + 0.5; + vec2 uvPrevious = ndcPrevious * 0.5 + 0.5; + + return uvPrevious - uv; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionVectorMinMax.comp b/projects/indirect_dispatch/resources/shaders/motionVectorMinMax.comp new file mode 100644 index 0000000000000000000000000000000000000000..4ad350b0d5300aa63a66d7aceb00ea0b642d07ee --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionVectorMinMax.comp @@ -0,0 +1,48 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable +#include "motionBlurConfig.inc" + +layout(set=0, binding=0) uniform texture2D inMotion; +layout(set=0, binding=1) uniform sampler textureSampler; +layout(set=0, binding=2, rg16) uniform image2D outMotionMax; +layout(set=0, binding=3, rg16) uniform image2D outMotionMin; + +layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + +void main(){ + + ivec2 outImageRes = imageSize(outMotionMax); + ivec2 motionTileCoord = ivec2(gl_GlobalInvocationID.xy); + + if(any(greaterThanEqual(motionTileCoord, outImageRes))) + return; + + float velocityMax = 0; + vec2 motionMax = vec2(0); + + float velocityMin = 100000; + vec2 motionMin = vec2(0); + + ivec2 motionBufferBaseCoord = motionTileCoord * motionTileSize; + + for(int x = 0; x < motionTileSize; x++){ + for(int y = 0; y < motionTileSize; y++){ + ivec2 sampleCoord = motionBufferBaseCoord + ivec2(x, y); + vec2 motionSample = texelFetch(sampler2D(inMotion, textureSampler), sampleCoord, 0).rg; + float velocitySample = length(motionSample); + + if(velocitySample > velocityMax){ + velocityMax = velocitySample; + motionMax = motionSample; + } + + if(velocitySample < velocityMin){ + velocityMin = velocitySample; + motionMin = motionSample; + } + } + } + + imageStore(outMotionMax, motionTileCoord, vec4(motionMax, 0, 0)); + imageStore(outMotionMin, motionTileCoord, vec4(motionMin, 0, 0)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionVectorMinMaxNeighbourhood.comp b/projects/indirect_dispatch/resources/shaders/motionVectorMinMaxNeighbourhood.comp new file mode 100644 index 0000000000000000000000000000000000000000..4d6e7c0af6115e816ba087570e5585ffde23b1e6 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionVectorMinMaxNeighbourhood.comp @@ -0,0 +1,51 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +layout(set=0, binding=0) uniform texture2D inMotionMax; +layout(set=0, binding=1) uniform texture2D inMotionMin; +layout(set=0, binding=2) uniform sampler textureSampler; +layout(set=0, binding=3, rg16) uniform image2D outMotionMaxNeighbourhood; +layout(set=0, binding=4, rg16) uniform image2D outMotionMinNeighbourhood; + +layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + +void main(){ + + ivec2 outImageRes = imageSize(outMotionMaxNeighbourhood); + ivec2 motionTileCoord = ivec2(gl_GlobalInvocationID.xy); + + if(any(greaterThanEqual(motionTileCoord, outImageRes))) + return; + + float velocityMax = 0; + vec2 motionMax = vec2(0); + + float velocityMin = 10000; + vec2 motionMin = vec2(0); + + for(int x = -1; x <= 1; x++){ + for(int y = -1; y <= 1; y++){ + ivec2 sampleCoord = motionTileCoord + ivec2(x, y); + + vec2 motionSampleMax = texelFetch(sampler2D(inMotionMax, textureSampler), sampleCoord, 0).rg; + float velocitySampleMax = length(motionSampleMax); + + if(velocitySampleMax > velocityMax){ + velocityMax = velocitySampleMax; + motionMax = motionSampleMax; + } + + + vec2 motionSampleMin = texelFetch(sampler2D(inMotionMin, textureSampler), sampleCoord, 0).rg; + float velocitySampleMin = length(motionSampleMin); + + if(velocitySampleMin < velocityMin){ + velocityMin = velocitySampleMin; + motionMin = motionSampleMin; + } + } + } + + imageStore(outMotionMaxNeighbourhood, motionTileCoord, vec4(motionMax, 0, 0)); + imageStore(outMotionMinNeighbourhood, motionTileCoord, vec4(motionMin, 0, 0)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/motionVectorVisualisation.comp b/projects/indirect_dispatch/resources/shaders/motionVectorVisualisation.comp new file mode 100644 index 0000000000000000000000000000000000000000..1cfb09c87e8288b8ea80c6ddfbe5f0d4918b7f2e --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/motionVectorVisualisation.comp @@ -0,0 +1,30 @@ +#version 440 +#extension GL_GOOGLE_include_directive : enable + +#include "motionBlurConfig.inc" + +layout(set=0, binding=0) uniform texture2D inMotion; +layout(set=0, binding=1) uniform sampler textureSampler; +layout(set=0, binding=2, r11f_g11f_b10f) uniform image2D outImage; + +layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; + +layout( push_constant ) uniform constants{ + float range; +}; + +void main(){ + + ivec2 outImageRes = imageSize(outImage); + ivec2 coord = ivec2(gl_GlobalInvocationID.xy); + + if(any(greaterThanEqual(coord, outImageRes))) + return; + + vec2 motionVector = texelFetch(sampler2D(inMotion, textureSampler), coord / motionTileSize, 0).rg; + vec2 motionVectorNormalized = clamp(motionVector / range, -1, 1); + + vec2 color = motionVectorNormalized * 0.5 + 0.5; + + imageStore(outImage, coord, vec4(color, 0.5, 0)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/prepass.frag b/projects/indirect_dispatch/resources/shaders/prepass.frag new file mode 100644 index 0000000000000000000000000000000000000000..ccfc84d982253f7b89551c099a92b5686a811163 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/prepass.frag @@ -0,0 +1,14 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_GOOGLE_include_directive : enable + +#include "motionVector.inc" + +layout(location = 0) in vec4 passNDC; +layout(location = 1) in vec4 passNDCPrevious; + +layout(location = 0) out vec2 outMotion; + +void main() { + outMotion = computeMotionVector(passNDC, passNDCPrevious); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/prepass.vert b/projects/indirect_dispatch/resources/shaders/prepass.vert new file mode 100644 index 0000000000000000000000000000000000000000..230346208007fae0bb7724b5b6d05f62726c4ded --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/prepass.vert @@ -0,0 +1,18 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 inPosition; + +layout(location = 0) out vec4 passNDC; +layout(location = 1) out vec4 passNDCPrevious; + +layout( push_constant ) uniform constants{ + mat4 mvp; + mat4 mvpPrevious; +}; + +void main() { + gl_Position = mvp * vec4(inPosition, 1.0); + passNDC = gl_Position; + passNDCPrevious = mvpPrevious * vec4(inPosition, 1.0); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/sky.frag b/projects/indirect_dispatch/resources/shaders/sky.frag new file mode 100644 index 0000000000000000000000000000000000000000..efc0e03b2d6ee1c71930c866293da66857bd56c7 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/sky.frag @@ -0,0 +1,8 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) out vec3 outColor; + +void main() { + outColor = vec3(0, 0.2, 0.9); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/sky.vert b/projects/indirect_dispatch/resources/shaders/sky.vert new file mode 100644 index 0000000000000000000000000000000000000000..44b48cd7f3bfc44e2e43edef0d474581d50608de --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/sky.vert @@ -0,0 +1,13 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 inPosition; + +layout( push_constant ) uniform constants{ + mat4 viewProjection; +}; + +void main() { + gl_Position = viewProjection * vec4(inPosition, 0.0); + gl_Position.w = gl_Position.z; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/skyPrepass.frag b/projects/indirect_dispatch/resources/shaders/skyPrepass.frag new file mode 100644 index 0000000000000000000000000000000000000000..64ec4f18bbcf89153d70019ace570da53d44a505 --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/skyPrepass.frag @@ -0,0 +1,14 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_GOOGLE_include_directive : enable + +#include "motionVector.inc" + +layout(location = 0) out vec2 outMotion; + +layout(location = 0) in vec4 passNDC; +layout(location = 1) in vec4 passNDCPrevious; + +void main() { + outMotion = computeMotionVector(passNDC, passNDCPrevious); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/resources/shaders/skyPrepass.vert b/projects/indirect_dispatch/resources/shaders/skyPrepass.vert new file mode 100644 index 0000000000000000000000000000000000000000..31b9016a592d097825a09e1daa888cb7b72b2cbc --- /dev/null +++ b/projects/indirect_dispatch/resources/shaders/skyPrepass.vert @@ -0,0 +1,22 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 inPosition; + +layout( push_constant ) uniform constants{ + mat4 viewProjection; + mat4 viewProjectionPrevious; +}; + +layout(location = 0) out vec4 passNDC; +layout(location = 1) out vec4 passNDCPrevious; + +void main() { + gl_Position = viewProjection * vec4(inPosition, 0.0); + gl_Position.w = gl_Position.z; + + passNDC = gl_Position; + + passNDCPrevious = viewProjectionPrevious * vec4(inPosition, 0.0); + passNDCPrevious.w = passNDCPrevious.z; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/App.cpp b/projects/indirect_dispatch/src/App.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92d548acde9c5a27e69c6daf4d92ca1da9d50a2c --- /dev/null +++ b/projects/indirect_dispatch/src/App.cpp @@ -0,0 +1,369 @@ +#include "App.hpp" +#include "AppConfig.hpp" +#include <chrono> +#include <vkcv/gui/GUI.hpp> +#include <functional> + +App::App() : + m_applicationName("Indirect Dispatch"), + m_windowWidth(AppConfig::defaultWindowWidth), + m_windowHeight(AppConfig::defaultWindowHeight), + m_window(vkcv::Window::create( + m_applicationName, + m_windowWidth, + m_windowHeight, + true)), + m_core(vkcv::Core::create( + m_window, + m_applicationName, + VK_MAKE_VERSION(0, 0, 1), + { vk::QueueFlagBits::eGraphics ,vk::QueueFlagBits::eCompute , vk::QueueFlagBits::eTransfer }, + {}, + { "VK_KHR_swapchain" })), + m_cameraManager(m_window){} + +bool App::initialize() { + + if (!loadMeshPass(m_core, &m_meshPass)) + return false; + + if (!loadSkyPass(m_core, &m_skyPass)) + return false; + + if (!loadPrePass(m_core, &m_prePass)) + return false; + + if (!loadSkyPrePass(m_core, &m_skyPrePass)) + return false; + + if (!loadComputePass(m_core, "resources/shaders/gammaCorrection.comp", &m_gammaCorrectionPass)) + return false; + + if (!loadMesh(m_core, "resources/models/cube.gltf", &m_cubeMesh)) + return false; + + if (!loadMesh(m_core, "resources/models/ground.gltf", &m_groundMesh)) + return false; + + if(!loadImage(m_core, "resources/models/grid.png", &m_gridTexture)) + return false; + + if (!m_motionBlur.initialize(&m_core, m_windowWidth, m_windowHeight)) + return false; + + m_linearSampler = m_core.createSampler( + vkcv::SamplerFilterType::LINEAR, + vkcv::SamplerFilterType::LINEAR, + vkcv::SamplerMipmapMode::LINEAR, + vkcv::SamplerAddressMode::CLAMP_TO_EDGE); + + m_renderTargets = createRenderTargets(m_core, m_windowWidth, m_windowHeight); + + const int cameraIndex = m_cameraManager.addCamera(vkcv::camera::ControllerType::PILOT); + m_cameraManager.getCamera(cameraIndex).setPosition(glm::vec3(0, 1, -3)); + m_cameraManager.getCamera(cameraIndex).setNearFar(0.1f, 30.f); + + vkcv::DescriptorWrites meshPassDescriptorWrites; + meshPassDescriptorWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, m_gridTexture) }; + meshPassDescriptorWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, m_linearSampler) }; + m_core.writeDescriptorSet(m_meshPass.descriptorSet, meshPassDescriptorWrites); + + return true; +} + +void App::run() { + + auto frameStartTime = std::chrono::system_clock::now(); + const auto appStartTime = std::chrono::system_clock::now(); + const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); + const vkcv::DrawcallInfo skyDrawcall(m_cubeMesh.mesh, {}, 1); + + vkcv::gui::GUI gui(m_core, m_window); + + eMotionVectorVisualisationMode motionVectorVisualisationMode = eMotionVectorVisualisationMode::None; + eMotionBlurMode motionBlurMode = eMotionBlurMode::Default; + + bool freezeFrame = false; + float motionBlurTileOffsetLength = 3; + float objectVerticalSpeed = 5; + float objectAmplitude = 0; + float objectMeanHeight = 1; + float objectRotationSpeedX = 5; + float objectRotationSpeedY = 5; + int cameraShutterSpeedInverse = 24; + float motionVectorVisualisationRange = 0.008; + float motionBlurFastPathThreshold = 1; + + glm::mat4 viewProjection = m_cameraManager.getActiveCamera().getMVP(); + glm::mat4 viewProjectionPrevious = m_cameraManager.getActiveCamera().getMVP(); + + struct Object { + MeshResources meshResources; + glm::mat4 modelMatrix = glm::mat4(1.f); + glm::mat4 mvp = glm::mat4(1.f); + glm::mat4 mvpPrevious = glm::mat4(1.f); + std::function<void(float, Object&)> modelMatrixUpdate; + }; + std::vector<Object> sceneObjects; + + Object ground; + ground.meshResources = m_groundMesh; + sceneObjects.push_back(ground); + + Object sphere; + sphere.meshResources = m_cubeMesh; + sphere.modelMatrixUpdate = [&](float time, Object& obj) { + const float currentHeight = objectMeanHeight + objectAmplitude * glm::sin(time * objectVerticalSpeed); + const glm::mat4 translation = glm::translate(glm::mat4(1), glm::vec3(0, currentHeight, 0)); + const glm::mat4 rotationX = glm::rotate(glm::mat4(1), objectRotationSpeedX * time, glm::vec3(1, 0, 0)); + const glm::mat4 rotationY = glm::rotate(glm::mat4(1), objectRotationSpeedY * time, glm::vec3(0, 1, 0)); + obj.modelMatrix = translation * rotationX * rotationY; + }; + sceneObjects.push_back(sphere); + + bool spaceWasPressed = false; + + m_window.e_key.add([&](int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_SPACE) { + if (action == GLFW_PRESS) { + if (!spaceWasPressed) { + freezeFrame = !freezeFrame; + } + spaceWasPressed = true; + } + else if (action == GLFW_RELEASE) { + spaceWasPressed = false; + } + } + }); + + auto frameEndTime = std::chrono::system_clock::now(); + + while (m_window.isWindowOpen()) { + + vkcv::Window::pollEvents(); + + if (!freezeFrame) { + + frameStartTime = frameEndTime; + viewProjectionPrevious = viewProjection; + + for (Object& obj : sceneObjects) { + obj.mvpPrevious = obj.mvp; + } + } + + if (m_window.getHeight() == 0 || m_window.getWidth() == 0) + continue; + + uint32_t swapchainWidth, swapchainHeight; + if (!m_core.beginFrame(swapchainWidth, swapchainHeight)) + continue; + + const bool hasResolutionChanged = (swapchainWidth != m_windowWidth) || (swapchainHeight != m_windowHeight); + if (hasResolutionChanged) { + m_windowWidth = swapchainWidth; + m_windowHeight = swapchainHeight; + + m_renderTargets = createRenderTargets(m_core, m_windowWidth, m_windowHeight); + m_motionBlur.setResolution(m_windowWidth, m_windowHeight); + } + + if(!freezeFrame) + frameEndTime = std::chrono::system_clock::now(); + + const float microsecondToSecond = 0.000001; + const float fDeltaTimeSeconds = microsecondToSecond * std::chrono::duration_cast<std::chrono::microseconds>(frameEndTime - frameStartTime).count(); + + m_cameraManager.update(fDeltaTimeSeconds); + + const auto time = frameEndTime - appStartTime; + const float fCurrentTime = std::chrono::duration_cast<std::chrono::milliseconds>(time).count() * 0.001f; + + // update matrices + if (!freezeFrame) { + + viewProjection = m_cameraManager.getActiveCamera().getMVP(); + + for (Object& obj : sceneObjects) { + if (obj.modelMatrixUpdate) { + obj.modelMatrixUpdate(fCurrentTime, obj); + } + obj.mvp = viewProjection * obj.modelMatrix; + } + } + + const vkcv::CommandStreamHandle cmdStream = m_core.createCommandStream(vkcv::QueueType::Graphics); + + // prepass + vkcv::PushConstants prepassPushConstants(sizeof(glm::mat4) * 2); + + for (const Object& obj : sceneObjects) { + glm::mat4 prepassMatrices[2] = { obj.mvp, obj.mvpPrevious }; + prepassPushConstants.appendDrawcall(prepassMatrices); + } + + const std::vector<vkcv::ImageHandle> prepassRenderTargets = { + m_renderTargets.motionBuffer, + m_renderTargets.depthBuffer }; + + std::vector<vkcv::DrawcallInfo> prepassSceneDrawcalls; + for (const Object& obj : sceneObjects) { + prepassSceneDrawcalls.push_back(vkcv::DrawcallInfo(obj.meshResources.mesh, {})); + } + + m_core.recordDrawcallsToCmdStream( + cmdStream, + m_prePass.renderPass, + m_prePass.pipeline, + prepassPushConstants, + prepassSceneDrawcalls, + prepassRenderTargets); + + // sky prepass + glm::mat4 skyPrepassMatrices[2] = { + viewProjection, + viewProjectionPrevious }; + vkcv::PushConstants skyPrepassPushConstants(sizeof(glm::mat4) * 2); + skyPrepassPushConstants.appendDrawcall(skyPrepassMatrices); + + m_core.recordDrawcallsToCmdStream( + cmdStream, + m_skyPrePass.renderPass, + m_skyPrePass.pipeline, + skyPrepassPushConstants, + { skyDrawcall }, + prepassRenderTargets); + + // main pass + const std::vector<vkcv::ImageHandle> renderTargets = { + m_renderTargets.colorBuffer, + m_renderTargets.depthBuffer }; + + vkcv::PushConstants meshPushConstants(2 * sizeof(glm::mat4)); + for (const Object& obj : sceneObjects) { + glm::mat4 matrices[2] = { obj.mvp, obj.modelMatrix }; + meshPushConstants.appendDrawcall(matrices); + } + + std::vector<vkcv::DrawcallInfo> forwardSceneDrawcalls; + for (const Object& obj : sceneObjects) { + forwardSceneDrawcalls.push_back(vkcv::DrawcallInfo( + obj.meshResources.mesh, + { vkcv::DescriptorSetUsage(0, m_core.getDescriptorSet(m_meshPass.descriptorSet).vulkanHandle) })); + } + + m_core.recordDrawcallsToCmdStream( + cmdStream, + m_meshPass.renderPass, + m_meshPass.pipeline, + meshPushConstants, + forwardSceneDrawcalls, + renderTargets); + + // sky + vkcv::PushConstants skyPushConstants(sizeof(glm::mat4)); + skyPushConstants.appendDrawcall(viewProjection); + + m_core.recordDrawcallsToCmdStream( + cmdStream, + m_skyPass.renderPass, + m_skyPass.pipeline, + skyPushConstants, + { skyDrawcall }, + renderTargets); + + // motion blur + vkcv::ImageHandle motionBlurOutput; + + if (motionVectorVisualisationMode == eMotionVectorVisualisationMode::None) { + float cameraNear; + float cameraFar; + m_cameraManager.getActiveCamera().getNearFar(cameraNear, cameraFar); + + motionBlurOutput = m_motionBlur.render( + cmdStream, + m_renderTargets.motionBuffer, + m_renderTargets.colorBuffer, + m_renderTargets.depthBuffer, + motionBlurMode, + cameraNear, + cameraFar, + fDeltaTimeSeconds, + cameraShutterSpeedInverse, + motionBlurTileOffsetLength, + motionBlurFastPathThreshold); + } + else { + motionBlurOutput = m_motionBlur.renderMotionVectorVisualisation( + cmdStream, + m_renderTargets.motionBuffer, + motionVectorVisualisationMode, + motionVectorVisualisationRange); + } + + // gamma correction + vkcv::DescriptorWrites gammaCorrectionDescriptorWrites; + gammaCorrectionDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, motionBlurOutput) }; + gammaCorrectionDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(1, m_linearSampler) }; + gammaCorrectionDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(2, swapchainInput) }; + + m_core.writeDescriptorSet(m_gammaCorrectionPass.descriptorSet, gammaCorrectionDescriptorWrites); + + m_core.prepareImageForSampling(cmdStream, motionBlurOutput); + m_core.prepareImageForStorage (cmdStream, swapchainInput); + + const uint32_t fullScreenImageDispatch[3] = { + static_cast<uint32_t>((m_windowWidth + 7) / 8), + static_cast<uint32_t>((m_windowHeight + 7) / 8), + static_cast<uint32_t>(1) }; + + m_core.recordComputeDispatchToCmdStream( + cmdStream, + m_gammaCorrectionPass.pipeline, + fullScreenImageDispatch, + { vkcv::DescriptorSetUsage(0, m_core.getDescriptorSet(m_gammaCorrectionPass.descriptorSet).vulkanHandle) }, + vkcv::PushConstants(0)); + + m_core.prepareSwapchainImageForPresent(cmdStream); + m_core.submitCommandStream(cmdStream); + + gui.beginGUI(); + ImGui::Begin("Settings"); + + ImGui::Checkbox("Freeze frame", &freezeFrame); + ImGui::InputFloat("Motion tile offset length", &motionBlurTileOffsetLength); + ImGui::InputFloat("Motion blur fast path threshold", &motionBlurFastPathThreshold); + + ImGui::Combo( + "Motion blur mode", + reinterpret_cast<int*>(&motionBlurMode), + MotionBlurModeLabels, + static_cast<int>(eMotionBlurMode::OptionCount)); + + ImGui::Combo( + "Debug view", + reinterpret_cast<int*>(&motionVectorVisualisationMode), + MotionVectorVisualisationModeLabels, + static_cast<int>(eMotionVectorVisualisationMode::OptionCount)); + + if (motionVectorVisualisationMode != eMotionVectorVisualisationMode::None) + ImGui::InputFloat("Motion vector visualisation range", &motionVectorVisualisationRange); + + ImGui::InputInt("Camera shutter speed inverse", &cameraShutterSpeedInverse); + + ImGui::InputFloat("Object movement speed", &objectVerticalSpeed); + ImGui::InputFloat("Object movement amplitude", &objectAmplitude); + ImGui::InputFloat("Object mean height", &objectMeanHeight); + ImGui::InputFloat("Object rotation speed X", &objectRotationSpeedX); + ImGui::InputFloat("Object rotation speed Y", &objectRotationSpeedY); + + ImGui::End(); + gui.endGUI(); + + m_core.endFrame(); + } +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/App.hpp b/projects/indirect_dispatch/src/App.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d580793b0fdc4e7dc8c8654d29a75f04e14ea422 --- /dev/null +++ b/projects/indirect_dispatch/src/App.hpp @@ -0,0 +1,38 @@ +#pragma once +#include <vkcv/Core.hpp> +#include <vkcv/camera/CameraManager.hpp> +#include "AppSetup.hpp" +#include "MotionBlur.hpp" + +class App { +public: + App(); + bool initialize(); + void run(); +private: + const char* m_applicationName; + + int m_windowWidth; + int m_windowHeight; + + vkcv::Window m_window; + vkcv::Core m_core; + vkcv::camera::CameraManager m_cameraManager; + + MotionBlur m_motionBlur; + + vkcv::ImageHandle m_gridTexture; + + MeshResources m_cubeMesh; + MeshResources m_groundMesh; + + GraphicPassHandles m_meshPass; + GraphicPassHandles m_skyPass; + GraphicPassHandles m_prePass; + GraphicPassHandles m_skyPrePass; + + ComputePassHandles m_gammaCorrectionPass; + + AppRenderTargets m_renderTargets; + vkcv::SamplerHandle m_linearSampler; +}; \ No newline at end of file diff --git a/projects/indirect_dispatch/src/AppConfig.hpp b/projects/indirect_dispatch/src/AppConfig.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c89c34ea8e3c0c45708ca998a642faffb31403d3 --- /dev/null +++ b/projects/indirect_dispatch/src/AppConfig.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "vulkan/vulkan.hpp" + +namespace AppConfig{ + const int defaultWindowWidth = 1280; + const int defaultWindowHeight = 720; + const vk::Format depthBufferFormat = vk::Format::eD32Sfloat; + const vk::Format colorBufferFormat = vk::Format::eB10G11R11UfloatPack32; + const vk::Format motionBufferFormat = vk::Format::eR16G16Sfloat; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/AppSetup.cpp b/projects/indirect_dispatch/src/AppSetup.cpp new file mode 100644 index 0000000000000000000000000000000000000000..023e30fc63360d426856be3940749e95089f2577 --- /dev/null +++ b/projects/indirect_dispatch/src/AppSetup.cpp @@ -0,0 +1,301 @@ +#include "AppSetup.hpp" +#include "AppConfig.hpp" +#include <vkcv/asset/asset_loader.hpp> +#include <vkcv/shader/GLSLCompiler.hpp> + +bool loadMesh(vkcv::Core& core, const std::filesystem::path& path, MeshResources* outMesh) { + assert(outMesh); + + vkcv::asset::Scene scene; + const int meshLoadResult = vkcv::asset::loadScene(path.string(), scene); + + if (meshLoadResult != 1) { + vkcv_log(vkcv::LogLevel::ERROR, "Mesh loading failed"); + return false; + } + + if (scene.meshes.size() < 1) { + vkcv_log(vkcv::LogLevel::ERROR, "Cube mesh scene does not contain any vertex groups"); + return false; + } + assert(!scene.vertexGroups.empty()); + + auto& vertexData = scene.vertexGroups[0].vertexBuffer; + auto& indexData = scene.vertexGroups[0].indexBuffer; + + vkcv::Buffer vertexBuffer = core.createBuffer<uint8_t>( + vkcv::BufferType::VERTEX, + vertexData.data.size(), + vkcv::BufferMemoryType::DEVICE_LOCAL); + + vkcv::Buffer indexBuffer = core.createBuffer<uint8_t>( + vkcv::BufferType::INDEX, + indexData.data.size(), + vkcv::BufferMemoryType::DEVICE_LOCAL); + + vertexBuffer.fill(vertexData.data); + indexBuffer.fill(indexData.data); + + outMesh->vertexBuffer = vertexBuffer.getHandle(); + outMesh->indexBuffer = indexBuffer.getHandle(); + + auto& attributes = vertexData.attributes; + + std::sort(attributes.begin(), attributes.end(), + [](const vkcv::asset::VertexAttribute& x, const vkcv::asset::VertexAttribute& y) { + return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); + }); + + const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { + vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[0].offset), vertexBuffer.getVulkanHandle()), + vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[1].offset), vertexBuffer.getVulkanHandle()), + vkcv::VertexBufferBinding(static_cast<vk::DeviceSize>(attributes[2].offset), vertexBuffer.getVulkanHandle()) }; + + outMesh->mesh = vkcv::Mesh(vertexBufferBindings, indexBuffer.getVulkanHandle(), scene.vertexGroups[0].numIndices); + + return true; +} + +bool loadImage(vkcv::Core& core, const std::filesystem::path& path, vkcv::ImageHandle* outImage) { + + assert(outImage); + + const vkcv::asset::TextureData textureData = vkcv::asset::loadTexture(path); + + if (textureData.componentCount != 4) { + vkcv_log(vkcv::LogLevel::ERROR, "Expecting image with four components"); + return false; + } + + vkcv::Image image = core.createImage( + vk::Format::eR8G8B8A8Srgb, + textureData.width, + textureData.height, + 1, + true); + + image.fill(textureData.data.data(), textureData.data.size()); + image.generateMipChainImmediate(); + image.switchLayout(vk::ImageLayout::eReadOnlyOptimalKHR); + + *outImage = image.getHandle(); + return true; +} + +bool loadGraphicPass( + vkcv::Core& core, + const std::filesystem::path vertexPath, + const std::filesystem::path fragmentPath, + const vkcv::PassConfig& passConfig, + const vkcv::DepthTest depthTest, + GraphicPassHandles* outPassHandles) { + + assert(outPassHandles); + + outPassHandles->renderPass = core.createPass(passConfig); + + if (!outPassHandles->renderPass) { + vkcv_log(vkcv::LogLevel::ERROR, "Error: Could not create renderpass"); + return false; + } + + vkcv::ShaderProgram shaderProgram; + vkcv::shader::GLSLCompiler compiler; + + compiler.compile(vkcv::ShaderStage::VERTEX, vertexPath, + [&shaderProgram](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + shaderProgram.addShader(shaderStage, path); + }); + + compiler.compile(vkcv::ShaderStage::FRAGMENT, fragmentPath, + [&shaderProgram](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + shaderProgram.addShader(shaderStage, path); + }); + + const std::vector<vkcv::VertexAttachment> vertexAttachments = shaderProgram.getVertexAttachments(); + std::vector<vkcv::VertexBinding> bindings; + for (size_t i = 0; i < vertexAttachments.size(); i++) { + bindings.push_back(vkcv::VertexBinding(i, { vertexAttachments[i] })); + } + + const vkcv::VertexLayout vertexLayout(bindings); + + const auto descriptorBindings = shaderProgram.getReflectedDescriptors(); + const bool hasDescriptor = descriptorBindings.size() > 0; + if (hasDescriptor) + outPassHandles->descriptorSet = core.createDescriptorSet(descriptorBindings[0]); + + std::vector<vk::DescriptorSetLayout> descriptorLayouts; + if (hasDescriptor) + descriptorLayouts.push_back(core.getDescriptorSet(outPassHandles->descriptorSet).layout); + + vkcv::PipelineConfig pipelineConfig{ + shaderProgram, + UINT32_MAX, + UINT32_MAX, + outPassHandles->renderPass, + { vertexLayout }, + descriptorLayouts, + true }; + pipelineConfig.m_depthTest = depthTest; + outPassHandles->pipeline = core.createGraphicsPipeline(pipelineConfig); + + if (!outPassHandles->pipeline) { + vkcv_log(vkcv::LogLevel::ERROR, "Error: Could not create graphics pipeline"); + return false; + } + + return true; +} + +bool loadMeshPass(vkcv::Core& core, GraphicPassHandles* outHandles) { + + assert(outHandles); + + vkcv::AttachmentDescription colorAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::DONT_CARE, + AppConfig::colorBufferFormat); + + vkcv::AttachmentDescription depthAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::LOAD, + AppConfig::depthBufferFormat); + + return loadGraphicPass( + core, + "resources/shaders/mesh.vert", + "resources/shaders/mesh.frag", + vkcv::PassConfig({ colorAttachment, depthAttachment }), + vkcv::DepthTest::Equal, + outHandles); +} + +bool loadSkyPass(vkcv::Core& core, GraphicPassHandles* outHandles) { + + assert(outHandles); + + vkcv::AttachmentDescription colorAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::LOAD, + AppConfig::colorBufferFormat); + + vkcv::AttachmentDescription depthAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::LOAD, + AppConfig::depthBufferFormat); + + return loadGraphicPass( + core, + "resources/shaders/sky.vert", + "resources/shaders/sky.frag", + vkcv::PassConfig({ colorAttachment, depthAttachment }), + vkcv::DepthTest::Equal, + outHandles); +} + +bool loadPrePass(vkcv::Core& core, GraphicPassHandles* outHandles) { + assert(outHandles); + + vkcv::AttachmentDescription motionAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::CLEAR, + AppConfig::motionBufferFormat); + + vkcv::AttachmentDescription depthAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::CLEAR, + AppConfig::depthBufferFormat); + + return loadGraphicPass( + core, + "resources/shaders/prepass.vert", + "resources/shaders/prepass.frag", + vkcv::PassConfig({ motionAttachment, depthAttachment }), + vkcv::DepthTest::LessEqual, + outHandles); +} + +bool loadSkyPrePass(vkcv::Core& core, GraphicPassHandles* outHandles) { + assert(outHandles); + + vkcv::AttachmentDescription motionAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::LOAD, + AppConfig::motionBufferFormat); + + vkcv::AttachmentDescription depthAttachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::LOAD, + AppConfig::depthBufferFormat); + + return loadGraphicPass( + core, + "resources/shaders/skyPrepass.vert", + "resources/shaders/skyPrepass.frag", + vkcv::PassConfig({ motionAttachment, depthAttachment }), + vkcv::DepthTest::LessEqual, + outHandles); +} + +bool loadComputePass(vkcv::Core& core, const std::filesystem::path& path, ComputePassHandles* outComputePass) { + + assert(outComputePass); + vkcv::ShaderProgram shaderProgram; + vkcv::shader::GLSLCompiler compiler; + + compiler.compile(vkcv::ShaderStage::COMPUTE, path, + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + shaderProgram.addShader(shaderStage, path); + }); + + if (shaderProgram.getReflectedDescriptors().size() < 1) { + vkcv_log(vkcv::LogLevel::ERROR, "Compute shader has no descriptor set"); + return false; + } + + outComputePass->descriptorSet = core.createDescriptorSet(shaderProgram.getReflectedDescriptors()[0]); + + outComputePass->pipeline = core.createComputePipeline( + shaderProgram, + { core.getDescriptorSet(outComputePass->descriptorSet).layout }); + + if (!outComputePass->pipeline) { + vkcv_log(vkcv::LogLevel::ERROR, "Compute shader pipeline creation failed"); + return false; + } + + return true; +} + +AppRenderTargets createRenderTargets(vkcv::Core& core, const uint32_t width, const uint32_t height) { + + AppRenderTargets targets; + + targets.depthBuffer = core.createImage( + AppConfig::depthBufferFormat, + width, + height, + 1, + false).getHandle(); + + targets.colorBuffer = core.createImage( + AppConfig::colorBufferFormat, + width, + height, + 1, + false, + false, + true).getHandle(); + + targets.motionBuffer = core.createImage( + AppConfig::motionBufferFormat, + width, + height, + 1, + false, + false, + true).getHandle(); + + return targets; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/AppSetup.hpp b/projects/indirect_dispatch/src/AppSetup.hpp new file mode 100644 index 0000000000000000000000000000000000000000..3125bc516b553de715d6e51bbda259e3e16f758f --- /dev/null +++ b/projects/indirect_dispatch/src/AppSetup.hpp @@ -0,0 +1,47 @@ +#pragma once +#include <vkcv/Core.hpp> + +struct AppRenderTargets { + vkcv::ImageHandle depthBuffer; + vkcv::ImageHandle colorBuffer; + vkcv::ImageHandle motionBuffer; +}; + +struct GraphicPassHandles { + vkcv::PipelineHandle pipeline; + vkcv::PassHandle renderPass; + vkcv::DescriptorSetHandle descriptorSet; +}; + +struct ComputePassHandles { + vkcv::PipelineHandle pipeline; + vkcv::DescriptorSetHandle descriptorSet; +}; + +struct MeshResources { + vkcv::Mesh mesh; + vkcv::BufferHandle vertexBuffer; + vkcv::BufferHandle indexBuffer; +}; + +// loads position, uv and normal of the first mesh in a scene +bool loadMesh(vkcv::Core& core, const std::filesystem::path& path, MeshResources* outMesh); + +bool loadImage(vkcv::Core& core, const std::filesystem::path& path, vkcv::ImageHandle* outImage); + +bool loadGraphicPass( + vkcv::Core& core, + const std::filesystem::path vertexPath, + const std::filesystem::path fragmentPath, + const vkcv::PassConfig& passConfig, + const vkcv::DepthTest depthTest, + GraphicPassHandles* outPassHandles); + +bool loadMeshPass (vkcv::Core& core, GraphicPassHandles* outHandles); +bool loadSkyPass (vkcv::Core& core, GraphicPassHandles* outHandles); +bool loadPrePass (vkcv::Core& core, GraphicPassHandles* outHandles); +bool loadSkyPrePass(vkcv::Core& core, GraphicPassHandles* outHandles); + +bool loadComputePass(vkcv::Core& core, const std::filesystem::path& path, ComputePassHandles* outComputePass); + +AppRenderTargets createRenderTargets(vkcv::Core& core, const uint32_t width, const uint32_t height); \ No newline at end of file diff --git a/projects/indirect_dispatch/src/MotionBlur.cpp b/projects/indirect_dispatch/src/MotionBlur.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49f650a97e2fea5821959ae53f468e6fe7de6ffe --- /dev/null +++ b/projects/indirect_dispatch/src/MotionBlur.cpp @@ -0,0 +1,437 @@ +#include "MotionBlur.hpp" +#include "MotionBlurConfig.hpp" +#include "MotionBlurSetup.hpp" +#include <array> + +std::array<uint32_t, 3> computeFullscreenDispatchSize( + const uint32_t imageWidth, + const uint32_t imageHeight, + const uint32_t localGroupSize) { + + // optimized divide and ceil + return std::array<uint32_t, 3>{ + static_cast<uint32_t>(imageWidth + (localGroupSize - 1)) / localGroupSize, + static_cast<uint32_t>(imageHeight + (localGroupSize - 1)) / localGroupSize, + static_cast<uint32_t>(1) }; +} + +bool MotionBlur::initialize(vkcv::Core* corePtr, const uint32_t targetWidth, const uint32_t targetHeight) { + + if (!corePtr) { + vkcv_log(vkcv::LogLevel::ERROR, "MotionBlur got invalid corePtr") + return false; + } + + m_core = corePtr; + + if (!loadComputePass(*m_core, "resources/shaders/motionBlur.comp", &m_motionBlurPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionVectorMinMax.comp", &m_motionVectorMinMaxPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionVectorMinMaxNeighbourhood.comp", &m_motionVectorMinMaxNeighbourhoodPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionVectorVisualisation.comp", &m_motionVectorVisualisationPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionBlurColorCopy.comp", &m_colorCopyPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionBlurTileClassification.comp", &m_tileClassificationPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionBlurWorkTileReset.comp", &m_tileResetPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionBlurTileClassificationVis.comp", &m_tileVisualisationPass)) + return false; + + if (!loadComputePass(*m_core, "resources/shaders/motionBlurFastPath.comp", &m_motionBlurFastPathPass)) + return false; + + // work tile buffers and descriptors + const uint32_t workTileBufferSize = static_cast<uint32_t>(2 * sizeof(uint32_t)) * (3 + + ((MotionBlurConfig::maxWidth + MotionBlurConfig::maxMotionTileSize - 1) / MotionBlurConfig::maxMotionTileSize) * + ((MotionBlurConfig::maxHeight + MotionBlurConfig::maxMotionTileSize - 1) / MotionBlurConfig::maxMotionTileSize)); + + m_copyPathWorkTileBuffer = m_core->createBuffer<uint32_t>( + vkcv::BufferType::STORAGE, + workTileBufferSize, + vkcv::BufferMemoryType::DEVICE_LOCAL, + true).getHandle(); + + m_fullPathWorkTileBuffer = m_core->createBuffer<uint32_t>( + vkcv::BufferType::STORAGE, + workTileBufferSize, + vkcv::BufferMemoryType::DEVICE_LOCAL, + true).getHandle(); + + m_fastPathWorkTileBuffer = m_core->createBuffer<uint32_t>( + vkcv::BufferType::STORAGE, + workTileBufferSize, + vkcv::BufferMemoryType::DEVICE_LOCAL, + true).getHandle(); + + vkcv::DescriptorWrites tileResetDescriptorWrites; + tileResetDescriptorWrites.storageBufferWrites = { + vkcv::BufferDescriptorWrite(0, m_fullPathWorkTileBuffer), + vkcv::BufferDescriptorWrite(1, m_copyPathWorkTileBuffer), + vkcv::BufferDescriptorWrite(2, m_fastPathWorkTileBuffer) }; + + m_core->writeDescriptorSet(m_tileResetPass.descriptorSet, tileResetDescriptorWrites); + + + m_renderTargets = MotionBlurSetup::createRenderTargets(targetWidth, targetHeight, *m_core); + + m_nearestSampler = m_core->createSampler( + vkcv::SamplerFilterType::NEAREST, + vkcv::SamplerFilterType::NEAREST, + vkcv::SamplerMipmapMode::NEAREST, + vkcv::SamplerAddressMode::CLAMP_TO_EDGE); + + return true; +} + +void MotionBlur::setResolution(const uint32_t targetWidth, const uint32_t targetHeight) { + m_renderTargets = MotionBlurSetup::createRenderTargets(targetWidth, targetHeight, *m_core); +} + +vkcv::ImageHandle MotionBlur::render( + const vkcv::CommandStreamHandle cmdStream, + const vkcv::ImageHandle motionBufferFullRes, + const vkcv::ImageHandle colorBuffer, + const vkcv::ImageHandle depthBuffer, + const eMotionBlurMode mode, + const float cameraNear, + const float cameraFar, + const float deltaTimeSeconds, + const float cameraShutterSpeedInverse, + const float motionTileOffsetLength, + const float fastPathThreshold) { + + computeMotionTiles(cmdStream, motionBufferFullRes); + + // work tile reset + const uint32_t dispatchSizeOne[3] = { 1, 1, 1 }; + + m_core->recordComputeDispatchToCmdStream( + cmdStream, + m_tileResetPass.pipeline, + dispatchSizeOne, + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_tileResetPass.descriptorSet).vulkanHandle) }, + vkcv::PushConstants(0)); + + m_core->recordBufferMemoryBarrier(cmdStream, m_fullPathWorkTileBuffer); + m_core->recordBufferMemoryBarrier(cmdStream, m_copyPathWorkTileBuffer); + m_core->recordBufferMemoryBarrier(cmdStream, m_fastPathWorkTileBuffer); + + // work tile classification + vkcv::DescriptorWrites tileClassificationDescriptorWrites; + tileClassificationDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, m_renderTargets.motionMaxNeighbourhood), + vkcv::SampledImageDescriptorWrite(1, m_renderTargets.motionMinNeighbourhood) }; + tileClassificationDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(2, m_nearestSampler) }; + tileClassificationDescriptorWrites.storageBufferWrites = { + vkcv::BufferDescriptorWrite(3, m_fullPathWorkTileBuffer), + vkcv::BufferDescriptorWrite(4, m_copyPathWorkTileBuffer), + vkcv::BufferDescriptorWrite(5, m_fastPathWorkTileBuffer) }; + + m_core->writeDescriptorSet(m_tileClassificationPass.descriptorSet, tileClassificationDescriptorWrites); + + const auto tileClassificationDispatch = computeFullscreenDispatchSize( + m_core->getImageWidth(m_renderTargets.motionMaxNeighbourhood), + m_core->getImageHeight(m_renderTargets.motionMaxNeighbourhood), + 8); + + struct ClassificationConstants { + uint32_t width; + uint32_t height; + float fastPathThreshold; + }; + ClassificationConstants classificationConstants; + classificationConstants.width = m_core->getImageWidth(m_renderTargets.outputColor); + classificationConstants.height = m_core->getImageHeight(m_renderTargets.outputColor); + classificationConstants.fastPathThreshold = fastPathThreshold; + + vkcv::PushConstants classificationPushConstants(sizeof(ClassificationConstants)); + classificationPushConstants.appendDrawcall(classificationConstants); + + m_core->prepareImageForSampling(cmdStream, m_renderTargets.motionMaxNeighbourhood); + m_core->prepareImageForSampling(cmdStream, m_renderTargets.motionMinNeighbourhood); + + m_core->recordComputeDispatchToCmdStream( + cmdStream, + m_tileClassificationPass.pipeline, + tileClassificationDispatch.data(), + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_tileClassificationPass.descriptorSet).vulkanHandle) }, + classificationPushConstants); + + m_core->recordBufferMemoryBarrier(cmdStream, m_fullPathWorkTileBuffer); + m_core->recordBufferMemoryBarrier(cmdStream, m_copyPathWorkTileBuffer); + m_core->recordBufferMemoryBarrier(cmdStream, m_fastPathWorkTileBuffer); + + vkcv::DescriptorWrites motionBlurDescriptorWrites; + motionBlurDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, colorBuffer), + vkcv::SampledImageDescriptorWrite(1, depthBuffer), + vkcv::SampledImageDescriptorWrite(2, motionBufferFullRes), + vkcv::SampledImageDescriptorWrite(3, m_renderTargets.motionMaxNeighbourhood) }; + motionBlurDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(4, m_nearestSampler) }; + motionBlurDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(5, m_renderTargets.outputColor) }; + motionBlurDescriptorWrites.storageBufferWrites = { + vkcv::BufferDescriptorWrite(6, m_fullPathWorkTileBuffer)}; + + m_core->writeDescriptorSet(m_motionBlurPass.descriptorSet, motionBlurDescriptorWrites); + + + vkcv::DescriptorWrites colorCopyDescriptorWrites; + colorCopyDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, colorBuffer) }; + colorCopyDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(1, m_nearestSampler) }; + colorCopyDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(2, m_renderTargets.outputColor) }; + colorCopyDescriptorWrites.storageBufferWrites = { + vkcv::BufferDescriptorWrite(3, m_copyPathWorkTileBuffer) }; + + m_core->writeDescriptorSet(m_colorCopyPass.descriptorSet, colorCopyDescriptorWrites); + + + vkcv::DescriptorWrites fastPathDescriptorWrites; + fastPathDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, colorBuffer), + vkcv::SampledImageDescriptorWrite(1, m_renderTargets.motionMaxNeighbourhood) }; + fastPathDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(2, m_nearestSampler) }; + fastPathDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(3, m_renderTargets.outputColor) }; + fastPathDescriptorWrites.storageBufferWrites = { + vkcv::BufferDescriptorWrite(4, m_fastPathWorkTileBuffer) }; + + m_core->writeDescriptorSet(m_motionBlurFastPathPass.descriptorSet, fastPathDescriptorWrites); + + // must match layout in "motionBlur.comp" + struct MotionBlurConstantData { + float motionFactor; + float cameraNearPlane; + float cameraFarPlane; + float motionTileOffsetLength; + }; + MotionBlurConstantData motionBlurConstantData; + + const float deltaTimeMotionBlur = deltaTimeSeconds; + + motionBlurConstantData.motionFactor = 1 / (deltaTimeMotionBlur * cameraShutterSpeedInverse); + motionBlurConstantData.cameraNearPlane = cameraNear; + motionBlurConstantData.cameraFarPlane = cameraFar; + motionBlurConstantData.motionTileOffsetLength = motionTileOffsetLength; + + vkcv::PushConstants motionBlurPushConstants(sizeof(motionBlurConstantData)); + motionBlurPushConstants.appendDrawcall(motionBlurConstantData); + + struct FastPathConstants { + float motionFactor; + }; + FastPathConstants fastPathConstants; + fastPathConstants.motionFactor = motionBlurConstantData.motionFactor; + + vkcv::PushConstants fastPathPushConstants(sizeof(FastPathConstants)); + fastPathPushConstants.appendDrawcall(fastPathConstants); + + m_core->prepareImageForStorage(cmdStream, m_renderTargets.outputColor); + m_core->prepareImageForSampling(cmdStream, colorBuffer); + m_core->prepareImageForSampling(cmdStream, depthBuffer); + m_core->prepareImageForSampling(cmdStream, m_renderTargets.motionMaxNeighbourhood); + + if (mode == eMotionBlurMode::Default) { + m_core->recordComputeIndirectDispatchToCmdStream( + cmdStream, + m_motionBlurPass.pipeline, + m_fullPathWorkTileBuffer, + 0, + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_motionBlurPass.descriptorSet).vulkanHandle) }, + motionBlurPushConstants); + + m_core->recordComputeIndirectDispatchToCmdStream( + cmdStream, + m_colorCopyPass.pipeline, + m_copyPathWorkTileBuffer, + 0, + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_colorCopyPass.descriptorSet).vulkanHandle) }, + vkcv::PushConstants(0)); + + m_core->recordComputeIndirectDispatchToCmdStream( + cmdStream, + m_motionBlurFastPathPass.pipeline, + m_fastPathWorkTileBuffer, + 0, + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_motionBlurFastPathPass.descriptorSet).vulkanHandle) }, + fastPathPushConstants); + } + else if(mode == eMotionBlurMode::Disabled) { + return colorBuffer; + } + else if (mode == eMotionBlurMode::TileVisualisation) { + + vkcv::DescriptorWrites visualisationDescriptorWrites; + visualisationDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, colorBuffer) }; + visualisationDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(1, m_nearestSampler) }; + visualisationDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(2, m_renderTargets.outputColor)}; + visualisationDescriptorWrites.storageBufferWrites = { + vkcv::BufferDescriptorWrite(3, m_fullPathWorkTileBuffer), + vkcv::BufferDescriptorWrite(4, m_copyPathWorkTileBuffer), + vkcv::BufferDescriptorWrite(5, m_fastPathWorkTileBuffer) }; + + m_core->writeDescriptorSet(m_tileVisualisationPass.descriptorSet, visualisationDescriptorWrites); + + const uint32_t tileCount = + (m_core->getImageWidth(m_renderTargets.outputColor) + MotionBlurConfig::maxMotionTileSize - 1) / MotionBlurConfig::maxMotionTileSize * + (m_core->getImageHeight(m_renderTargets.outputColor) + MotionBlurConfig::maxMotionTileSize - 1) / MotionBlurConfig::maxMotionTileSize; + + const uint32_t dispatchCounts[3] = { + tileCount, + 1, + 1 }; + + m_core->recordComputeDispatchToCmdStream( + cmdStream, + m_tileVisualisationPass.pipeline, + dispatchCounts, + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_tileVisualisationPass.descriptorSet).vulkanHandle) }, + vkcv::PushConstants(0)); + } + else { + vkcv_log(vkcv::LogLevel::ERROR, "Unknown eMotionBlurMode enum option"); + return colorBuffer; + } + + return m_renderTargets.outputColor; +} + +vkcv::ImageHandle MotionBlur::renderMotionVectorVisualisation( + const vkcv::CommandStreamHandle cmdStream, + const vkcv::ImageHandle motionBuffer, + const eMotionVectorVisualisationMode mode, + const float velocityRange) { + + computeMotionTiles(cmdStream, motionBuffer); + + vkcv::ImageHandle visualisationInput; + if ( mode == eMotionVectorVisualisationMode::FullResolution) + visualisationInput = motionBuffer; + else if (mode == eMotionVectorVisualisationMode::MaxTile) + visualisationInput = m_renderTargets.motionMax; + else if (mode == eMotionVectorVisualisationMode::MaxTileNeighbourhood) + visualisationInput = m_renderTargets.motionMaxNeighbourhood; + else if (mode == eMotionVectorVisualisationMode::MinTile) + visualisationInput = m_renderTargets.motionMin; + else if (mode == eMotionVectorVisualisationMode::MinTileNeighbourhood) + visualisationInput = m_renderTargets.motionMinNeighbourhood; + else if (mode == eMotionVectorVisualisationMode::None) { + vkcv_log(vkcv::LogLevel::ERROR, "renderMotionVectorVisualisation called with visualisation mode 'None'"); + return motionBuffer; + } + else { + vkcv_log(vkcv::LogLevel::ERROR, "Unknown eDebugView enum value"); + return motionBuffer; + } + + vkcv::DescriptorWrites motionVectorVisualisationDescriptorWrites; + motionVectorVisualisationDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, visualisationInput) }; + motionVectorVisualisationDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(1, m_nearestSampler) }; + motionVectorVisualisationDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(2, m_renderTargets.outputColor) }; + + m_core->writeDescriptorSet( + m_motionVectorVisualisationPass.descriptorSet, + motionVectorVisualisationDescriptorWrites); + + m_core->prepareImageForSampling(cmdStream, visualisationInput); + m_core->prepareImageForStorage(cmdStream, m_renderTargets.outputColor); + + vkcv::PushConstants motionVectorVisualisationPushConstants(sizeof(float)); + motionVectorVisualisationPushConstants.appendDrawcall(velocityRange); + + const auto dispatchSizes = computeFullscreenDispatchSize( + m_core->getImageWidth(m_renderTargets.outputColor), + m_core->getImageHeight(m_renderTargets.outputColor), + 8); + + m_core->recordComputeDispatchToCmdStream( + cmdStream, + m_motionVectorVisualisationPass.pipeline, + dispatchSizes.data(), + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_motionVectorVisualisationPass.descriptorSet).vulkanHandle) }, + motionVectorVisualisationPushConstants); + + return m_renderTargets.outputColor; +} + +void MotionBlur::computeMotionTiles( + const vkcv::CommandStreamHandle cmdStream, + const vkcv::ImageHandle motionBufferFullRes) { + + // motion vector min max tiles + vkcv::DescriptorWrites motionVectorMaxTilesDescriptorWrites; + motionVectorMaxTilesDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, motionBufferFullRes) }; + motionVectorMaxTilesDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(1, m_nearestSampler) }; + motionVectorMaxTilesDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(2, m_renderTargets.motionMax), + vkcv::StorageImageDescriptorWrite(3, m_renderTargets.motionMin) }; + + m_core->writeDescriptorSet(m_motionVectorMinMaxPass.descriptorSet, motionVectorMaxTilesDescriptorWrites); + + m_core->prepareImageForSampling(cmdStream, motionBufferFullRes); + m_core->prepareImageForStorage(cmdStream, m_renderTargets.motionMax); + m_core->prepareImageForStorage(cmdStream, m_renderTargets.motionMin); + + const std::array<uint32_t, 3> motionTileDispatchCounts = computeFullscreenDispatchSize( + m_core->getImageWidth( m_renderTargets.motionMax), + m_core->getImageHeight(m_renderTargets.motionMax), + 8); + + m_core->recordComputeDispatchToCmdStream( + cmdStream, + m_motionVectorMinMaxPass.pipeline, + motionTileDispatchCounts.data(), + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_motionVectorMinMaxPass.descriptorSet).vulkanHandle) }, + vkcv::PushConstants(0)); + + // motion vector min max neighbourhood + vkcv::DescriptorWrites motionVectorMaxNeighbourhoodDescriptorWrites; + motionVectorMaxNeighbourhoodDescriptorWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, m_renderTargets.motionMax), + vkcv::SampledImageDescriptorWrite(1, m_renderTargets.motionMin) }; + motionVectorMaxNeighbourhoodDescriptorWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(2, m_nearestSampler) }; + motionVectorMaxNeighbourhoodDescriptorWrites.storageImageWrites = { + vkcv::StorageImageDescriptorWrite(3, m_renderTargets.motionMaxNeighbourhood), + vkcv::StorageImageDescriptorWrite(4, m_renderTargets.motionMinNeighbourhood) }; + + m_core->writeDescriptorSet(m_motionVectorMinMaxNeighbourhoodPass.descriptorSet, motionVectorMaxNeighbourhoodDescriptorWrites); + + m_core->prepareImageForSampling(cmdStream, m_renderTargets.motionMax); + m_core->prepareImageForSampling(cmdStream, m_renderTargets.motionMin); + + m_core->prepareImageForStorage(cmdStream, m_renderTargets.motionMaxNeighbourhood); + m_core->prepareImageForStorage(cmdStream, m_renderTargets.motionMinNeighbourhood); + + m_core->recordComputeDispatchToCmdStream( + cmdStream, + m_motionVectorMinMaxNeighbourhoodPass.pipeline, + motionTileDispatchCounts.data(), + { vkcv::DescriptorSetUsage(0, m_core->getDescriptorSet(m_motionVectorMinMaxNeighbourhoodPass.descriptorSet).vulkanHandle) }, + vkcv::PushConstants(0)); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/MotionBlur.hpp b/projects/indirect_dispatch/src/MotionBlur.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b50f0af60d566dc0e4fc00c31b7b834e11679bf5 --- /dev/null +++ b/projects/indirect_dispatch/src/MotionBlur.hpp @@ -0,0 +1,84 @@ +#pragma once +#include <vkcv/Core.hpp> +#include "AppSetup.hpp" +#include "MotionBlurSetup.hpp" + +// selection for motion blur input and visualisation +enum class eMotionVectorVisualisationMode : int { + None = 0, + FullResolution = 1, + MaxTile = 2, + MaxTileNeighbourhood = 3, + MinTile = 4, + MinTileNeighbourhood = 5, + OptionCount = 6 }; + +static const char* MotionVectorVisualisationModeLabels[6] = { + "None", + "Full resolution", + "Max tile", + "Tile neighbourhood max", + "Min Tile", + "Tile neighbourhood min"}; + +enum class eMotionBlurMode : int { + Default = 0, + Disabled = 1, + TileVisualisation = 2, + OptionCount = 3 }; + +static const char* MotionBlurModeLabels[3] = { + "Default", + "Disabled", + "Tile visualisation" }; + +class MotionBlur { +public: + + bool initialize(vkcv::Core* corePtr, const uint32_t targetWidth, const uint32_t targetHeight); + void setResolution(const uint32_t targetWidth, const uint32_t targetHeight); + + vkcv::ImageHandle render( + const vkcv::CommandStreamHandle cmdStream, + const vkcv::ImageHandle motionBufferFullRes, + const vkcv::ImageHandle colorBuffer, + const vkcv::ImageHandle depthBuffer, + const eMotionBlurMode mode, + const float cameraNear, + const float cameraFar, + const float deltaTimeSeconds, + const float cameraShutterSpeedInverse, + const float motionTileOffsetLength, + const float fastPathThreshold); + + vkcv::ImageHandle renderMotionVectorVisualisation( + const vkcv::CommandStreamHandle cmdStream, + const vkcv::ImageHandle motionBuffer, + const eMotionVectorVisualisationMode mode, + const float velocityRange); + +private: + // computes max per tile and neighbourhood tile max + void computeMotionTiles( + const vkcv::CommandStreamHandle cmdStream, + const vkcv::ImageHandle motionBufferFullRes); + + vkcv::Core* m_core; + + MotionBlurRenderTargets m_renderTargets; + vkcv::SamplerHandle m_nearestSampler; + + ComputePassHandles m_motionBlurPass; + ComputePassHandles m_motionVectorMinMaxPass; + ComputePassHandles m_motionVectorMinMaxNeighbourhoodPass; + ComputePassHandles m_motionVectorVisualisationPass; + ComputePassHandles m_colorCopyPass; + ComputePassHandles m_tileClassificationPass; + ComputePassHandles m_tileResetPass; + ComputePassHandles m_tileVisualisationPass; + ComputePassHandles m_motionBlurFastPathPass; + + vkcv::BufferHandle m_fullPathWorkTileBuffer; + vkcv::BufferHandle m_copyPathWorkTileBuffer; + vkcv::BufferHandle m_fastPathWorkTileBuffer; +}; \ No newline at end of file diff --git a/projects/indirect_dispatch/src/MotionBlurConfig.hpp b/projects/indirect_dispatch/src/MotionBlurConfig.hpp new file mode 100644 index 0000000000000000000000000000000000000000..7552abd246ca8d2e7489c5065f43ef8b48af7cd2 --- /dev/null +++ b/projects/indirect_dispatch/src/MotionBlurConfig.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "vulkan/vulkan.hpp" + +namespace MotionBlurConfig { + const vk::Format motionVectorTileFormat = vk::Format::eR16G16Sfloat; + const vk::Format outputColorFormat = vk::Format::eB10G11R11UfloatPack32; + const uint32_t maxMotionTileSize = 16; // must match "motionTileSize" in motionBlurConfig.inc + const uint32_t maxWidth = 3840; + const uint32_t maxHeight = 2160; +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/MotionBlurSetup.cpp b/projects/indirect_dispatch/src/MotionBlurSetup.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82d2593a5b976f9389b58dddac43e3a45d1db303 --- /dev/null +++ b/projects/indirect_dispatch/src/MotionBlurSetup.cpp @@ -0,0 +1,57 @@ +#include "MotionBlurSetup.hpp" +#include "MotionBlurConfig.hpp" + +namespace MotionBlurSetup { + +MotionBlurRenderTargets createRenderTargets(const uint32_t width, const uint32_t height, vkcv::Core& core) { + + MotionBlurRenderTargets targets; + + // divide and ceil to int + const uint32_t motionMaxWidth = (width + (MotionBlurConfig::maxMotionTileSize - 1)) / MotionBlurConfig::maxMotionTileSize; + const uint32_t motionMaxheight = (height + (MotionBlurConfig::maxMotionTileSize - 1)) / MotionBlurConfig::maxMotionTileSize; + + targets.motionMax = core.createImage( + MotionBlurConfig::motionVectorTileFormat, + motionMaxWidth, + motionMaxheight, + 1, + false, + true).getHandle(); + + targets.motionMaxNeighbourhood = core.createImage( + MotionBlurConfig::motionVectorTileFormat, + motionMaxWidth, + motionMaxheight, + 1, + false, + true).getHandle(); + + targets.motionMin = core.createImage( + MotionBlurConfig::motionVectorTileFormat, + motionMaxWidth, + motionMaxheight, + 1, + false, + true).getHandle(); + + targets.motionMinNeighbourhood = core.createImage( + MotionBlurConfig::motionVectorTileFormat, + motionMaxWidth, + motionMaxheight, + 1, + false, + true).getHandle(); + + targets.outputColor = core.createImage( + MotionBlurConfig::outputColorFormat, + width, + height, + 1, + false, + true).getHandle(); + + return targets; +} + +} // namespace MotionBlurSetup \ No newline at end of file diff --git a/projects/indirect_dispatch/src/MotionBlurSetup.hpp b/projects/indirect_dispatch/src/MotionBlurSetup.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ca169d7c6b04aa152d42ba36c3d2e02e563bbd91 --- /dev/null +++ b/projects/indirect_dispatch/src/MotionBlurSetup.hpp @@ -0,0 +1,14 @@ +#pragma once +#include <vkcv/Core.hpp> + +struct MotionBlurRenderTargets { + vkcv::ImageHandle outputColor; + vkcv::ImageHandle motionMax; + vkcv::ImageHandle motionMaxNeighbourhood; + vkcv::ImageHandle motionMin; + vkcv::ImageHandle motionMinNeighbourhood; +}; + +namespace MotionBlurSetup { + MotionBlurRenderTargets createRenderTargets(const uint32_t width, const uint32_t height, vkcv::Core& core); +} \ No newline at end of file diff --git a/projects/indirect_dispatch/src/main.cpp b/projects/indirect_dispatch/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b27e0bcb8f1991d76b570b79da9cc4734cf52950 --- /dev/null +++ b/projects/indirect_dispatch/src/main.cpp @@ -0,0 +1,13 @@ +#include "App.hpp" + +int main(int argc, const char** argv) { + + App app; + if (!app.initialize()) { + std::cerr << "Application initialization failed, exiting" << std::endl; + return 1; + } + app.run(); + + return 0; +} diff --git a/projects/mesh_shader/src/main.cpp b/projects/mesh_shader/src/main.cpp index 3a94de5842f3e70625729c9755b8c88048ece2ec..611a324f875f5726ebd674e3ee51d27ad2d8e849 100644 --- a/projects/mesh_shader/src/main.cpp +++ b/projects/mesh_shader/src/main.cpp @@ -98,11 +98,6 @@ int main(int argc, const char** argv) { vkcv::gui::GUI gui (core, window); - const auto& context = core.getContext(); - const vk::Instance& instance = context.getInstance(); - const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice(); - const vk::Device& device = context.getDevice(); - vkcv::asset::Scene mesh; const char* path = argc > 1 ? argv[1] : "resources/Bunny/Bunny.glb"; vkcv::asset::loadScene(path, mesh); diff --git a/src/vkcv/BufferManager.cpp b/src/vkcv/BufferManager.cpp index 1998198513b18d061446201f178ccd96cb7d5b6a..f22d56650654f66dd1fea4141a449004dcad88cc 100644 --- a/src/vkcv/BufferManager.cpp +++ b/src/vkcv/BufferManager.cpp @@ -19,7 +19,7 @@ namespace vkcv { return; } - m_stagingBuffer = createBuffer(BufferType::STAGING, 1024 * 1024, BufferMemoryType::HOST_VISIBLE); + m_stagingBuffer = createBuffer(BufferType::STAGING, 1024 * 1024, BufferMemoryType::HOST_VISIBLE, false); } BufferManager::~BufferManager() noexcept { @@ -28,7 +28,7 @@ namespace vkcv { } } - BufferHandle BufferManager::createBuffer(BufferType type, size_t size, BufferMemoryType memoryType) { + BufferHandle BufferManager::createBuffer(BufferType type, size_t size, BufferMemoryType memoryType, bool supportIndirect) { vk::BufferCreateFlags createFlags; vk::BufferUsageFlags usageFlags; @@ -56,6 +56,8 @@ namespace vkcv { if (memoryType == BufferMemoryType::DEVICE_LOCAL) { usageFlags |= vk::BufferUsageFlagBits::eTransferDst; } + if (supportIndirect) + usageFlags |= vk::BufferUsageFlagBits::eIndirectBuffer; const vma::Allocator& allocator = m_core->getContext().getAllocator(); diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index e8e172dd236ac5cb49d0e2caf03599c198a07092..92e2df4f18f59355868e9dcce7a78c4e1a9c5cb7 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -507,6 +507,42 @@ namespace vkcv recordCommandsToStream(cmdStreamHandle, submitFunction, nullptr); } + void Core::recordComputeIndirectDispatchToCmdStream( + const CommandStreamHandle cmdStream, + const PipelineHandle computePipeline, + const vkcv::BufferHandle buffer, + const size_t bufferArgOffset, + const std::vector<DescriptorSetUsage>& descriptorSetUsages, + const PushConstants& pushConstants) { + + auto submitFunction = [&](const vk::CommandBuffer& cmdBuffer) { + + const auto pipelineLayout = m_PipelineManager->getVkPipelineLayout(computePipeline); + + cmdBuffer.bindPipeline(vk::PipelineBindPoint::eCompute, m_PipelineManager->getVkPipeline(computePipeline)); + for (const auto& usage : descriptorSetUsages) { + cmdBuffer.bindDescriptorSets( + vk::PipelineBindPoint::eCompute, + pipelineLayout, + usage.setLocation, + { usage.vulkanHandle }, + usage.dynamicOffsets + ); + } + if (pushConstants.getSizePerDrawcall() > 0) { + cmdBuffer.pushConstants( + pipelineLayout, + vk::ShaderStageFlagBits::eCompute, + 0, + pushConstants.getSizePerDrawcall(), + pushConstants.getData()); + } + cmdBuffer.dispatchIndirect(m_BufferManager->getBuffer(buffer), bufferArgOffset); + }; + + recordCommandsToStream(cmdStream, submitFunction, nullptr); + } + void Core::endFrame() { if (m_currentSwapchainImageIndex == std::numeric_limits<uint32_t>::max()) { return; diff --git a/src/vkcv/ImageManager.cpp b/src/vkcv/ImageManager.cpp index 1cb6ad3a1187c08cf1aa014ae4ae259591f5c786..4ddd7f8c44c6023a80831bc8b4b092692e84ec86 100644 --- a/src/vkcv/ImageManager.cpp +++ b/src/vkcv/ImageManager.cpp @@ -387,7 +387,7 @@ namespace vkcv { const size_t max_size = std::min(size, image_size); BufferHandle bufferHandle = m_bufferManager.createBuffer( - BufferType::STAGING, max_size, BufferMemoryType::HOST_VISIBLE + BufferType::STAGING, max_size, BufferMemoryType::HOST_VISIBLE, false ); m_bufferManager.fillBuffer(bufferHandle, data, max_size, 0); diff --git a/src/vkcv/Window.cpp b/src/vkcv/Window.cpp index aea00fb10d579aea0dc5be789ced3e6582b868bf..072efcd00eb6520fa4f20379721b559668339f6e 100644 --- a/src/vkcv/Window.cpp +++ b/src/vkcv/Window.cpp @@ -4,6 +4,7 @@ * @brief Window class to handle a basic rendering surface and input */ +#include <thread> #include <vector> #include <GLFW/glfw3.h> @@ -80,12 +81,17 @@ namespace vkcv { window->e_key.unlock(); window->e_char.unlock(); window->e_gamepad.unlock(); - } + } + + glfwPollEvents(); + + // fixes subtle mouse stutter, which is made visible by motion blur + // FIXME: proper solution + // probably caused by main thread locking events before glfw callbacks are executed + std::this_thread::sleep_for(std::chrono::milliseconds(1)); - glfwPollEvents(); - - for (int gamepadIndex = GLFW_JOYSTICK_1; gamepadIndex <= GLFW_JOYSTICK_LAST; gamepadIndex++) { - if (glfwJoystickPresent(gamepadIndex)) { + for (int gamepadIndex = GLFW_JOYSTICK_1; gamepadIndex <= GLFW_JOYSTICK_LAST; gamepadIndex++) { + if (glfwJoystickPresent(gamepadIndex)) { onGamepadEvent(gamepadIndex); } }