diff --git a/config/Sources.cmake b/config/Sources.cmake index 6fc477cc9552d0a9a8921151ca4435b894630755..a5b2ddae5e0a194e4ee887da5f37097821e41d0f 100644 --- a/config/Sources.cmake +++ b/config/Sources.cmake @@ -69,4 +69,12 @@ set(vkcv_sources ${vkcv_source}/vkcv/SamplerManager.cpp ${vkcv_include}/vkcv/DescriptorWrites.hpp + + ${vkcv_include}/vkcv/DrawcallRecording.hpp + ${vkcv_source}/vkcv/DrawcallRecording.cpp + + ${vkcv_include}/vkcv/CommandStreamManager.hpp + ${vkcv_source}/vkcv/CommandStreamManager.cpp + + ${vkcv_include}/vkcv/CommandRecordingFunctionTypes.hpp ) diff --git a/include/vkcv/Buffer.hpp b/include/vkcv/Buffer.hpp index d327067ce409e845bcac5e4c9f56e7de59df89c2..ae935ba9501d4d7776cad7e3ba190a2dd02e5e38 100644 --- a/include/vkcv/Buffer.hpp +++ b/include/vkcv/Buffer.hpp @@ -37,6 +37,11 @@ namespace vkcv { size_t getSize() const { return m_count * sizeof(T); } + + [[nodiscard]] + const vk::Buffer getVulkanHandle() const { + return m_manager->getBuffer(m_handle); + } void fill(const T* data, size_t count = 0, size_t offset = 0) { m_manager->fillBuffer(m_handle, data, count * sizeof(T), offset * sizeof(T)); diff --git a/include/vkcv/CommandRecordingFunctionTypes.hpp b/include/vkcv/CommandRecordingFunctionTypes.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c236fb2c717afd2a3bafc4b3a22708cdac942ffe --- /dev/null +++ b/include/vkcv/CommandRecordingFunctionTypes.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "vkcv/Event.hpp" +#include <vulkan/vulkan.hpp> + +namespace vkcv { + typedef typename event_function<const vk::CommandBuffer&>::type RecordCommandFunction; + typedef typename event_function<>::type FinishCommandFunction; +} \ No newline at end of file diff --git a/include/vkcv/CommandStreamManager.hpp b/include/vkcv/CommandStreamManager.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4af2127ccf6271f1076e3dde05304b8f9c556139 --- /dev/null +++ b/include/vkcv/CommandStreamManager.hpp @@ -0,0 +1,55 @@ +#pragma once +#include <vulkan/vulkan.hpp> +#include <vector> +#include "vkcv/Event.hpp" +#include "vkcv/Handles.hpp" +#include "vkcv/CommandRecordingFunctionTypes.hpp" + +namespace vkcv { + + class Core; + + class CommandStreamManager + { + friend class Core; + private: + struct CommandStream { + inline CommandStream(vk::CommandBuffer cmdBuffer, vk::Queue queue, vk::CommandPool cmdPool) + : cmdBuffer(cmdBuffer), cmdPool(cmdPool), queue(queue) {}; + vk::CommandBuffer cmdBuffer; + vk::CommandPool cmdPool; + vk::Queue queue; + std::vector<FinishCommandFunction> callbacks; + }; + + Core* m_core; + std::vector<CommandStream> m_commandStreams; + + CommandStreamManager() noexcept; + + void init(Core* core); + + public: + ~CommandStreamManager() noexcept; + + CommandStreamManager(CommandStreamManager&& other) = delete; + CommandStreamManager(const CommandStreamManager& other) = delete; + + CommandStreamManager& operator=(CommandStreamManager&& other) = delete; + CommandStreamManager& operator=(const CommandStreamManager& other) = delete; + + CommandStreamHandle createCommandStream( + const vk::Queue queue, + vk::CommandPool cmdPool); + + void recordCommandsToStream(const CommandStreamHandle handle, const RecordCommandFunction record); + void addFinishCallbackToStream(const CommandStreamHandle handle, const FinishCommandFunction finish); + void submitCommandStreamSynchronous( + const CommandStreamHandle handle, + std::vector<vk::Semaphore> &waitSemaphores, + std::vector<vk::Semaphore> &signalSemaphores); + + vk::CommandBuffer getStreamCommandBuffer(const CommandStreamHandle handle); + }; + +} \ No newline at end of file diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index 0a50765fedb07f1117b5e0ecaa43c03b362c23ce..8a165adf43561b1204490a12afa00d2a3fabdbf4 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -22,13 +22,11 @@ #include "Sampler.hpp" #include "DescriptorWrites.hpp" #include "Event.hpp" +#include "DrawcallRecording.hpp" +#include "CommandRecordingFunctionTypes.hpp" namespace vkcv { - struct VertexBufferBinding { - vk::DeviceSize offset; - BufferHandle buffer; - }; // forward declarations class PassManager; @@ -37,15 +35,13 @@ namespace vkcv class BufferManager; class SamplerManager; class ImageManager; + class CommandStreamManager; struct SubmitInfo { QueueType queueType; std::vector<vk::Semaphore> waitSemaphores; std::vector<vk::Semaphore> signalSemaphores; }; - - typedef typename event_function<const vk::CommandBuffer&>::type RecordCommandFunction; - typedef typename event_function<>::type FinishCommandFunction; class Core final { @@ -65,27 +61,30 @@ namespace vkcv Context m_Context; - SwapChain m_swapchain; - std::vector<vk::ImageView> m_swapchainImageViews; - const Window& m_window; - - std::unique_ptr<PassManager> m_PassManager; - std::unique_ptr<PipelineManager> m_PipelineManager; - std::unique_ptr<DescriptorManager> m_DescriptorManager; - std::unique_ptr<BufferManager> m_BufferManager; - std::unique_ptr<SamplerManager> m_SamplerManager; - std::unique_ptr<ImageManager> m_ImageManager; + SwapChain m_swapchain; + std::vector<vk::ImageView> m_swapchainImageViews; + std::vector<vk::Image> m_swapchainImages; + std::vector<vk::ImageLayout> m_swapchainImageLayouts; + const Window& m_window; - CommandResources m_CommandResources; - SyncResources m_SyncResources; - uint32_t m_currentSwapchainImageIndex; + std::unique_ptr<PassManager> m_PassManager; + std::unique_ptr<PipelineManager> m_PipelineManager; + std::unique_ptr<DescriptorManager> m_DescriptorManager; + std::unique_ptr<BufferManager> m_BufferManager; + std::unique_ptr<SamplerManager> m_SamplerManager; + std::unique_ptr<ImageManager> m_ImageManager; + std::unique_ptr<CommandStreamManager> m_CommandStreamManager; - ImageHandle m_DepthImage; + CommandResources m_CommandResources; + SyncResources m_SyncResources; + uint32_t m_currentSwapchainImageIndex; std::function<void(int, int)> e_resizeHandle; static std::vector<vk::ImageView> createImageViews( Context &context, SwapChain& swapChain); + void recordSwapchainImageLayoutTransition(vk::CommandBuffer cmdBuffer, vk::ImageLayout newLayout); + public: /** * Destructor of #Core destroys the Vulkan objects contained in the core's context. @@ -211,29 +210,23 @@ namespace vkcv * @return */ [[nodiscard]] - ResourcesHandle createResourceDescription(const std::vector<DescriptorSetConfig> &descriptorSets); - void writeResourceDescription(ResourcesHandle handle, size_t setIndex, const DescriptorWrites& writes); + DescriptorSetHandle createDescriptorSet(const std::vector<DescriptorBinding> &bindings); + void writeResourceDescription(DescriptorSetHandle handle, size_t setIndex, const DescriptorWrites& writes); - vk::DescriptorSetLayout getDescriptorSetLayout(ResourcesHandle handle, size_t setIndex); + DescriptorSet getDescriptorSet(const DescriptorSetHandle handle) const; /** * @brief start recording command buffers and increment frame index */ - void beginFrame(); + bool beginFrame(uint32_t& width, uint32_t& height); - /** - * @brief render a beautiful triangle - */ - void renderMesh( - const PassHandle &renderpassHandle, - const PipelineHandle &pipelineHandle, - const size_t pushConstantSize, - const void* pushConstantData, - const std::vector<VertexBufferBinding> &vertexBufferBindings, - const BufferHandle &indexBuffer, - const size_t indexCount, - const vkcv::ResourcesHandle resourceHandle, - const size_t resourceDescriptorSetIndex); + void recordDrawcallsToCmdStream( + const CommandStreamHandle cmdStreamHandle, + const PassHandle renderpassHandle, + const PipelineHandle pipelineHandle, + const PushConstantData &pushConstantData, + const std::vector<DrawcallInfo> &drawcalls, + const std::vector<ImageHandle> &renderTargets); /** * @brief end recording and present image @@ -251,6 +244,20 @@ namespace vkcv * @param record Record-command-function * @param finish Finish-command-function or nullptr */ - void submitCommands(const SubmitInfo &submitInfo, const RecordCommandFunction& record, const FinishCommandFunction& finish); + void recordAndSubmitCommands( + const SubmitInfo &submitInfo, + const RecordCommandFunction &record, + const FinishCommandFunction &finish); + + CommandStreamHandle createCommandStream(QueueType queueType); + + void recordCommandsToStream( + const CommandStreamHandle cmdStreamHandle, + const RecordCommandFunction &record, + const FinishCommandFunction &finish); + + void submitCommandStream(const CommandStreamHandle handle); + void prepareSwapchainImageForPresent(const CommandStreamHandle handle); + void prepareImageForSampling(const CommandStreamHandle cmdStream, const ImageHandle image); }; } diff --git a/include/vkcv/DescriptorConfig.hpp b/include/vkcv/DescriptorConfig.hpp index 161273db1ec3bd0be290ecdd1042d12d181d303e..86c2e20eb37633e4519749bef507161133e57425 100644 --- a/include/vkcv/DescriptorConfig.hpp +++ b/include/vkcv/DescriptorConfig.hpp @@ -1,8 +1,16 @@ #pragma once #include <vkcv/ShaderProgram.hpp> +#include <vkcv/Handles.hpp> +#include <vulkan/vulkan.hpp> namespace vkcv { + struct DescriptorSet + { + vk::DescriptorSet vulkanHandle; + vk::DescriptorSetLayout layout; + }; + /* * All the types of descriptors (resources) that can be retrieved by the shaders */ @@ -24,7 +32,6 @@ namespace vkcv */ struct DescriptorBinding { - DescriptorBinding() = delete; DescriptorBinding( DescriptorType descriptorType, uint32_t descriptorCount, @@ -35,16 +42,4 @@ namespace vkcv uint32_t descriptorCount; ShaderStage shaderStage; }; - - /* - * One descriptor set struct that contains all the necessary information for the actual creation. - * @param[in] a number of bindings that were created beforehand - * @param[in] the number of (identical) sets that should be created from the attached bindings - */ - struct DescriptorSetConfig - { - explicit DescriptorSetConfig(std::vector<DescriptorBinding> bindings) noexcept; - - std::vector<DescriptorBinding> bindings; - }; } diff --git a/include/vkcv/DrawcallRecording.hpp b/include/vkcv/DrawcallRecording.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0929ad038fb95ec1573e7c76e5ce13adb84ab760 --- /dev/null +++ b/include/vkcv/DrawcallRecording.hpp @@ -0,0 +1,54 @@ +#pragma once +#include <vulkan/vulkan.hpp> +#include <vkcv/Handles.hpp> +#include <vkcv/DescriptorConfig.hpp> + +namespace vkcv { + struct VertexBufferBinding { + inline VertexBufferBinding(vk::DeviceSize offset, vk::Buffer buffer) noexcept + : offset(offset), buffer(buffer) {} + + vk::DeviceSize offset; + vk::Buffer buffer; + }; + + struct DescriptorSetUsage { + inline DescriptorSetUsage(uint32_t setLocation, vk::DescriptorSet vulkanHandle) noexcept + : setLocation(setLocation), vulkanHandle(vulkanHandle) {} + + const uint32_t setLocation; + const vk::DescriptorSet vulkanHandle; + }; + + struct Mesh { + inline Mesh(std::vector<VertexBufferBinding> vertexBufferBindings, vk::Buffer indexBuffer, size_t indexCount) noexcept + : vertexBufferBindings(vertexBufferBindings), indexBuffer(indexBuffer), indexCount(indexCount){} + + std::vector<VertexBufferBinding> vertexBufferBindings; + vk::Buffer indexBuffer; + size_t indexCount; + }; + + struct PushConstantData { + inline PushConstantData(void* data, size_t sizePerDrawcall) : data(data), sizePerDrawcall(sizePerDrawcall) {} + + void* data; + size_t sizePerDrawcall; + }; + + struct DrawcallInfo { + inline DrawcallInfo(const Mesh& mesh, const std::vector<DescriptorSetUsage>& descriptorSets) + : mesh(mesh), descriptorSets(descriptorSets) {} + + Mesh mesh; + std::vector<DescriptorSetUsage> descriptorSets; + }; + + void recordDrawcall( + const DrawcallInfo &drawcall, + vk::CommandBuffer cmdBuffer, + vk::PipelineLayout pipelineLayout, + const PushConstantData &pushConstantData, + const size_t drawcallIndex); + +} \ No newline at end of file diff --git a/include/vkcv/Handles.hpp b/include/vkcv/Handles.hpp index bb0438d3e2482bb119c633a4e987cf28dac2375f..ea05bd212dd9314957e4771070bedb3963b1b2dc 100644 --- a/include/vkcv/Handles.hpp +++ b/include/vkcv/Handles.hpp @@ -79,7 +79,7 @@ namespace vkcv using Handle::Handle; }; - class ResourcesHandle : public Handle { + class DescriptorSetHandle : public Handle { friend class DescriptorManager; private: using Handle::Handle; @@ -93,14 +93,19 @@ namespace vkcv class ImageHandle : public Handle { friend class ImageManager; - private: using Handle::Handle; - + public: [[nodiscard]] bool isSwapchainImage() const; static ImageHandle createSwapchainImageHandle(const HandleDestroyFunction& destroy = nullptr); }; + + class CommandStreamHandle : public Handle { + friend class CommandStreamManager; + private: + using Handle::Handle; + }; } diff --git a/include/vkcv/Image.hpp b/include/vkcv/Image.hpp index d76bd12169f622f05f1731b0eb3065d6133a5f2f..a1219ce4147ebbb8ae0650da8a87766f8967874b 100644 --- a/include/vkcv/Image.hpp +++ b/include/vkcv/Image.hpp @@ -9,8 +9,12 @@ #include "Handles.hpp" namespace vkcv { - - class ImageManager; + + // forward declares + class ImageManager; + + bool isDepthFormat(const vk::Format format); + class Image { friend class Core; public: @@ -37,11 +41,9 @@ namespace vkcv { void fill(void* data, size_t size = SIZE_MAX); private: ImageManager* const m_manager; - const ImageHandle m_handle; - const vk::Format m_format; - vk::ImageLayout m_layout; + const ImageHandle m_handle; - Image(ImageManager* manager, const ImageHandle& handle, vk::Format format); + Image(ImageManager* manager, const ImageHandle& handle); static Image create(ImageManager* manager, vk::Format format, uint32_t width, uint32_t height, uint32_t depth); diff --git a/include/vkcv/PassConfig.hpp b/include/vkcv/PassConfig.hpp index d9a5bcd83acca5f5ba86b4e6ce6973acbed89de6..8f3b516d4b4451c513366fbd8469908bccde6a5f 100644 --- a/include/vkcv/PassConfig.hpp +++ b/include/vkcv/PassConfig.hpp @@ -32,23 +32,15 @@ namespace vkcv struct AttachmentDescription { - AttachmentDescription() = delete; AttachmentDescription( - AttachmentLayout initial, - AttachmentLayout in_pass, - AttachmentLayout final, - AttachmentOperation store_op, - AttachmentOperation load_op, - vk::Format format) noexcept; - - AttachmentLayout layout_initial; - AttachmentLayout layout_in_pass; - AttachmentLayout layout_final; + AttachmentOperation store_op, + AttachmentOperation load_op, + vk::Format format) noexcept; AttachmentOperation store_operation; AttachmentOperation load_operation; - vk::Format format; + vk::Format format; }; struct PassConfig diff --git a/include/vkcv/PipelineConfig.hpp b/include/vkcv/PipelineConfig.hpp index a8e1334f3ee5d6774b9fed35edd685f19747814d..7dc6f2200db7efc1abdddab81145daec27540c49 100644 --- a/include/vkcv/PipelineConfig.hpp +++ b/include/vkcv/PipelineConfig.hpp @@ -24,19 +24,22 @@ namespace vkcv { * @param passHandle handle for Render Pass */ PipelineConfig( - const ShaderProgram& shaderProgram, - uint32_t width, - uint32_t height, - PassHandle &passHandle, - const std::vector<VertexAttribute> &vertexAttributes, - const std::vector<vk::DescriptorSetLayout> &descriptorLayouts); + const ShaderProgram& shaderProgram, + uint32_t width, + uint32_t height, + const PassHandle &passHandle, + const std::vector<VertexAttribute> &vertexAttributes, + const std::vector<vk::DescriptorSetLayout> &descriptorLayouts, + bool useDynamicViewport); + + ShaderProgram m_ShaderProgram; + uint32_t m_Height; + uint32_t m_Width; + PassHandle m_PassHandle; + std::vector<VertexAttribute> m_VertexAttributes; + std::vector<vk::DescriptorSetLayout> m_DescriptorLayouts; + bool m_UseDynamicViewport; - ShaderProgram m_ShaderProgram; - uint32_t m_Height; - uint32_t m_Width; - PassHandle m_PassHandle; - std::vector<VertexAttribute> m_vertexAttributes; - std::vector<vk::DescriptorSetLayout> m_descriptorLayouts; }; } \ No newline at end of file diff --git a/include/vkcv/ShaderProgram.hpp b/include/vkcv/ShaderProgram.hpp index ef5d1f00ea3eeb97d97d8824439ded1ed326f33c..459125bbd3208ffb40815e1e3fd4c9615ce21724 100644 --- a/include/vkcv/ShaderProgram.hpp +++ b/include/vkcv/ShaderProgram.hpp @@ -58,10 +58,12 @@ namespace vkcv { void reflectShader(ShaderStage shaderStage); const VertexLayout &getVertexLayout() const; + size_t getPushConstantSize() const; private: std::unordered_map<ShaderStage, Shader> m_Shaders; VertexLayout m_VertexLayout; + size_t m_pushConstantSize = 0; }; } diff --git a/include/vkcv/SwapChain.hpp b/include/vkcv/SwapChain.hpp index 53f7b783051307c7d327f20f389d7bef0fb9a16c..089205d1633551b4ad9f11d0bdd5540b2bb61bbb 100644 --- a/include/vkcv/SwapChain.hpp +++ b/include/vkcv/SwapChain.hpp @@ -107,7 +107,7 @@ namespace vkcv /** * */ - void recreateSwapchain(); + void signalSwapchainRecreation(); /** * TODO diff --git a/modules/camera/src/vkcv/camera/Camera.cpp b/modules/camera/src/vkcv/camera/Camera.cpp index b1d7381e3d548c9edf5d41e8d084c7edb1d02647..af89ed86881acc8b754a041d32599a78caac57b4 100644 --- a/modules/camera/src/vkcv/camera/Camera.cpp +++ b/modules/camera/src/vkcv/camera/Camera.cpp @@ -90,7 +90,7 @@ namespace vkcv { } float Camera::getRatio() const { - return 0.0f; + return m_ratio; } void Camera::setNearFar( float near, float far){ diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index 7ca73a0811df7f1568508b56312ce3348237a695..ccbdaf4101c5dabb3e9d43788e255eab85ad5776 100644 --- a/projects/CMakeLists.txt +++ b/projects/CMakeLists.txt @@ -2,3 +2,4 @@ # Add new projects/examples here: add_subdirectory(first_triangle) add_subdirectory(first_mesh) +add_subdirectory(cmd_sync_test) \ No newline at end of file diff --git a/projects/cmd_sync_test/.gitignore b/projects/cmd_sync_test/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..16f72da367245ad14a38ee756816f06f8cbbe3d2 --- /dev/null +++ b/projects/cmd_sync_test/.gitignore @@ -0,0 +1 @@ +cmd_sync_test \ No newline at end of file diff --git a/projects/cmd_sync_test/CMakeLists.txt b/projects/cmd_sync_test/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..da1d12949d9e8c918d78ab5cb0484106fae69b6a --- /dev/null +++ b/projects/cmd_sync_test/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.16) +project(cmd_sync_test) + +# 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(cmd_sync_test src/main.cpp) + +# this should fix the execution path to load local files from the project (for MSVC) +if(MSVC) + set_target_properties(cmd_sync_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) + set_target_properties(cmd_sync_test 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(cmd_sync_test PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) +endif() + +# including headers of dependencies and the VkCV framework +target_include_directories(cmd_sync_test SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_asset_loader_include} ${vkcv_camera_include}) + +# linking with libraries from all dependencies and the VkCV framework +target_link_libraries(cmd_sync_test vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_camera) diff --git a/projects/cmd_sync_test/resources/cube/boards2_vcyc_jpg.jpg b/projects/cmd_sync_test/resources/cube/boards2_vcyc_jpg.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2636039e272289c0fba3fa2d88a060b857501248 --- /dev/null +++ b/projects/cmd_sync_test/resources/cube/boards2_vcyc_jpg.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cca33a6e58ddd1b37a6e6853a9aa0e7b15ca678937119194752393dd2a0a0564 +size 1192476 diff --git a/projects/cmd_sync_test/resources/cube/cube.bin b/projects/cmd_sync_test/resources/cube/cube.bin new file mode 100644 index 0000000000000000000000000000000000000000..3303cd8635848bee18e10ab8754d5e4e7218db92 --- /dev/null +++ b/projects/cmd_sync_test/resources/cube/cube.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9bb9b6b8bbe50a0aaa517057f245ee844f80afa7426dacb2aed4128f71629ce4 +size 840 diff --git a/projects/cmd_sync_test/resources/cube/cube.blend b/projects/cmd_sync_test/resources/cube/cube.blend new file mode 100644 index 0000000000000000000000000000000000000000..62ccb2c742094bcfb5ed194ab905bffae86bfd65 --- /dev/null +++ b/projects/cmd_sync_test/resources/cube/cube.blend @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6c1e245f259c610528c9485db6688928faac0ab2addee9e3c2dde7740e4dd09 +size 774920 diff --git a/projects/cmd_sync_test/resources/cube/cube.blend1 b/projects/cmd_sync_test/resources/cube/cube.blend1 new file mode 100644 index 0000000000000000000000000000000000000000..13f21dcca218d7bc7a07a8a9682b5e1d9e607736 --- /dev/null +++ b/projects/cmd_sync_test/resources/cube/cube.blend1 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4496f423569b8ca81f3b3a55fad00f925557e0193fb9dbe6cdce7e71fb48f7b +size 774920 diff --git a/projects/cmd_sync_test/resources/cube/cube.glb b/projects/cmd_sync_test/resources/cube/cube.glb new file mode 100644 index 0000000000000000000000000000000000000000..66a42c65e71dcf375e04cc378256024dd3c7834d --- /dev/null +++ b/projects/cmd_sync_test/resources/cube/cube.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:198568b715f397d78f7c358c0f709a419e7fd677e54cdec7c19f71b5ed264897 +size 1194508 diff --git a/projects/cmd_sync_test/resources/cube/cube.gltf b/projects/cmd_sync_test/resources/cube/cube.gltf new file mode 100644 index 0000000000000000000000000000000000000000..428176144843dd06c78fe1d11a6392a0ea02b22d --- /dev/null +++ b/projects/cmd_sync_test/resources/cube/cube.gltf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f82f455647a84ca6242882ae26a79a499d3ce594f8de317ab89488c5b79721ac +size 2823 diff --git a/projects/cmd_sync_test/resources/shaders/compile.bat b/projects/cmd_sync_test/resources/shaders/compile.bat new file mode 100644 index 0000000000000000000000000000000000000000..516c2f2f78001e1a5d182356e7c3fe82d66a45ee --- /dev/null +++ b/projects/cmd_sync_test/resources/shaders/compile.bat @@ -0,0 +1,5 @@ +%VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv +%VULKAN_SDK%\Bin32\glslc.exe shader.frag -o frag.spv +%VULKAN_SDK%\Bin32\glslc.exe shadow.vert -o shadow_vert.spv +%VULKAN_SDK%\Bin32\glslc.exe shadow.frag -o shadow_frag.spv +pause \ No newline at end of file diff --git a/projects/cmd_sync_test/resources/shaders/frag.spv b/projects/cmd_sync_test/resources/shaders/frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..ff3110571871d65ce119dc6c5006e7e67aa53546 Binary files /dev/null and b/projects/cmd_sync_test/resources/shaders/frag.spv differ diff --git a/projects/cmd_sync_test/resources/shaders/shader.frag b/projects/cmd_sync_test/resources/shaders/shader.frag new file mode 100644 index 0000000000000000000000000000000000000000..95f1b3319e1ca5c7c34ff94e5e7198819c0233c1 --- /dev/null +++ b/projects/cmd_sync_test/resources/shaders/shader.frag @@ -0,0 +1,44 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 passNormal; +layout(location = 1) in vec2 passUV; +layout(location = 2) in vec3 passPos; + +layout(location = 0) out vec3 outColor; + +layout(set=0, binding=0) uniform texture2D meshTexture; +layout(set=0, binding=1) uniform sampler textureSampler; +layout(set=0, binding=2) uniform sunBuffer { + vec3 L; float padding; + mat4 lightMatrix; +}; +layout(set=0, binding=3) uniform texture2D shadowMap; +layout(set=0, binding=4) uniform sampler shadowMapSampler; + +float shadowTest(vec3 worldPos){ + vec4 lightPos = lightMatrix * vec4(worldPos, 1); + lightPos /= lightPos.w; + lightPos.xy = lightPos.xy * 0.5 + 0.5; + + if(any(lessThan(lightPos.xy, vec2(0))) || any(greaterThan(lightPos.xy, vec2(1)))){ + return 1; + } + + lightPos.z = clamp(lightPos.z, 0, 1); + + float shadowMapSample = texture(sampler2D(shadowMap, shadowMapSampler), lightPos.xy).r; + float bias = 0.01f; + shadowMapSample += bias; + return shadowMapSample < lightPos.z ? 0 : 1; +} + +void main() { + vec3 N = normalize(passNormal); + vec3 sunColor = vec3(1); + vec3 sun = sunColor * clamp(dot(N, L), 0, 1); + sun *= shadowTest(passPos); + vec3 ambient = vec3(0.1); + vec3 albedo = texture(sampler2D(meshTexture, textureSampler), passUV).rgb; + outColor = albedo * (sun + ambient); +} \ No newline at end of file diff --git a/projects/cmd_sync_test/resources/shaders/shader.vert b/projects/cmd_sync_test/resources/shaders/shader.vert new file mode 100644 index 0000000000000000000000000000000000000000..0ab82c203806356d0f35dc52c0a6988b286d90d1 --- /dev/null +++ b/projects/cmd_sync_test/resources/shaders/shader.vert @@ -0,0 +1,22 @@ +#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(location = 2) out vec3 passPos; + +layout( push_constant ) uniform constants{ + mat4 mvp; + mat4 model; +}; + +void main() { + gl_Position = mvp * vec4(inPosition, 1.0); + passNormal = inNormal; + passUV = inUV; + passPos = (model * vec4(inPosition, 1)).xyz; +} \ No newline at end of file diff --git a/projects/cmd_sync_test/resources/shaders/shadow.frag b/projects/cmd_sync_test/resources/shaders/shadow.frag new file mode 100644 index 0000000000000000000000000000000000000000..848f853f556660b4900b5db7fb6fc98d57c1cd5b --- /dev/null +++ b/projects/cmd_sync_test/resources/shaders/shadow.frag @@ -0,0 +1,6 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +void main() { + +} \ No newline at end of file diff --git a/projects/cmd_sync_test/resources/shaders/shadow.vert b/projects/cmd_sync_test/resources/shaders/shadow.vert new file mode 100644 index 0000000000000000000000000000000000000000..e0f41d42d575fa64fedbfa04adf89ac0f4aeebe8 --- /dev/null +++ b/projects/cmd_sync_test/resources/shaders/shadow.vert @@ -0,0 +1,12 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 inPosition; + +layout( push_constant ) uniform constants{ + mat4 mvp; +}; + +void main() { + gl_Position = mvp * vec4(inPosition, 1.0); +} \ No newline at end of file diff --git a/projects/cmd_sync_test/resources/shaders/shadow_frag.spv b/projects/cmd_sync_test/resources/shaders/shadow_frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..6be3bd2518a3b1f234e39aea2503ba86cfb3314b Binary files /dev/null and b/projects/cmd_sync_test/resources/shaders/shadow_frag.spv differ diff --git a/projects/cmd_sync_test/resources/shaders/shadow_vert.spv b/projects/cmd_sync_test/resources/shaders/shadow_vert.spv new file mode 100644 index 0000000000000000000000000000000000000000..afaa0824ee9be2c22209d611943c6512587dce24 Binary files /dev/null and b/projects/cmd_sync_test/resources/shaders/shadow_vert.spv differ diff --git a/projects/cmd_sync_test/resources/shaders/vert.spv b/projects/cmd_sync_test/resources/shaders/vert.spv new file mode 100644 index 0000000000000000000000000000000000000000..5e514eef5983927316465679af5461f507497130 Binary files /dev/null and b/projects/cmd_sync_test/resources/shaders/vert.spv differ diff --git a/projects/cmd_sync_test/resources/triangle/Triangle.bin b/projects/cmd_sync_test/resources/triangle/Triangle.bin new file mode 100644 index 0000000000000000000000000000000000000000..57f26ad96592b64377e6aa93823d96a94e6c5022 --- /dev/null +++ b/projects/cmd_sync_test/resources/triangle/Triangle.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:412ebd5f7242c266b4957e7e26be13aa331dbcb7bbb854ab334a2437ae8ed959 +size 104 diff --git a/projects/cmd_sync_test/resources/triangle/Triangle.blend b/projects/cmd_sync_test/resources/triangle/Triangle.blend new file mode 100644 index 0000000000000000000000000000000000000000..2421dc5e1bb029d73a9ec09cc4530c5196851fd7 --- /dev/null +++ b/projects/cmd_sync_test/resources/triangle/Triangle.blend @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:387e544df173219fbf292a64a6656d1d782bbf71a5a9e9fdef0a308f47b05477 +size 758144 diff --git a/projects/cmd_sync_test/resources/triangle/Triangle.glb b/projects/cmd_sync_test/resources/triangle/Triangle.glb new file mode 100644 index 0000000000000000000000000000000000000000..4148620cd6af0dadbc791aa1c52bb5431a40884b --- /dev/null +++ b/projects/cmd_sync_test/resources/triangle/Triangle.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4be087a605212d139416b5352a018283b26b99260cbcddb7013a1beeb331227 +size 980 diff --git a/projects/cmd_sync_test/resources/triangle/Triangle.gltf b/projects/cmd_sync_test/resources/triangle/Triangle.gltf new file mode 100644 index 0000000000000000000000000000000000000000..a188e6ee16a5e8486cf307c7bda8cfd99bdbeea6 --- /dev/null +++ b/projects/cmd_sync_test/resources/triangle/Triangle.gltf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d5fc354e040f79cff329e919677b194c75e3a522c6406f75c1108ad9575f12ec +size 2202 diff --git a/projects/cmd_sync_test/src/main.cpp b/projects/cmd_sync_test/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0fb29fafe24a4ae1279161dc8814c0d8f52765f --- /dev/null +++ b/projects/cmd_sync_test/src/main.cpp @@ -0,0 +1,301 @@ +#include <iostream> +#include <vkcv/Core.hpp> +#include <GLFW/glfw3.h> +#include <vkcv/camera/CameraManager.hpp> +#include <chrono> +#include <vkcv/asset/asset_loader.hpp> + +int main(int argc, const char** argv) { + const char* applicationName = "First Mesh"; + + uint32_t windowWidth = 800; + uint32_t windowHeight = 600; + + vkcv::Window window = vkcv::Window::create( + applicationName, + windowWidth, + windowHeight, + true + ); + + vkcv::CameraManager cameraManager(window, windowWidth, windowHeight); + cameraManager.getCamera().setPosition(glm::vec3(0.f, 0.f, 3.f)); + cameraManager.getCamera().setNearFar(0.1, 30); + + window.initEvents(); + + vkcv::Core core = vkcv::Core::create( + window, + applicationName, + VK_MAKE_VERSION(0, 0, 1), + { vk::QueueFlagBits::eTransfer,vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute }, + {}, + { "VK_KHR_swapchain" } + ); + + vkcv::asset::Mesh mesh; + + const char* path = argc > 1 ? argv[1] : "resources/cube/cube.gltf"; + int result = vkcv::asset::loadMesh(path, mesh); + + if (result == 1) { + std::cout << "Mesh loading successful!" << std::endl; + } + else { + std::cout << "Mesh loading failed: " << result << std::endl; + return 1; + } + + assert(mesh.vertexGroups.size() > 0); + auto vertexBuffer = core.createBuffer<uint8_t>( + vkcv::BufferType::VERTEX, + mesh.vertexGroups[0].vertexBuffer.data.size(), + vkcv::BufferMemoryType::DEVICE_LOCAL + ); + + vertexBuffer.fill(mesh.vertexGroups[0].vertexBuffer.data); + + auto indexBuffer = core.createBuffer<uint8_t>( + vkcv::BufferType::INDEX, + mesh.vertexGroups[0].indexBuffer.data.size(), + vkcv::BufferMemoryType::DEVICE_LOCAL + ); + + indexBuffer.fill(mesh.vertexGroups[0].indexBuffer.data); + + auto& attributes = mesh.vertexGroups[0].vertexBuffer.attributes; + + std::sort(attributes.begin(), attributes.end(), [](const vkcv::VertexAttribute& x, const vkcv::VertexAttribute& y) { + return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); + }); + + const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { + vkcv::VertexBufferBinding(attributes[0].offset, vertexBuffer.getVulkanHandle()), + vkcv::VertexBufferBinding(attributes[1].offset, vertexBuffer.getVulkanHandle()), + vkcv::VertexBufferBinding(attributes[2].offset, vertexBuffer.getVulkanHandle()) }; + + const vkcv::Mesh loadedMesh(vertexBufferBindings, indexBuffer.getVulkanHandle(), mesh.vertexGroups[0].numIndices); + + // an example attachment for passes that output to the window + const vkcv::AttachmentDescription present_color_attachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::CLEAR, + core.getSwapchainImageFormat() + ); + + const vkcv::AttachmentDescription depth_attachment( + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::CLEAR, + vk::Format::eD32Sfloat + ); + + vkcv::PassConfig trianglePassDefinition({ present_color_attachment, depth_attachment }); + vkcv::PassHandle trianglePass = core.createPass(trianglePassDefinition); + + if (!trianglePass) { + std::cout << "Error. Could not create renderpass. Exiting." << std::endl; + return EXIT_FAILURE; + } + + vkcv::ShaderProgram triangleShaderProgram{}; + triangleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("resources/shaders/vert.spv")); + triangleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("resources/shaders/frag.spv")); + triangleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX); + triangleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); + + std::vector<vkcv::DescriptorBinding> descriptorBindings = { + vkcv::DescriptorBinding(vkcv::DescriptorType::IMAGE_SAMPLED, 1, vkcv::ShaderStage::FRAGMENT), + vkcv::DescriptorBinding(vkcv::DescriptorType::SAMPLER, 1, vkcv::ShaderStage::FRAGMENT), + vkcv::DescriptorBinding(vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT), + vkcv::DescriptorBinding(vkcv::DescriptorType::IMAGE_SAMPLED, 1, vkcv::ShaderStage::FRAGMENT) , + vkcv::DescriptorBinding(vkcv::DescriptorType::SAMPLER, 1, vkcv::ShaderStage::FRAGMENT) }; + vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorBindings); + + const vkcv::PipelineConfig trianglePipelineDefinition( + triangleShaderProgram, + windowWidth, + windowHeight, + trianglePass, + attributes, + { core.getDescriptorSet(descriptorSet).layout }, + true); + vkcv::PipelineHandle trianglePipeline = core.createGraphicsPipeline(trianglePipelineDefinition); + + if (!trianglePipeline) { + std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; + return EXIT_FAILURE; + } + + vkcv::Image texture = core.createImage(vk::Format::eR8G8B8A8Srgb, mesh.texture_hack.w, mesh.texture_hack.h); + texture.fill(mesh.texture_hack.img); + + vkcv::SamplerHandle sampler = core.createSampler( + vkcv::SamplerFilterType::LINEAR, + vkcv::SamplerFilterType::LINEAR, + vkcv::SamplerMipmapMode::LINEAR, + vkcv::SamplerAddressMode::REPEAT + ); + + vkcv::SamplerHandle shadowSampler = core.createSampler( + vkcv::SamplerFilterType::NEAREST, + vkcv::SamplerFilterType::NEAREST, + vkcv::SamplerMipmapMode::NEAREST, + vkcv::SamplerAddressMode::CLAMP_TO_EDGE + ); + + vkcv::ImageHandle depthBuffer = core.createImage(vk::Format::eD32Sfloat, windowWidth, windowHeight).getHandle(); + + const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); + + const vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle); + + const std::vector<glm::vec3> instancePositions = { + glm::vec3( 0.f, -2.f, 0.f), + glm::vec3( 3.f, 0.f, 0.f), + glm::vec3(-3.f, 0.f, 0.f), + glm::vec3( 0.f, 2.f, 0.f), + glm::vec3( 0.f, -5.f, 0.f) + }; + + std::vector<glm::mat4> modelMatrices; + std::vector<vkcv::DrawcallInfo> drawcalls; + std::vector<vkcv::DrawcallInfo> shadowDrawcalls; + for (const auto& position : instancePositions) { + modelMatrices.push_back(glm::translate(glm::mat4(1.f), position)); + drawcalls.push_back(vkcv::DrawcallInfo(loadedMesh, { descriptorUsage })); + shadowDrawcalls.push_back(vkcv::DrawcallInfo(loadedMesh, {})); + } + + modelMatrices.back() *= glm::scale(glm::mat4(1.f), glm::vec3(10.f, 1.f, 10.f)); + + std::vector<std::array<glm::mat4, 2>> mainPassMatrices; + std::vector<glm::mat4> mvpLight; + + vkcv::ShaderProgram shadowShader; + shadowShader.addShader(vkcv::ShaderStage::VERTEX, "resources/shaders/shadow_vert.spv"); + shadowShader.addShader(vkcv::ShaderStage::FRAGMENT, "resources/shaders/shadow_frag.spv"); + shadowShader.reflectShader(vkcv::ShaderStage::VERTEX); + shadowShader.reflectShader(vkcv::ShaderStage::FRAGMENT); + + const vk::Format shadowMapFormat = vk::Format::eD16Unorm; + const std::vector<vkcv::AttachmentDescription> shadowAttachments = { + vkcv::AttachmentDescription(vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::CLEAR, shadowMapFormat) + }; + const vkcv::PassConfig shadowPassConfig(shadowAttachments); + const vkcv::PassHandle shadowPass = core.createPass(shadowPassConfig); + + const uint32_t shadowMapResolution = 1024; + const vkcv::Image shadowMap = core.createImage(shadowMapFormat, shadowMapResolution, shadowMapResolution, 1); + const vkcv::PipelineConfig shadowPipeConfig( + shadowShader, + shadowMapResolution, + shadowMapResolution, + shadowPass, + attributes, + {}, + false); + const vkcv::PipelineHandle shadowPipe = core.createGraphicsPipeline(shadowPipeConfig); + + struct LightInfo { + glm::vec3 direction; + float padding; + glm::mat4 lightMatrix; + }; + LightInfo lightInfo; + vkcv::Buffer lightBuffer = core.createBuffer<LightInfo>(vkcv::BufferType::UNIFORM, sizeof(glm::vec3)); + + vkcv::DescriptorWrites setWrites; + setWrites.sampledImageWrites = { + vkcv::SampledImageDescriptorWrite(0, texture.getHandle()), + vkcv::SampledImageDescriptorWrite(3, shadowMap.getHandle()) }; + setWrites.samplerWrites = { + vkcv::SamplerDescriptorWrite(1, sampler), + vkcv::SamplerDescriptorWrite(4, shadowSampler) }; + setWrites.uniformBufferWrites = { vkcv::UniformBufferDescriptorWrite(2, lightBuffer.getHandle()) }; + core.writeResourceDescription(descriptorSet, 0, setWrites); + + auto start = std::chrono::system_clock::now(); + const auto appStartTime = start; + while (window.isWindowOpen()) { + vkcv::Window::pollEvents(); + + uint32_t swapchainWidth, swapchainHeight; + if (!core.beginFrame(swapchainWidth, swapchainHeight)) { + continue; + } + + if ((swapchainWidth != windowWidth) || ((swapchainHeight != windowHeight))) { + depthBuffer = core.createImage(vk::Format::eD32Sfloat, swapchainWidth, swapchainHeight).getHandle(); + + windowWidth = swapchainWidth; + windowHeight = swapchainHeight; + } + + auto end = std::chrono::system_clock::now(); + auto deltatime = std::chrono::duration_cast<std::chrono::microseconds>(end - start); + start = end; + cameraManager.getCamera().updateView(deltatime.count() * 0.000001); + + const float sunTheta = std::chrono::duration_cast<std::chrono::milliseconds>(end - appStartTime).count() * 0.001f; + lightInfo.direction = glm::normalize(glm::vec3(std::cos(sunTheta), 1, std::sin(sunTheta))); + + const float shadowProjectionSize = 5.f; + glm::mat4 projectionLight = glm::ortho( + -shadowProjectionSize, + shadowProjectionSize, + -shadowProjectionSize, + shadowProjectionSize, + -shadowProjectionSize, + shadowProjectionSize); + + glm::mat4 vulkanCorrectionMatrix(1.f); + vulkanCorrectionMatrix[2][2] = 0.5; + vulkanCorrectionMatrix[3][2] = 0.5; + projectionLight = vulkanCorrectionMatrix * projectionLight; + + const glm::mat4 viewLight = glm::lookAt(glm::vec3(0), -lightInfo.direction, glm::vec3(0, -1, 0)); + + lightInfo.lightMatrix = projectionLight * viewLight; + lightBuffer.fill({ lightInfo }); + + const glm::mat4 viewProjectionCamera = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); + + mainPassMatrices.clear(); + mvpLight.clear(); + for (const auto& m : modelMatrices) { + mainPassMatrices.push_back({ viewProjectionCamera * m, m }); + mvpLight.push_back(lightInfo.lightMatrix* m); + } + + vkcv::PushConstantData pushConstantData((void*)mainPassMatrices.data(), 2 * sizeof(glm::mat4)); + const std::vector<vkcv::ImageHandle> renderTargets = { swapchainInput, depthBuffer }; + + vkcv::PushConstantData shadowPushConstantData((void*)mvpLight.data(), sizeof(glm::mat4)); + + auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); + + core.recordDrawcallsToCmdStream( + cmdStream, + shadowPass, + shadowPipe, + shadowPushConstantData, + shadowDrawcalls, + { shadowMap.getHandle() }); + + core.prepareImageForSampling(cmdStream, shadowMap.getHandle()); + + core.recordDrawcallsToCmdStream( + cmdStream, + trianglePass, + trianglePipeline, + pushConstantData, + drawcalls, + renderTargets); + core.prepareSwapchainImageForPresent(cmdStream); + core.submitCommandStream(cmdStream); + + core.endFrame(); + } + + return 0; +} diff --git a/projects/first_mesh/src/main.cpp b/projects/first_mesh/src/main.cpp index d0f8619ac89d89192629dd448527c0151c4c8189..599eae46ca0243d7eaacc06ec59c713edd5f1e72 100644 --- a/projects/first_mesh/src/main.cpp +++ b/projects/first_mesh/src/main.cpp @@ -8,10 +8,13 @@ int main(int argc, const char** argv) { const char* applicationName = "First Mesh"; + uint32_t windowWidth = 800; + uint32_t windowHeight = 600; + vkcv::Window window = vkcv::Window::create( applicationName, - 800, - 600, + windowWidth, + windowHeight, true ); @@ -60,18 +63,12 @@ int main(int argc, const char** argv) { // an example attachment for passes that output to the window const vkcv::AttachmentDescription present_color_attachment( - vkcv::AttachmentLayout::UNDEFINED, - vkcv::AttachmentLayout::COLOR_ATTACHMENT, - vkcv::AttachmentLayout::PRESENTATION, vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::CLEAR, core.getSwapchainImageFormat() ); const vkcv::AttachmentDescription depth_attachment( - vkcv::AttachmentLayout::UNDEFINED, - vkcv::AttachmentLayout::DEPTH_STENCIL_ATTACHMENT, - vkcv::AttachmentLayout::DEPTH_STENCIL_ATTACHMENT, vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::CLEAR, vk::Format::eD32Sfloat @@ -97,17 +94,10 @@ int main(int argc, const char** argv) { return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); }); - vkcv::DescriptorSetConfig setConfig({ + std::vector<vkcv::DescriptorBinding> descriptorBindings = { vkcv::DescriptorBinding(vkcv::DescriptorType::IMAGE_SAMPLED, 1, vkcv::ShaderStage::FRAGMENT), - vkcv::DescriptorBinding(vkcv::DescriptorType::SAMPLER, 1, vkcv::ShaderStage::FRAGMENT) - }); - vkcv::ResourcesHandle set = core.createResourceDescription({ setConfig }); - - //only exemplary code for testing - for (int i = 0; i < 1001; i++) { - vkcv::ResourcesHandle furtherSets = core.createResourceDescription({ setConfig }); - } - //end of exemplary code + vkcv::DescriptorBinding(vkcv::DescriptorType::SAMPLER, 1, vkcv::ShaderStage::FRAGMENT) }; + vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorBindings); const vkcv::PipelineConfig trianglePipelineDefinition( triangleShaderProgram, @@ -115,7 +105,8 @@ int main(int argc, const char** argv) { UINT32_MAX, trianglePass, mesh.vertexGroups[0].vertexBuffer.attributes, - { core.getDescriptorSetLayout(set, 0) }); + { core.getDescriptorSet(descriptorSet).layout }, + true); vkcv::PipelineHandle trianglePipeline = core.createGraphicsPipeline(trianglePipelineDefinition); if (!trianglePipeline) { @@ -133,42 +124,65 @@ int main(int argc, const char** argv) { vkcv::SamplerAddressMode::REPEAT ); - std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { - { mesh.vertexGroups[0].vertexBuffer.attributes[0].offset, vertexBuffer.getHandle() }, - { mesh.vertexGroups[0].vertexBuffer.attributes[1].offset, vertexBuffer.getHandle() }, - { mesh.vertexGroups[0].vertexBuffer.attributes[2].offset, vertexBuffer.getHandle() } + const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { + vkcv::VertexBufferBinding( mesh.vertexGroups[0].vertexBuffer.attributes[0].offset, vertexBuffer.getVulkanHandle() ), + vkcv::VertexBufferBinding( mesh.vertexGroups[0].vertexBuffer.attributes[1].offset, vertexBuffer.getVulkanHandle() ), + vkcv::VertexBufferBinding( mesh.vertexGroups[0].vertexBuffer.attributes[2].offset, vertexBuffer.getVulkanHandle() ) }; vkcv::DescriptorWrites setWrites; setWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, texture.getHandle()) }; setWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, sampler) }; - core.writeResourceDescription(set, 0, setWrites); + core.writeResourceDescription(descriptorSet, 0, setWrites); + + vkcv::ImageHandle depthBuffer = core.createImage(vk::Format::eD32Sfloat, windowWidth, windowHeight).getHandle(); + + const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); + + const vkcv::Mesh renderMesh(vertexBufferBindings, indexBuffer.getVulkanHandle(), mesh.vertexGroups[0].numIndices); + + vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle); + vkcv::DrawcallInfo drawcall(renderMesh, { descriptorUsage }); auto start = std::chrono::system_clock::now(); while (window.isWindowOpen()) { vkcv::Window::pollEvents(); - - if(window.getHeight() == 0 || window.getWidth() == 0) - continue; - - core.beginFrame(); + + if(window.getHeight() == 0 || window.getWidth() == 0) + continue; + + uint32_t swapchainWidth, swapchainHeight; + if (!core.beginFrame(swapchainWidth, swapchainHeight)) { + continue; + } + + if ((swapchainWidth != windowWidth) || ((swapchainHeight != windowHeight))) { + depthBuffer = core.createImage(vk::Format::eD32Sfloat, swapchainWidth, swapchainHeight).getHandle(); + + windowWidth = swapchainWidth; + windowHeight = swapchainHeight; + } + auto end = std::chrono::system_clock::now(); auto deltatime = end - start; start = end; cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); - core.renderMesh( + vkcv::PushConstantData pushConstantData((void*)&mvp, sizeof(glm::mat4)); + + const std::vector<vkcv::ImageHandle> renderTargets = { swapchainInput, depthBuffer }; + auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); + + core.recordDrawcallsToCmdStream( + cmdStream, trianglePass, trianglePipeline, - sizeof(mvp), - &mvp, - vertexBufferBindings, - indexBuffer.getHandle(), - mesh.vertexGroups[0].numIndices, - set, - 0 - ); + pushConstantData, + { drawcall }, + renderTargets); + core.prepareSwapchainImageForPresent(cmdStream); + core.submitCommandStream(cmdStream); core.endFrame(); } diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp index 397498e60375ee34497b60fef7d27cbb2c0c7989..2ede653ff98e19159e0155b282cab1b309a13816 100644 --- a/projects/first_triangle/src/main.cpp +++ b/projects/first_triangle/src/main.cpp @@ -79,9 +79,6 @@ int main(int argc, const char** argv) { // an example attachment for passes that output to the window const vkcv::AttachmentDescription present_color_attachment( - vkcv::AttachmentLayout::UNDEFINED, - vkcv::AttachmentLayout::COLOR_ATTACHMENT, - vkcv::AttachmentLayout::PRESENTATION, vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::CLEAR, core.getSwapchainImageFormat()); @@ -103,21 +100,21 @@ int main(int argc, const char** argv) { const vkcv::PipelineConfig trianglePipelineDefinition( triangleShaderProgram, - windowWidth, - windowHeight, + (uint32_t)windowWidth, + (uint32_t)windowHeight, trianglePass, {}, - {}); + {}, + false); + vkcv::PipelineHandle trianglePipeline = core.createGraphicsPipeline(trianglePipelineDefinition); - + if (!trianglePipeline) { std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; return EXIT_FAILURE; } - std::vector<vkcv::VertexBufferBinding> vertexBufferBindings; - /* * BufferHandle triangleVertices = core.createBuffer(vertices); * BufferHandle triangleIndices = core.createBuffer(indices); @@ -133,27 +130,42 @@ int main(int argc, const char** argv) { * * PipelineHandle trianglePipeline = core.CreatePipeline(trianglePipeline); */ - auto start = std::chrono::system_clock::now(); + auto start = std::chrono::system_clock::now(); + + vkcv::ImageHandle swapchainImageHandle = vkcv::ImageHandle::createSwapchainImageHandle(); + + const vkcv::Mesh renderMesh({}, triangleIndexBuffer.getVulkanHandle(), 3); + vkcv::DrawcallInfo drawcall(renderMesh, {}); + + const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); + while (window.isWindowOpen()) { - core.beginFrame(); window.pollEvents(); + + uint32_t swapchainWidth, swapchainHeight; // No resizing = No problem + if (!core.beginFrame(swapchainWidth, swapchainHeight)) { + continue; + } + auto end = std::chrono::system_clock::now(); auto deltatime = end - start; start = end; cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); - core.renderMesh( + vkcv::PushConstantData pushConstantData((void*)&mvp, sizeof(glm::mat4)); + auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); + + core.recordDrawcallsToCmdStream( + cmdStream, trianglePass, trianglePipeline, - sizeof(mvp), - &mvp, - vertexBufferBindings, - triangleIndexBuffer.getHandle(), - 3, - vkcv::ResourcesHandle(), - 0); + pushConstantData, + { drawcall }, + { swapchainInput }); + core.prepareSwapchainImageForPresent(cmdStream); + core.submitCommandStream(cmdStream); core.endFrame(); } diff --git a/src/vkcv/BufferManager.cpp b/src/vkcv/BufferManager.cpp index ef874606ed0f30f33e4b1e0720d4f3455a8e137e..6d494c4ec90726d46039007607464378624f1c75 100644 --- a/src/vkcv/BufferManager.cpp +++ b/src/vkcv/BufferManager.cpp @@ -158,7 +158,7 @@ namespace vkcv { SubmitInfo submitInfo; submitInfo.queueType = QueueType::Transfer; - core->submitCommands( + core->recordAndSubmitCommands( submitInfo, [&info, &mapped_size](const vk::CommandBuffer& commandBuffer) { const vk::BufferCopy region ( diff --git a/src/vkcv/CommandStreamManager.cpp b/src/vkcv/CommandStreamManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d0a236a4eaa5a166be77d143370a018b9ea7e73 --- /dev/null +++ b/src/vkcv/CommandStreamManager.cpp @@ -0,0 +1,119 @@ +#include "vkcv/CommandStreamManager.hpp" +#include "vkcv/Core.hpp" + +namespace vkcv { + CommandStreamManager::CommandStreamManager() noexcept : m_core(nullptr){} + + CommandStreamManager::~CommandStreamManager() noexcept { + for (const auto& stream : m_commandStreams) { + if (stream.cmdBuffer && stream.cmdBuffer) { + m_core->getContext().getDevice().freeCommandBuffers(stream.cmdPool, stream.cmdBuffer); + } + } + } + + void CommandStreamManager::init(Core* core) { + if (!core) { + std::cerr << "Error: CommandStreamManager::init requires valid core pointer" << std::endl; + } + m_core = core; + } + + CommandStreamHandle CommandStreamManager::createCommandStream( + const vk::Queue queue, + vk::CommandPool cmdPool) { + + const vk::CommandBuffer cmdBuffer = allocateCommandBuffer(m_core->getContext().getDevice(), cmdPool); + + CommandStream stream(cmdBuffer, queue, cmdPool); + beginCommandBuffer(stream.cmdBuffer, vk::CommandBufferUsageFlagBits::eOneTimeSubmit); + + // find unused stream + int unusedStreamIndex = -1; + for (int i = 0; i < m_commandStreams.size(); i++) { + if (m_commandStreams[i].cmdBuffer) { + // still in use + } + else { + unusedStreamIndex = i; + break; + } + } + + const bool foundUnusedStream = unusedStreamIndex >= 0; + if (foundUnusedStream) { + m_commandStreams[unusedStreamIndex] = stream; + return CommandStreamHandle(unusedStreamIndex); + } + + CommandStreamHandle handle(m_commandStreams.size()); + m_commandStreams.push_back(stream); + return handle; + } + + void CommandStreamManager::recordCommandsToStream( + const CommandStreamHandle handle, + const RecordCommandFunction record) { + + const size_t id = handle.getId(); + if (id >= m_commandStreams.size()) { + std::cerr << "Error: CommandStreamManager::recordCommandsToStream requires valid handle" << std::endl; + return; + } + + CommandStream& stream = m_commandStreams[id]; + record(stream.cmdBuffer); + } + + void CommandStreamManager::addFinishCallbackToStream( + const CommandStreamHandle handle, + const FinishCommandFunction finish) { + + const size_t id = handle.getId(); + if (id >= m_commandStreams.size()) { + std::cerr << "Error: CommandStreamManager::addFinishCallbackToStream requires valid handle" << std::endl; + return; + } + + CommandStream& stream = m_commandStreams[id]; + stream.callbacks.push_back(finish); + } + + void CommandStreamManager::submitCommandStreamSynchronous( + const CommandStreamHandle handle, + std::vector<vk::Semaphore> &waitSemaphores, + std::vector<vk::Semaphore> &signalSemaphores) { + + const size_t id = handle.getId(); + if (id >= m_commandStreams.size()) { + std::cerr << "Error: CommandStreamManager::submitCommandStreamSynchronous requires valid handle" << std::endl; + return; + } + CommandStream& stream = m_commandStreams[id]; + stream.cmdBuffer.end(); + + const auto device = m_core->getContext().getDevice(); + const vk::Fence waitFence = createFence(device); + submitCommandBufferToQueue(stream.queue, stream.cmdBuffer, waitFence, waitSemaphores, signalSemaphores); + waitForFence(device, waitFence); + device.destroyFence(waitFence); + + device.freeCommandBuffers(stream.cmdPool, stream.cmdBuffer); + stream.cmdBuffer = nullptr; + stream.cmdPool = nullptr; + stream.queue = nullptr; + + for (const auto& finishCallback : stream.callbacks) { + finishCallback(); + } + } + + vk::CommandBuffer CommandStreamManager::getStreamCommandBuffer(const CommandStreamHandle handle) { + const size_t id = handle.getId(); + if (id >= m_commandStreams.size()) { + std::cerr << "Error: CommandStreamManager::submitCommandStreamSynchronous requires valid handle" << std::endl; + return nullptr; + } + return m_commandStreams[id].cmdBuffer; + } +} \ No newline at end of file diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index 27e84ee5cdb062adc0e9f96cdfa7e761e6f24279..9ed83d2a224119bd20fcfc81c5720b425de06bb6 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -14,6 +14,7 @@ #include "ImageManager.hpp" #include "DescriptorManager.hpp" #include "ImageLayoutTransitions.hpp" +#include "vkcv/CommandStreamManager.hpp" namespace vkcv { @@ -52,8 +53,8 @@ namespace vkcv return m_Context; } - Core::Core(Context &&context, Window &window, const SwapChain& swapChain, std::vector<vk::ImageView> imageViews, - const CommandResources& commandResources, const SyncResources& syncResources) noexcept : + Core::Core(Context &&context, Window &window, const SwapChain& swapChain, std::vector<vk::ImageView> imageViews, + const CommandResources& commandResources, const SyncResources& syncResources) noexcept : m_Context(std::move(context)), m_window(window), m_swapchain(swapChain), @@ -61,20 +62,25 @@ namespace vkcv m_PassManager{std::make_unique<PassManager>(m_Context.m_Device)}, m_PipelineManager{std::make_unique<PipelineManager>(m_Context.m_Device)}, m_DescriptorManager(std::make_unique<DescriptorManager>(m_Context.m_Device)), - m_BufferManager{std::unique_ptr<BufferManager>(new BufferManager())}, - m_SamplerManager(std::unique_ptr<SamplerManager>(new SamplerManager(m_Context.m_Device))), - m_ImageManager{std::unique_ptr<ImageManager>(new ImageManager(*m_BufferManager))}, + m_BufferManager{std::unique_ptr<BufferManager>(new BufferManager())}, + m_SamplerManager(std::unique_ptr<SamplerManager>(new SamplerManager(m_Context.m_Device))), + m_ImageManager{std::unique_ptr<ImageManager>(new ImageManager(*m_BufferManager))}, + m_CommandStreamManager{std::unique_ptr<CommandStreamManager>(new CommandStreamManager)}, m_CommandResources(commandResources), m_SyncResources(syncResources) { - m_BufferManager->m_core = this; - m_BufferManager->init(); - - m_ImageManager->m_core = this; + m_BufferManager->m_core = this; + m_BufferManager->init(); + m_CommandStreamManager->init(this); + + m_ImageManager->m_core = this; - e_resizeHandle = window.e_resize.add( [&](int width, int height) { - m_swapchain.recreateSwapchain(); - }); + e_resizeHandle = window.e_resize.add( [&](int width, int height) { + m_swapchain.signalSwapchainRecreation(); + }); + + m_swapchainImages = m_Context.getDevice().getSwapchainImagesKHR(m_swapchain.getSwapchain()); + m_swapchainImageLayouts.resize(m_swapchainImages.size(), vk::ImageLayout::eUndefined); } Core::~Core() noexcept { @@ -103,17 +109,23 @@ namespace vkcv Result Core::acquireSwapchainImage() { uint32_t imageIndex; + + vk::Result result; - const auto& acquireResult = m_Context.getDevice().acquireNextImageKHR( - m_swapchain.getSwapchain(), - std::numeric_limits<uint64_t>::max(), - m_SyncResources.swapchainImageAcquired, - nullptr, - &imageIndex, {} - ); + try { + result = m_Context.getDevice().acquireNextImageKHR( + m_swapchain.getSwapchain(), + std::numeric_limits<uint64_t>::max(), + m_SyncResources.swapchainImageAcquired, + nullptr, + &imageIndex, {} + ); + } catch (vk::OutOfDateKHRError e) { + result = vk::Result::eErrorOutOfDateKHR; + } - if (acquireResult != vk::Result::eSuccess) { - std::cerr << vk::to_string(acquireResult) << std::endl; + if (result != vk::Result::eSuccess) { + std::cerr << vk::to_string(result) << std::endl; return Result::ERROR; } @@ -121,7 +133,7 @@ namespace vkcv return Result::SUCCESS; } - void Core::beginFrame() { + bool Core::beginFrame(uint32_t& width, uint32_t& height) { if (m_swapchain.shouldUpdateSwapchain()) { m_Context.getDevice().waitIdle(); @@ -130,6 +142,10 @@ namespace vkcv m_swapchain.updateSwapchain(m_Context, m_window); m_swapchainImageViews = createImageViews(m_Context, m_swapchain); + m_swapchainImages = m_Context.getDevice().getSwapchainImagesKHR(m_swapchain.getSwapchain()); + + m_swapchainImageLayouts.clear(); + m_swapchainImageLayouts.resize(m_swapchainImages.size(), vk::ImageLayout::eUndefined); } if (acquireSwapchainImage() != Result::SUCCESS) { @@ -139,69 +155,83 @@ namespace vkcv } m_Context.getDevice().waitIdle(); // TODO: this is a sin against graphics programming, but its getting late - Alex + + const auto& extent = m_swapchain.getExtent(); + + width = extent.width; + height = extent.height; + + return (m_currentSwapchainImageIndex != std::numeric_limits<uint32_t>::max()); } - void Core::renderMesh( - const PassHandle &renderpassHandle, - const PipelineHandle &pipelineHandle, - const size_t pushConstantSize, - const void *pushConstantData, - const std::vector<VertexBufferBinding>& vertexBufferBindings, - const BufferHandle &indexBuffer, - const size_t indexCount, - const vkcv::ResourcesHandle resourceHandle, - const size_t resourceDescriptorSetIndex - ) { + void Core::recordDrawcallsToCmdStream( + const CommandStreamHandle cmdStreamHandle, + const PassHandle renderpassHandle, + const PipelineHandle pipelineHandle, + const PushConstantData &pushConstantData, + const std::vector<DrawcallInfo> &drawcalls, + const std::vector<ImageHandle> &renderTargets) { if (m_currentSwapchainImageIndex == std::numeric_limits<uint32_t>::max()) { return; } - - const vk::Extent2D& extent = m_swapchain.getExtent(); - - const uint32_t width = extent.width; - const uint32_t height = extent.height; - const vk::RenderPass renderpass = m_PassManager->getVkPass(renderpassHandle); - const PassConfig passConfig = m_PassManager->getPassConfig(renderpassHandle); - - const bool checkForDepthImage = ( - (!m_DepthImage) || - (width != m_ImageManager->getImageWidth(m_DepthImage)) || - (height != m_ImageManager->getImageHeight(m_DepthImage)) - ); - - if (checkForDepthImage) { - for (const auto &attachment : passConfig.attachments) { - if (attachment.layout_final == AttachmentLayout::DEPTH_STENCIL_ATTACHMENT) { - m_DepthImage = m_ImageManager->createImage(width, height, 1, attachment.format); - break; - } + uint32_t width; + uint32_t height; + if (renderTargets.size() > 0) { + const vkcv::ImageHandle firstImage = renderTargets[0]; + if (firstImage.isSwapchainImage()) { + const auto& swapchainExtent = m_swapchain.getExtent(); + width = swapchainExtent.width; + height = swapchainExtent.height; + } + else { + width = m_ImageManager->getImageWidth(firstImage); + height = m_ImageManager->getImageHeight(firstImage); } } - - const vk::ImageView imageView = m_swapchainImageViews[m_currentSwapchainImageIndex]; + else { + width = 1; + height = 1; + } + // TODO: validate that width/height match for all attachments + + const vk::RenderPass renderpass = m_PassManager->getVkPass(renderpassHandle); + const PassConfig passConfig = m_PassManager->getPassConfig(renderpassHandle); + const vk::Pipeline pipeline = m_PipelineManager->getVkPipeline(pipelineHandle); - const vk::PipelineLayout pipelineLayout = m_PipelineManager->getVkPipelineLayout(pipelineHandle); - const vkcv::PipelineConfig pipelineConfig = m_PipelineManager->getPipelineConfig(pipelineHandle); + const vk::PipelineLayout pipelineLayout = m_PipelineManager->getVkPipelineLayout(pipelineHandle); const vk::Rect2D renderArea(vk::Offset2D(0, 0), vk::Extent2D(width, height)); - const vk::Buffer vulkanIndexBuffer = m_BufferManager->getBuffer(indexBuffer); - std::vector<vk::ImageView> attachments; - attachments.push_back(imageView); - - if (m_DepthImage) { - attachments.push_back(m_ImageManager->getVulkanImageView(m_DepthImage)); + const vk::ImageView swapchainImageView = m_swapchainImageViews[m_currentSwapchainImageIndex]; + + std::vector<vk::ImageView> attachmentsViews; + for (const ImageHandle handle : renderTargets) { + vk::ImageView targetHandle; + const auto cmdBuffer = m_CommandStreamManager->getStreamCommandBuffer(cmdStreamHandle); + if (handle.isSwapchainImage()) { + recordSwapchainImageLayoutTransition(cmdBuffer, vk::ImageLayout::eColorAttachmentOptimal); + targetHandle = m_swapchainImageViews[m_currentSwapchainImageIndex]; + } + else { + targetHandle = m_ImageManager->getVulkanImageView(handle); + const bool isDepthImage = isDepthFormat(m_ImageManager->getImageFormat(handle)); + const vk::ImageLayout targetLayout = + isDepthFormat ? vk::ImageLayout::eDepthStencilAttachmentOptimal : vk::ImageLayout::eColorAttachmentOptimal; + m_ImageManager->recordImageLayoutTransition(handle, targetLayout, cmdBuffer); + } + attachmentsViews.push_back(targetHandle); } vk::Framebuffer framebuffer = nullptr; - const vk::FramebufferCreateInfo createInfo({}, - renderpass, - static_cast<uint32_t>(attachments.size()), - attachments.data(), - width, - height, - 1); + const vk::FramebufferCreateInfo createInfo( + {}, + renderpass, + static_cast<uint32_t>(attachmentsViews.size()), + attachmentsViews.data(), + width, + height, + 1); if(m_Context.m_Device.createFramebuffer(&createInfo, nullptr, &framebuffer) != vk::Result::eSuccess) { std::cout << "FAILED TO CREATE TEMPORARY FRAMEBUFFER!" << std::endl; @@ -209,8 +239,8 @@ namespace vkcv } vk::Viewport dynamicViewport(0.0f, 0.0f, - static_cast<float>(width), static_cast<float>(height), - 0.0f, 1.0f); + static_cast<float>(width), static_cast<float>(height), + 0.0f, 1.0f); vk::Rect2D dynamicScissor({0, 0}, {width, height}); @@ -227,7 +257,7 @@ namespace vkcv if (attachment.load_operation == AttachmentOperation::CLEAR) { float clear = 0.0f; - if (attachment.layout_final == AttachmentLayout::DEPTH_STENCIL_ATTACHMENT) { + if (isDepthFormat(attachment.format)) { clear = 1.0f; } @@ -246,35 +276,26 @@ namespace vkcv cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline, {}); - if(pipelineConfig.m_Height == UINT32_MAX && pipelineConfig.m_Width == UINT32_MAX) + const PipelineConfig &pipeConfig = m_PipelineManager->getPipelineConfig(pipelineHandle); + if(pipeConfig.m_UseDynamicViewport) { cmdBuffer.setViewport(0, 1, &dynamicViewport); cmdBuffer.setScissor(0, 1, &dynamicScissor); } - for (uint32_t i = 0; i < vertexBufferBindings.size(); i++) { - const auto &vertexBinding = vertexBufferBindings[i]; - const auto vertexBuffer = bufferManager->getBuffer(vertexBinding.buffer); - cmdBuffer.bindVertexBuffers(i, (vertexBuffer), (vertexBinding.offset)); + for (int i = 0; i < drawcalls.size(); i++) { + recordDrawcall(drawcalls[i], cmdBuffer, pipelineLayout, pushConstantData, i); } - if (resourceHandle) { - const vk::DescriptorSet descriptorSet = m_DescriptorManager->getDescriptorSet(resourceHandle, resourceDescriptorSetIndex); - cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr); - } - - cmdBuffer.bindIndexBuffer(vulkanIndexBuffer, 0, vk::IndexType::eUint16); //FIXME: choose proper size - cmdBuffer.pushConstants(pipelineLayout, vk::ShaderStageFlagBits::eAll, 0, pushConstantSize, pushConstantData); - cmdBuffer.drawIndexed(indexCount, 1, 0, 0, {}); cmdBuffer.endRenderPass(); }; - auto finishFunction = [&]() + auto finishFunction = [framebuffer, this]() { m_Context.m_Device.destroy(framebuffer); }; - submitCommands(submitInfo, submitFunction, finishFunction); + recordCommandsToStream(cmdStreamHandle, submitFunction, finishFunction); } void Core::endFrame() { @@ -289,16 +310,22 @@ namespace vkcv m_SyncResources.renderFinished, m_SyncResources.swapchainImageAcquired }; - vk::Result presentResult; const vk::SwapchainKHR& swapchain = m_swapchain.getSwapchain(); const vk::PresentInfoKHR presentInfo( waitSemaphores, swapchain, - m_currentSwapchainImageIndex, - presentResult); - queueManager.getPresentQueue().handle.presentKHR(presentInfo); - if (presentResult != vk::Result::eSuccess) { - std::cout << "Error: swapchain present failed" << std::endl; + m_currentSwapchainImageIndex); + + vk::Result result; + + try { + result = queueManager.getPresentQueue().handle.presentKHR(presentInfo); + } catch (vk::OutOfDateKHRError e) { + result = vk::Result::eErrorOutOfDateKHR; + } + + if (result != vk::Result::eSuccess) { + std::cout << "Error: swapchain present failed... " << vk::to_string(result) << std::endl; } } @@ -306,7 +333,10 @@ namespace vkcv return m_swapchain.getSwapchainFormat(); } - void Core::submitCommands(const SubmitInfo &submitInfo, const RecordCommandFunction& record, const FinishCommandFunction& finish) + void Core::recordAndSubmitCommands( + const SubmitInfo &submitInfo, + const RecordCommandFunction &record, + const FinishCommandFunction &finish) { const vk::Device& device = m_Context.getDevice(); @@ -329,23 +359,50 @@ namespace vkcv finish(); } } - + + CommandStreamHandle Core::createCommandStream(QueueType queueType) { + + const vk::Device& device = m_Context.getDevice(); + const vkcv::Queue queue = getQueueForSubmit(queueType, m_Context.getQueueManager()); + const vk::CommandPool cmdPool = chooseCmdPool(queue, m_CommandResources); + + return m_CommandStreamManager->createCommandStream(queue.handle, cmdPool); + } + + void Core::recordCommandsToStream( + const CommandStreamHandle cmdStreamHandle, + const RecordCommandFunction &record, + const FinishCommandFunction &finish) { + + m_CommandStreamManager->recordCommandsToStream(cmdStreamHandle, record); + if (finish) { + m_CommandStreamManager->addFinishCallbackToStream(cmdStreamHandle, finish); + } + } + + void Core::submitCommandStream(const CommandStreamHandle handle) { + std::vector<vk::Semaphore> waitSemaphores; + // FIXME: add proper user controllable sync + std::vector<vk::Semaphore> signalSemaphores = { m_SyncResources.renderFinished }; + m_CommandStreamManager->submitCommandStreamSynchronous(handle, waitSemaphores, signalSemaphores); + } + SamplerHandle Core::createSampler(SamplerFilterType magFilter, SamplerFilterType minFilter, SamplerMipmapMode mipmapMode, SamplerAddressMode addressMode) { - return m_SamplerManager->createSampler(magFilter, minFilter, mipmapMode, addressMode); - } - + return m_SamplerManager->createSampler(magFilter, minFilter, mipmapMode, addressMode); + } + Image Core::createImage(vk::Format format, uint32_t width, uint32_t height, uint32_t depth) { return Image::create(m_ImageManager.get(), format, width, height, depth); } - ResourcesHandle Core::createResourceDescription(const std::vector<DescriptorSetConfig> &descriptorSets) + DescriptorSetHandle Core::createDescriptorSet(const std::vector<DescriptorBinding>& bindings) { - return m_DescriptorManager->createResourceDescription(descriptorSets); + return m_DescriptorManager->createDescriptorSet(bindings); } - void Core::writeResourceDescription(ResourcesHandle handle, size_t setIndex, const DescriptorWrites &writes) { + void Core::writeResourceDescription(DescriptorSetHandle handle, size_t setIndex, const DescriptorWrites &writes) { m_DescriptorManager->writeResourceDescription( handle, setIndex, @@ -355,8 +412,8 @@ namespace vkcv *m_SamplerManager); } - vk::DescriptorSetLayout Core::getDescriptorSetLayout(ResourcesHandle handle, size_t setIndex) { - return m_DescriptorManager->getDescriptorSetLayout(handle, setIndex); + DescriptorSet Core::getDescriptorSet(const DescriptorSetHandle handle) const { + return m_DescriptorManager->getDescriptorSet(handle); } std::vector<vk::ImageView> Core::createImageViews( Context &context, SwapChain& swapChain){ @@ -380,11 +437,32 @@ namespace vkcv vk::ImageViewType::e2D, swapChain.getSwapchainFormat(), componentMapping, - subResourceRange - ); + subResourceRange); imageViews.push_back(context.getDevice().createImageView(imageViewCreateInfo)); } return imageViews; } + + void Core::recordSwapchainImageLayoutTransition(vk::CommandBuffer cmdBuffer, vk::ImageLayout newLayout) { + auto& imageLayout = m_swapchainImageLayouts[m_currentSwapchainImageIndex]; + const auto transitionBarrier = createSwapchainImageLayoutTransitionBarrier( + m_swapchainImages[m_currentSwapchainImageIndex], + imageLayout, + newLayout); + recordImageBarrier(cmdBuffer, transitionBarrier); + imageLayout = newLayout; + } + + void Core::prepareSwapchainImageForPresent(const CommandStreamHandle handle) { + m_CommandStreamManager->recordCommandsToStream(handle, [&](vk::CommandBuffer cmdBuffer) { + recordSwapchainImageLayoutTransition(cmdBuffer, vk::ImageLayout::ePresentSrcKHR); + }); + } + + void Core::prepareImageForSampling(const CommandStreamHandle cmdStream, const ImageHandle image) { + recordCommandsToStream(cmdStream, [image, this](const vk::CommandBuffer cmdBuffer) { + m_ImageManager->recordImageLayoutTransition(image, vk::ImageLayout::eShaderReadOnlyOptimal, cmdBuffer); + }, nullptr); + } } diff --git a/src/vkcv/DescriptorConfig.cpp b/src/vkcv/DescriptorConfig.cpp index c4f6e326560e91747d206fecc983525a5b7bb6dc..be6cfc9b40baad6636e6bc6a2b6e803aacd7ddc0 100644 --- a/src/vkcv/DescriptorConfig.cpp +++ b/src/vkcv/DescriptorConfig.cpp @@ -1,20 +1,13 @@ #include "vkcv/DescriptorConfig.hpp" -#include <utility> - namespace vkcv { - - DescriptorBinding::DescriptorBinding( - DescriptorType descriptorType, - uint32_t descriptorCount, - ShaderStage shaderStage - ) noexcept : - descriptorType{descriptorType}, - descriptorCount{descriptorCount}, - shaderStage{shaderStage} - {}; - - DescriptorSetConfig::DescriptorSetConfig(std::vector<DescriptorBinding> bindings) noexcept : - bindings{std::move(bindings)} - {}; + DescriptorBinding::DescriptorBinding( + DescriptorType descriptorType, + uint32_t descriptorCount, + ShaderStage shaderStage) noexcept + : + descriptorType(descriptorType), + descriptorCount(descriptorCount), + shaderStage(shaderStage) {} + } diff --git a/src/vkcv/DescriptorManager.cpp b/src/vkcv/DescriptorManager.cpp index 2ecb23bd39cd4656c12d1816ece3db404ecdc248..a2efecbe7055122d28a864b7c722a5998be460e4 100644 --- a/src/vkcv/DescriptorManager.cpp +++ b/src/vkcv/DescriptorManager.cpp @@ -2,11 +2,6 @@ namespace vkcv { - DescriptorManager::ResourceDescription::ResourceDescription(std::vector<vk::DescriptorSet> sets, - std::vector<vk::DescriptorSetLayout> layouts) noexcept : - descriptorSets{std::move(sets)}, - descriptorSetLayouts{std::move(layouts)} - {} DescriptorManager::DescriptorManager(vk::Device device) noexcept: m_Device{ device } { @@ -29,67 +24,63 @@ namespace vkcv DescriptorManager::~DescriptorManager() noexcept { - for (uint64_t id = 0; id < m_ResourceDescriptions.size(); id++) { - destroyResourceDescriptionById(id); + for (uint64_t id = 0; id < m_DescriptorSets.size(); id++) { + destroyDescriptorSetById(id); } + m_DescriptorSets.clear(); for (const auto &pool : m_Pools) { m_Device.destroy(pool); } } - ResourcesHandle DescriptorManager::createResourceDescription(const std::vector<DescriptorSetConfig> &descriptorSets) + DescriptorSetHandle DescriptorManager::createDescriptorSet(const std::vector<DescriptorBinding>& bindings) { - std::vector<vk::DescriptorSet> vk_sets; - std::vector<vk::DescriptorSetLayout> vk_setLayouts; + std::vector<vk::DescriptorSetLayoutBinding> setBindings = {}; - for (const auto &set : descriptorSets) { - std::vector<vk::DescriptorSetLayoutBinding> setBindings = {}; + //create each set's binding + for (uint32_t i = 0; i < bindings.size(); i++) { + vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding( + i, + convertDescriptorTypeFlag(bindings[i].descriptorType), + bindings[i].descriptorCount, + convertShaderStageFlag(bindings[i].shaderStage)); + setBindings.push_back(descriptorSetLayoutBinding); + } - //create each set's binding - for (uint32_t j = 0; j < set.bindings.size(); j++) { - vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding( - j, - convertDescriptorTypeFlag(set.bindings[j].descriptorType), - set.bindings[j].descriptorCount, - convertShaderStageFlag(set.bindings[j].shaderStage)); - setBindings.push_back(descriptorSetLayoutBinding); - } + DescriptorSet set; - //create the descriptor set's layout from the bindings gathered above - vk::DescriptorSetLayoutCreateInfo layoutInfo({}, setBindings); - vk::DescriptorSetLayout layout = nullptr; - if(m_Device.createDescriptorSetLayout(&layoutInfo, nullptr, &layout) != vk::Result::eSuccess) - { - std::cout << "FAILED TO CREATE DESCRIPTOR SET LAYOUT" << std::endl; - return ResourcesHandle(); - }; - vk_setLayouts.push_back(layout); - } - //create and allocate the set(s) based on the layouts that have been gathered above - vk_sets.resize(vk_setLayouts.size()); - vk::DescriptorSetAllocateInfo allocInfo(m_Pools.back(), vk_sets.size(), vk_setLayouts.data()); - auto result = m_Device.allocateDescriptorSets(&allocInfo, vk_sets.data()); + //create the descriptor set's layout from the bindings gathered above + vk::DescriptorSetLayoutCreateInfo layoutInfo({}, setBindings); + if(m_Device.createDescriptorSetLayout(&layoutInfo, nullptr, &set.layout) != vk::Result::eSuccess) + { + std::cout << "FAILED TO CREATE DESCRIPTOR SET LAYOUT" << std::endl; + return DescriptorSetHandle(); + }; + + //create and allocate the set based on the layout that have been gathered above + vk::DescriptorSetAllocateInfo allocInfo(m_Pools.back(), 1, &set.layout); + auto result = m_Device.allocateDescriptorSets(&allocInfo, &set.vulkanHandle); if(result != vk::Result::eSuccess) { //create a new descriptor pool if the previous one ran out of memory if (result == vk::Result::eErrorOutOfPoolMemory) { allocateDescriptorPool(); allocInfo.setDescriptorPool(m_Pools.back()); - result = m_Device.allocateDescriptorSets(&allocInfo, vk_sets.data()); + result = m_Device.allocateDescriptorSets(&allocInfo, &set.vulkanHandle); } if (result != vk::Result::eSuccess) { std::cout << "FAILED TO ALLOCATE DESCRIPTOR SET" << std::endl; std::cout << vk::to_string(result) << std::endl; - for (const auto& layout : vk_setLayouts) - m_Device.destroy(layout); + m_Device.destroy(set.layout); - return ResourcesHandle(); + return DescriptorSetHandle(); } }; - const uint64_t id = m_ResourceDescriptions.size(); - m_ResourceDescriptions.emplace_back(vk_sets, vk_setLayouts); - return ResourcesHandle(id, [&](uint64_t id) { destroyResourceDescriptionById(id); }); + const uint64_t id = m_DescriptorSets.size(); + + m_DescriptorSets.push_back(set); + return DescriptorSetHandle(id, [&](uint64_t id) { destroyDescriptorSetById(id); }); } struct WriteDescriptorSetInfo { @@ -100,14 +91,14 @@ namespace vkcv }; void DescriptorManager::writeResourceDescription( - const ResourcesHandle &handle, + const DescriptorSetHandle &handle, size_t setIndex, const DescriptorWrites &writes, const ImageManager &imageManager, const BufferManager &bufferManager, const SamplerManager &samplerManager) { - vk::DescriptorSet set = m_ResourceDescriptions[handle.getId()].descriptorSets[setIndex]; + vk::DescriptorSet set = m_DescriptorSets[handle.getId()].vulkanHandle; std::vector<vk::DescriptorImageInfo> imageInfos; std::vector<vk::DescriptorBufferInfo> bufferInfos; @@ -230,12 +221,8 @@ namespace vkcv m_Device.updateDescriptorSets(vulkanWrites, nullptr); } - vk::DescriptorSet DescriptorManager::getDescriptorSet(const ResourcesHandle &handle, size_t index) const { - return m_ResourceDescriptions[handle.getId()].descriptorSets[index]; - } - - vk::DescriptorSetLayout DescriptorManager::getDescriptorSetLayout(const ResourcesHandle &handle, size_t index) const { - return m_ResourceDescriptions[handle.getId()].descriptorSetLayouts[index]; + DescriptorSet DescriptorManager::getDescriptorSet(const DescriptorSetHandle handle) const { + return m_DescriptorSets[handle.getId()]; } vk::DescriptorType DescriptorManager::convertDescriptorTypeFlag(DescriptorType type) { @@ -277,18 +264,18 @@ namespace vkcv } } - void DescriptorManager::destroyResourceDescriptionById(uint64_t id) { - if (id >= m_ResourceDescriptions.size()) { + void DescriptorManager::destroyDescriptorSetById(uint64_t id) { + if (id >= m_DescriptorSets.size()) { + std::cerr << "Error: DescriptorManager::destroyResourceDescriptionById invalid id" << std::endl; return; } - auto& resourceDescription = m_ResourceDescriptions[id]; - - for(const auto &layout : resourceDescription.descriptorSetLayouts) { - m_Device.destroyDescriptorSetLayout(layout); + auto& set = m_DescriptorSets[id]; + if (set.layout) { + m_Device.destroyDescriptorSetLayout(set.layout); + set.layout = nullptr; } - - resourceDescription.descriptorSetLayouts.clear(); + // FIXME: descriptor set itself not destroyed } vk::DescriptorPool DescriptorManager::allocateDescriptorPool() { diff --git a/src/vkcv/DescriptorManager.hpp b/src/vkcv/DescriptorManager.hpp index 22042c703256055e3852b7a4729faad39b5d0dbb..d8607b9312b25e71c7eb4af009efd92b834b40ec 100644 --- a/src/vkcv/DescriptorManager.hpp +++ b/src/vkcv/DescriptorManager.hpp @@ -21,17 +21,10 @@ namespace vkcv explicit DescriptorManager(vk::Device device) noexcept; ~DescriptorManager() noexcept; - /** - * Creates all vk::DescriptorSets and allocates them from the pool. - * DescriptorSets are put inside a ResourceDescription struct. - * Structs are then put into m_ResourceDescriptions. - * @param[in] vector of filled vkcv::DescriptorSet structs - * @return index into that objects a resource handle - */ - ResourcesHandle createResourceDescription(const std::vector<DescriptorSetConfig> & descriptorSets); + DescriptorSetHandle createDescriptorSet(const std::vector<DescriptorBinding> &descriptorBindings); void writeResourceDescription( - const ResourcesHandle &handle, + const DescriptorSetHandle &handle, size_t setIndex, const DescriptorWrites &writes, const ImageManager &imageManager, @@ -39,34 +32,19 @@ namespace vkcv const SamplerManager &samplerManager); [[nodiscard]] - vk::DescriptorSet getDescriptorSet(const ResourcesHandle &handle, size_t index) const; - [[nodiscard]] - vk::DescriptorSetLayout getDescriptorSetLayout(const ResourcesHandle &handle, size_t index) const; + DescriptorSet getDescriptorSet(const DescriptorSetHandle handle) const; private: - vk::Device m_Device; + vk::Device m_Device; std::vector<vk::DescriptorPool> m_Pools; std::vector<vk::DescriptorPoolSize> m_PoolSizes; vk::DescriptorPoolCreateInfo m_PoolInfo; - /** - * Container for all resources requested by the user in one call of createResourceDescription. - * Includes descriptor sets and the respective descriptor set layouts. - */ - struct ResourceDescription - { - ResourceDescription() = delete; - ResourceDescription(std::vector<vk::DescriptorSet> sets, std::vector<vk::DescriptorSetLayout> layouts) noexcept; - - std::vector<vk::DescriptorSet> descriptorSets; - std::vector<vk::DescriptorSetLayout> descriptorSetLayouts; - }; - /** * Contains all the resource descriptions that were requested by the user in calls of createResourceDescription. */ - std::vector<ResourceDescription> m_ResourceDescriptions; + std::vector<DescriptorSet> m_DescriptorSets; /** * Converts the flags of the descriptor types from VulkanCV (vkcv) to Vulkan (vk). @@ -85,7 +63,7 @@ namespace vkcv * Destroys a specific resource description * @param[in] the handle id of the respective resource description */ - void destroyResourceDescriptionById(uint64_t id); + void destroyDescriptorSetById(uint64_t id); /** * creates a descriptor pool based on the poolSizes and poolInfo defined in the constructor diff --git a/src/vkcv/DrawcallRecording.cpp b/src/vkcv/DrawcallRecording.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85b6eeb5fa413223b7b7f10f77b868252912041b --- /dev/null +++ b/src/vkcv/DrawcallRecording.cpp @@ -0,0 +1,41 @@ +#include <vkcv/DrawcallRecording.hpp> + +namespace vkcv { + + void recordDrawcall( + const DrawcallInfo &drawcall, + vk::CommandBuffer cmdBuffer, + vk::PipelineLayout pipelineLayout, + const PushConstantData &pushConstantData, + const size_t drawcallIndex) { + + for (uint32_t i = 0; i < drawcall.mesh.vertexBufferBindings.size(); i++) { + const auto& vertexBinding = drawcall.mesh.vertexBufferBindings[i]; + cmdBuffer.bindVertexBuffers(i, vertexBinding.buffer, vertexBinding.offset); + } + + for (const auto& descriptorUsage : drawcall.descriptorSets) { + cmdBuffer.bindDescriptorSets( + vk::PipelineBindPoint::eGraphics, + pipelineLayout, + descriptorUsage.setLocation, + descriptorUsage.vulkanHandle, + nullptr); + } + + cmdBuffer.bindIndexBuffer(drawcall.mesh.indexBuffer, 0, vk::IndexType::eUint16); //FIXME: choose proper size + + const size_t drawcallPushConstantOffset = drawcallIndex * pushConstantData.sizePerDrawcall; + // char* cast because void* does not support pointer arithmetic + const void* drawcallPushConstantData = drawcallPushConstantOffset + (char*)pushConstantData.data; + + cmdBuffer.pushConstants( + pipelineLayout, + vk::ShaderStageFlagBits::eAll, + 0, + pushConstantData.sizePerDrawcall, + drawcallPushConstantData); + + cmdBuffer.drawIndexed(drawcall.mesh.indexCount, 1, 0, 0, {}); + } +} \ No newline at end of file diff --git a/src/vkcv/Handles.cpp b/src/vkcv/Handles.cpp index 33e0cdba7f30fe6c3576803de1d22f912c935dd0..020489418c8e2db6ce2062d6fd20f06f90a05c37 100644 --- a/src/vkcv/Handles.cpp +++ b/src/vkcv/Handles.cpp @@ -93,7 +93,7 @@ namespace vkcv { } ImageHandle ImageHandle::createSwapchainImageHandle(const HandleDestroyFunction &destroy) { - return ImageHandle(UINT64_MAX - 1, destroy); + return ImageHandle(uint64_t(UINT64_MAX - 1), destroy); } } diff --git a/src/vkcv/Image.cpp b/src/vkcv/Image.cpp index 9ce5c25a01957ca43c75c14f6b37a7a3e82feee4..f861daeb1cd7de9697e2f649de444666b8b0e63c 100644 --- a/src/vkcv/Image.cpp +++ b/src/vkcv/Image.cpp @@ -8,13 +8,24 @@ namespace vkcv{ + bool isDepthFormat(const vk::Format format) { + switch (format) { + case(vk::Format::eD16Unorm): return true; + case(vk::Format::eD16UnormS8Uint): return true; + case(vk::Format::eD24UnormS8Uint): return true; + case(vk::Format::eD32Sfloat): return true; + case(vk::Format::eD32SfloatS8Uint): return true; + default: return false; + } + } + Image Image::create(ImageManager* manager, vk::Format format, uint32_t width, uint32_t height, uint32_t depth) { - return Image(manager, manager->createImage(width, height, depth, format), format); + return Image(manager, manager->createImage(width, height, depth, format)); } vk::Format Image::getFormat() const { - return m_format; + return m_manager->getImageFormat(m_handle); } uint32_t Image::getWidth() const { @@ -28,15 +39,10 @@ namespace vkcv{ uint32_t Image::getDepth() const { return m_manager->getImageDepth(m_handle); } - - vk::ImageLayout Image::getLayout() const { - return m_layout; - } void Image::switchLayout(vk::ImageLayout newLayout) { - m_manager->switchImageLayout(m_handle, m_layout, newLayout); - m_layout = newLayout; + m_manager->switchImageLayoutImmediate(m_handle, newLayout); } vkcv::ImageHandle Image::getHandle() const { @@ -47,12 +53,9 @@ namespace vkcv{ m_manager->fillImage(m_handle, data, size); } - Image::Image(ImageManager* manager, const ImageHandle& handle, vk::Format format) : + Image::Image(ImageManager* manager, const ImageHandle& handle) : m_manager(manager), - m_handle(handle), - m_format(format), - m_layout(vk::ImageLayout::eUndefined) - { - } + m_handle(handle) + {} } diff --git a/src/vkcv/ImageLayoutTransitions.cpp b/src/vkcv/ImageLayoutTransitions.cpp index 0b08819489c41c5cde3ceddbb0629a5d2ae3cd30..cb0f90a79d188cd80a5744d8c6ad7718e542d473 100644 --- a/src/vkcv/ImageLayoutTransitions.cpp +++ b/src/vkcv/ImageLayoutTransitions.cpp @@ -1,24 +1,68 @@ #include "ImageLayoutTransitions.hpp" +#include "vkcv/Image.hpp" namespace vkcv { - void transitionImageLayoutImmediate(const vk::CommandBuffer cmdBuffer, const vk::Image image, - const vk::ImageLayout oldLayout, const vk::ImageLayout newLayout) { + vk::ImageMemoryBarrier createImageLayoutTransitionBarrier(const ImageManager::Image &image, vk::ImageLayout newLayout) { - // TODO: proper src and dst masks - const vk::PipelineStageFlags srcStageMask = vk::PipelineStageFlagBits::eAllCommands; - const vk::PipelineStageFlags dstStageMask = vk::PipelineStageFlagBits::eAllCommands; - const vk::DependencyFlags dependecyFlags = {}; + vk::ImageAspectFlags aspectFlags; + if (isDepthFormat(image.m_format)) { + aspectFlags = vk::ImageAspectFlagBits::eDepth; + } + else { + aspectFlags = vk::ImageAspectFlagBits::eColor; + } - // TODO: proper src and dst masks - const vk::AccessFlags srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; - const vk::AccessFlags dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; + vk::ImageSubresourceRange imageSubresourceRange( + aspectFlags, + 0, + image.m_levels, + 0, + image.m_layers + ); - // TODO: proper aspect flags - const vk::ImageAspectFlags aspectFlags = vk::ImageAspectFlagBits::eColor; + // TODO: precise AccessFlagBits, will require a lot of context + return vk::ImageMemoryBarrier( + vk::AccessFlagBits::eMemoryWrite, + vk::AccessFlagBits::eMemoryRead, + image.m_layout, + newLayout, + VK_QUEUE_FAMILY_IGNORED, + VK_QUEUE_FAMILY_IGNORED, + image.m_handle, + imageSubresourceRange); + } + + vk::ImageMemoryBarrier createSwapchainImageLayoutTransitionBarrier( + vk::Image vulkanHandle, + vk::ImageLayout oldLayout, + vk::ImageLayout newLayout) { - const vk::ImageSubresourceRange subresourceRange(aspectFlags, 0, 1, 0, 1); - vk::ImageMemoryBarrier imageBarrier(srcAccessMask, dstAccessMask, oldLayout, newLayout, 0, 0, image, subresourceRange); + vk::ImageSubresourceRange imageSubresourceRange( + vk::ImageAspectFlagBits::eColor, + 0, + 1, + 0, + 1); + + // TODO: precise AccessFlagBits, will require a lot of context + return vk::ImageMemoryBarrier( + vk::AccessFlagBits::eMemoryWrite, + vk::AccessFlagBits::eMemoryRead, + oldLayout, + newLayout, + VK_QUEUE_FAMILY_IGNORED, + VK_QUEUE_FAMILY_IGNORED, + vulkanHandle, + imageSubresourceRange); + } - cmdBuffer.pipelineBarrier(srcStageMask, dstStageMask, dependecyFlags, 0, nullptr, 0, nullptr, 1, &imageBarrier, {}); + void recordImageBarrier(vk::CommandBuffer cmdBuffer, vk::ImageMemoryBarrier barrier) { + cmdBuffer.pipelineBarrier( + vk::PipelineStageFlagBits::eTopOfPipe, + vk::PipelineStageFlagBits::eBottomOfPipe, + {}, + nullptr, + nullptr, + barrier); } } \ No newline at end of file diff --git a/src/vkcv/ImageLayoutTransitions.hpp b/src/vkcv/ImageLayoutTransitions.hpp index 3dbfbdf6690a0683b30a96f400e7e4b6ec25c379..5c147f133a6492746ad410367e5e627be000d7be 100644 --- a/src/vkcv/ImageLayoutTransitions.hpp +++ b/src/vkcv/ImageLayoutTransitions.hpp @@ -1,7 +1,13 @@ #pragma once #include <vulkan/vulkan.hpp> +#include "ImageManager.hpp" namespace vkcv { - void transitionImageLayoutImmediate(const vk::CommandBuffer cmdBuffer, const vk::Image image, - const vk::ImageLayout oldLayout, const vk::ImageLayout newLayout); + vk::ImageMemoryBarrier createImageLayoutTransitionBarrier(const ImageManager::Image& image, vk::ImageLayout newLayout); + vk::ImageMemoryBarrier createSwapchainImageLayoutTransitionBarrier( + vk::Image vulkanHandle, + vk::ImageLayout oldLayout, + vk::ImageLayout newLayout); + + void recordImageBarrier(vk::CommandBuffer cmdBuffer, vk::ImageMemoryBarrier barrier); } \ No newline at end of file diff --git a/src/vkcv/ImageManager.cpp b/src/vkcv/ImageManager.cpp index 4b46b5fc422e1666049b22d422a16fec10fe08aa..cdfd32b009a8007b606c86bf087b3f921b2bb89f 100644 --- a/src/vkcv/ImageManager.cpp +++ b/src/vkcv/ImageManager.cpp @@ -5,11 +5,34 @@ */ #include "ImageManager.hpp" #include "vkcv/Core.hpp" +#include "ImageLayoutTransitions.hpp" #include <algorithm> namespace vkcv { + ImageManager::Image::Image( + vk::Image handle, + vk::DeviceMemory memory, + vk::ImageView view, + uint32_t width, + uint32_t height, + uint32_t depth, + vk::Format format, + uint32_t layers, + uint32_t levels) + : + m_handle(handle), + m_memory(memory), + m_view(view), + m_width(width), + m_height(height), + m_depth(depth), + m_format(format), + m_layers(layers), + m_levels(levels) + {} + /** * @brief searches memory type index for image allocation, combines requirements of image and application * @param physicalMemoryProperties Memory Properties of physical device @@ -89,6 +112,11 @@ namespace vkcv { } } + if (isDepthFormat) { + imageType = vk::ImageType::e2D; + imageViewType = vk::ImageViewType::e2D; + } + vk::ImageTiling imageTiling = vk::ImageTiling::eOptimal; if (!formatProperties.optimalTilingFeatures) { @@ -166,7 +194,7 @@ namespace vkcv { vk::ImageView view = device.createImageView(imageViewCreateInfo); const uint64_t id = m_images.size(); - m_images.push_back({ image, memory, view, width, height, depth, format, arrayLayers, mipLevels }); + m_images.push_back(Image(image, memory, view, width, height, depth, format, arrayLayers, mipLevels)); return ImageHandle(id, [&](uint64_t id) { destroyImageById(id); }); } @@ -178,6 +206,7 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::getVulkanImage invalid handle" << std::endl; return nullptr; } @@ -190,6 +219,7 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::getVulkanDeviceMemory invalid handle" << std::endl; return nullptr; } @@ -202,6 +232,7 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::getVulkanImageView invalid handle" << std::endl; return nullptr; } @@ -210,84 +241,53 @@ namespace vkcv { return image.m_view; } - void ImageManager::switchImageLayout(const ImageHandle& handle, vk::ImageLayout oldLayout, vk::ImageLayout newLayout) { + void ImageManager::switchImageLayoutImmediate(const ImageHandle& handle, vk::ImageLayout newLayout) { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::switchImageLayout invalid handle" << std::endl; return; } auto& image = m_images[id]; - - //alternativly we could use switch case for every variable to set - vk::AccessFlags sourceAccessMask; - vk::PipelineStageFlags sourceStage; - - vk::AccessFlags destinationAccessMask; - vk::PipelineStageFlags destinationStage; - - if ((oldLayout == vk::ImageLayout::eUndefined) && - (newLayout == vk::ImageLayout::eTransferDstOptimal)) - { - destinationAccessMask = vk::AccessFlagBits::eTransferWrite; - - sourceStage = vk::PipelineStageFlagBits::eTopOfPipe; - destinationStage = vk::PipelineStageFlagBits::eTransfer; - } - else if ((oldLayout == vk::ImageLayout::eTransferDstOptimal) && - (newLayout == vk::ImageLayout::eShaderReadOnlyOptimal)) - { - sourceAccessMask = vk::AccessFlagBits::eTransferWrite; - destinationAccessMask = vk::AccessFlagBits::eShaderRead; - - sourceStage = vk::PipelineStageFlagBits::eTransfer; - destinationStage = vk::PipelineStageFlagBits::eFragmentShader; - } - - vk::ImageAspectFlags aspectFlags; - - if (isDepthImageFormat(image.m_format)) { - aspectFlags = vk::ImageAspectFlagBits::eDepth; - } else { - aspectFlags = vk::ImageAspectFlagBits::eColor; - } - - vk::ImageSubresourceRange imageSubresourceRange( - aspectFlags, - 0, - image.m_levels, - 0, - image.m_layers - ); - - vk::ImageMemoryBarrier imageMemoryBarrier( - sourceAccessMask, - destinationAccessMask, - oldLayout, - newLayout, - VK_QUEUE_FAMILY_IGNORED, - VK_QUEUE_FAMILY_IGNORED, - image.m_handle, - imageSubresourceRange - ); + const auto transitionBarrier = createImageLayoutTransitionBarrier(image, newLayout); SubmitInfo submitInfo; submitInfo.queueType = QueueType::Graphics; - m_core->submitCommands( + m_core->recordAndSubmitCommands( submitInfo, - [sourceStage, destinationStage, imageMemoryBarrier](const vk::CommandBuffer& commandBuffer) { - commandBuffer.pipelineBarrier( - sourceStage, - destinationStage, - {}, - nullptr, - nullptr, - imageMemoryBarrier + [transitionBarrier](const vk::CommandBuffer& commandBuffer) { + // TODO: precise PipelineStageFlagBits, will require a lot of context + commandBuffer.pipelineBarrier( + vk::PipelineStageFlagBits::eTopOfPipe, + vk::PipelineStageFlagBits::eBottomOfPipe, + {}, + nullptr, + nullptr, + transitionBarrier ); }, - nullptr - ); + nullptr); + image.m_layout = newLayout; + } + + void ImageManager::recordImageLayoutTransition( + const ImageHandle& handle, + vk::ImageLayout newLayout, + vk::CommandBuffer cmdBuffer) { + + const uint64_t id = handle.getId(); + + if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::switchImageLayout invalid handle" << std::endl; + return; + } + + auto& image = m_images[id]; + const auto transitionBarrier = createImageLayoutTransitionBarrier(image, newLayout); + recordImageBarrier(cmdBuffer, transitionBarrier); + image.m_layout = newLayout; } void ImageManager::fillImage(const ImageHandle& handle, void* data, size_t size) @@ -295,16 +295,15 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::fillImage invalid handle" << std::endl; return; } auto& image = m_images[id]; - switchImageLayout( + switchImageLayoutImmediate( handle, - vk::ImageLayout::eUndefined, - vk::ImageLayout::eTransferDstOptimal - ); + vk::ImageLayout::eTransferDstOptimal); uint32_t channels = 4; // TODO: check image.m_format const size_t image_size = ( @@ -324,7 +323,7 @@ namespace vkcv { SubmitInfo submitInfo; submitInfo.queueType = QueueType::Transfer; - m_core->submitCommands( + m_core->recordAndSubmitCommands( submitInfo, [&image, &stagingBuffer](const vk::CommandBuffer& commandBuffer) { vk::ImageAspectFlags aspectFlags; @@ -358,9 +357,8 @@ namespace vkcv { ); }, [&]() { - switchImageLayout( + switchImageLayoutImmediate( handle, - vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal ); } @@ -371,6 +369,7 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::getImageWidth invalid handle" << std::endl; return 0; } @@ -383,6 +382,7 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::getImageHeight invalid handle" << std::endl; return 0; } @@ -395,6 +395,7 @@ namespace vkcv { const uint64_t id = handle.getId(); if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::getImageDepth invalid handle" << std::endl; return 0; } @@ -406,6 +407,7 @@ namespace vkcv { void ImageManager::destroyImageById(uint64_t id) { if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::destroyImageById invalid handle" << std::endl; return; } @@ -429,5 +431,16 @@ namespace vkcv { } } + vk::Format ImageManager::getImageFormat(const ImageHandle& handle) const { + + const uint64_t id = handle.getId(); + + if (id >= m_images.size()) { + std::cerr << "Error: ImageManager::destroyImageById invalid handle" << std::endl; + return vk::Format::eUndefined; + } + + return m_images[id].m_format; + } } \ No newline at end of file diff --git a/src/vkcv/ImageManager.hpp b/src/vkcv/ImageManager.hpp index f1bd9d0be774478f62098cbc2833b5c268be2eea..b9fccb25ec16bc1fd9569ab1a94627bd7ff06b18 100644 --- a/src/vkcv/ImageManager.hpp +++ b/src/vkcv/ImageManager.hpp @@ -11,23 +11,38 @@ #include "vkcv/Handles.hpp" namespace vkcv { - + class ImageManager { friend class Core; - private: + public: struct Image { - vk::Image m_handle; - vk::DeviceMemory m_memory; - vk::ImageView m_view; - uint32_t m_width = 0; - uint32_t m_height = 0; - uint32_t m_depth = 0; - vk::Format m_format; - uint32_t m_layers = 1; - uint32_t m_levels = 1; + vk::Image m_handle; + vk::DeviceMemory m_memory; + vk::ImageView m_view; + uint32_t m_width = 0; + uint32_t m_height = 0; + uint32_t m_depth = 0; + vk::Format m_format; + uint32_t m_layers = 1; + uint32_t m_levels = 1; + vk::ImageLayout m_layout = vk::ImageLayout::eUndefined; + private: + // struct is public so utility functions can access members, but only ImageManager can create Image + friend ImageManager; + Image( + vk::Image handle, + vk::DeviceMemory memory, + vk::ImageView view, + uint32_t width, + uint32_t height, + uint32_t depth, + vk::Format format, + uint32_t layers, + uint32_t levels); }; + private: Core* m_core; BufferManager& m_bufferManager; @@ -64,8 +79,13 @@ namespace vkcv { [[nodiscard]] vk::ImageView getVulkanImageView(const ImageHandle& handle) const; - - void switchImageLayout(const ImageHandle& handle, vk::ImageLayout oldLayout, vk::ImageLayout newLayout); + + void switchImageLayoutImmediate(const ImageHandle& handle, vk::ImageLayout newLayout); + void recordImageLayoutTransition( + const ImageHandle& handle, + vk::ImageLayout newLayout, + vk::CommandBuffer cmdBuffer); + void fillImage(const ImageHandle& handle, void* data, size_t size); [[nodiscard]] @@ -77,5 +97,7 @@ namespace vkcv { [[nodiscard]] uint32_t getImageDepth(const ImageHandle& handle) const; + [[nodiscard]] + vk::Format getImageFormat(const ImageHandle& handle) const; }; } \ No newline at end of file diff --git a/src/vkcv/PassConfig.cpp b/src/vkcv/PassConfig.cpp index ef07d3ee8d6170ae893cd055eefcc971cd1b87a3..602f1d3e2a8100ebd9bbb83772312d3d659abe86 100644 --- a/src/vkcv/PassConfig.cpp +++ b/src/vkcv/PassConfig.cpp @@ -5,15 +5,9 @@ namespace vkcv { AttachmentDescription::AttachmentDescription( - AttachmentLayout initial, - AttachmentLayout in_pass, - AttachmentLayout final, AttachmentOperation store_op, AttachmentOperation load_op, vk::Format format) noexcept : - layout_initial{initial}, - layout_in_pass{in_pass}, - layout_final{final}, store_operation{store_op}, load_operation{load_op}, format(format) diff --git a/src/vkcv/PassManager.cpp b/src/vkcv/PassManager.cpp index 8b59495ae7eaa68b6f42ed2a4786ae688e4a6375..c34b0d3631c48561f42eb7f21ba5578156910f51 100644 --- a/src/vkcv/PassManager.cpp +++ b/src/vkcv/PassManager.cpp @@ -1,4 +1,5 @@ #include "PassManager.hpp" +#include "vkcv/Image.hpp" namespace vkcv { @@ -73,63 +74,63 @@ namespace vkcv for (uint32_t i = 0; i < config.attachments.size(); i++) { // TODO: Renderpass struct should hold proper format information - vk::Format format = config.attachments[i].format; + vk::Format format = config.attachments[i].format; + vk::ImageLayout layout; - if (config.attachments[i].layout_in_pass == AttachmentLayout::DEPTH_STENCIL_ATTACHMENT) + if (isDepthFormat(config.attachments[i].format)) { + layout = vk::ImageLayout::eDepthStencilAttachmentOptimal; depthAttachmentReference.attachment = i; - depthAttachmentReference.layout = getVkLayoutFromAttachLayout(config.attachments[i].layout_in_pass); - pDepthAttachment = &depthAttachmentReference; + depthAttachmentReference.layout = layout; + pDepthAttachment = &depthAttachmentReference; } else { - vk::AttachmentReference attachmentRef(i, getVkLayoutFromAttachLayout(config.attachments[i].layout_in_pass)); + layout = vk::ImageLayout::eColorAttachmentOptimal; + vk::AttachmentReference attachmentRef(i, layout); colorAttachmentReferences.push_back(attachmentRef); } vk::AttachmentDescription attachmentDesc( - {}, - format, - vk::SampleCountFlagBits::e1, - getVKLoadOpFromAttachOp(config.attachments[i].load_operation), - getVkStoreOpFromAttachOp(config.attachments[i].store_operation), - vk::AttachmentLoadOp::eDontCare, - vk::AttachmentStoreOp::eDontCare, - getVkLayoutFromAttachLayout(config.attachments[i].layout_initial), - getVkLayoutFromAttachLayout(config.attachments[i].layout_final) - ); - + {}, + format, + vk::SampleCountFlagBits::e1, + getVKLoadOpFromAttachOp(config.attachments[i].load_operation), + getVkStoreOpFromAttachOp(config.attachments[i].store_operation), + vk::AttachmentLoadOp::eDontCare, + vk::AttachmentStoreOp::eDontCare, + layout, + layout); + attachmentDescriptions.push_back(attachmentDesc); } const vk::SubpassDescription subpassDescription( - {}, - vk::PipelineBindPoint::eGraphics, - 0, - {}, - static_cast<uint32_t>(colorAttachmentReferences.size()), - colorAttachmentReferences.data(), - {}, - pDepthAttachment, - 0, - {} - ); + {}, + vk::PipelineBindPoint::eGraphics, + 0, + {}, + static_cast<uint32_t>(colorAttachmentReferences.size()), + colorAttachmentReferences.data(), + {}, + pDepthAttachment, + 0, + {}); const vk::RenderPassCreateInfo passInfo( - {}, - static_cast<uint32_t>(attachmentDescriptions.size()), - attachmentDescriptions.data(), - 1, - &subpassDescription, - 0, - {} - ); + {}, + static_cast<uint32_t>(attachmentDescriptions.size()), + attachmentDescriptions.data(), + 1, + &subpassDescription, + 0, + {}); vk::RenderPass renderPass = m_Device.createRenderPass(passInfo); - + const uint64_t id = m_Passes.size(); - m_Passes.push_back({ renderPass, config }); - return PassHandle(id, [&](uint64_t id) { destroyPassById(id); }); + m_Passes.push_back({ renderPass, config }); + return PassHandle(id, [&](uint64_t id) { destroyPassById(id); }); } vk::RenderPass PassManager::getVkPass(const PassHandle &handle) const diff --git a/src/vkcv/PipelineConfig.cpp b/src/vkcv/PipelineConfig.cpp index d317258470bde76e8b8ba8e1f9bc684ea469b6c0..ad8437ca2a6c07862f66485c74c89ccba0d69ebe 100644 --- a/src/vkcv/PipelineConfig.cpp +++ b/src/vkcv/PipelineConfig.cpp @@ -9,18 +9,20 @@ namespace vkcv { PipelineConfig::PipelineConfig( - const ShaderProgram& shaderProgram, - uint32_t width, - uint32_t height, - PassHandle &passHandle, - const std::vector<VertexAttribute> &vertexAttributes, - const std::vector<vk::DescriptorSetLayout> &descriptorLayouts) + const ShaderProgram& shaderProgram, + uint32_t width, + uint32_t height, + const PassHandle &passHandle, + const std::vector<VertexAttribute> &vertexAttributes, + const std::vector<vk::DescriptorSetLayout> &descriptorLayouts, + bool useDynamicViewport) : m_ShaderProgram(shaderProgram), m_Height(height), m_Width(width), m_PassHandle(passHandle), - m_vertexAttributes(vertexAttributes), - m_descriptorLayouts(descriptorLayouts) + m_VertexAttributes(vertexAttributes), + m_DescriptorLayouts(descriptorLayouts), + m_UseDynamicViewport(useDynamicViewport) {} } diff --git a/src/vkcv/PipelineManager.cpp b/src/vkcv/PipelineManager.cpp index 16b08b7a5127769a19b7e0abe47b61f58406bafe..28a64a243b9a7a8fc9372409ef3783901219c868 100644 --- a/src/vkcv/PipelineManager.cpp +++ b/src/vkcv/PipelineManager.cpp @@ -1,4 +1,5 @@ #include "PipelineManager.hpp" +#include "vkcv/Image.hpp" namespace vkcv { @@ -93,7 +94,7 @@ namespace vkcv vk::Format vertexFormat = vertexFormatToVulkanFormat(attachment.format); //FIXME: hoping that order is the same and compatible: add explicit mapping and validation - const VertexAttribute attribute = config.m_vertexAttributes[i]; + const VertexAttribute attribute = config.m_VertexAttributes[i]; vertexAttributeDescriptions.emplace_back(location, binding, vertexFormatToVulkanFormat(attachment.format), 0); vertexBindingDescriptions.emplace_back(vk::VertexInputBindingDescription( @@ -173,13 +174,13 @@ namespace vkcv { 1.f,1.f,1.f,1.f } ); - const size_t matrixPushConstantSize = 4 * 4 * sizeof(float); + const size_t matrixPushConstantSize = config.m_ShaderProgram.getPushConstantSize(); const vk::PushConstantRange pushConstantRange(vk::ShaderStageFlagBits::eAll, 0, matrixPushConstantSize); // pipeline layout vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo( {}, - (config.m_descriptorLayouts), + (config.m_DescriptorLayouts), (pushConstantRange)); vk::PipelineLayout vkPipelineLayout{}; if (m_Device.createPipelineLayout(&pipelineLayoutCreateInfo, nullptr, &vkPipelineLayout) != vk::Result::eSuccess) @@ -207,14 +208,14 @@ namespace vkcv const PassConfig& passConfig = passManager.getPassConfig(config.m_PassHandle); for (const auto& attachment : passConfig.attachments) { - if (attachment.layout_final == AttachmentLayout::DEPTH_STENCIL_ATTACHMENT) { + if (isDepthFormat(attachment.format)) { p_depthStencilCreateInfo = &depthStencilCreateInfo; break; } } std::vector<vk::DynamicState> dynamicStates = {}; - if(config.m_Width == UINT32_MAX && config.m_Height == UINT32_MAX) + if(config.m_UseDynamicViewport) { dynamicStates.push_back(vk::DynamicState::eViewport); dynamicStates.push_back(vk::DynamicState::eScissor); diff --git a/src/vkcv/ShaderProgram.cpp b/src/vkcv/ShaderProgram.cpp index 5185b8b402eae5cd514689ba51a06e1a437271bf..3eea7ed21d99af2768d48e62af47cbdef94c7ef1 100644 --- a/src/vkcv/ShaderProgram.cpp +++ b/src/vkcv/ShaderProgram.cpp @@ -5,6 +5,7 @@ */ #include "vkcv/ShaderProgram.hpp" +#include <algorithm> namespace vkcv { /** @@ -128,9 +129,19 @@ namespace vkcv { m_VertexLayout = VertexLayout(inputVec); } + for (const auto &pushConstantBuffer : resources.push_constant_buffers) { + for (const auto &range : comp.get_active_buffer_ranges(pushConstantBuffer.id)) { + const size_t size = range.range + range.offset; + m_pushConstantSize = std::max(m_pushConstantSize, size); + } + } } const VertexLayout& ShaderProgram::getVertexLayout() const{ return m_VertexLayout; } + + size_t ShaderProgram::getPushConstantSize() const { + return m_pushConstantSize; + } } diff --git a/src/vkcv/SwapChain.cpp b/src/vkcv/SwapChain.cpp index 39c310de60db2c3678749f142c926a26ac127b58..b787536b66cfd802dfd435a773a584c875eeb391 100644 --- a/src/vkcv/SwapChain.cpp +++ b/src/vkcv/SwapChain.cpp @@ -244,7 +244,7 @@ namespace vkcv m_Extent = extent2D; } - void SwapChain::recreateSwapchain() { + void SwapChain::signalSwapchainRecreation() { m_RecreationRequired = true; }