diff --git a/include/vkcv/BufferManager.hpp b/include/vkcv/BufferManager.hpp index c7f32d9f134108bafa87ff493bca4e113d53003a..7144350afede0dc597f0fe6c6da2e4af5a9075e9 100644 --- a/include/vkcv/BufferManager.hpp +++ b/include/vkcv/BufferManager.hpp @@ -135,6 +135,13 @@ namespace vkcv void recordBufferMemoryBarrier( const BufferHandle& handle, vk::CommandBuffer cmdBuffer); + + void readBufferMemoryBarrier( + const BufferHandle& handle, + vk::CommandBuffer cmdBuffer, + void* data); + + void readBuffer(const BufferHandle& handle, void *data); }; } diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index cd3676f45bf0891de97ab88ff74cdd980f6920da..5db834cbb9976d856594263887847d71d42ce264 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -289,6 +289,7 @@ namespace vkcv void prepareImageForStorage(const CommandStreamHandle cmdStream, const ImageHandle image); void recordImageMemoryBarrier(const CommandStreamHandle cmdStream, const ImageHandle image); void recordBufferMemoryBarrier(const CommandStreamHandle cmdStream, const BufferHandle buffer); + void readBufferMemoryBarrier(const CommandStreamHandle cmdStream, const BufferHandle buffer, void *data); void resolveMSAAImage(CommandStreamHandle cmdStream, ImageHandle src, ImageHandle dst); vk::ImageView getSwapchainImageView() const; diff --git a/projects/neural_network/src/main.cpp b/projects/neural_network/src/main.cpp index 1da1ce402b83f72c13bcae40aa755c037b68239a..612ebd7a8c5dad5a0a8729eed6859cbda1c3dfe8 100644 --- a/projects/neural_network/src/main.cpp +++ b/projects/neural_network/src/main.cpp @@ -75,8 +75,10 @@ int main(int argc, const char** argv) { { vkcv::DescriptorSetUsage(0,core.getDescriptorSet(computeDescriptorSet).vulkanHandle) }, pushConstantsCompute); - core.recordBufferMemoryBarrier(cmdStream, inputBuffer.getHandle()); + int output[64] = { 0 }; + core.readBufferMemoryBarrier(cmdStream, inputBuffer.getHandle(), &output); core.submitCommandStream(cmdStream); + std::cout << output << std::endl; return 0; } diff --git a/src/vkcv/BufferManager.cpp b/src/vkcv/BufferManager.cpp index cfa233290b89702f196ed97c706254e002a0551b..85515f3180489057dbddd13781cec12f7b8261d0 100644 --- a/src/vkcv/BufferManager.cpp +++ b/src/vkcv/BufferManager.cpp @@ -330,4 +330,78 @@ namespace vkcv { nullptr); } + void BufferManager::readBuffer(const BufferHandle& handle, void *data) { + const uint64_t id = handle.getId(); + size_t size = getBufferSize(handle); + + if (size == 0) { + size = SIZE_MAX; + } + + if (id >= m_buffers.size()) { + return; + } + + auto& buffer = m_buffers[id]; + + const vma::Allocator& allocator = m_core->getContext().getAllocator(); + + + const size_t max_size = std::min(size, buffer.m_size); + + void* mapped = allocator.mapMemory(buffer.m_allocation); + memcpy(data, reinterpret_cast<char*>(mapped) , max_size); + allocator.unmapMemory(buffer.m_allocation); + + } + + void BufferManager ::readBufferMemoryBarrier(const BufferHandle& handle, vk::CommandBuffer cmdBuffer, void *data) { + + const uint64_t id = handle.getId(); + + if (id >= m_buffers.size()) { + vkcv_log(vkcv::LogLevel::ERROR, "Invalid buffer handle"); + return; + } + + auto& buffer = m_buffers[id]; + + vk::BufferMemoryBarrier memoryBarrier( + vk::AccessFlagBits::eShaderWrite, + vk::AccessFlagBits::eTransferRead, + 0, + 0, + buffer.m_handle, + 0, + buffer.m_size); + + cmdBuffer.pipelineBarrier( + vk::PipelineStageFlagBits::eTopOfPipe, + vk::PipelineStageFlagBits::eBottomOfPipe, + {}, + nullptr, + memoryBarrier, + nullptr); + + readBuffer(handle, data); + + vk::BufferMemoryBarrier memoryBarrier2( + vk::AccessFlagBits::eTransferWrite, + vk::AccessFlagBits::eHostRead, + 0, + 0, + buffer.m_handle, + 0, + buffer.m_size); + + cmdBuffer.pipelineBarrier( + vk::PipelineStageFlagBits::eTopOfPipe, + vk::PipelineStageFlagBits::eBottomOfPipe, + {}, + nullptr, + memoryBarrier2, + nullptr); + } + + } diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index a66c1e6220261679a85241fff42de08a57428d4c..bdfbdeef1411f9728969fd43c16bbec5a1a8c136 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -574,6 +574,12 @@ namespace vkcv m_BufferManager->recordBufferMemoryBarrier(buffer, cmdBuffer); }, nullptr); } + + void Core::readBufferMemoryBarrier(const CommandStreamHandle cmdStream, const BufferHandle buffer, void *data) { + recordCommandsToStream(cmdStream, [buffer, this, data](const vk::CommandBuffer cmdBuffer) { + m_BufferManager->readBufferMemoryBarrier(buffer, cmdBuffer, data); + }, nullptr); + } void Core::resolveMSAAImage(CommandStreamHandle cmdStream, ImageHandle src, ImageHandle dst) { recordCommandsToStream(cmdStream, [src, dst, this](const vk::CommandBuffer cmdBuffer) {