From 77db369cae93a3d7b4ebdb5c70806fed99484f6e Mon Sep 17 00:00:00 2001 From: nihuini Date: Tue, 2 Feb 2021 16:47:08 +0800 Subject: [PATCH] fix upload data may be invalidated by previous pipelines with the same memory on integrated device, fix #2640 --- src/command.cpp | 56 ++++++++++++++----------------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index b892a3d28..ec6f48678 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -378,14 +378,7 @@ void VkCompute::record_upload(const Mat& src, VkMat& dst, const Option& opt) // upload VkMat dst_staging; - if (opt.blob_vkallocator->mappable) - { - dst_staging.create_like(src_fp16, opt.blob_vkallocator); - } - else - { - dst_staging.create_like(src_fp16, opt.staging_vkallocator); - } + dst_staging.create_like(src_fp16, opt.staging_vkallocator); if (dst_staging.empty()) return; @@ -443,14 +436,7 @@ void VkCompute::record_upload(const Mat& src, VkImageMat& dst, const Option& opt // upload VkMat dst_staging; - if (opt.blob_vkallocator->mappable) - { - dst_staging.create_like(src_fp16, opt.blob_vkallocator); - } - else - { - dst_staging.create_like(src_fp16, opt.staging_vkallocator); - } + dst_staging.create_like(src_fp16, opt.staging_vkallocator); if (dst_staging.empty()) return; @@ -782,35 +768,25 @@ void VkCompute::record_clone(const Mat& src, VkMat& dst, const Option& opt) { // NCNN_LOGE("record_clone host to buffer"); - if (!opt.blob_vkallocator->mappable) - { - // host to staging - VkMat dst_staging; - Option opt_staging = opt; - opt_staging.blob_vkallocator = opt.staging_vkallocator; - record_clone(src, dst_staging, opt_staging); - - // staging to device - record_clone(dst_staging, dst, opt); - - // stash staging - d->upload_staging_buffers.push_back(dst_staging); - - return; - } - - // create dst - dst.create_like(src, opt.blob_vkallocator); - if (dst.empty()) + // host to staging + VkMat dst_staging; + dst_staging.create_like(src, opt.staging_vkallocator); + if (dst_staging.empty()) return; // memcpy src to device - memcpy(dst.mapped_ptr(), src.data, src.total() * src.elemsize); - dst.allocator->flush(dst.data); + memcpy(dst_staging.mapped_ptr(), src.data, src.total() * src.elemsize); + dst_staging.allocator->flush(dst_staging.data); // mark device host-write @ null - dst.data->access_flags = VK_ACCESS_HOST_WRITE_BIT; - dst.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; + dst_staging.data->access_flags = VK_ACCESS_HOST_WRITE_BIT; + dst_staging.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; + + // staging to device + record_clone(dst_staging, dst, opt); + + // stash staging + d->upload_staging_buffers.push_back(dst_staging); } void VkCompute::record_clone(const Mat& src, VkImageMat& dst, const Option& opt)