| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "command.h" |
| 16 | |
| 17 | #if NCNN_VULKAN |
| 18 | |
| 19 | #include "option.h" |
| 20 | #include "pipeline.h" |
| 21 | |
| 22 | namespace ncnn { |
| 23 | |
| 24 | class VkComputePrivate |
| 25 | { |
| 26 | public: |
| 27 | VkComputePrivate(const VulkanDevice* _vkdev); |
| 28 | ~VkComputePrivate(); |
| 29 | |
| 30 | int init(); |
| 31 | int begin_command_buffer(); |
| 32 | int end_command_buffer(); |
| 33 | |
| 34 | const VulkanDevice* vkdev; |
| 35 | |
| 36 | VkCommandPool compute_command_pool; |
| 37 | |
| 38 | VkCommandBuffer compute_command_buffer; |
| 39 | |
| 40 | VkFence compute_command_fence; |
| 41 | |
| 42 | std::vector<VkMat> upload_staging_buffers; |
| 43 | std::vector<VkMat> download_post_buffers; |
| 44 | std::vector<Mat> download_post_mats_fp16; |
| 45 | std::vector<Mat> download_post_mats; |
| 46 | |
| 47 | std::vector<VkImageMemory*> image_blocks_to_destroy; |
| 48 | |
| 49 | // the good-old path for device without VK_KHR_push_descriptor |
| 50 | std::vector<VkDescriptorPool> descriptor_pools; |
| 51 | std::vector<VkDescriptorSet> descriptorsets; |
| 52 | |
| 53 | struct record |
| 54 | { |
| 55 | enum |
| 56 | { |
| 57 | TYPE_copy_buffer, |
| 58 | TYPE_copy_image, |
| 59 | TYPE_copy_buffer_to_image, |
| 60 | TYPE_copy_image_to_buffer, |
| 61 | TYPE_bind_pipeline, |
| 62 | TYPE_bind_descriptorsets, |
| 63 | TYPE_push_constants, |
| 64 | TYPE_dispatch, |
| 65 | TYPE_memory_barrers, |
| 66 | TYPE_buffer_barrers, |
| 67 | TYPE_image_barrers, |
| 68 | |
| 69 | #if NCNN_BENCHMARK |
| 70 | TYPE_write_timestamp, |
| 71 | #endif // NCNN_BENCHMARK |
| 72 | |
| 73 | TYPE_post_download, |
| 74 | TYPE_post_cast_float16_to_float32, |
| 75 | }; |
| 76 | |
| 77 | int type; |
| 78 | VkCommandBuffer command_buffer; |
| 79 | |
| 80 | union |
| 81 | { |
| 82 | struct |
| 83 | { |
| 84 | VkBuffer src; |
| 85 | VkBuffer dst; |
| 86 | uint32_t region_count; |
| 87 | const VkBufferCopy* regions; |
| 88 | } copy_buffer; |
| 89 | struct |
| 90 | { |
| 91 | VkImage src; |
| 92 | VkImageLayout src_layout; |
| 93 | VkImage dst; |
| 94 | VkImageLayout dst_layout; |
| 95 | uint32_t region_count; |
| 96 | const VkImageCopy* regions; |
| 97 | } copy_image; |
| 98 | struct |
| 99 | { |
| 100 | VkBuffer src; |
| 101 | VkImage dst; |
| 102 | VkImageLayout layout; |
| 103 | uint32_t region_count; |
| 104 | const VkBufferImageCopy* regions; |
| 105 | } copy_buffer_to_image; |
| 106 | struct |
| 107 | { |
| 108 | VkImage src; |
| 109 | VkImageLayout layout; |
| 110 | VkBuffer dst; |
| 111 | uint32_t region_count; |
| 112 | const VkBufferImageCopy* regions; |
| 113 | } copy_image_to_buffer; |
| 114 | |
| 115 | struct |
| 116 | { |
| 117 | VkPipelineBindPoint bind_point; |
| 118 | VkPipeline pipeline; |
| 119 | } bind_pipeline; |
| 120 | struct |
| 121 | { |
| 122 | VkPipelineBindPoint bind_point; |
| 123 | VkPipelineLayout pipeline_layout; |
| 124 | uint32_t descriptorset_count; |
| 125 | uint32_t descriptorset_offset; |
| 126 | } bind_descriptorsets; |
| 127 | struct |
| 128 | { |
| 129 | VkPipelineLayout pipeline_layout; |
| 130 | VkShaderStageFlags stage_flags; |
| 131 | uint32_t size; |
| 132 | const void* values; |
| 133 | } push_constants; |
| 134 | |
| 135 | struct |
| 136 | { |
| 137 | uint32_t group_count_x; |
| 138 | uint32_t group_count_y; |
| 139 | uint32_t group_count_z; |
| 140 | } dispatch; |
| 141 | |
| 142 | struct |
| 143 | { |
| 144 | VkPipelineStageFlags src_stage; |
| 145 | VkPipelineStageFlags dst_stage; |
| 146 | uint32_t barrier_count; |
| 147 | const VkMemoryBarrier* barriers; |
| 148 | } memory_barrers; |
| 149 | struct |
| 150 | { |
| 151 | VkPipelineStageFlags src_stage; |
| 152 | VkPipelineStageFlags dst_stage; |
| 153 | uint32_t barrier_count; |
| 154 | const VkBufferMemoryBarrier* barriers; |
| 155 | } buffer_barrers; |
| 156 | struct |
| 157 | { |
| 158 | VkPipelineStageFlags src_stage; |
| 159 | VkPipelineStageFlags dst_stage; |
| 160 | uint32_t barrier_count; |
| 161 | const VkImageMemoryBarrier* barriers; |
| 162 | } image_barrers; |
| 163 | |
| 164 | #if NCNN_BENCHMARK |
| 165 | struct |
| 166 | { |
| 167 | uint32_t query; |
| 168 | } write_timestamp; |
| 169 | #endif // NCNN_BENCHMARK |
| 170 | |
| 171 | struct |
| 172 | { |
| 173 | uint32_t download_post_buffer_mat_offset; |
| 174 | uint32_t download_post_mat_fp16_offset; |
| 175 | } post_download; |
| 176 | struct |
| 177 | { |
| 178 | uint32_t download_post_mat_fp16_offset; |
| 179 | uint32_t download_post_mat_offset; |
| 180 | int num_threads; |
| 181 | } post_cast_float16_to_float32; |
| 182 | }; |
| 183 | }; |
| 184 | |
| 185 | std::vector<record> delayed_records; |
| 186 | |
| 187 | #if NCNN_BENCHMARK |
| 188 | uint32_t query_count; |
| 189 | VkQueryPool query_pool; |
| 190 | #endif // NCNN_BENCHMARK |
| 191 | }; |
| 192 | |
| 193 | VkComputePrivate::VkComputePrivate(const VulkanDevice* _vkdev) |
| 194 | : vkdev(_vkdev) |
| 195 | { |
| 196 | compute_command_pool = 0; |
| 197 | compute_command_buffer = 0; |
| 198 | compute_command_fence = 0; |
| 199 | |
| 200 | #if NCNN_BENCHMARK |
| 201 | query_count = 0; |
| 202 | query_pool = 0; |
| 203 | #endif // NCNN_BENCHMARK |
| 204 | |
| 205 | init(); |
| 206 | } |
| 207 | |
| 208 | VkComputePrivate::~VkComputePrivate() |
| 209 | { |
| 210 | for (size_t i = 0; i < image_blocks_to_destroy.size(); i++) |
| 211 | { |
| 212 | VkImageMemory* ptr = image_blocks_to_destroy[i]; |
| 213 | |
| 214 | int old_command_refcount = NCNN_XADD(&ptr->command_refcount, -1); |
| 215 | if (ptr->refcount == 0 && old_command_refcount == 1) |
| 216 | { |
| 217 | // no userspace reference and we are the last command reference |
| 218 | vkDestroyImageView(vkdev->vkdevice(), ptr->imageview, 0); |
| 219 | vkDestroyImage(vkdev->vkdevice(), ptr->image, 0); |
| 220 | |
| 221 | delete ptr; |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | // reference exists in user code or other command |
| 226 | } |
| 227 | } |
| 228 | image_blocks_to_destroy.clear(); |
| 229 | |
| 230 | if (!vkdev->info.support_VK_KHR_push_descriptor()) |
| 231 | { |
| 232 | for (size_t i = 0; i < descriptorsets.size(); i++) |
| 233 | { |
| 234 | vkFreeDescriptorSets(vkdev->vkdevice(), descriptor_pools[i], 1, &descriptorsets[i]); |
| 235 | vkDestroyDescriptorPool(vkdev->vkdevice(), descriptor_pools[i], 0); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | #if NCNN_BENCHMARK |
| 240 | if (query_pool) |
| 241 | { |
| 242 | // all submitted commands that refer to queryPool must have completed execution |
| 243 | vkResetCommandBuffer(compute_command_buffer, 0); |
| 244 | |
| 245 | vkDestroyQueryPool(vkdev->vkdevice(), query_pool, 0); |
| 246 | } |
| 247 | #endif // NCNN_BENCHMARK |
| 248 | |
| 249 | vkDestroyFence(vkdev->vkdevice(), compute_command_fence, 0); |
| 250 | |
| 251 | vkFreeCommandBuffers(vkdev->vkdevice(), compute_command_pool, 1, &compute_command_buffer); |
| 252 | vkDestroyCommandPool(vkdev->vkdevice(), compute_command_pool, 0); |
| 253 | } |
| 254 | |
| 255 | int VkComputePrivate::init() |
| 256 | { |
| 257 | // compute_command_pool |
| 258 | { |
| 259 | VkCommandPoolCreateInfo commandPoolCreateInfo; |
| 260 | commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 261 | commandPoolCreateInfo.pNext = 0; |
| 262 | commandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
| 263 | commandPoolCreateInfo.queueFamilyIndex = vkdev->info.compute_queue_family_index(); |
| 264 | |
| 265 | VkResult ret = vkCreateCommandPool(vkdev->vkdevice(), &commandPoolCreateInfo, 0, &compute_command_pool); |
| 266 | if (ret != VK_SUCCESS) |
| 267 | { |
| 268 | NCNN_LOGE("vkCreateCommandPool failed %d", ret); |
| 269 | return -1; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // compute_command_buffer |
| 274 | { |
| 275 | VkCommandBufferAllocateInfo commandBufferAllocateInfo; |
| 276 | commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 277 | commandBufferAllocateInfo.pNext = 0; |
| 278 | commandBufferAllocateInfo.commandPool = compute_command_pool; |
| 279 | commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 280 | commandBufferAllocateInfo.commandBufferCount = 1; |
| 281 | |
| 282 | VkResult ret = vkAllocateCommandBuffers(vkdev->vkdevice(), &commandBufferAllocateInfo, &compute_command_buffer); |
| 283 | if (ret != VK_SUCCESS) |
| 284 | { |
| 285 | NCNN_LOGE("vkAllocateCommandBuffers failed %d", ret); |
| 286 | return -1; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // compute_command_fence |
| 291 | { |
| 292 | VkFenceCreateInfo fenceCreateInfo; |
| 293 | fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 294 | fenceCreateInfo.pNext = 0; |
| 295 | fenceCreateInfo.flags = 0; |
| 296 | |
| 297 | VkResult ret = vkCreateFence(vkdev->vkdevice(), &fenceCreateInfo, 0, &compute_command_fence); |
| 298 | if (ret != VK_SUCCESS) |
| 299 | { |
| 300 | NCNN_LOGE("vkCreateFence failed %d", ret); |
| 301 | return -1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 306 | { |
| 307 | begin_command_buffer(); |
| 308 | |
| 309 | #if NCNN_BENCHMARK |
| 310 | if (query_pool) |
| 311 | vkCmdResetQueryPool(compute_command_buffer, query_pool, 0, query_count); |
| 312 | #endif // NCNN_BENCHMARK |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | int VkComputePrivate::begin_command_buffer() |
| 319 | { |
| 320 | VkCommandBufferBeginInfo commandBufferBeginInfo; |
| 321 | commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 322 | commandBufferBeginInfo.pNext = 0; |
| 323 | commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 324 | commandBufferBeginInfo.pInheritanceInfo = 0; |
| 325 | |
| 326 | VkResult ret = vkBeginCommandBuffer(compute_command_buffer, &commandBufferBeginInfo); |
| 327 | if (ret != VK_SUCCESS) |
| 328 | { |
| 329 | NCNN_LOGE("vkBeginCommandBuffer failed %d", ret); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int VkComputePrivate::end_command_buffer() |
| 337 | { |
| 338 | VkResult ret = vkEndCommandBuffer(compute_command_buffer); |
| 339 | if (ret != VK_SUCCESS) |
| 340 | { |
| 341 | NCNN_LOGE("vkEndCommandBuffer failed %d", ret); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | VkCompute::VkCompute(const VulkanDevice* _vkdev) |
| 349 | : vkdev(_vkdev), d(new VkComputePrivate(_vkdev)) |
| 350 | { |
| 351 | } |
| 352 | |
| 353 | VkCompute::~VkCompute() |
| 354 | { |
| 355 | delete d; |
| 356 | } |
| 357 | |
| 358 | void VkCompute::record_upload(const Mat& src, VkMat& dst, const Option& opt) |
| 359 | { |
| 360 | // NCNN_LOGE("record_upload buffer"); |
| 361 | |
| 362 | Mat src_fp16; |
| 363 | if (src.elemsize == src.elempack * 4u) |
| 364 | { |
| 365 | // cpu cast to fp16 (discrete gpu) |
| 366 | if (vkdev->info.type() == 0 && (opt.use_fp16_storage || (opt.use_fp16_packed && src.elempack % 4 == 0))) |
| 367 | { |
| 368 | ncnn::cast_float32_to_float16(src, src_fp16, opt); |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | src_fp16 = src; |
| 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | src_fp16 = src; |
| 378 | } |
| 379 | |
| 380 | // upload |
| 381 | VkMat dst_staging; |
| 382 | dst_staging.create_like(src_fp16, opt.staging_vkallocator); |
| 383 | if (dst_staging.empty()) |
| 384 | return; |
| 385 | |
| 386 | // stash staging |
| 387 | d->upload_staging_buffers.push_back(dst_staging); |
| 388 | |
| 389 | // NCNN_LOGE("upload_staging_buffer %p -> %p +%d ~%d", src_fp16.data, dst_staging.buffer(), dst_staging.buffer_offset(), dst_staging.buffer_capacity()); |
| 390 | |
| 391 | // memcpy src to device |
| 392 | memcpy(dst_staging.mapped_ptr(), src_fp16.data, src_fp16.total() * src_fp16.elemsize); |
| 393 | dst_staging.allocator->flush(dst_staging.data); |
| 394 | |
| 395 | // mark device host-write @ null |
| 396 | dst_staging.data->access_flags = VK_ACCESS_HOST_WRITE_BIT; |
| 397 | dst_staging.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; |
| 398 | |
| 399 | // resolve dst_elempack |
| 400 | int dims = src_fp16.dims; |
| 401 | int elemcount = 0; |
| 402 | if (dims == 1) elemcount = src_fp16.elempack * src_fp16.w; |
| 403 | if (dims == 2) elemcount = src_fp16.elempack * src_fp16.h; |
| 404 | if (dims == 3 || dims == 4) elemcount = src_fp16.elempack * src_fp16.c; |
| 405 | |
| 406 | int dst_elempack = 1; |
| 407 | if (opt.use_shader_pack8) |
| 408 | dst_elempack = elemcount % 8 == 0 ? 8 : elemcount % 4 == 0 ? 4 : 1; |
| 409 | else |
| 410 | dst_elempack = elemcount % 4 == 0 ? 4 : 1; |
| 411 | |
| 412 | // gpu cast to fp16 on the fly (integrated gpu) |
| 413 | vkdev->convert_packing(dst_staging, dst, dst_elempack, *this, opt); |
| 414 | } |
| 415 | |
| 416 | void VkCompute::record_upload(const Mat& src, VkImageMat& dst, const Option& opt) |
| 417 | { |
| 418 | // NCNN_LOGE("record_upload image"); |
| 419 | |
| 420 | Mat src_fp16; |
| 421 | if (src.elemsize == src.elempack * 4u) |
| 422 | { |
| 423 | // cpu cast to fp16 (discrete gpu) |
| 424 | if (vkdev->info.type() == 0 && (opt.use_fp16_storage || (opt.use_fp16_packed && src.elempack % 4 == 0))) |
| 425 | { |
| 426 | ncnn::cast_float32_to_float16(src, src_fp16, opt); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | src_fp16 = src; |
| 431 | } |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | src_fp16 = src; |
| 436 | } |
| 437 | |
| 438 | // upload |
| 439 | VkMat dst_staging; |
| 440 | dst_staging.create_like(src_fp16, opt.staging_vkallocator); |
| 441 | if (dst_staging.empty()) |
| 442 | return; |
| 443 | |
| 444 | // stash staging |
| 445 | d->upload_staging_buffers.push_back(dst_staging); |
| 446 | |
| 447 | // memcpy src to device |
| 448 | memcpy(dst_staging.mapped_ptr(), src_fp16.data, src_fp16.total() * src_fp16.elemsize); |
| 449 | dst_staging.allocator->flush(dst_staging.data); |
| 450 | |
| 451 | // mark device host-write @ null |
| 452 | dst_staging.data->access_flags = VK_ACCESS_HOST_WRITE_BIT; |
| 453 | dst_staging.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; |
| 454 | |
| 455 | // resolve dst_elempack |
| 456 | int dims = src_fp16.dims; |
| 457 | int elemcount = 0; |
| 458 | if (dims == 1) elemcount = src_fp16.elempack * src_fp16.w; |
| 459 | if (dims == 2) elemcount = src_fp16.elempack * src_fp16.h; |
| 460 | if (dims == 3 || dims == 4) elemcount = src_fp16.elempack * src_fp16.c; |
| 461 | |
| 462 | int dst_elempack = 1; |
| 463 | if (opt.use_shader_pack8) |
| 464 | dst_elempack = elemcount % 8 == 0 ? 8 : elemcount % 4 == 0 ? 4 : 1; |
| 465 | else |
| 466 | dst_elempack = elemcount % 4 == 0 ? 4 : 1; |
| 467 | |
| 468 | // gpu cast to fp16 on the fly (integrated gpu) |
| 469 | if (vkdev->info.bug_buffer_image_load_zero()) |
| 470 | { |
| 471 | // clone buffer to bridge image |
| 472 | VkImageMat dst_image; |
| 473 | record_clone(dst_staging, dst_image, opt); |
| 474 | if (dst_image.empty()) |
| 475 | return; |
| 476 | |
| 477 | vkdev->convert_packing(dst_image, dst, dst_elempack, *this, opt); |
| 478 | |
| 479 | // image and imageview can not be destroyed until command execution ends |
| 480 | NCNN_XADD(&dst_image.data->command_refcount, 1); |
| 481 | d->image_blocks_to_destroy.push_back(dst_image.data); |
| 482 | |
| 483 | submit_and_wait(); |
| 484 | reset(); |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | vkdev->convert_packing(dst_staging, dst, dst_elempack, *this, opt); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void VkCompute::record_download(const VkMat& src, Mat& dst, const Option& opt) |
| 493 | { |
| 494 | // NCNN_LOGE("record_download buffer"); |
| 495 | |
| 496 | // resolve dst_elempack |
| 497 | int dims = src.dims; |
| 498 | int elemcount = 0; |
| 499 | if (dims == 1) elemcount = src.elempack * src.w; |
| 500 | if (dims == 2) elemcount = src.elempack * src.h; |
| 501 | if (dims == 3 || dims == 4) elemcount = src.elempack * src.c; |
| 502 | |
| 503 | int dst_elempack = 1; |
| 504 | if (opt.use_packing_layout) |
| 505 | dst_elempack = elemcount % 4 == 0 ? 4 : 1; |
| 506 | else |
| 507 | dst_elempack = 1; |
| 508 | |
| 509 | // gpu cast to fp32 on the fly (integrated gpu) |
| 510 | Option opt_staging = opt; |
| 511 | if (vkdev->info.type() != 0) |
| 512 | { |
| 513 | opt_staging.use_fp16_packed = false; |
| 514 | opt_staging.use_fp16_storage = false; |
| 515 | } |
| 516 | if (!opt_staging.blob_vkallocator->mappable) |
| 517 | { |
| 518 | opt_staging.blob_vkallocator = opt.staging_vkallocator; |
| 519 | } |
| 520 | |
| 521 | VkMat dst_staging; |
| 522 | vkdev->convert_packing(src, dst_staging, dst_elempack, *this, opt_staging); |
| 523 | |
| 524 | // barrier device any @ compute to host-read @ compute |
| 525 | if (dst_staging.data->access_flags & VK_ACCESS_HOST_WRITE_BIT || dst_staging.data->stage_flags != VK_PIPELINE_STAGE_HOST_BIT) |
| 526 | { |
| 527 | VkBufferMemoryBarrier* barriers = new VkBufferMemoryBarrier[1]; |
| 528 | barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 529 | barriers[0].pNext = 0; |
| 530 | barriers[0].srcAccessMask = dst_staging.data->access_flags; |
| 531 | barriers[0].dstAccessMask = VK_ACCESS_HOST_READ_BIT; |
| 532 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 533 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 534 | barriers[0].buffer = dst_staging.buffer(); |
| 535 | barriers[0].offset = dst_staging.buffer_offset(); |
| 536 | barriers[0].size = dst_staging.buffer_capacity(); |
| 537 | |
| 538 | VkPipelineStageFlags src_stage = dst_staging.data->stage_flags; |
| 539 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_HOST_BIT; |
| 540 | |
| 541 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 542 | { |
| 543 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, barriers, 0, 0); |
| 544 | delete[] barriers; |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | VkComputePrivate::record r; |
| 549 | r.type = VkComputePrivate::record::TYPE_buffer_barrers; |
| 550 | r.command_buffer = d->compute_command_buffer; |
| 551 | r.buffer_barrers.src_stage = src_stage; |
| 552 | r.buffer_barrers.dst_stage = dst_stage; |
| 553 | r.buffer_barrers.barrier_count = 1; |
| 554 | r.buffer_barrers.barriers = barriers; |
| 555 | d->delayed_records.push_back(r); |
| 556 | } |
| 557 | |
| 558 | // mark device host-read @ any |
| 559 | dst_staging.data->access_flags = VK_ACCESS_HOST_READ_BIT; |
| 560 | dst_staging.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; |
| 561 | } |
| 562 | |
| 563 | // create dst |
| 564 | Mat dst_fp16; |
| 565 | dst_fp16.create_like(dst_staging, opt.blob_allocator); |
| 566 | if (dst_fp16.empty()) |
| 567 | return; |
| 568 | |
| 569 | // download |
| 570 | d->download_post_buffers.push_back(dst_staging); |
| 571 | d->download_post_mats_fp16.push_back(dst_fp16); |
| 572 | |
| 573 | // post memcpy device to dst |
| 574 | { |
| 575 | VkComputePrivate::record r; |
| 576 | r.type = VkComputePrivate::record::TYPE_post_download; |
| 577 | r.command_buffer = 0; |
| 578 | r.post_download.download_post_buffer_mat_offset = d->download_post_buffers.size() - 1; |
| 579 | r.post_download.download_post_mat_fp16_offset = d->download_post_mats_fp16.size() - 1; |
| 580 | d->delayed_records.push_back(r); |
| 581 | } |
| 582 | |
| 583 | // cast to fp32 (discrete gpu) |
| 584 | if (dst_fp16.elemsize == dst_fp16.elempack * 2u) |
| 585 | { |
| 586 | if (vkdev->info.type() == 0 && (opt.use_fp16_storage || (opt.use_fp16_packed && dst_fp16.elempack % 4 == 0))) |
| 587 | { |
| 588 | int dims = dst_fp16.dims; |
| 589 | if (dims == 1) |
| 590 | dst.create(dst_fp16.w, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 591 | if (dims == 2) |
| 592 | dst.create(dst_fp16.w, dst_fp16.h, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 593 | if (dims == 3) |
| 594 | dst.create(dst_fp16.w, dst_fp16.h, dst_fp16.c, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 595 | if (dims == 4) |
| 596 | dst.create(dst_fp16.w, dst_fp16.h, dst_fp16.d, dst_fp16.c, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 597 | |
| 598 | d->download_post_mats.push_back(dst); |
| 599 | |
| 600 | VkComputePrivate::record r; |
| 601 | r.type = VkComputePrivate::record::TYPE_post_cast_float16_to_float32; |
| 602 | r.command_buffer = 0; |
| 603 | r.post_cast_float16_to_float32.download_post_mat_fp16_offset = d->download_post_mats_fp16.size() - 1; |
| 604 | r.post_cast_float16_to_float32.download_post_mat_offset = d->download_post_mats.size() - 1; |
| 605 | r.post_cast_float16_to_float32.num_threads = opt.num_threads; |
| 606 | d->delayed_records.push_back(r); |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | dst = dst_fp16; |
| 611 | } |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | dst = dst_fp16; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void VkCompute::record_download(const VkImageMat& src, Mat& dst, const Option& opt) |
| 620 | { |
| 621 | // NCNN_LOGE("record_download image"); |
| 622 | |
| 623 | // resolve dst_elempack |
| 624 | int dims = src.dims; |
| 625 | int elemcount = 0; |
| 626 | if (dims == 1) elemcount = src.elempack * src.w; |
| 627 | if (dims == 2) elemcount = src.elempack * src.h; |
| 628 | if (dims == 3 || dims == 4) elemcount = src.elempack * src.c; |
| 629 | |
| 630 | int dst_elempack = 1; |
| 631 | if (opt.use_packing_layout) |
| 632 | dst_elempack = elemcount % 4 == 0 ? 4 : 1; |
| 633 | else |
| 634 | dst_elempack = 1; |
| 635 | |
| 636 | // gpu cast to fp32 on the fly (integrated gpu) |
| 637 | Option opt_staging = opt; |
| 638 | if (vkdev->info.type() != 0) |
| 639 | { |
| 640 | opt_staging.use_fp16_packed = false; |
| 641 | opt_staging.use_fp16_storage = false; |
| 642 | } |
| 643 | if (!opt_staging.blob_vkallocator->mappable) |
| 644 | { |
| 645 | opt_staging.blob_vkallocator = opt.staging_vkallocator; |
| 646 | } |
| 647 | |
| 648 | VkMat dst_staging; |
| 649 | if (vkdev->info.bug_buffer_image_load_zero()) |
| 650 | { |
| 651 | VkImageMat src_image; |
| 652 | vkdev->convert_packing(src, src_image, dst_elempack, *this, opt); |
| 653 | if (src_image.empty()) |
| 654 | return; |
| 655 | |
| 656 | record_clone(src_image, dst_staging, opt_staging); |
| 657 | |
| 658 | // image and imageview can not be destroyed until command execution ends |
| 659 | NCNN_XADD(&src_image.data->command_refcount, 1); |
| 660 | d->image_blocks_to_destroy.push_back(src_image.data); |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | vkdev->convert_packing(src, dst_staging, dst_elempack, *this, opt_staging); |
| 665 | } |
| 666 | |
| 667 | // image and imageview can not be destroyed until command execution ends |
| 668 | NCNN_XADD(&src.data->command_refcount, 1); |
| 669 | d->image_blocks_to_destroy.push_back(src.data); |
| 670 | |
| 671 | // barrier device any @ compute to host-read @ compute |
| 672 | if (dst_staging.data->access_flags & VK_ACCESS_HOST_WRITE_BIT || dst_staging.data->stage_flags != VK_PIPELINE_STAGE_HOST_BIT) |
| 673 | { |
| 674 | VkBufferMemoryBarrier* barriers = new VkBufferMemoryBarrier[1]; |
| 675 | barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 676 | barriers[0].pNext = 0; |
| 677 | barriers[0].srcAccessMask = dst_staging.data->access_flags; |
| 678 | barriers[0].dstAccessMask = VK_ACCESS_HOST_READ_BIT; |
| 679 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 680 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 681 | barriers[0].buffer = dst_staging.buffer(); |
| 682 | barriers[0].offset = dst_staging.buffer_offset(); |
| 683 | barriers[0].size = dst_staging.buffer_capacity(); |
| 684 | |
| 685 | VkPipelineStageFlags src_stage = dst_staging.data->stage_flags; |
| 686 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_HOST_BIT; |
| 687 | |
| 688 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 689 | { |
| 690 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, barriers, 0, 0); |
| 691 | delete[] barriers; |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | VkComputePrivate::record r; |
| 696 | r.type = VkComputePrivate::record::TYPE_buffer_barrers; |
| 697 | r.command_buffer = d->compute_command_buffer; |
| 698 | r.buffer_barrers.src_stage = src_stage; |
| 699 | r.buffer_barrers.dst_stage = dst_stage; |
| 700 | r.buffer_barrers.barrier_count = 1; |
| 701 | r.buffer_barrers.barriers = barriers; |
| 702 | d->delayed_records.push_back(r); |
| 703 | } |
| 704 | |
| 705 | // mark device host-read @ any |
| 706 | dst_staging.data->access_flags = VK_ACCESS_HOST_READ_BIT; |
| 707 | dst_staging.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; |
| 708 | } |
| 709 | |
| 710 | // create dst |
| 711 | Mat dst_fp16; |
| 712 | dst_fp16.create_like(dst_staging, opt.blob_allocator); |
| 713 | if (dst_fp16.empty()) |
| 714 | return; |
| 715 | |
| 716 | // download |
| 717 | d->download_post_buffers.push_back(dst_staging); |
| 718 | d->download_post_mats_fp16.push_back(dst_fp16); |
| 719 | |
| 720 | // post memcpy device to dst |
| 721 | { |
| 722 | VkComputePrivate::record r; |
| 723 | r.type = VkComputePrivate::record::TYPE_post_download; |
| 724 | r.command_buffer = 0; |
| 725 | r.post_download.download_post_buffer_mat_offset = d->download_post_buffers.size() - 1; |
| 726 | r.post_download.download_post_mat_fp16_offset = d->download_post_mats_fp16.size() - 1; |
| 727 | d->delayed_records.push_back(r); |
| 728 | } |
| 729 | |
| 730 | // cast to fp32 (discrete gpu) |
| 731 | if (dst_fp16.elemsize == dst_fp16.elempack * 2u) |
| 732 | { |
| 733 | if (vkdev->info.type() == 0 && (opt.use_fp16_storage || (opt.use_fp16_packed && dst_fp16.elempack % 4 == 0))) |
| 734 | { |
| 735 | int dims = dst_fp16.dims; |
| 736 | if (dims == 1) |
| 737 | dst.create(dst_fp16.w, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 738 | if (dims == 2) |
| 739 | dst.create(dst_fp16.w, dst_fp16.h, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 740 | if (dims == 3) |
| 741 | dst.create(dst_fp16.w, dst_fp16.h, dst_fp16.c, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 742 | if (dims == 4) |
| 743 | dst.create(dst_fp16.w, dst_fp16.h, dst_fp16.d, dst_fp16.c, (size_t)(dst_fp16.elempack * 4u), dst_fp16.elempack, opt.blob_allocator); |
| 744 | |
| 745 | d->download_post_mats.push_back(dst); |
| 746 | |
| 747 | VkComputePrivate::record r; |
| 748 | r.type = VkComputePrivate::record::TYPE_post_cast_float16_to_float32; |
| 749 | r.command_buffer = 0; |
| 750 | r.post_cast_float16_to_float32.download_post_mat_fp16_offset = d->download_post_mats_fp16.size() - 1; |
| 751 | r.post_cast_float16_to_float32.download_post_mat_offset = d->download_post_mats.size() - 1; |
| 752 | r.post_cast_float16_to_float32.num_threads = opt.num_threads; |
| 753 | d->delayed_records.push_back(r); |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | dst = dst_fp16; |
| 758 | } |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | dst = dst_fp16; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void VkCompute::record_buffer_to_image(const VkMat& src, VkImageMat& dst, const Option& opt) |
| 767 | { |
| 768 | // NCNN_LOGE("record_buffer_to_image"); |
| 769 | |
| 770 | // resolve dst_elempack |
| 771 | int dims = src.dims; |
| 772 | int elemcount = 0; |
| 773 | if (dims == 1) elemcount = src.elempack * src.w; |
| 774 | if (dims == 2) elemcount = src.elempack * src.h; |
| 775 | if (dims == 3 || dims == 4) elemcount = src.elempack * src.c; |
| 776 | |
| 777 | int dst_elempack = 1; |
| 778 | if (opt.use_shader_pack8) |
| 779 | dst_elempack = elemcount % 8 == 0 ? 8 : elemcount % 4 == 0 ? 4 : 1; |
| 780 | else |
| 781 | dst_elempack = elemcount % 4 == 0 ? 4 : 1; |
| 782 | |
| 783 | if (vkdev->info.bug_buffer_image_load_zero()) |
| 784 | { |
| 785 | // clone buffer to bridge image |
| 786 | VkImageMat src_image; |
| 787 | record_clone(src, src_image, opt); |
| 788 | if (src_image.empty()) |
| 789 | return; |
| 790 | |
| 791 | vkdev->convert_packing(src_image, dst, dst_elempack, *this, opt); |
| 792 | |
| 793 | // image and imageview can not be destroyed until command execution ends |
| 794 | NCNN_XADD(&src_image.data->command_refcount, 1); |
| 795 | d->image_blocks_to_destroy.push_back(src_image.data); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | vkdev->convert_packing(src, dst, dst_elempack, *this, opt); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | void VkCompute::record_image_to_buffer(const VkImageMat& src, VkMat& dst, const Option& opt) |
| 804 | { |
| 805 | // NCNN_LOGE("record_image_to_buffer"); |
| 806 | |
| 807 | // resolve dst_elempack |
| 808 | int dims = src.dims; |
| 809 | int elemcount = 0; |
| 810 | if (dims == 1) elemcount = src.elempack * src.w; |
| 811 | if (dims == 2) elemcount = src.elempack * src.h; |
| 812 | if (dims == 3 || dims == 4) elemcount = src.elempack * src.c; |
| 813 | |
| 814 | int dst_elempack = 1; |
| 815 | if (opt.use_shader_pack8) |
| 816 | dst_elempack = elemcount % 8 == 0 ? 8 : elemcount % 4 == 0 ? 4 : 1; |
| 817 | else |
| 818 | dst_elempack = elemcount % 4 == 0 ? 4 : 1; |
| 819 | |
| 820 | if (vkdev->info.bug_buffer_image_load_zero()) |
| 821 | { |
| 822 | VkImageMat src_image; |
| 823 | Option opt_image = opt; |
| 824 | opt_image.blob_vkallocator = src.allocator; |
| 825 | vkdev->convert_packing(src, src_image, dst_elempack, *this, opt_image); |
| 826 | if (src_image.empty()) |
| 827 | return; |
| 828 | |
| 829 | record_clone(src_image, dst, opt); |
| 830 | |
| 831 | // image and imageview can not be destroyed until command execution ends |
| 832 | NCNN_XADD(&src_image.data->command_refcount, 1); |
| 833 | d->image_blocks_to_destroy.push_back(src_image.data); |
| 834 | } |
| 835 | else |
| 836 | { |
| 837 | vkdev->convert_packing(src, dst, dst_elempack, *this, opt); |
| 838 | } |
| 839 | |
| 840 | // image and imageview can not be destroyed until command execution ends |
| 841 | NCNN_XADD(&src.data->command_refcount, 1); |
| 842 | d->image_blocks_to_destroy.push_back(src.data); |
| 843 | } |
| 844 | |
| 845 | void VkCompute::record_clone(const Mat& src, VkMat& dst, const Option& opt) |
| 846 | { |
| 847 | // NCNN_LOGE("record_clone host to buffer"); |
| 848 | |
| 849 | // host to staging |
| 850 | VkMat dst_staging; |
| 851 | dst_staging.create_like(src, opt.staging_vkallocator); |
| 852 | if (dst_staging.empty()) |
| 853 | return; |
| 854 | |
| 855 | // memcpy src to device |
| 856 | memcpy(dst_staging.mapped_ptr(), src.data, src.total() * src.elemsize); |
| 857 | dst_staging.allocator->flush(dst_staging.data); |
| 858 | |
| 859 | // mark device host-write @ null |
| 860 | dst_staging.data->access_flags = VK_ACCESS_HOST_WRITE_BIT; |
| 861 | dst_staging.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; |
| 862 | |
| 863 | // staging to device |
| 864 | record_clone(dst_staging, dst, opt); |
| 865 | |
| 866 | // stash staging |
| 867 | d->upload_staging_buffers.push_back(dst_staging); |
| 868 | } |
| 869 | |
| 870 | void VkCompute::record_clone(const Mat& src, VkImageMat& dst, const Option& opt) |
| 871 | { |
| 872 | // NCNN_LOGE("record_clone host to image"); |
| 873 | |
| 874 | // host to staging |
| 875 | VkMat dst_staging; |
| 876 | Option opt_staging = opt; |
| 877 | opt_staging.blob_vkallocator = opt.staging_vkallocator; |
| 878 | record_clone(src, dst_staging, opt_staging); |
| 879 | |
| 880 | // staging to image |
| 881 | record_clone(dst_staging, dst, opt); |
| 882 | |
| 883 | // stash staging |
| 884 | d->upload_staging_buffers.push_back(dst_staging); |
| 885 | } |
| 886 | |
| 887 | void VkCompute::record_clone(const VkMat& src, Mat& dst, const Option& opt) |
| 888 | { |
| 889 | // NCNN_LOGE("record_clone buffer to host"); |
| 890 | |
| 891 | if (!src.allocator->mappable) |
| 892 | { |
| 893 | // device to staging |
| 894 | VkMat src_staging; |
| 895 | Option opt_staging = opt; |
| 896 | opt_staging.blob_vkallocator = opt.staging_vkallocator; |
| 897 | record_clone(src, src_staging, opt_staging); |
| 898 | |
| 899 | // staging to host |
| 900 | record_clone(src_staging, dst, opt); |
| 901 | |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | // create dst |
| 906 | dst.create_like(src, opt.blob_allocator); |
| 907 | if (dst.empty()) |
| 908 | return; |
| 909 | |
| 910 | // barrier device any @ compute to host-read @ compute |
| 911 | if (src.data->access_flags & VK_ACCESS_HOST_WRITE_BIT || src.data->stage_flags != VK_PIPELINE_STAGE_HOST_BIT) |
| 912 | { |
| 913 | VkBufferMemoryBarrier* barriers = new VkBufferMemoryBarrier[1]; |
| 914 | barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 915 | barriers[0].pNext = 0; |
| 916 | barriers[0].srcAccessMask = src.data->access_flags; |
| 917 | barriers[0].dstAccessMask = VK_ACCESS_HOST_READ_BIT; |
| 918 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 919 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 920 | barriers[0].buffer = src.buffer(); |
| 921 | barriers[0].offset = src.buffer_offset(); |
| 922 | barriers[0].size = src.buffer_capacity(); |
| 923 | |
| 924 | VkPipelineStageFlags src_stage = src.data->stage_flags; |
| 925 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_HOST_BIT; |
| 926 | |
| 927 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 928 | { |
| 929 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, barriers, 0, 0); |
| 930 | delete[] barriers; |
| 931 | } |
| 932 | else |
| 933 | { |
| 934 | VkComputePrivate::record r; |
| 935 | r.type = VkComputePrivate::record::TYPE_buffer_barrers; |
| 936 | r.command_buffer = d->compute_command_buffer; |
| 937 | r.buffer_barrers.src_stage = src_stage; |
| 938 | r.buffer_barrers.dst_stage = dst_stage; |
| 939 | r.buffer_barrers.barrier_count = 1; |
| 940 | r.buffer_barrers.barriers = barriers; |
| 941 | d->delayed_records.push_back(r); |
| 942 | } |
| 943 | |
| 944 | // mark device host-read @ any |
| 945 | src.data->access_flags = VK_ACCESS_HOST_READ_BIT; |
| 946 | src.data->stage_flags = VK_PIPELINE_STAGE_HOST_BIT; |
| 947 | } |
| 948 | |
| 949 | // stash download post buffer and mat |
| 950 | d->download_post_buffers.push_back(src); |
| 951 | d->download_post_mats_fp16.push_back(dst); |
| 952 | |
| 953 | // post memcpy device to dst |
| 954 | { |
| 955 | VkComputePrivate::record r; |
| 956 | r.type = VkComputePrivate::record::TYPE_post_download; |
| 957 | r.command_buffer = 0; |
| 958 | r.post_download.download_post_buffer_mat_offset = d->download_post_buffers.size() - 1; |
| 959 | r.post_download.download_post_mat_fp16_offset = d->download_post_mats_fp16.size() - 1; |
| 960 | d->delayed_records.push_back(r); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | void VkCompute::record_clone(const VkImageMat& src, Mat& dst, const Option& opt) |
| 965 | { |
| 966 | // NCNN_LOGE("record_clone image to host"); |
| 967 | |
| 968 | // image to staging |
| 969 | VkMat src_staging; |
| 970 | Option opt_staging = opt; |
| 971 | opt_staging.blob_vkallocator = opt.staging_vkallocator; |
| 972 | record_clone(src, src_staging, opt_staging); |
| 973 | |
| 974 | // staging to host |
| 975 | record_clone(src_staging, dst, opt); |
| 976 | } |
| 977 | |
| 978 | void VkCompute::record_clone(const VkMat& src, VkMat& dst, const Option& opt) |
| 979 | { |
| 980 | // NCNN_LOGE("record_clone buffer to buffer"); |
| 981 | |
| 982 | // create dst |
| 983 | dst.create_like(src, opt.blob_vkallocator); |
| 984 | if (dst.empty()) |
| 985 | return; |
| 986 | |
| 987 | if (src.data->access_flags & VK_ACCESS_TRANSFER_WRITE_BIT || src.data->stage_flags != VK_PIPELINE_STAGE_TRANSFER_BIT) |
| 988 | { |
| 989 | // barrier device any @ compute to transfer-read @ compute |
| 990 | VkBufferMemoryBarrier* barriers = new VkBufferMemoryBarrier[1]; |
| 991 | barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 992 | barriers[0].pNext = 0; |
| 993 | barriers[0].srcAccessMask = src.data->access_flags; |
| 994 | barriers[0].dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 995 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 996 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 997 | barriers[0].buffer = src.buffer(); |
| 998 | barriers[0].offset = src.buffer_offset(); |
| 999 | barriers[0].size = src.buffer_capacity(); |
| 1000 | |
| 1001 | VkPipelineStageFlags src_stage = src.data->stage_flags; |
| 1002 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1003 | |
| 1004 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1005 | { |
| 1006 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, barriers, 0, 0); |
| 1007 | delete[] barriers; |
| 1008 | } |
| 1009 | else |
| 1010 | { |
| 1011 | VkComputePrivate::record r; |
| 1012 | r.type = VkComputePrivate::record::TYPE_buffer_barrers; |
| 1013 | r.command_buffer = d->compute_command_buffer; |
| 1014 | r.buffer_barrers.src_stage = src_stage; |
| 1015 | r.buffer_barrers.dst_stage = dst_stage; |
| 1016 | r.buffer_barrers.barrier_count = 1; |
| 1017 | r.buffer_barrers.barriers = barriers; |
| 1018 | d->delayed_records.push_back(r); |
| 1019 | } |
| 1020 | |
| 1021 | // mark device transfer-read @ transfer |
| 1022 | src.data->access_flags = VK_ACCESS_TRANSFER_READ_BIT; |
| 1023 | src.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1024 | } |
| 1025 | |
| 1026 | { |
| 1027 | // barrier device any @ null to transfer-write @ compute |
| 1028 | |
| 1029 | // mark device transfer-write @ transfer |
| 1030 | dst.data->access_flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1031 | dst.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1032 | } |
| 1033 | |
| 1034 | // record device to staging |
| 1035 | { |
| 1036 | VkBufferCopy* regions = new VkBufferCopy[1]; |
| 1037 | regions[0].srcOffset = src.buffer_offset(); |
| 1038 | regions[0].dstOffset = dst.buffer_offset(); |
| 1039 | regions[0].size = std::min(src.buffer_capacity(), dst.buffer_capacity()); |
| 1040 | |
| 1041 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1042 | { |
| 1043 | vkCmdCopyBuffer(d->compute_command_buffer, src.buffer(), dst.buffer(), 1, regions); |
| 1044 | delete[] regions; |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | VkComputePrivate::record r; |
| 1049 | r.type = VkComputePrivate::record::TYPE_copy_buffer; |
| 1050 | r.command_buffer = d->compute_command_buffer; |
| 1051 | r.copy_buffer.src = src.buffer(); |
| 1052 | r.copy_buffer.dst = dst.buffer(); |
| 1053 | r.copy_buffer.region_count = 1; |
| 1054 | r.copy_buffer.regions = regions; |
| 1055 | d->delayed_records.push_back(r); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | void VkCompute::record_clone(const VkImageMat& src, VkImageMat& dst, const Option& opt) |
| 1061 | { |
| 1062 | // NCNN_LOGE("record_clone image to image"); |
| 1063 | |
| 1064 | // create dst |
| 1065 | dst.create_like(src, opt.blob_vkallocator); |
| 1066 | if (dst.empty()) |
| 1067 | return; |
| 1068 | |
| 1069 | // image layout transform any @ any to transfer-src-optimal @ compute |
| 1070 | if (src.data->access_flags & VK_ACCESS_TRANSFER_WRITE_BIT || src.data->image_layout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL || src.data->stage_flags != VK_PIPELINE_STAGE_TRANSFER_BIT) |
| 1071 | { |
| 1072 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 1073 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1074 | barriers[0].pNext = 0; |
| 1075 | barriers[0].srcAccessMask = src.data->access_flags; |
| 1076 | barriers[0].dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1077 | barriers[0].oldLayout = src.data->image_layout; |
| 1078 | barriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 1079 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1080 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1081 | barriers[0].image = src.image(); |
| 1082 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1083 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 1084 | barriers[0].subresourceRange.levelCount = 1; |
| 1085 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 1086 | barriers[0].subresourceRange.layerCount = 1; |
| 1087 | |
| 1088 | VkPipelineStageFlags src_stage = src.data->stage_flags; |
| 1089 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1090 | |
| 1091 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1092 | { |
| 1093 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 1094 | delete[] barriers; |
| 1095 | } |
| 1096 | else |
| 1097 | { |
| 1098 | VkComputePrivate::record r; |
| 1099 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 1100 | r.command_buffer = d->compute_command_buffer; |
| 1101 | r.image_barrers.src_stage = src_stage; |
| 1102 | r.image_barrers.dst_stage = dst_stage; |
| 1103 | r.image_barrers.barrier_count = 1; |
| 1104 | r.image_barrers.barriers = barriers; |
| 1105 | d->delayed_records.push_back(r); |
| 1106 | } |
| 1107 | |
| 1108 | // mark image transfer-src-optimal @ compute |
| 1109 | src.data->access_flags = VK_ACCESS_TRANSFER_READ_BIT; |
| 1110 | src.data->image_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 1111 | src.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1112 | } |
| 1113 | |
| 1114 | // image layout transform undefined @ null to transfer-dst-optimal @ compute |
| 1115 | { |
| 1116 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 1117 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1118 | barriers[0].pNext = 0; |
| 1119 | barriers[0].srcAccessMask = 0; |
| 1120 | barriers[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1121 | barriers[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1122 | barriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1123 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1124 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1125 | barriers[0].image = dst.image(); |
| 1126 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1127 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 1128 | barriers[0].subresourceRange.levelCount = 1; |
| 1129 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 1130 | barriers[0].subresourceRange.layerCount = 1; |
| 1131 | |
| 1132 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1133 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1134 | |
| 1135 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1136 | { |
| 1137 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 1138 | delete[] barriers; |
| 1139 | } |
| 1140 | else |
| 1141 | { |
| 1142 | VkComputePrivate::record r; |
| 1143 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 1144 | r.command_buffer = d->compute_command_buffer; |
| 1145 | r.image_barrers.src_stage = src_stage; |
| 1146 | r.image_barrers.dst_stage = dst_stage; |
| 1147 | r.image_barrers.barrier_count = 1; |
| 1148 | r.image_barrers.barriers = barriers; |
| 1149 | d->delayed_records.push_back(r); |
| 1150 | } |
| 1151 | |
| 1152 | // mark image transfer-dst-optimal @ compute |
| 1153 | dst.data->access_flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1154 | dst.data->image_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1155 | dst.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1156 | } |
| 1157 | |
| 1158 | // record device to staging |
| 1159 | { |
| 1160 | VkImageCopy* regions = new VkImageCopy[1]; |
| 1161 | regions[0].srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1162 | regions[0].srcSubresource.mipLevel = 0; |
| 1163 | regions[0].srcSubresource.baseArrayLayer = 0; |
| 1164 | regions[0].srcSubresource.layerCount = 1; |
| 1165 | regions[0].srcOffset.x = 0; |
| 1166 | regions[0].srcOffset.y = 0; |
| 1167 | regions[0].srcOffset.z = 0; |
| 1168 | regions[0].dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1169 | regions[0].dstSubresource.mipLevel = 0; |
| 1170 | regions[0].dstSubresource.baseArrayLayer = 0; |
| 1171 | regions[0].dstSubresource.layerCount = 1; |
| 1172 | regions[0].dstOffset.x = 0; |
| 1173 | regions[0].dstOffset.y = 0; |
| 1174 | regions[0].dstOffset.z = 0; |
| 1175 | regions[0].extent.width = src.data->width; |
| 1176 | regions[0].extent.height = src.data->height; |
| 1177 | regions[0].extent.depth = src.data->depth; |
| 1178 | |
| 1179 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1180 | { |
| 1181 | vkCmdCopyImage(d->compute_command_buffer, src.image(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst.image(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, regions); |
| 1182 | delete[] regions; |
| 1183 | } |
| 1184 | else |
| 1185 | { |
| 1186 | VkComputePrivate::record r; |
| 1187 | r.type = VkComputePrivate::record::TYPE_copy_image; |
| 1188 | r.command_buffer = d->compute_command_buffer; |
| 1189 | r.copy_image.src = src.image(); |
| 1190 | r.copy_image.src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 1191 | r.copy_image.dst = dst.image(); |
| 1192 | r.copy_image.dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1193 | r.copy_image.region_count = 1; |
| 1194 | r.copy_image.regions = regions; |
| 1195 | d->delayed_records.push_back(r); |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | // image and imageview can not be destroyed until command execution ends |
| 1200 | NCNN_XADD(&src.data->command_refcount, 1); |
| 1201 | NCNN_XADD(&dst.data->command_refcount, 1); |
| 1202 | d->image_blocks_to_destroy.push_back(src.data); |
| 1203 | d->image_blocks_to_destroy.push_back(dst.data); |
| 1204 | } |
| 1205 | |
| 1206 | void VkCompute::record_clone(const VkMat& src, VkImageMat& dst, const Option& opt) |
| 1207 | { |
| 1208 | // NCNN_LOGE("record_clone buffer to image"); |
| 1209 | |
| 1210 | // create dst |
| 1211 | dst.create_like(src, opt.blob_vkallocator); |
| 1212 | if (dst.empty()) |
| 1213 | return; |
| 1214 | |
| 1215 | // barrier device any @ any to transfer-read @ compute |
| 1216 | if (src.data->access_flags & VK_ACCESS_SHADER_WRITE_BIT || src.data->stage_flags != VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT) |
| 1217 | { |
| 1218 | VkBufferMemoryBarrier* barriers = new VkBufferMemoryBarrier[1]; |
| 1219 | barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 1220 | barriers[0].pNext = 0; |
| 1221 | barriers[0].srcAccessMask = src.data->access_flags; |
| 1222 | barriers[0].dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1223 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1224 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1225 | barriers[0].buffer = src.buffer(); |
| 1226 | barriers[0].offset = src.buffer_offset(); |
| 1227 | barriers[0].size = src.buffer_capacity(); |
| 1228 | |
| 1229 | VkPipelineStageFlags src_stage = src.data->stage_flags; |
| 1230 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1231 | |
| 1232 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1233 | { |
| 1234 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, barriers, 0, 0); |
| 1235 | delete[] barriers; |
| 1236 | } |
| 1237 | else |
| 1238 | { |
| 1239 | VkComputePrivate::record r; |
| 1240 | r.type = VkComputePrivate::record::TYPE_buffer_barrers; |
| 1241 | r.command_buffer = d->compute_command_buffer; |
| 1242 | r.buffer_barrers.src_stage = src_stage; |
| 1243 | r.buffer_barrers.dst_stage = dst_stage; |
| 1244 | r.buffer_barrers.barrier_count = 1; |
| 1245 | r.buffer_barrers.barriers = barriers; |
| 1246 | d->delayed_records.push_back(r); |
| 1247 | } |
| 1248 | |
| 1249 | // mark device transfer-read @ compute |
| 1250 | src.data->access_flags = VK_ACCESS_TRANSFER_READ_BIT; |
| 1251 | src.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1252 | } |
| 1253 | |
| 1254 | // image layout transform undefined @ null to transfer-dst-optimal @ compute |
| 1255 | { |
| 1256 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 1257 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1258 | barriers[0].pNext = 0; |
| 1259 | barriers[0].srcAccessMask = 0; |
| 1260 | barriers[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1261 | barriers[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1262 | barriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1263 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1264 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1265 | barriers[0].image = dst.image(); |
| 1266 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1267 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 1268 | barriers[0].subresourceRange.levelCount = 1; |
| 1269 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 1270 | barriers[0].subresourceRange.layerCount = 1; |
| 1271 | |
| 1272 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1273 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1274 | |
| 1275 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1276 | { |
| 1277 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 1278 | delete[] barriers; |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | VkComputePrivate::record r; |
| 1283 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 1284 | r.command_buffer = d->compute_command_buffer; |
| 1285 | r.image_barrers.src_stage = src_stage; |
| 1286 | r.image_barrers.dst_stage = dst_stage; |
| 1287 | r.image_barrers.barrier_count = 1; |
| 1288 | r.image_barrers.barriers = barriers; |
| 1289 | d->delayed_records.push_back(r); |
| 1290 | } |
| 1291 | |
| 1292 | // mark image transfer-dst-optimal @ compute |
| 1293 | dst.data->access_flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1294 | dst.data->image_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1295 | dst.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1296 | } |
| 1297 | |
| 1298 | // record device to image |
| 1299 | { |
| 1300 | int region_count; |
| 1301 | VkBufferImageCopy* regions; |
| 1302 | if (dst.elemsize * dst.w * dst.h % 16 == 0) |
| 1303 | { |
| 1304 | region_count = 1; |
| 1305 | regions = new VkBufferImageCopy[1]; |
| 1306 | regions[0].bufferOffset = src.buffer_offset(); |
| 1307 | regions[0].bufferRowLength = 0; |
| 1308 | regions[0].bufferImageHeight = 0; |
| 1309 | regions[0].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1310 | regions[0].imageSubresource.mipLevel = 0; |
| 1311 | regions[0].imageSubresource.baseArrayLayer = 0; |
| 1312 | regions[0].imageSubresource.layerCount = 1; |
| 1313 | regions[0].imageOffset.x = 0; |
| 1314 | regions[0].imageOffset.y = 0; |
| 1315 | regions[0].imageOffset.z = 0; |
| 1316 | regions[0].imageExtent.width = dst.data->width; |
| 1317 | regions[0].imageExtent.height = dst.data->height; |
| 1318 | regions[0].imageExtent.depth = dst.data->depth; |
| 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | region_count = dst.c; |
| 1323 | regions = new VkBufferImageCopy[region_count]; |
| 1324 | for (int i = 0; i < region_count; i++) |
| 1325 | { |
| 1326 | regions[i].bufferOffset = src.buffer_offset() + src.cstep * src.elemsize * i; |
| 1327 | regions[i].bufferRowLength = 0; |
| 1328 | regions[i].bufferImageHeight = 0; |
| 1329 | regions[i].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1330 | regions[i].imageSubresource.mipLevel = 0; |
| 1331 | regions[i].imageSubresource.baseArrayLayer = 0; |
| 1332 | regions[i].imageSubresource.layerCount = 1; |
| 1333 | regions[i].imageOffset.x = 0; |
| 1334 | regions[i].imageOffset.y = 0; |
| 1335 | regions[i].imageOffset.z = i; |
| 1336 | regions[i].imageExtent.width = dst.data->width; |
| 1337 | regions[i].imageExtent.height = dst.data->height; |
| 1338 | regions[i].imageExtent.depth = 1; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1343 | { |
| 1344 | vkCmdCopyBufferToImage(d->compute_command_buffer, src.buffer(), dst.image(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, region_count, regions); |
| 1345 | delete[] regions; |
| 1346 | } |
| 1347 | else |
| 1348 | { |
| 1349 | VkComputePrivate::record r; |
| 1350 | r.type = VkComputePrivate::record::TYPE_copy_buffer_to_image; |
| 1351 | r.command_buffer = d->compute_command_buffer; |
| 1352 | r.copy_buffer_to_image.src = src.buffer(); |
| 1353 | r.copy_buffer_to_image.dst = dst.image(); |
| 1354 | r.copy_buffer_to_image.layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1355 | r.copy_buffer_to_image.region_count = region_count; |
| 1356 | r.copy_buffer_to_image.regions = regions; |
| 1357 | d->delayed_records.push_back(r); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | // image and imageview can not be destroyed until command execution ends |
| 1362 | NCNN_XADD(&dst.data->command_refcount, 1); |
| 1363 | d->image_blocks_to_destroy.push_back(dst.data); |
| 1364 | } |
| 1365 | |
| 1366 | void VkCompute::record_clone(const VkImageMat& src, VkMat& dst, const Option& opt) |
| 1367 | { |
| 1368 | // NCNN_LOGE("record_clone image to buffer"); |
| 1369 | |
| 1370 | // create dst |
| 1371 | dst.create_like(src, opt.blob_vkallocator); |
| 1372 | if (dst.empty()) |
| 1373 | return; |
| 1374 | |
| 1375 | // image layout transform any @ any to transfer-src-optimal @ compute |
| 1376 | if (src.data->access_flags & VK_ACCESS_TRANSFER_WRITE_BIT || src.data->image_layout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL || src.data->stage_flags != VK_PIPELINE_STAGE_TRANSFER_BIT) |
| 1377 | { |
| 1378 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 1379 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1380 | barriers[0].pNext = 0; |
| 1381 | barriers[0].srcAccessMask = src.data->access_flags; |
| 1382 | barriers[0].dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1383 | barriers[0].oldLayout = src.data->image_layout; |
| 1384 | barriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 1385 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1386 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1387 | barriers[0].image = src.image(); |
| 1388 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1389 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 1390 | barriers[0].subresourceRange.levelCount = 1; |
| 1391 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 1392 | barriers[0].subresourceRange.layerCount = 1; |
| 1393 | |
| 1394 | VkPipelineStageFlags src_stage = src.data->stage_flags; |
| 1395 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1396 | |
| 1397 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1398 | { |
| 1399 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 1400 | delete[] barriers; |
| 1401 | } |
| 1402 | else |
| 1403 | { |
| 1404 | VkComputePrivate::record r; |
| 1405 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 1406 | r.command_buffer = d->compute_command_buffer; |
| 1407 | r.image_barrers.src_stage = src_stage; |
| 1408 | r.image_barrers.dst_stage = dst_stage; |
| 1409 | r.image_barrers.barrier_count = 1; |
| 1410 | r.image_barrers.barriers = barriers; |
| 1411 | d->delayed_records.push_back(r); |
| 1412 | } |
| 1413 | |
| 1414 | // mark image transfer-src-optimal @ compute |
| 1415 | src.data->access_flags = VK_ACCESS_TRANSFER_READ_BIT; |
| 1416 | src.data->image_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 1417 | src.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1418 | } |
| 1419 | |
| 1420 | { |
| 1421 | // barrier device any @ null to transfer-write @ compute |
| 1422 | |
| 1423 | // mark device transfer-write @ transfer |
| 1424 | dst.data->access_flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1425 | dst.data->stage_flags = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1426 | } |
| 1427 | |
| 1428 | // record image to device |
| 1429 | { |
| 1430 | int region_count; |
| 1431 | VkBufferImageCopy* regions; |
| 1432 | if (src.elemsize * src.w * src.h % 16 == 0) |
| 1433 | { |
| 1434 | region_count = 1; |
| 1435 | regions = new VkBufferImageCopy[1]; |
| 1436 | regions[0].bufferOffset = dst.buffer_offset(); |
| 1437 | regions[0].bufferRowLength = 0; |
| 1438 | regions[0].bufferImageHeight = 0; |
| 1439 | regions[0].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1440 | regions[0].imageSubresource.mipLevel = 0; |
| 1441 | regions[0].imageSubresource.baseArrayLayer = 0; |
| 1442 | regions[0].imageSubresource.layerCount = 1; |
| 1443 | regions[0].imageOffset.x = 0; |
| 1444 | regions[0].imageOffset.y = 0; |
| 1445 | regions[0].imageOffset.z = 0; |
| 1446 | regions[0].imageExtent.width = src.data->width; |
| 1447 | regions[0].imageExtent.height = src.data->height; |
| 1448 | regions[0].imageExtent.depth = src.data->depth; |
| 1449 | } |
| 1450 | else |
| 1451 | { |
| 1452 | region_count = src.c; |
| 1453 | regions = new VkBufferImageCopy[region_count]; |
| 1454 | for (int i = 0; i < region_count; i++) |
| 1455 | { |
| 1456 | regions[i].bufferOffset = dst.buffer_offset() + dst.cstep * dst.elemsize * i; |
| 1457 | regions[i].bufferRowLength = 0; |
| 1458 | regions[i].bufferImageHeight = 0; |
| 1459 | regions[i].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1460 | regions[i].imageSubresource.mipLevel = 0; |
| 1461 | regions[i].imageSubresource.baseArrayLayer = 0; |
| 1462 | regions[i].imageSubresource.layerCount = 1; |
| 1463 | regions[i].imageOffset.x = 0; |
| 1464 | regions[i].imageOffset.y = 0; |
| 1465 | regions[i].imageOffset.z = i; |
| 1466 | regions[i].imageExtent.width = src.data->width; |
| 1467 | regions[i].imageExtent.height = src.data->height; |
| 1468 | regions[i].imageExtent.depth = 1; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1473 | { |
| 1474 | vkCmdCopyImageToBuffer(d->compute_command_buffer, src.image(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst.buffer(), region_count, regions); |
| 1475 | delete[] regions; |
| 1476 | } |
| 1477 | else |
| 1478 | { |
| 1479 | VkComputePrivate::record r; |
| 1480 | r.type = VkComputePrivate::record::TYPE_copy_image_to_buffer; |
| 1481 | r.command_buffer = d->compute_command_buffer; |
| 1482 | r.copy_image_to_buffer.src = src.image(); |
| 1483 | r.copy_image_to_buffer.layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 1484 | r.copy_image_to_buffer.dst = dst.buffer(); |
| 1485 | r.copy_image_to_buffer.region_count = region_count; |
| 1486 | r.copy_image_to_buffer.regions = regions; |
| 1487 | d->delayed_records.push_back(r); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // image and imageview can not be destroyed until command execution ends |
| 1492 | NCNN_XADD(&src.data->command_refcount, 1); |
| 1493 | d->image_blocks_to_destroy.push_back(src.data); |
| 1494 | } |
| 1495 | |
| 1496 | void VkCompute::record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& bindings, const std::vector<vk_constant_type>& constants, const VkMat& dispatcher) |
| 1497 | { |
| 1498 | record_pipeline(pipeline, bindings, std::vector<VkImageMat>(), constants, dispatcher); |
| 1499 | } |
| 1500 | |
| 1501 | void VkCompute::record_pipeline(const Pipeline* pipeline, const std::vector<VkImageMat>& bindings, const std::vector<vk_constant_type>& constants, const VkImageMat& dispatcher) |
| 1502 | { |
| 1503 | record_pipeline(pipeline, std::vector<VkMat>(), bindings, constants, dispatcher); |
| 1504 | } |
| 1505 | |
| 1506 | void VkCompute::record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& buffer_bindings, const std::vector<VkImageMat>& image_bindings, const std::vector<vk_constant_type>& constants, const VkMat& dispatcher) |
| 1507 | { |
| 1508 | Mat dispatcher_mat(dispatcher.w, dispatcher.h, dispatcher.d, dispatcher.c, (void*)0); |
| 1509 | |
| 1510 | record_pipeline(pipeline, buffer_bindings, image_bindings, constants, dispatcher_mat); |
| 1511 | } |
| 1512 | |
| 1513 | void VkCompute::record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& buffer_bindings, const std::vector<VkImageMat>& image_bindings, const std::vector<vk_constant_type>& constants, const VkImageMat& dispatcher) |
| 1514 | { |
| 1515 | Mat dispatcher_mat(dispatcher.w, dispatcher.h, dispatcher.d, dispatcher.c, (void*)0); |
| 1516 | |
| 1517 | record_pipeline(pipeline, buffer_bindings, image_bindings, constants, dispatcher_mat); |
| 1518 | } |
| 1519 | |
| 1520 | void VkCompute::record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& buffer_bindings, const std::vector<VkImageMat>& image_bindings, const std::vector<vk_constant_type>& constants, const Mat& dispatcher) |
| 1521 | { |
| 1522 | // NCNN_LOGE("record_pipeline %p", pipeline); |
| 1523 | |
| 1524 | const int buffer_binding_count = (int)buffer_bindings.size(); |
| 1525 | const int image_binding_count = (int)image_bindings.size(); |
| 1526 | const int constant_count = (int)constants.size(); |
| 1527 | |
| 1528 | const int binding_count = buffer_binding_count + image_binding_count; |
| 1529 | const ShaderInfo& shader_info = pipeline->shader_info(); |
| 1530 | |
| 1531 | if (binding_count != shader_info.binding_count) |
| 1532 | { |
| 1533 | NCNN_LOGE("binding_count not match, expect %d but got %d + %d", shader_info.binding_count, buffer_binding_count, image_binding_count); |
| 1534 | } |
| 1535 | |
| 1536 | if (constant_count != shader_info.push_constant_count) |
| 1537 | { |
| 1538 | NCNN_LOGE("push_constant_count not match, expect %d but got %d", shader_info.push_constant_count, constant_count); |
| 1539 | } |
| 1540 | |
| 1541 | int buffer_index = 0; |
| 1542 | int image_index = 0; |
| 1543 | for (int i = 0; i < binding_count; i++) |
| 1544 | { |
| 1545 | int binding_type = shader_info.binding_types[i]; |
| 1546 | |
| 1547 | if (binding_type == 1) |
| 1548 | { |
| 1549 | const VkMat& binding = buffer_bindings[buffer_index].empty() ? vkdev->get_dummy_buffer() : buffer_bindings[buffer_index]; |
| 1550 | buffer_index++; |
| 1551 | |
| 1552 | // NCNN_LOGE("binding #%d buffer = %d %d %d %d @ %lu %d = %p +%ld ~%ld", i, binding.dims, binding.w, binding.h, binding.c, binding.elemsize, binding.elempack, binding.buffer(), binding.buffer_offset(), binding.buffer_capacity()); |
| 1553 | |
| 1554 | barrier_readwrite(binding); |
| 1555 | } |
| 1556 | else if (binding_type == 2) |
| 1557 | { |
| 1558 | const VkImageMat& binding = image_bindings[image_index].empty() ? vkdev->get_dummy_image() : image_bindings[image_index]; |
| 1559 | image_index++; |
| 1560 | |
| 1561 | // NCNN_LOGE("binding #%d image = %d %d %d %d @ %lu %d = %p +%ld ~%ld %p", i, binding.dims, binding.w, binding.h, binding.c, binding.elemsize, binding.elempack, binding.image(), binding.data->bind_offset, binding.data->bind_capacity, binding.imageview()); |
| 1562 | |
| 1563 | barrier_readwrite(binding); |
| 1564 | |
| 1565 | // image and imageview can not be destroyed until command execution ends |
| 1566 | NCNN_XADD(&binding.data->command_refcount, 1); |
| 1567 | d->image_blocks_to_destroy.push_back(binding.data); |
| 1568 | } |
| 1569 | else // if (binding_type == 3) |
| 1570 | { |
| 1571 | const VkImageMat& binding = image_bindings[image_index].empty() ? vkdev->get_dummy_image_readonly() : image_bindings[image_index]; |
| 1572 | image_index++; |
| 1573 | |
| 1574 | // NCNN_LOGE("binding #%d sampler = %d %d %d %d @ %lu %d = %p +%ld ~%ld %p", i, binding.dims, binding.w, binding.h, binding.c, binding.elemsize, binding.elempack, binding.image(), binding.data->bind_offset, binding.data->bind_capacity, binding.imageview()); |
| 1575 | |
| 1576 | // if the same image used for both storage image and combined image sampler |
| 1577 | // only apply image layout transition to general |
| 1578 | bool image_read_write = false; |
| 1579 | for (int j = 0; j < image_binding_count; j++) |
| 1580 | { |
| 1581 | if (shader_info.binding_types[j] == 2 && binding.data == image_bindings[j].data) |
| 1582 | { |
| 1583 | // the same image is used as storage image, skip it |
| 1584 | image_read_write = true; |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | if (image_read_write) |
| 1589 | continue; |
| 1590 | |
| 1591 | barrier_readonly(binding); |
| 1592 | |
| 1593 | // image and imageview can not be destroyed until command execution ends |
| 1594 | NCNN_XADD(&binding.data->command_refcount, 1); |
| 1595 | d->image_blocks_to_destroy.push_back(binding.data); |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | // record bind pipeline |
| 1600 | { |
| 1601 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1602 | { |
| 1603 | vkCmdBindPipeline(d->compute_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->pipeline()); |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | VkComputePrivate::record r; |
| 1608 | r.type = VkComputePrivate::record::TYPE_bind_pipeline; |
| 1609 | r.command_buffer = d->compute_command_buffer; |
| 1610 | r.bind_pipeline.bind_point = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 1611 | r.bind_pipeline.pipeline = pipeline->pipeline(); |
| 1612 | d->delayed_records.push_back(r); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | // record update bindings |
| 1617 | if (binding_count > 0) |
| 1618 | { |
| 1619 | std::vector<unsigned char> descriptorInfos; |
| 1620 | { |
| 1621 | descriptorInfos.resize(sizeof(VkDescriptorBufferInfo) * buffer_binding_count + sizeof(VkDescriptorImageInfo) * image_binding_count); |
| 1622 | |
| 1623 | unsigned char* p_descriptorInfos = descriptorInfos.data(); |
| 1624 | int descriptorBufferInfo_index = 0; |
| 1625 | int descriptorImageInfo_index = 0; |
| 1626 | for (int i = 0; i < binding_count; i++) |
| 1627 | { |
| 1628 | int binding_type = shader_info.binding_types[i]; |
| 1629 | |
| 1630 | if (binding_type == 1) |
| 1631 | { |
| 1632 | const VkMat& binding = buffer_bindings[descriptorBufferInfo_index].empty() ? vkdev->get_dummy_buffer() : buffer_bindings[descriptorBufferInfo_index]; |
| 1633 | descriptorBufferInfo_index++; |
| 1634 | |
| 1635 | VkDescriptorBufferInfo descriptorBufferInfo; |
| 1636 | descriptorBufferInfo.buffer = binding.buffer(); |
| 1637 | descriptorBufferInfo.offset = binding.buffer_offset(); |
| 1638 | descriptorBufferInfo.range = binding.total() * binding.elemsize; |
| 1639 | |
| 1640 | memcpy(p_descriptorInfos, &descriptorBufferInfo, sizeof(VkDescriptorBufferInfo)); |
| 1641 | p_descriptorInfos += sizeof(VkDescriptorBufferInfo); |
| 1642 | } |
| 1643 | else //if (binding_type == 2 || binding_type == 3) |
| 1644 | { |
| 1645 | const VkImageMat& binding = image_bindings[descriptorImageInfo_index].empty() ? vkdev->get_dummy_image() : image_bindings[descriptorImageInfo_index]; |
| 1646 | descriptorImageInfo_index++; |
| 1647 | |
| 1648 | // we always use immutable nearest sampler set in descroptor layout during pipeline creation |
| 1649 | VkDescriptorImageInfo descriptorImageInfo; |
| 1650 | descriptorImageInfo.sampler = 0; |
| 1651 | descriptorImageInfo.imageView = binding.imageview(); |
| 1652 | descriptorImageInfo.imageLayout = binding.data->image_layout; |
| 1653 | |
| 1654 | memcpy(p_descriptorInfos, &descriptorImageInfo, sizeof(VkDescriptorImageInfo)); |
| 1655 | p_descriptorInfos += sizeof(VkDescriptorImageInfo); |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1661 | { |
| 1662 | vkdev->vkCmdPushDescriptorSetWithTemplateKHR(d->compute_command_buffer, pipeline->descriptor_update_template(), pipeline->pipeline_layout(), 0, descriptorInfos.data()); |
| 1663 | } |
| 1664 | else |
| 1665 | { |
| 1666 | // create new descriptor_pool and descriptorset |
| 1667 | VkDescriptorPool descriptor_pool; |
| 1668 | { |
| 1669 | int image_binding_count = 0; |
| 1670 | int sampler_binding_count = 0; |
| 1671 | for (int i = 0; i < binding_count; i++) |
| 1672 | { |
| 1673 | int binding_type = shader_info.binding_types[i]; |
| 1674 | |
| 1675 | if (binding_type == 2) |
| 1676 | image_binding_count++; |
| 1677 | else // if (binding_type == 3) |
| 1678 | sampler_binding_count++; |
| 1679 | } |
| 1680 | |
| 1681 | VkDescriptorPoolSize poolSizes[3]; |
| 1682 | poolSizes[0].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1683 | poolSizes[0].descriptorCount = buffer_binding_count; |
| 1684 | poolSizes[1].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 1685 | poolSizes[1].descriptorCount = image_binding_count; |
| 1686 | poolSizes[2].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1687 | poolSizes[2].descriptorCount = sampler_binding_count; |
| 1688 | |
| 1689 | VkDescriptorPoolCreateInfo descriptorPoolCreateInfo; |
| 1690 | descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 1691 | descriptorPoolCreateInfo.pNext = 0; |
| 1692 | descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; |
| 1693 | descriptorPoolCreateInfo.maxSets = 1; |
| 1694 | descriptorPoolCreateInfo.poolSizeCount = 3; |
| 1695 | descriptorPoolCreateInfo.pPoolSizes = poolSizes; |
| 1696 | |
| 1697 | VkResult ret = vkCreateDescriptorPool(vkdev->vkdevice(), &descriptorPoolCreateInfo, 0, &descriptor_pool); |
| 1698 | if (ret != VK_SUCCESS) |
| 1699 | { |
| 1700 | NCNN_LOGE("vkCreateDescriptorPool failed %d", ret); |
| 1701 | return; |
| 1702 | } |
| 1703 | } |
| 1704 | d->descriptor_pools.push_back(descriptor_pool); |
| 1705 | |
| 1706 | VkDescriptorSet descriptorset; |
| 1707 | { |
| 1708 | VkDescriptorSetLayout descriptorset_layout = pipeline->descriptorset_layout(); |
| 1709 | |
| 1710 | VkDescriptorSetAllocateInfo descriptorSetAllocateInfo; |
| 1711 | descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 1712 | descriptorSetAllocateInfo.pNext = 0; |
| 1713 | descriptorSetAllocateInfo.descriptorPool = descriptor_pool; |
| 1714 | descriptorSetAllocateInfo.descriptorSetCount = 1; |
| 1715 | descriptorSetAllocateInfo.pSetLayouts = &descriptorset_layout; |
| 1716 | |
| 1717 | VkResult ret = vkAllocateDescriptorSets(vkdev->vkdevice(), &descriptorSetAllocateInfo, &descriptorset); |
| 1718 | if (ret != VK_SUCCESS) |
| 1719 | { |
| 1720 | NCNN_LOGE("vkAllocateDescriptorSets failed %d", ret); |
| 1721 | return; |
| 1722 | } |
| 1723 | } |
| 1724 | d->descriptorsets.push_back(descriptorset); |
| 1725 | |
| 1726 | if (vkdev->info.support_VK_KHR_descriptor_update_template()) |
| 1727 | { |
| 1728 | vkdev->vkUpdateDescriptorSetWithTemplateKHR(vkdev->vkdevice(), descriptorset, pipeline->descriptor_update_template(), descriptorInfos.data()); |
| 1729 | } |
| 1730 | else |
| 1731 | { |
| 1732 | std::vector<VkWriteDescriptorSet> writeDescriptorSets(binding_count); |
| 1733 | { |
| 1734 | const unsigned char* p_descriptorInfos = descriptorInfos.data(); |
| 1735 | for (int i = 0; i < binding_count; i++) |
| 1736 | { |
| 1737 | int binding_type = shader_info.binding_types[i]; |
| 1738 | |
| 1739 | writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1740 | writeDescriptorSets[i].pNext = 0; |
| 1741 | writeDescriptorSets[i].dstSet = descriptorset; |
| 1742 | writeDescriptorSets[i].dstBinding = i; |
| 1743 | writeDescriptorSets[i].dstArrayElement = 0; |
| 1744 | writeDescriptorSets[i].descriptorCount = 1; |
| 1745 | writeDescriptorSets[i].pTexelBufferView = 0; |
| 1746 | |
| 1747 | if (binding_type == 1) |
| 1748 | { |
| 1749 | writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1750 | writeDescriptorSets[i].pImageInfo = 0; |
| 1751 | writeDescriptorSets[i].pBufferInfo = (const VkDescriptorBufferInfo*)p_descriptorInfos; |
| 1752 | |
| 1753 | p_descriptorInfos += sizeof(VkDescriptorBufferInfo); |
| 1754 | } |
| 1755 | else if (binding_type == 2) |
| 1756 | { |
| 1757 | writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 1758 | writeDescriptorSets[i].pImageInfo = (const VkDescriptorImageInfo*)p_descriptorInfos; |
| 1759 | writeDescriptorSets[i].pBufferInfo = 0; |
| 1760 | |
| 1761 | p_descriptorInfos += sizeof(VkDescriptorImageInfo); |
| 1762 | } |
| 1763 | else // if (binding_type == 3) |
| 1764 | { |
| 1765 | writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1766 | writeDescriptorSets[i].pImageInfo = (const VkDescriptorImageInfo*)p_descriptorInfos; |
| 1767 | writeDescriptorSets[i].pBufferInfo = 0; |
| 1768 | |
| 1769 | p_descriptorInfos += sizeof(VkDescriptorImageInfo); |
| 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | vkUpdateDescriptorSets(vkdev->vkdevice(), binding_count, writeDescriptorSets.data(), 0, 0); |
| 1775 | } |
| 1776 | |
| 1777 | VkComputePrivate::record r; |
| 1778 | r.type = VkComputePrivate::record::TYPE_bind_descriptorsets; |
| 1779 | r.command_buffer = d->compute_command_buffer; |
| 1780 | r.bind_descriptorsets.bind_point = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 1781 | r.bind_descriptorsets.pipeline_layout = pipeline->pipeline_layout(); |
| 1782 | r.bind_descriptorsets.descriptorset_count = 1; |
| 1783 | r.bind_descriptorsets.descriptorset_offset = d->descriptorsets.size() - 1; |
| 1784 | d->delayed_records.push_back(r); |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | // record push constants |
| 1789 | if (constant_count > 0) |
| 1790 | { |
| 1791 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1792 | { |
| 1793 | vkCmdPushConstants(d->compute_command_buffer, pipeline->pipeline_layout(), VK_SHADER_STAGE_COMPUTE_BIT, 0, constant_count * sizeof(vk_constant_type), constants.data()); |
| 1794 | } |
| 1795 | else |
| 1796 | { |
| 1797 | uint32_t size = constant_count * sizeof(vk_constant_type); |
| 1798 | unsigned char* constant_values = new unsigned char[size]; |
| 1799 | memcpy(constant_values, constants.data(), size); |
| 1800 | |
| 1801 | VkComputePrivate::record r; |
| 1802 | r.type = VkComputePrivate::record::TYPE_push_constants; |
| 1803 | r.command_buffer = d->compute_command_buffer; |
| 1804 | r.push_constants.pipeline_layout = pipeline->pipeline_layout(); |
| 1805 | r.push_constants.stage_flags = VK_SHADER_STAGE_COMPUTE_BIT; |
| 1806 | r.push_constants.size = size; |
| 1807 | r.push_constants.values = constant_values; |
| 1808 | d->delayed_records.push_back(r); |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | // record dispatch |
| 1813 | { |
| 1814 | uint32_t group_count_x = (dispatcher.w + pipeline->local_size_x() - 1) / pipeline->local_size_x(); |
| 1815 | uint32_t group_count_y = (dispatcher.h * (dispatcher.d ? dispatcher.d : 1) + pipeline->local_size_y() - 1) / pipeline->local_size_y(); |
| 1816 | uint32_t group_count_z = (dispatcher.c + pipeline->local_size_z() - 1) / pipeline->local_size_z(); |
| 1817 | |
| 1818 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1819 | { |
| 1820 | vkCmdDispatch(d->compute_command_buffer, group_count_x, group_count_y, group_count_z); |
| 1821 | } |
| 1822 | else |
| 1823 | { |
| 1824 | VkComputePrivate::record r; |
| 1825 | r.type = VkComputePrivate::record::TYPE_dispatch; |
| 1826 | r.command_buffer = d->compute_command_buffer; |
| 1827 | r.dispatch.group_count_x = group_count_x; |
| 1828 | r.dispatch.group_count_y = group_count_y; |
| 1829 | r.dispatch.group_count_z = group_count_z; |
| 1830 | d->delayed_records.push_back(r); |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | #if NCNN_BENCHMARK |
| 1836 | void VkCompute::record_write_timestamp(uint32_t query) |
| 1837 | { |
| 1838 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1839 | { |
| 1840 | if (d->query_pool) |
| 1841 | vkCmdWriteTimestamp(d->compute_command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, d->query_pool, query); |
| 1842 | } |
| 1843 | else |
| 1844 | { |
| 1845 | VkComputePrivate::record r; |
| 1846 | r.type = VkComputePrivate::record::TYPE_write_timestamp; |
| 1847 | r.command_buffer = d->compute_command_buffer; |
| 1848 | r.write_timestamp.query = query; |
| 1849 | d->delayed_records.push_back(r); |
| 1850 | } |
| 1851 | } |
| 1852 | #endif // NCNN_BENCHMARK |
| 1853 | |
| 1854 | #if NCNN_PLATFORM_API |
| 1855 | #if __ANDROID_API__ >= 26 |
| 1856 | void VkCompute::record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkMat& dst) |
| 1857 | { |
| 1858 | // image layout transform undefined @ null to general @ compute |
| 1859 | { |
| 1860 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 1861 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1862 | barriers[0].pNext = 0; |
| 1863 | barriers[0].srcAccessMask = 0; |
| 1864 | barriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 1865 | barriers[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1866 | barriers[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 1867 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1868 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1869 | barriers[0].image = src.image(); |
| 1870 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1871 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 1872 | barriers[0].subresourceRange.levelCount = 1; |
| 1873 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 1874 | barriers[0].subresourceRange.layerCount = 1; |
| 1875 | |
| 1876 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1877 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 1878 | |
| 1879 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1880 | { |
| 1881 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 1882 | delete[] barriers; |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | VkComputePrivate::record r; |
| 1887 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 1888 | r.command_buffer = d->compute_command_buffer; |
| 1889 | r.image_barrers.src_stage = src_stage; |
| 1890 | r.image_barrers.dst_stage = dst_stage; |
| 1891 | r.image_barrers.barrier_count = 1; |
| 1892 | r.image_barrers.barriers = barriers; |
| 1893 | d->delayed_records.push_back(r); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | // record bind pipeline |
| 1898 | { |
| 1899 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1900 | { |
| 1901 | vkCmdBindPipeline(d->compute_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->pipeline()); |
| 1902 | } |
| 1903 | else |
| 1904 | { |
| 1905 | VkComputePrivate::record r; |
| 1906 | r.type = VkComputePrivate::record::TYPE_bind_pipeline; |
| 1907 | r.command_buffer = d->compute_command_buffer; |
| 1908 | r.bind_pipeline.bind_point = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 1909 | r.bind_pipeline.pipeline = pipeline->pipeline(); |
| 1910 | d->delayed_records.push_back(r); |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | // record update bindings |
| 1915 | { |
| 1916 | VkDescriptorImageInfo descriptorImageInfo; |
| 1917 | descriptorImageInfo.sampler = pipeline->sampler; |
| 1918 | descriptorImageInfo.imageView = src.imageview(); |
| 1919 | descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 1920 | |
| 1921 | VkDescriptorBufferInfo descriptorBufferInfo; |
| 1922 | descriptorBufferInfo.buffer = dst.buffer(); |
| 1923 | descriptorBufferInfo.offset = dst.buffer_offset(); |
| 1924 | descriptorBufferInfo.range = dst.total() * dst.elemsize; |
| 1925 | |
| 1926 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 1927 | { |
| 1928 | struct ImportAndroidHardwareBufferDescriptorInfo |
| 1929 | { |
| 1930 | VkDescriptorImageInfo imageInfo; |
| 1931 | VkDescriptorBufferInfo bufferInfo; |
| 1932 | VkDescriptorBufferInfo buffer4Info; |
| 1933 | }; |
| 1934 | |
| 1935 | ImportAndroidHardwareBufferDescriptorInfo info; |
| 1936 | info.imageInfo = descriptorImageInfo; |
| 1937 | info.bufferInfo = descriptorBufferInfo; |
| 1938 | info.buffer4Info = descriptorBufferInfo; |
| 1939 | |
| 1940 | vkdev->vkCmdPushDescriptorSetWithTemplateKHR(d->compute_command_buffer, pipeline->descriptor_update_template(), pipeline->pipeline_layout(), 0, &info); |
| 1941 | } |
| 1942 | else |
| 1943 | { |
| 1944 | // create new descriptor_pool and descriptorset |
| 1945 | VkDescriptorPool descriptor_pool; |
| 1946 | { |
| 1947 | VkDescriptorPoolSize poolSizes[2]; |
| 1948 | poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1949 | poolSizes[0].descriptorCount = 1; |
| 1950 | poolSizes[1].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1951 | poolSizes[1].descriptorCount = 2; |
| 1952 | |
| 1953 | VkDescriptorPoolCreateInfo descriptorPoolCreateInfo; |
| 1954 | descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 1955 | descriptorPoolCreateInfo.pNext = 0; |
| 1956 | descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; |
| 1957 | descriptorPoolCreateInfo.maxSets = 1; |
| 1958 | descriptorPoolCreateInfo.poolSizeCount = 2; |
| 1959 | descriptorPoolCreateInfo.pPoolSizes = poolSizes; |
| 1960 | |
| 1961 | VkResult ret = vkCreateDescriptorPool(vkdev->vkdevice(), &descriptorPoolCreateInfo, 0, &descriptor_pool); |
| 1962 | if (ret != VK_SUCCESS) |
| 1963 | { |
| 1964 | NCNN_LOGE("vkCreateDescriptorPool failed %d", ret); |
| 1965 | return; |
| 1966 | } |
| 1967 | } |
| 1968 | d->descriptor_pools.push_back(descriptor_pool); |
| 1969 | |
| 1970 | VkDescriptorSet descriptorset; |
| 1971 | { |
| 1972 | VkDescriptorSetLayout descriptorset_layout = pipeline->descriptorset_layout(); |
| 1973 | |
| 1974 | VkDescriptorSetAllocateInfo descriptorSetAllocateInfo; |
| 1975 | descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 1976 | descriptorSetAllocateInfo.pNext = 0; |
| 1977 | descriptorSetAllocateInfo.descriptorPool = descriptor_pool; |
| 1978 | descriptorSetAllocateInfo.descriptorSetCount = 1; |
| 1979 | descriptorSetAllocateInfo.pSetLayouts = &descriptorset_layout; |
| 1980 | |
| 1981 | VkResult ret = vkAllocateDescriptorSets(vkdev->vkdevice(), &descriptorSetAllocateInfo, &descriptorset); |
| 1982 | if (ret != VK_SUCCESS) |
| 1983 | { |
| 1984 | NCNN_LOGE("vkAllocateDescriptorSets failed %d", ret); |
| 1985 | return; |
| 1986 | } |
| 1987 | } |
| 1988 | d->descriptorsets.push_back(descriptorset); |
| 1989 | |
| 1990 | if (vkdev->info.support_VK_KHR_descriptor_update_template()) |
| 1991 | { |
| 1992 | struct ImportAndroidHardwareBufferDescriptorInfo |
| 1993 | { |
| 1994 | VkDescriptorImageInfo imageInfo; |
| 1995 | VkDescriptorBufferInfo bufferInfo; |
| 1996 | VkDescriptorBufferInfo buffer4Info; |
| 1997 | }; |
| 1998 | |
| 1999 | ImportAndroidHardwareBufferDescriptorInfo info; |
| 2000 | info.imageInfo = descriptorImageInfo; |
| 2001 | info.bufferInfo = descriptorBufferInfo; |
| 2002 | info.buffer4Info = descriptorBufferInfo; |
| 2003 | |
| 2004 | vkdev->vkUpdateDescriptorSetWithTemplateKHR(vkdev->vkdevice(), descriptorset, pipeline->descriptor_update_template(), &info); |
| 2005 | } |
| 2006 | else |
| 2007 | { |
| 2008 | VkWriteDescriptorSet writeDescriptorSets[3]; |
| 2009 | writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 2010 | writeDescriptorSets[0].pNext = 0; |
| 2011 | writeDescriptorSets[0].dstSet = descriptorset; |
| 2012 | writeDescriptorSets[0].dstBinding = 0; |
| 2013 | writeDescriptorSets[0].dstArrayElement = 0; |
| 2014 | writeDescriptorSets[0].descriptorCount = 1; |
| 2015 | writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 2016 | writeDescriptorSets[0].pImageInfo = &descriptorImageInfo; |
| 2017 | writeDescriptorSets[0].pBufferInfo = 0; |
| 2018 | writeDescriptorSets[0].pTexelBufferView = 0; |
| 2019 | writeDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 2020 | writeDescriptorSets[1].pNext = 0; |
| 2021 | writeDescriptorSets[1].dstSet = descriptorset; |
| 2022 | writeDescriptorSets[1].dstBinding = 1; |
| 2023 | writeDescriptorSets[1].dstArrayElement = 0; |
| 2024 | writeDescriptorSets[1].descriptorCount = 1; |
| 2025 | writeDescriptorSets[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2026 | writeDescriptorSets[1].pImageInfo = 0; |
| 2027 | writeDescriptorSets[1].pBufferInfo = &descriptorBufferInfo; |
| 2028 | writeDescriptorSets[1].pTexelBufferView = 0; |
| 2029 | writeDescriptorSets[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 2030 | writeDescriptorSets[2].pNext = 0; |
| 2031 | writeDescriptorSets[2].dstSet = descriptorset; |
| 2032 | writeDescriptorSets[2].dstBinding = 2; |
| 2033 | writeDescriptorSets[2].dstArrayElement = 0; |
| 2034 | writeDescriptorSets[2].descriptorCount = 1; |
| 2035 | writeDescriptorSets[2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2036 | writeDescriptorSets[2].pImageInfo = 0; |
| 2037 | writeDescriptorSets[2].pBufferInfo = &descriptorBufferInfo; |
| 2038 | writeDescriptorSets[2].pTexelBufferView = 0; |
| 2039 | |
| 2040 | vkUpdateDescriptorSets(vkdev->vkdevice(), 3, writeDescriptorSets, 0, 0); |
| 2041 | } |
| 2042 | |
| 2043 | VkComputePrivate::record r; |
| 2044 | r.type = VkComputePrivate::record::TYPE_bind_descriptorsets; |
| 2045 | r.command_buffer = d->compute_command_buffer; |
| 2046 | r.bind_descriptorsets.bind_point = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 2047 | r.bind_descriptorsets.pipeline_layout = pipeline->pipeline_layout(); |
| 2048 | r.bind_descriptorsets.descriptorset_count = 1; |
| 2049 | r.bind_descriptorsets.descriptorset_offset = d->descriptorsets.size() - 1; |
| 2050 | d->delayed_records.push_back(r); |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | // record dispatch |
| 2055 | { |
| 2056 | uint32_t group_count_x = (dst.w + pipeline->local_size_x() - 1) / pipeline->local_size_x(); |
| 2057 | uint32_t group_count_y = (dst.h + pipeline->local_size_y() - 1) / pipeline->local_size_y(); |
| 2058 | uint32_t group_count_z = (dst.c + pipeline->local_size_z() - 1) / pipeline->local_size_z(); |
| 2059 | |
| 2060 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2061 | { |
| 2062 | vkCmdDispatch(d->compute_command_buffer, group_count_x, group_count_y, group_count_z); |
| 2063 | } |
| 2064 | else |
| 2065 | { |
| 2066 | VkComputePrivate::record r; |
| 2067 | r.type = VkComputePrivate::record::TYPE_dispatch; |
| 2068 | r.command_buffer = d->compute_command_buffer; |
| 2069 | r.dispatch.group_count_x = group_count_x; |
| 2070 | r.dispatch.group_count_y = group_count_y; |
| 2071 | r.dispatch.group_count_z = group_count_z; |
| 2072 | d->delayed_records.push_back(r); |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | void VkCompute::record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkImageMat& dst) |
| 2078 | { |
| 2079 | // image layout transform undefined @ null to general @ compute |
| 2080 | { |
| 2081 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[2]; |
| 2082 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 2083 | barriers[0].pNext = 0; |
| 2084 | barriers[0].srcAccessMask = 0; |
| 2085 | barriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 2086 | barriers[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 2087 | barriers[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 2088 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2089 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2090 | barriers[0].image = src.image(); |
| 2091 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2092 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 2093 | barriers[0].subresourceRange.levelCount = 1; |
| 2094 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 2095 | barriers[0].subresourceRange.layerCount = 1; |
| 2096 | barriers[1].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 2097 | barriers[1].pNext = 0; |
| 2098 | barriers[1].srcAccessMask = 0; |
| 2099 | barriers[1].dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 2100 | barriers[1].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 2101 | barriers[1].newLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 2102 | barriers[1].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2103 | barriers[1].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2104 | barriers[1].image = dst.image(); |
| 2105 | barriers[1].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2106 | barriers[1].subresourceRange.baseMipLevel = 0; |
| 2107 | barriers[1].subresourceRange.levelCount = 1; |
| 2108 | barriers[1].subresourceRange.baseArrayLayer = 0; |
| 2109 | barriers[1].subresourceRange.layerCount = 1; |
| 2110 | |
| 2111 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 2112 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2113 | |
| 2114 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2115 | { |
| 2116 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 2, barriers); |
| 2117 | delete[] barriers; |
| 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | VkComputePrivate::record r; |
| 2122 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 2123 | r.command_buffer = d->compute_command_buffer; |
| 2124 | r.image_barrers.src_stage = src_stage; |
| 2125 | r.image_barrers.dst_stage = dst_stage; |
| 2126 | r.image_barrers.barrier_count = 2; |
| 2127 | r.image_barrers.barriers = barriers; |
| 2128 | d->delayed_records.push_back(r); |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | // record bind pipeline |
| 2133 | { |
| 2134 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2135 | { |
| 2136 | vkCmdBindPipeline(d->compute_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->pipeline()); |
| 2137 | } |
| 2138 | else |
| 2139 | { |
| 2140 | VkComputePrivate::record r; |
| 2141 | r.type = VkComputePrivate::record::TYPE_bind_pipeline; |
| 2142 | r.command_buffer = d->compute_command_buffer; |
| 2143 | r.bind_pipeline.bind_point = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 2144 | r.bind_pipeline.pipeline = pipeline->pipeline(); |
| 2145 | d->delayed_records.push_back(r); |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | // record update bindings |
| 2150 | { |
| 2151 | VkDescriptorImageInfo descriptorImageInfos[3]; |
| 2152 | descriptorImageInfos[0].sampler = pipeline->sampler; |
| 2153 | descriptorImageInfos[0].imageView = src.imageview(); |
| 2154 | descriptorImageInfos[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 2155 | descriptorImageInfos[1].sampler = 0; |
| 2156 | descriptorImageInfos[1].imageView = dst.imageview(); |
| 2157 | descriptorImageInfos[1].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 2158 | descriptorImageInfos[2].sampler = 0; |
| 2159 | descriptorImageInfos[2].imageView = dst.imageview(); |
| 2160 | descriptorImageInfos[2].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 2161 | |
| 2162 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2163 | { |
| 2164 | vkdev->vkCmdPushDescriptorSetWithTemplateKHR(d->compute_command_buffer, pipeline->descriptor_update_template(), pipeline->pipeline_layout(), 0, descriptorImageInfos); |
| 2165 | } |
| 2166 | else |
| 2167 | { |
| 2168 | // create new descriptor_pool and descriptorset |
| 2169 | VkDescriptorPool descriptor_pool; |
| 2170 | { |
| 2171 | VkDescriptorPoolSize poolSizes[2]; |
| 2172 | poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 2173 | poolSizes[0].descriptorCount = 1; |
| 2174 | poolSizes[1].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 2175 | poolSizes[1].descriptorCount = 2; |
| 2176 | |
| 2177 | VkDescriptorPoolCreateInfo descriptorPoolCreateInfo; |
| 2178 | descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 2179 | descriptorPoolCreateInfo.pNext = 0; |
| 2180 | descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; |
| 2181 | descriptorPoolCreateInfo.maxSets = 1; |
| 2182 | descriptorPoolCreateInfo.poolSizeCount = 2; |
| 2183 | descriptorPoolCreateInfo.pPoolSizes = poolSizes; |
| 2184 | |
| 2185 | VkResult ret = vkCreateDescriptorPool(vkdev->vkdevice(), &descriptorPoolCreateInfo, 0, &descriptor_pool); |
| 2186 | if (ret != VK_SUCCESS) |
| 2187 | { |
| 2188 | NCNN_LOGE("vkCreateDescriptorPool failed %d", ret); |
| 2189 | return; |
| 2190 | } |
| 2191 | } |
| 2192 | d->descriptor_pools.push_back(descriptor_pool); |
| 2193 | |
| 2194 | VkDescriptorSet descriptorset; |
| 2195 | { |
| 2196 | VkDescriptorSetLayout descriptorset_layout = pipeline->descriptorset_layout(); |
| 2197 | |
| 2198 | VkDescriptorSetAllocateInfo descriptorSetAllocateInfo; |
| 2199 | descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 2200 | descriptorSetAllocateInfo.pNext = 0; |
| 2201 | descriptorSetAllocateInfo.descriptorPool = descriptor_pool; |
| 2202 | descriptorSetAllocateInfo.descriptorSetCount = 1; |
| 2203 | descriptorSetAllocateInfo.pSetLayouts = &descriptorset_layout; |
| 2204 | |
| 2205 | VkResult ret = vkAllocateDescriptorSets(vkdev->vkdevice(), &descriptorSetAllocateInfo, &descriptorset); |
| 2206 | if (ret != VK_SUCCESS) |
| 2207 | { |
| 2208 | NCNN_LOGE("vkAllocateDescriptorSets failed %d", ret); |
| 2209 | return; |
| 2210 | } |
| 2211 | } |
| 2212 | d->descriptorsets.push_back(descriptorset); |
| 2213 | |
| 2214 | if (vkdev->info.support_VK_KHR_descriptor_update_template()) |
| 2215 | { |
| 2216 | vkdev->vkUpdateDescriptorSetWithTemplateKHR(vkdev->vkdevice(), descriptorset, pipeline->descriptor_update_template(), descriptorImageInfos); |
| 2217 | } |
| 2218 | else |
| 2219 | { |
| 2220 | VkWriteDescriptorSet writeDescriptorSets[3]; |
| 2221 | writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 2222 | writeDescriptorSets[0].pNext = 0; |
| 2223 | writeDescriptorSets[0].dstSet = descriptorset; |
| 2224 | writeDescriptorSets[0].dstBinding = 0; |
| 2225 | writeDescriptorSets[0].dstArrayElement = 0; |
| 2226 | writeDescriptorSets[0].descriptorCount = 1; |
| 2227 | writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 2228 | writeDescriptorSets[0].pImageInfo = &descriptorImageInfos[0]; |
| 2229 | writeDescriptorSets[0].pBufferInfo = 0; |
| 2230 | writeDescriptorSets[0].pTexelBufferView = 0; |
| 2231 | writeDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 2232 | writeDescriptorSets[1].pNext = 0; |
| 2233 | writeDescriptorSets[1].dstSet = descriptorset; |
| 2234 | writeDescriptorSets[1].dstBinding = 1; |
| 2235 | writeDescriptorSets[1].dstArrayElement = 0; |
| 2236 | writeDescriptorSets[1].descriptorCount = 1; |
| 2237 | writeDescriptorSets[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 2238 | writeDescriptorSets[1].pImageInfo = &descriptorImageInfos[1]; |
| 2239 | writeDescriptorSets[1].pBufferInfo = 0; |
| 2240 | writeDescriptorSets[1].pTexelBufferView = 0; |
| 2241 | writeDescriptorSets[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 2242 | writeDescriptorSets[2].pNext = 0; |
| 2243 | writeDescriptorSets[2].dstSet = descriptorset; |
| 2244 | writeDescriptorSets[2].dstBinding = 2; |
| 2245 | writeDescriptorSets[2].dstArrayElement = 0; |
| 2246 | writeDescriptorSets[2].descriptorCount = 1; |
| 2247 | writeDescriptorSets[2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 2248 | writeDescriptorSets[2].pImageInfo = &descriptorImageInfos[2]; |
| 2249 | writeDescriptorSets[2].pBufferInfo = 0; |
| 2250 | writeDescriptorSets[2].pTexelBufferView = 0; |
| 2251 | |
| 2252 | vkUpdateDescriptorSets(vkdev->vkdevice(), 3, writeDescriptorSets, 0, 0); |
| 2253 | } |
| 2254 | |
| 2255 | VkComputePrivate::record r; |
| 2256 | r.type = VkComputePrivate::record::TYPE_bind_descriptorsets; |
| 2257 | r.command_buffer = d->compute_command_buffer; |
| 2258 | r.bind_descriptorsets.bind_point = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 2259 | r.bind_descriptorsets.pipeline_layout = pipeline->pipeline_layout(); |
| 2260 | r.bind_descriptorsets.descriptorset_count = 1; |
| 2261 | r.bind_descriptorsets.descriptorset_offset = d->descriptorsets.size() - 1; |
| 2262 | d->delayed_records.push_back(r); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | // record dispatch |
| 2267 | { |
| 2268 | uint32_t group_count_x = (dst.w + pipeline->local_size_x() - 1) / pipeline->local_size_x(); |
| 2269 | uint32_t group_count_y = (dst.h + pipeline->local_size_y() - 1) / pipeline->local_size_y(); |
| 2270 | uint32_t group_count_z = (dst.c + pipeline->local_size_z() - 1) / pipeline->local_size_z(); |
| 2271 | |
| 2272 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2273 | { |
| 2274 | vkCmdDispatch(d->compute_command_buffer, group_count_x, group_count_y, group_count_z); |
| 2275 | } |
| 2276 | else |
| 2277 | { |
| 2278 | VkComputePrivate::record r; |
| 2279 | r.type = VkComputePrivate::record::TYPE_dispatch; |
| 2280 | r.command_buffer = d->compute_command_buffer; |
| 2281 | r.dispatch.group_count_x = group_count_x; |
| 2282 | r.dispatch.group_count_y = group_count_y; |
| 2283 | r.dispatch.group_count_z = group_count_z; |
| 2284 | d->delayed_records.push_back(r); |
| 2285 | } |
| 2286 | } |
| 2287 | } |
| 2288 | #endif // __ANDROID_API__ >= 26 |
| 2289 | #endif // NCNN_PLATFORM_API |
| 2290 | |
| 2291 | int VkCompute::submit_and_wait() |
| 2292 | { |
| 2293 | // NCNN_LOGE("submit_and_wait"); |
| 2294 | |
| 2295 | if (!vkdev->info.support_VK_KHR_push_descriptor()) |
| 2296 | { |
| 2297 | d->begin_command_buffer(); |
| 2298 | |
| 2299 | #if NCNN_BENCHMARK |
| 2300 | if (d->query_pool) |
| 2301 | vkCmdResetQueryPool(d->compute_command_buffer, d->query_pool, 0, d->query_count); |
| 2302 | #endif // NCNN_BENCHMARK |
| 2303 | |
| 2304 | const size_t record_count = d->delayed_records.size(); |
| 2305 | |
| 2306 | // handle delayed records |
| 2307 | for (size_t i = 0; i < record_count; i++) |
| 2308 | { |
| 2309 | const VkComputePrivate::record& r = d->delayed_records[i]; |
| 2310 | |
| 2311 | switch (r.type) |
| 2312 | { |
| 2313 | case VkComputePrivate::record::TYPE_copy_buffer: |
| 2314 | { |
| 2315 | vkCmdCopyBuffer(r.command_buffer, r.copy_buffer.src, r.copy_buffer.dst, r.copy_buffer.region_count, r.copy_buffer.regions); |
| 2316 | delete[] r.copy_buffer.regions; |
| 2317 | break; |
| 2318 | } |
| 2319 | case VkComputePrivate::record::TYPE_copy_image: |
| 2320 | { |
| 2321 | vkCmdCopyImage(r.command_buffer, r.copy_image.src, r.copy_image.src_layout, r.copy_image.dst, r.copy_image.dst_layout, r.copy_image.region_count, r.copy_image.regions); |
| 2322 | delete[] r.copy_image.regions; |
| 2323 | break; |
| 2324 | } |
| 2325 | case VkComputePrivate::record::TYPE_copy_buffer_to_image: |
| 2326 | { |
| 2327 | vkCmdCopyBufferToImage(r.command_buffer, r.copy_buffer_to_image.src, r.copy_buffer_to_image.dst, r.copy_buffer_to_image.layout, r.copy_buffer_to_image.region_count, r.copy_buffer_to_image.regions); |
| 2328 | delete[] r.copy_buffer_to_image.regions; |
| 2329 | break; |
| 2330 | } |
| 2331 | case VkComputePrivate::record::TYPE_copy_image_to_buffer: |
| 2332 | { |
| 2333 | vkCmdCopyImageToBuffer(r.command_buffer, r.copy_image_to_buffer.src, r.copy_image_to_buffer.layout, r.copy_image_to_buffer.dst, r.copy_image_to_buffer.region_count, r.copy_image_to_buffer.regions); |
| 2334 | delete[] r.copy_image_to_buffer.regions; |
| 2335 | break; |
| 2336 | } |
| 2337 | case VkComputePrivate::record::TYPE_bind_pipeline: |
| 2338 | { |
| 2339 | vkCmdBindPipeline(r.command_buffer, r.bind_pipeline.bind_point, r.bind_pipeline.pipeline); |
| 2340 | break; |
| 2341 | } |
| 2342 | case VkComputePrivate::record::TYPE_bind_descriptorsets: |
| 2343 | { |
| 2344 | vkCmdBindDescriptorSets(r.command_buffer, r.bind_descriptorsets.bind_point, r.bind_descriptorsets.pipeline_layout, 0, r.bind_descriptorsets.descriptorset_count, &d->descriptorsets[r.bind_descriptorsets.descriptorset_offset], 0, 0); |
| 2345 | break; |
| 2346 | } |
| 2347 | case VkComputePrivate::record::TYPE_push_constants: |
| 2348 | { |
| 2349 | vkCmdPushConstants(r.command_buffer, r.push_constants.pipeline_layout, r.push_constants.stage_flags, 0, r.push_constants.size, r.push_constants.values); |
| 2350 | delete[](unsigned char*) r.push_constants.values; |
| 2351 | break; |
| 2352 | } |
| 2353 | case VkComputePrivate::record::TYPE_dispatch: |
| 2354 | { |
| 2355 | vkCmdDispatch(r.command_buffer, r.dispatch.group_count_x, r.dispatch.group_count_y, r.dispatch.group_count_z); |
| 2356 | break; |
| 2357 | } |
| 2358 | case VkComputePrivate::record::TYPE_memory_barrers: |
| 2359 | { |
| 2360 | vkCmdPipelineBarrier(r.command_buffer, r.memory_barrers.src_stage, r.memory_barrers.dst_stage, 0, r.memory_barrers.barrier_count, r.memory_barrers.barriers, 0, 0, 0, 0); |
| 2361 | delete[] r.memory_barrers.barriers; |
| 2362 | break; |
| 2363 | } |
| 2364 | case VkComputePrivate::record::TYPE_buffer_barrers: |
| 2365 | { |
| 2366 | vkCmdPipelineBarrier(r.command_buffer, r.buffer_barrers.src_stage, r.buffer_barrers.dst_stage, 0, 0, 0, r.buffer_barrers.barrier_count, r.buffer_barrers.barriers, 0, 0); |
| 2367 | delete[] r.buffer_barrers.barriers; |
| 2368 | break; |
| 2369 | } |
| 2370 | case VkComputePrivate::record::TYPE_image_barrers: |
| 2371 | { |
| 2372 | vkCmdPipelineBarrier(r.command_buffer, r.image_barrers.src_stage, r.image_barrers.dst_stage, 0, 0, 0, 0, 0, r.image_barrers.barrier_count, r.image_barrers.barriers); |
| 2373 | delete[] r.image_barrers.barriers; |
| 2374 | break; |
| 2375 | } |
| 2376 | #if NCNN_BENCHMARK |
| 2377 | case VkComputePrivate::record::TYPE_write_timestamp: |
| 2378 | { |
| 2379 | if (d->query_pool) |
| 2380 | vkCmdWriteTimestamp(r.command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, d->query_pool, r.write_timestamp.query); |
| 2381 | break; |
| 2382 | } |
| 2383 | #endif // NCNN_BENCHMARK |
| 2384 | case VkComputePrivate::record::TYPE_post_download: |
| 2385 | case VkComputePrivate::record::TYPE_post_cast_float16_to_float32: |
| 2386 | default: |
| 2387 | break; |
| 2388 | } |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | // end command buffer |
| 2393 | { |
| 2394 | d->end_command_buffer(); |
| 2395 | } |
| 2396 | |
| 2397 | // acquire queue and reclaim on return |
| 2398 | VkQueue compute_queue = vkdev->acquire_queue(vkdev->info.compute_queue_family_index()); |
| 2399 | if (compute_queue == 0) |
| 2400 | { |
| 2401 | NCNN_LOGE("out of compute queue"); |
| 2402 | return -1; |
| 2403 | } |
| 2404 | |
| 2405 | // submit compute |
| 2406 | { |
| 2407 | VkSubmitInfo submitInfo; |
| 2408 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 2409 | submitInfo.pNext = 0; |
| 2410 | submitInfo.waitSemaphoreCount = 0; |
| 2411 | submitInfo.pWaitSemaphores = 0; |
| 2412 | submitInfo.pWaitDstStageMask = 0; |
| 2413 | submitInfo.commandBufferCount = 1; |
| 2414 | submitInfo.pCommandBuffers = &d->compute_command_buffer; |
| 2415 | submitInfo.signalSemaphoreCount = 0; |
| 2416 | submitInfo.pSignalSemaphores = 0; |
| 2417 | |
| 2418 | VkResult ret = vkQueueSubmit(compute_queue, 1, &submitInfo, d->compute_command_fence); |
| 2419 | if (ret != VK_SUCCESS) |
| 2420 | { |
| 2421 | NCNN_LOGE("vkQueueSubmit failed %d", ret); |
| 2422 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 2423 | return -1; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 2428 | |
| 2429 | // wait |
| 2430 | { |
| 2431 | VkResult ret = vkWaitForFences(vkdev->vkdevice(), 1, &d->compute_command_fence, VK_TRUE, (uint64_t)-1); |
| 2432 | if (ret != VK_SUCCESS) |
| 2433 | { |
| 2434 | NCNN_LOGE("vkWaitForFences failed %d", ret); |
| 2435 | return -1; |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | // handle delayed post records |
| 2440 | for (size_t i = 0; i < d->delayed_records.size(); i++) |
| 2441 | { |
| 2442 | const VkComputePrivate::record& r = d->delayed_records[i]; |
| 2443 | |
| 2444 | switch (r.type) |
| 2445 | { |
| 2446 | case VkComputePrivate::record::TYPE_post_download: |
| 2447 | { |
| 2448 | const VkMat& src = d->download_post_buffers[r.post_download.download_post_buffer_mat_offset]; |
| 2449 | Mat& dst = d->download_post_mats_fp16[r.post_download.download_post_mat_fp16_offset]; |
| 2450 | |
| 2451 | // NCNN_LOGE("post_download %p +%d ~%d -> %p", src.buffer(), src.buffer_offset(), src.buffer_capacity(), dst.data); |
| 2452 | |
| 2453 | src.allocator->invalidate(src.data); |
| 2454 | memcpy(dst.data, src.mapped_ptr(), dst.total() * dst.elemsize); |
| 2455 | break; |
| 2456 | } |
| 2457 | case VkComputePrivate::record::TYPE_post_cast_float16_to_float32: |
| 2458 | { |
| 2459 | // NCNN_LOGE("post_cast_float16_to_float32"); |
| 2460 | |
| 2461 | const Mat& src = d->download_post_mats_fp16[r.post_cast_float16_to_float32.download_post_mat_fp16_offset]; |
| 2462 | Mat& dst = d->download_post_mats[r.post_cast_float16_to_float32.download_post_mat_offset]; |
| 2463 | |
| 2464 | Option opt; |
| 2465 | opt.num_threads = r.post_cast_float16_to_float32.num_threads; |
| 2466 | opt.blob_allocator = dst.allocator; |
| 2467 | ncnn::cast_float16_to_float32(src, dst, opt); |
| 2468 | break; |
| 2469 | } |
| 2470 | default: |
| 2471 | break; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | d->delayed_records.clear(); |
| 2476 | |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
| 2480 | int VkCompute::reset() |
| 2481 | { |
| 2482 | d->upload_staging_buffers.clear(); |
| 2483 | d->download_post_buffers.clear(); |
| 2484 | d->download_post_mats_fp16.clear(); |
| 2485 | d->download_post_mats.clear(); |
| 2486 | |
| 2487 | for (size_t i = 0; i < d->image_blocks_to_destroy.size(); i++) |
| 2488 | { |
| 2489 | VkImageMemory* ptr = d->image_blocks_to_destroy[i]; |
| 2490 | |
| 2491 | int old_command_refcount = NCNN_XADD(&ptr->command_refcount, -1); |
| 2492 | if (ptr->refcount == 0 && old_command_refcount == 1) |
| 2493 | { |
| 2494 | // no userspace reference and we are the last command reference |
| 2495 | vkDestroyImageView(vkdev->vkdevice(), ptr->imageview, 0); |
| 2496 | vkDestroyImage(vkdev->vkdevice(), ptr->image, 0); |
| 2497 | |
| 2498 | delete ptr; |
| 2499 | } |
| 2500 | else |
| 2501 | { |
| 2502 | // reference exists in user code or other command |
| 2503 | } |
| 2504 | } |
| 2505 | d->image_blocks_to_destroy.clear(); |
| 2506 | |
| 2507 | if (!vkdev->info.support_VK_KHR_push_descriptor()) |
| 2508 | { |
| 2509 | for (size_t i = 0; i < d->descriptorsets.size(); i++) |
| 2510 | { |
| 2511 | vkFreeDescriptorSets(vkdev->vkdevice(), d->descriptor_pools[i], 1, &d->descriptorsets[i]); |
| 2512 | vkDestroyDescriptorPool(vkdev->vkdevice(), d->descriptor_pools[i], 0); |
| 2513 | } |
| 2514 | d->descriptor_pools.clear(); |
| 2515 | d->descriptorsets.clear(); |
| 2516 | } |
| 2517 | |
| 2518 | d->delayed_records.clear(); |
| 2519 | |
| 2520 | // reset command buffer and fence |
| 2521 | { |
| 2522 | VkResult ret = vkResetCommandBuffer(d->compute_command_buffer, 0); |
| 2523 | if (ret != VK_SUCCESS) |
| 2524 | { |
| 2525 | NCNN_LOGE("vkResetCommandBuffer failed %d", ret); |
| 2526 | return -1; |
| 2527 | } |
| 2528 | } |
| 2529 | { |
| 2530 | VkResult ret = vkResetFences(vkdev->vkdevice(), 1, &d->compute_command_fence); |
| 2531 | if (ret != VK_SUCCESS) |
| 2532 | { |
| 2533 | NCNN_LOGE("vkResetFences failed %d", ret); |
| 2534 | return -1; |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2539 | { |
| 2540 | d->begin_command_buffer(); |
| 2541 | |
| 2542 | #if NCNN_BENCHMARK |
| 2543 | if (d->query_pool) |
| 2544 | vkCmdResetQueryPool(d->compute_command_buffer, d->query_pool, 0, d->query_count); |
| 2545 | #endif // NCNN_BENCHMARK |
| 2546 | } |
| 2547 | |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
| 2551 | #if NCNN_BENCHMARK |
| 2552 | int VkCompute::create_query_pool(uint32_t _query_count) |
| 2553 | { |
| 2554 | d->query_count = _query_count; |
| 2555 | |
| 2556 | VkQueryPoolCreateInfo queryPoolCreateInfo; |
| 2557 | queryPoolCreateInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; |
| 2558 | queryPoolCreateInfo.pNext = 0; |
| 2559 | queryPoolCreateInfo.flags = 0; |
| 2560 | queryPoolCreateInfo.queryType = VK_QUERY_TYPE_TIMESTAMP; |
| 2561 | queryPoolCreateInfo.queryCount = d->query_count; |
| 2562 | queryPoolCreateInfo.pipelineStatistics = 0; |
| 2563 | |
| 2564 | VkResult ret = vkCreateQueryPool(vkdev->vkdevice(), &queryPoolCreateInfo, 0, &d->query_pool); |
| 2565 | if (ret != VK_SUCCESS) |
| 2566 | { |
| 2567 | NCNN_LOGE("vkCreateQueryPool failed %d", ret); |
| 2568 | return -1; |
| 2569 | } |
| 2570 | |
| 2571 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2572 | { |
| 2573 | if (d->query_pool) |
| 2574 | vkCmdResetQueryPool(d->compute_command_buffer, d->query_pool, 0, d->query_count); |
| 2575 | } |
| 2576 | |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | int VkCompute::get_query_pool_results(uint32_t first_query, uint32_t query_count, std::vector<uint64_t>& results) |
| 2581 | { |
| 2582 | if (results.size() < first_query + query_count) |
| 2583 | { |
| 2584 | NCNN_LOGE("results not large enough"); |
| 2585 | return -1; |
| 2586 | } |
| 2587 | |
| 2588 | VkResult ret = vkGetQueryPoolResults(vkdev->vkdevice(), d->query_pool, first_query, query_count, |
| 2589 | query_count * sizeof(uint64_t), results.data() + first_query, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); |
| 2590 | if (ret != VK_SUCCESS && ret != VK_NOT_READY) |
| 2591 | { |
| 2592 | NCNN_LOGE("vkGetQueryPoolResults failed %d", ret); |
| 2593 | return -1; |
| 2594 | } |
| 2595 | |
| 2596 | return 0; |
| 2597 | } |
| 2598 | #endif // NCNN_BENCHMARK |
| 2599 | |
| 2600 | void VkCompute::barrier_readwrite(const VkMat& binding) |
| 2601 | { |
| 2602 | if (binding.data->access_flags & VK_ACCESS_SHADER_WRITE_BIT || binding.data->stage_flags != VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT) |
| 2603 | { |
| 2604 | // barrier device any @ compute/null to shader-readwrite @ compute |
| 2605 | VkBufferMemoryBarrier* barriers = new VkBufferMemoryBarrier[1]; |
| 2606 | barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 2607 | barriers[0].pNext = 0; |
| 2608 | barriers[0].srcAccessMask = binding.data->access_flags; |
| 2609 | barriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 2610 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2611 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2612 | barriers[0].buffer = binding.buffer(); |
| 2613 | barriers[0].offset = binding.buffer_offset(); |
| 2614 | barriers[0].size = binding.buffer_capacity(); |
| 2615 | |
| 2616 | VkPipelineStageFlags src_stage = binding.data->stage_flags; |
| 2617 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2618 | |
| 2619 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2620 | { |
| 2621 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, barriers, 0, 0); |
| 2622 | delete[] barriers; |
| 2623 | } |
| 2624 | else |
| 2625 | { |
| 2626 | VkComputePrivate::record r; |
| 2627 | r.type = VkComputePrivate::record::TYPE_buffer_barrers; |
| 2628 | r.command_buffer = d->compute_command_buffer; |
| 2629 | r.buffer_barrers.src_stage = src_stage; |
| 2630 | r.buffer_barrers.dst_stage = dst_stage; |
| 2631 | r.buffer_barrers.barrier_count = 1; |
| 2632 | r.buffer_barrers.barriers = barriers; |
| 2633 | d->delayed_records.push_back(r); |
| 2634 | } |
| 2635 | |
| 2636 | // mark device shader-readwrite @ compute |
| 2637 | binding.data->access_flags = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 2638 | binding.data->stage_flags = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | void VkCompute::barrier_readwrite(const VkImageMat& binding) |
| 2643 | { |
| 2644 | if (binding.data->access_flags & VK_ACCESS_SHADER_WRITE_BIT || binding.data->image_layout != VK_IMAGE_LAYOUT_GENERAL || binding.data->stage_flags != VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT) |
| 2645 | { |
| 2646 | // image layout transform any @ any to shader-write @ compute |
| 2647 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 2648 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 2649 | barriers[0].pNext = 0; |
| 2650 | barriers[0].srcAccessMask = binding.data->access_flags; |
| 2651 | barriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 2652 | barriers[0].oldLayout = binding.data->image_layout; |
| 2653 | barriers[0].newLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 2654 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2655 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2656 | barriers[0].image = binding.image(); |
| 2657 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2658 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 2659 | barriers[0].subresourceRange.levelCount = 1; |
| 2660 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 2661 | barriers[0].subresourceRange.layerCount = 1; |
| 2662 | |
| 2663 | VkPipelineStageFlags src_stage = binding.data->stage_flags; |
| 2664 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2665 | |
| 2666 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2667 | { |
| 2668 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 2669 | delete[] barriers; |
| 2670 | } |
| 2671 | else |
| 2672 | { |
| 2673 | VkComputePrivate::record r; |
| 2674 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 2675 | r.command_buffer = d->compute_command_buffer; |
| 2676 | r.image_barrers.src_stage = src_stage; |
| 2677 | r.image_barrers.dst_stage = dst_stage; |
| 2678 | r.image_barrers.barrier_count = 1; |
| 2679 | r.image_barrers.barriers = barriers; |
| 2680 | d->delayed_records.push_back(r); |
| 2681 | } |
| 2682 | |
| 2683 | // mark image shader-write @ compute |
| 2684 | binding.data->access_flags = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 2685 | binding.data->image_layout = VK_IMAGE_LAYOUT_GENERAL; |
| 2686 | binding.data->stage_flags = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | void VkCompute::barrier_readonly(const VkImageMat& binding) |
| 2691 | { |
| 2692 | if (binding.data->access_flags & VK_ACCESS_SHADER_WRITE_BIT || binding.data->image_layout != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL || binding.data->stage_flags != VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT) |
| 2693 | { |
| 2694 | // image layout transform any @ any to shader-readonly-optimal @ compute |
| 2695 | VkImageMemoryBarrier* barriers = new VkImageMemoryBarrier[1]; |
| 2696 | barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 2697 | barriers[0].pNext = 0; |
| 2698 | barriers[0].srcAccessMask = binding.data->access_flags; |
| 2699 | barriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 2700 | barriers[0].oldLayout = binding.data->image_layout; |
| 2701 | barriers[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 2702 | barriers[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2703 | barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 2704 | barriers[0].image = binding.image(); |
| 2705 | barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2706 | barriers[0].subresourceRange.baseMipLevel = 0; |
| 2707 | barriers[0].subresourceRange.levelCount = 1; |
| 2708 | barriers[0].subresourceRange.baseArrayLayer = 0; |
| 2709 | barriers[0].subresourceRange.layerCount = 1; |
| 2710 | |
| 2711 | VkPipelineStageFlags src_stage = binding.data->stage_flags; |
| 2712 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2713 | |
| 2714 | if (vkdev->info.support_VK_KHR_push_descriptor()) |
| 2715 | { |
| 2716 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, barriers); |
| 2717 | delete[] barriers; |
| 2718 | } |
| 2719 | else |
| 2720 | { |
| 2721 | VkComputePrivate::record r; |
| 2722 | r.type = VkComputePrivate::record::TYPE_image_barrers; |
| 2723 | r.command_buffer = d->compute_command_buffer; |
| 2724 | r.image_barrers.src_stage = src_stage; |
| 2725 | r.image_barrers.dst_stage = dst_stage; |
| 2726 | r.image_barrers.barrier_count = 1; |
| 2727 | r.image_barrers.barriers = barriers; |
| 2728 | d->delayed_records.push_back(r); |
| 2729 | } |
| 2730 | |
| 2731 | // mark image shader-readonly-optimal @ compute |
| 2732 | binding.data->access_flags = VK_ACCESS_SHADER_READ_BIT; |
| 2733 | binding.data->image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 2734 | binding.data->stage_flags = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | class VkTransferPrivate |
| 2739 | { |
| 2740 | public: |
| 2741 | VkTransferPrivate(const VulkanDevice* _vkdev); |
| 2742 | ~VkTransferPrivate(); |
| 2743 | |
| 2744 | int init(); |
| 2745 | int begin_command_buffer(); |
| 2746 | int end_command_buffer(); |
| 2747 | |
| 2748 | const VulkanDevice* vkdev; |
| 2749 | |
| 2750 | VkCommandPool compute_command_pool; |
| 2751 | VkCommandPool transfer_command_pool; |
| 2752 | |
| 2753 | VkCommandBuffer upload_command_buffer; |
| 2754 | VkCommandBuffer compute_command_buffer; |
| 2755 | |
| 2756 | VkSemaphore upload_compute_semaphore; |
| 2757 | |
| 2758 | VkFence upload_command_fence; |
| 2759 | VkFence compute_command_fence; |
| 2760 | |
| 2761 | std::vector<VkMat> upload_staging_buffers; |
| 2762 | }; |
| 2763 | |
| 2764 | VkTransferPrivate::VkTransferPrivate(const VulkanDevice* _vkdev) |
| 2765 | : vkdev(_vkdev) |
| 2766 | { |
| 2767 | compute_command_pool = 0; |
| 2768 | transfer_command_pool = 0; |
| 2769 | |
| 2770 | upload_command_buffer = 0; |
| 2771 | compute_command_buffer = 0; |
| 2772 | |
| 2773 | upload_compute_semaphore = 0; |
| 2774 | |
| 2775 | upload_command_fence = 0; |
| 2776 | compute_command_fence = 0; |
| 2777 | |
| 2778 | init(); |
| 2779 | } |
| 2780 | |
| 2781 | VkTransferPrivate::~VkTransferPrivate() |
| 2782 | { |
| 2783 | vkDestroyFence(vkdev->vkdevice(), compute_command_fence, 0); |
| 2784 | |
| 2785 | vkFreeCommandBuffers(vkdev->vkdevice(), compute_command_pool, 1, &compute_command_buffer); |
| 2786 | vkDestroyCommandPool(vkdev->vkdevice(), compute_command_pool, 0); |
| 2787 | |
| 2788 | if (!vkdev->info.unified_compute_transfer_queue()) |
| 2789 | { |
| 2790 | vkDestroyFence(vkdev->vkdevice(), upload_command_fence, 0); |
| 2791 | |
| 2792 | vkDestroySemaphore(vkdev->vkdevice(), upload_compute_semaphore, 0); |
| 2793 | |
| 2794 | vkFreeCommandBuffers(vkdev->vkdevice(), transfer_command_pool, 1, &upload_command_buffer); |
| 2795 | vkDestroyCommandPool(vkdev->vkdevice(), transfer_command_pool, 0); |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | int VkTransferPrivate::init() |
| 2800 | { |
| 2801 | // compute_command_pool |
| 2802 | { |
| 2803 | VkCommandPoolCreateInfo commandPoolCreateInfo; |
| 2804 | commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 2805 | commandPoolCreateInfo.pNext = 0; |
| 2806 | commandPoolCreateInfo.flags = 0; |
| 2807 | commandPoolCreateInfo.queueFamilyIndex = vkdev->info.compute_queue_family_index(); |
| 2808 | |
| 2809 | VkResult ret = vkCreateCommandPool(vkdev->vkdevice(), &commandPoolCreateInfo, 0, &compute_command_pool); |
| 2810 | if (ret != VK_SUCCESS) |
| 2811 | { |
| 2812 | NCNN_LOGE("vkCreateCommandPool failed %d", ret); |
| 2813 | return -1; |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | // compute_command_buffer |
| 2818 | { |
| 2819 | VkCommandBufferAllocateInfo commandBufferAllocateInfo; |
| 2820 | commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 2821 | commandBufferAllocateInfo.pNext = 0; |
| 2822 | commandBufferAllocateInfo.commandPool = compute_command_pool; |
| 2823 | commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 2824 | commandBufferAllocateInfo.commandBufferCount = 1; |
| 2825 | |
| 2826 | VkResult ret = vkAllocateCommandBuffers(vkdev->vkdevice(), &commandBufferAllocateInfo, &compute_command_buffer); |
| 2827 | if (ret != VK_SUCCESS) |
| 2828 | { |
| 2829 | NCNN_LOGE("vkAllocateCommandBuffers failed %d", ret); |
| 2830 | return -1; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | // compute_command_fence |
| 2835 | { |
| 2836 | VkFenceCreateInfo fenceCreateInfo; |
| 2837 | fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 2838 | fenceCreateInfo.pNext = 0; |
| 2839 | fenceCreateInfo.flags = 0; |
| 2840 | |
| 2841 | VkResult ret = vkCreateFence(vkdev->vkdevice(), &fenceCreateInfo, 0, &compute_command_fence); |
| 2842 | if (ret != VK_SUCCESS) |
| 2843 | { |
| 2844 | NCNN_LOGE("vkCreateFence failed %d", ret); |
| 2845 | return -1; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | if (!vkdev->info.unified_compute_transfer_queue()) |
| 2850 | { |
| 2851 | // transfer_command_pool |
| 2852 | { |
| 2853 | VkCommandPoolCreateInfo commandPoolCreateInfo; |
| 2854 | commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 2855 | commandPoolCreateInfo.pNext = 0; |
| 2856 | commandPoolCreateInfo.flags = 0; |
| 2857 | commandPoolCreateInfo.queueFamilyIndex = vkdev->info.transfer_queue_family_index(); |
| 2858 | |
| 2859 | VkResult ret = vkCreateCommandPool(vkdev->vkdevice(), &commandPoolCreateInfo, 0, &transfer_command_pool); |
| 2860 | if (ret != VK_SUCCESS) |
| 2861 | { |
| 2862 | NCNN_LOGE("vkCreateCommandPool failed %d", ret); |
| 2863 | return -1; |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | // upload_command_buffer |
| 2868 | { |
| 2869 | VkCommandBufferAllocateInfo commandBufferAllocateInfo; |
| 2870 | commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 2871 | commandBufferAllocateInfo.pNext = 0; |
| 2872 | commandBufferAllocateInfo.commandPool = transfer_command_pool; |
| 2873 | commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 2874 | commandBufferAllocateInfo.commandBufferCount = 1; |
| 2875 | |
| 2876 | VkResult ret = vkAllocateCommandBuffers(vkdev->vkdevice(), &commandBufferAllocateInfo, &upload_command_buffer); |
| 2877 | if (ret != VK_SUCCESS) |
| 2878 | { |
| 2879 | NCNN_LOGE("vkAllocateCommandBuffers failed %d", ret); |
| 2880 | return -1; |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | // upload_compute_semaphore |
| 2885 | { |
| 2886 | VkSemaphoreCreateInfo semaphoreCreateInfo; |
| 2887 | semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 2888 | semaphoreCreateInfo.pNext = 0; |
| 2889 | semaphoreCreateInfo.flags = 0; |
| 2890 | |
| 2891 | VkResult ret = vkCreateSemaphore(vkdev->vkdevice(), &semaphoreCreateInfo, 0, &upload_compute_semaphore); |
| 2892 | if (ret != VK_SUCCESS) |
| 2893 | { |
| 2894 | NCNN_LOGE("vkCreateSemaphore failed %d", ret); |
| 2895 | return -1; |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | // upload_command_fence |
| 2900 | { |
| 2901 | VkFenceCreateInfo fenceCreateInfo; |
| 2902 | fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 2903 | fenceCreateInfo.pNext = 0; |
| 2904 | fenceCreateInfo.flags = 0; |
| 2905 | |
| 2906 | VkResult ret = vkCreateFence(vkdev->vkdevice(), &fenceCreateInfo, 0, &upload_command_fence); |
| 2907 | if (ret != VK_SUCCESS) |
| 2908 | { |
| 2909 | NCNN_LOGE("vkCreateFence failed %d", ret); |
| 2910 | return -1; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | begin_command_buffer(); |
| 2916 | |
| 2917 | return 0; |
| 2918 | } |
| 2919 | |
| 2920 | int VkTransferPrivate::begin_command_buffer() |
| 2921 | { |
| 2922 | { |
| 2923 | VkCommandBufferBeginInfo commandBufferBeginInfo; |
| 2924 | commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 2925 | commandBufferBeginInfo.pNext = 0; |
| 2926 | commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 2927 | commandBufferBeginInfo.pInheritanceInfo = 0; |
| 2928 | |
| 2929 | VkResult ret = vkBeginCommandBuffer(compute_command_buffer, &commandBufferBeginInfo); |
| 2930 | if (ret != VK_SUCCESS) |
| 2931 | { |
| 2932 | NCNN_LOGE("vkBeginCommandBuffer failed %d", ret); |
| 2933 | return -1; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | if (!vkdev->info.unified_compute_transfer_queue()) |
| 2938 | { |
| 2939 | { |
| 2940 | VkCommandBufferBeginInfo commandBufferBeginInfo; |
| 2941 | commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 2942 | commandBufferBeginInfo.pNext = 0; |
| 2943 | commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 2944 | commandBufferBeginInfo.pInheritanceInfo = 0; |
| 2945 | |
| 2946 | VkResult ret = vkBeginCommandBuffer(upload_command_buffer, &commandBufferBeginInfo); |
| 2947 | if (ret != VK_SUCCESS) |
| 2948 | { |
| 2949 | NCNN_LOGE("vkBeginCommandBuffer failed %d", ret); |
| 2950 | return -1; |
| 2951 | } |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
| 2958 | int VkTransferPrivate::end_command_buffer() |
| 2959 | { |
| 2960 | { |
| 2961 | VkResult ret = vkEndCommandBuffer(compute_command_buffer); |
| 2962 | if (ret != VK_SUCCESS) |
| 2963 | { |
| 2964 | NCNN_LOGE("vkEndCommandBuffer failed %d", ret); |
| 2965 | return -1; |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | if (!vkdev->info.unified_compute_transfer_queue()) |
| 2970 | { |
| 2971 | { |
| 2972 | VkResult ret = vkEndCommandBuffer(upload_command_buffer); |
| 2973 | if (ret != VK_SUCCESS) |
| 2974 | { |
| 2975 | NCNN_LOGE("vkEndCommandBuffer failed %d", ret); |
| 2976 | return -1; |
| 2977 | } |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | return 0; |
| 2982 | } |
| 2983 | |
| 2984 | VkTransfer::VkTransfer(const VulkanDevice* _vkdev) |
| 2985 | : vkdev(_vkdev), d(new VkTransferPrivate(_vkdev)) |
| 2986 | { |
| 2987 | } |
| 2988 | |
| 2989 | VkTransfer::~VkTransfer() |
| 2990 | { |
| 2991 | delete d; |
| 2992 | } |
| 2993 | |
| 2994 | void VkTransfer::record_upload(const Mat& src, VkMat& dst, const Option& opt, bool flatten) |
| 2995 | { |
| 2996 | // NCNN_LOGE("record_upload src = %d | %d %d %d @ %d", src.dims, src.w, src.h, src.c, src.elempack); |
| 2997 | |
| 2998 | // NOTE keep the hack here ? |
| 2999 | if (src.elembits() == 32) |
| 3000 | { |
| 3001 | if (opt.use_fp16_storage || (opt.use_fp16_packed && src.elempack % 4 == 0)) |
| 3002 | { |
| 3003 | Mat src_fp16; |
| 3004 | cast_float32_to_float16(src, src_fp16, opt); |
| 3005 | |
| 3006 | record_upload(src_fp16, dst, opt, flatten); |
| 3007 | |
| 3008 | return; |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | Mat src_flattened = flatten ? src.reshape(src.w * src.h * src.c) : src; |
| 3013 | |
| 3014 | // create dst |
| 3015 | dst.create_like(src_flattened, opt.blob_vkallocator); |
| 3016 | |
| 3017 | if (dst.empty()) |
| 3018 | { |
| 3019 | return; |
| 3020 | } |
| 3021 | |
| 3022 | if (dst.allocator->mappable) |
| 3023 | { |
| 3024 | // memcpy src_flattened to device |
| 3025 | memcpy(dst.mapped_ptr(), src_flattened.data, src_flattened.total() * src_flattened.elemsize); |
| 3026 | dst.allocator->flush(dst.data); |
| 3027 | |
| 3028 | // barrier device host-write @ null to shader-read @ compute |
| 3029 | { |
| 3030 | VkBufferMemoryBarrier barrier; |
| 3031 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3032 | barrier.pNext = 0; |
| 3033 | barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 3034 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 3035 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3036 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3037 | barrier.buffer = dst.buffer(); |
| 3038 | barrier.offset = dst.buffer_offset(); |
| 3039 | barrier.size = dst.buffer_capacity(); |
| 3040 | |
| 3041 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_HOST_BIT; |
| 3042 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3043 | |
| 3044 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, &barrier, 0, 0); |
| 3045 | } |
| 3046 | |
| 3047 | // mark device shader-readwrite @ compute |
| 3048 | dst.data->access_flags = VK_ACCESS_SHADER_READ_BIT; |
| 3049 | dst.data->stage_flags = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3050 | |
| 3051 | return; |
| 3052 | } |
| 3053 | |
| 3054 | // create staging |
| 3055 | VkMat dst_staging; |
| 3056 | dst_staging.create_like(src_flattened, opt.staging_vkallocator); |
| 3057 | |
| 3058 | // memcpy src_flattened to staging |
| 3059 | memcpy(dst_staging.mapped_ptr(), src_flattened.data, src_flattened.total() * src_flattened.elemsize); |
| 3060 | dst_staging.allocator->flush(dst_staging.data); |
| 3061 | |
| 3062 | VkCommandBuffer command_buffer; |
| 3063 | if (vkdev->info.unified_compute_transfer_queue()) |
| 3064 | { |
| 3065 | command_buffer = d->compute_command_buffer; |
| 3066 | } |
| 3067 | else |
| 3068 | { |
| 3069 | command_buffer = d->upload_command_buffer; |
| 3070 | } |
| 3071 | |
| 3072 | // barrier staging host-write @ null to transfer-read @ queue |
| 3073 | { |
| 3074 | VkBufferMemoryBarrier barrier; |
| 3075 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3076 | barrier.pNext = 0; |
| 3077 | barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 3078 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 3079 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3080 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3081 | barrier.buffer = dst_staging.buffer(); |
| 3082 | barrier.offset = dst_staging.buffer_offset(); |
| 3083 | barrier.size = dst_staging.buffer_capacity(); |
| 3084 | |
| 3085 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_HOST_BIT; |
| 3086 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3087 | |
| 3088 | vkCmdPipelineBarrier(command_buffer, src_stage, dst_stage, 0, 0, 0, 1, &barrier, 0, 0); |
| 3089 | } |
| 3090 | |
| 3091 | // record staging to device |
| 3092 | { |
| 3093 | VkBufferCopy region; |
| 3094 | region.srcOffset = dst_staging.buffer_offset(); |
| 3095 | region.dstOffset = dst.buffer_offset(); |
| 3096 | region.size = std::min(dst_staging.buffer_capacity(), dst.buffer_capacity()); |
| 3097 | |
| 3098 | vkCmdCopyBuffer(command_buffer, dst_staging.buffer(), dst.buffer(), 1, ®ion); |
| 3099 | } |
| 3100 | |
| 3101 | if (vkdev->info.unified_compute_transfer_queue()) |
| 3102 | { |
| 3103 | // barrier device transfer-write @ compute to shader-read @ compute |
| 3104 | { |
| 3105 | VkBufferMemoryBarrier barrier; |
| 3106 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3107 | barrier.pNext = 0; |
| 3108 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 3109 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 3110 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3111 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3112 | barrier.buffer = dst.buffer(); |
| 3113 | barrier.offset = dst.buffer_offset(); |
| 3114 | barrier.size = dst.buffer_capacity(); |
| 3115 | |
| 3116 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3117 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3118 | |
| 3119 | vkCmdPipelineBarrier(command_buffer, src_stage, dst_stage, 0, 0, 0, 1, &barrier, 0, 0); |
| 3120 | } |
| 3121 | } |
| 3122 | else |
| 3123 | { |
| 3124 | // queue ownership transfer transfer-write @ transfer to shader-read @ compute |
| 3125 | |
| 3126 | // release |
| 3127 | { |
| 3128 | VkBufferMemoryBarrier barrier; |
| 3129 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3130 | barrier.pNext = 0; |
| 3131 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 3132 | barrier.dstAccessMask = 0; |
| 3133 | barrier.srcQueueFamilyIndex = vkdev->info.transfer_queue_family_index(); |
| 3134 | barrier.dstQueueFamilyIndex = vkdev->info.compute_queue_family_index(); |
| 3135 | barrier.buffer = dst.buffer(); |
| 3136 | barrier.offset = dst.buffer_offset(); |
| 3137 | barrier.size = dst.buffer_capacity(); |
| 3138 | |
| 3139 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3140 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 3141 | |
| 3142 | vkCmdPipelineBarrier(d->upload_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, &barrier, 0, 0); |
| 3143 | } |
| 3144 | |
| 3145 | // acquire |
| 3146 | { |
| 3147 | VkBufferMemoryBarrier barrier; |
| 3148 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3149 | barrier.pNext = 0; |
| 3150 | barrier.srcAccessMask = 0; |
| 3151 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 3152 | barrier.srcQueueFamilyIndex = vkdev->info.transfer_queue_family_index(); |
| 3153 | barrier.dstQueueFamilyIndex = vkdev->info.compute_queue_family_index(); |
| 3154 | barrier.buffer = dst.buffer(); |
| 3155 | barrier.offset = dst.buffer_offset(); |
| 3156 | barrier.size = dst.buffer_capacity(); |
| 3157 | |
| 3158 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 3159 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3160 | |
| 3161 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 1, &barrier, 0, 0); |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | // mark device shader-readwrite @ compute |
| 3166 | dst.data->access_flags = VK_ACCESS_SHADER_READ_BIT; |
| 3167 | dst.data->stage_flags = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3168 | |
| 3169 | // stash staging |
| 3170 | d->upload_staging_buffers.push_back(dst_staging); |
| 3171 | } |
| 3172 | |
| 3173 | void VkTransfer::record_upload(const Mat& src, VkImageMat& dst, const Option& opt) |
| 3174 | { |
| 3175 | // NCNN_LOGE("record_upload image src = %d | %d %d %d @ %d", src.dims, src.w, src.h, src.c, src.elempack); |
| 3176 | |
| 3177 | // NOTE keep the hack here ? |
| 3178 | if (src.elembits() == 32) |
| 3179 | { |
| 3180 | if (opt.use_fp16_storage || (opt.use_fp16_packed && src.elempack % 4 == 0)) |
| 3181 | { |
| 3182 | Mat src_fp16; |
| 3183 | cast_float32_to_float16(src, src_fp16, opt); |
| 3184 | |
| 3185 | record_upload(src_fp16, dst, opt); |
| 3186 | |
| 3187 | return; |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | // create dst |
| 3192 | dst.create_like(src, opt.blob_vkallocator); |
| 3193 | if (dst.empty()) |
| 3194 | return; |
| 3195 | |
| 3196 | // create staging |
| 3197 | VkMat dst_staging; |
| 3198 | dst_staging.create_like(src, opt.staging_vkallocator); |
| 3199 | |
| 3200 | // memcpy src to staging |
| 3201 | memcpy(dst_staging.mapped_ptr(), src.data, src.total() * src.elemsize); |
| 3202 | dst_staging.allocator->flush(dst_staging.data); |
| 3203 | |
| 3204 | VkCommandBuffer command_buffer; |
| 3205 | if (vkdev->info.unified_compute_transfer_queue()) |
| 3206 | { |
| 3207 | command_buffer = d->compute_command_buffer; |
| 3208 | } |
| 3209 | else |
| 3210 | { |
| 3211 | command_buffer = d->upload_command_buffer; |
| 3212 | } |
| 3213 | |
| 3214 | // barrier staging host-write @ null to transfer-read @ queue |
| 3215 | { |
| 3216 | VkBufferMemoryBarrier barrier; |
| 3217 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3218 | barrier.pNext = 0; |
| 3219 | barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 3220 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 3221 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3222 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3223 | barrier.buffer = dst_staging.buffer(); |
| 3224 | barrier.offset = dst_staging.buffer_offset(); |
| 3225 | barrier.size = dst_staging.buffer_capacity(); |
| 3226 | |
| 3227 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_HOST_BIT; |
| 3228 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3229 | |
| 3230 | vkCmdPipelineBarrier(command_buffer, src_stage, dst_stage, 0, 0, 0, 1, &barrier, 0, 0); |
| 3231 | } |
| 3232 | |
| 3233 | // image layout transform undefined @ null to transfer-dst-optimal @ queue |
| 3234 | { |
| 3235 | VkImageMemoryBarrier barrier; |
| 3236 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3237 | barrier.pNext = 0; |
| 3238 | barrier.srcAccessMask = 0; |
| 3239 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 3240 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 3241 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 3242 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3243 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3244 | barrier.image = dst.image(); |
| 3245 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3246 | barrier.subresourceRange.baseMipLevel = 0; |
| 3247 | barrier.subresourceRange.levelCount = 1; |
| 3248 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3249 | barrier.subresourceRange.layerCount = 1; |
| 3250 | |
| 3251 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 3252 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3253 | |
| 3254 | vkCmdPipelineBarrier(command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, &barrier); |
| 3255 | } |
| 3256 | |
| 3257 | // record staging to image |
| 3258 | { |
| 3259 | const int channels = dst.c; |
| 3260 | VkBufferImageCopy* regions = new VkBufferImageCopy[channels]; |
| 3261 | for (int i = 0; i < channels; i++) |
| 3262 | { |
| 3263 | regions[i].bufferOffset = dst_staging.buffer_offset() + dst_staging.cstep * dst_staging.elemsize * i; |
| 3264 | regions[i].bufferRowLength = 0; |
| 3265 | regions[i].bufferImageHeight = 0; |
| 3266 | regions[i].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3267 | regions[i].imageSubresource.mipLevel = 0; |
| 3268 | regions[i].imageSubresource.baseArrayLayer = 0; |
| 3269 | regions[i].imageSubresource.layerCount = 1; |
| 3270 | regions[i].imageOffset.x = 0; |
| 3271 | regions[i].imageOffset.y = 0; |
| 3272 | regions[i].imageOffset.z = i; |
| 3273 | regions[i].imageExtent.width = dst.data->width; |
| 3274 | regions[i].imageExtent.height = dst.data->height; |
| 3275 | regions[i].imageExtent.depth = 1; |
| 3276 | } |
| 3277 | |
| 3278 | vkCmdCopyBufferToImage(command_buffer, dst_staging.buffer(), dst.image(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, channels, regions); |
| 3279 | delete[] regions; |
| 3280 | } |
| 3281 | |
| 3282 | if (vkdev->info.unified_compute_transfer_queue()) |
| 3283 | { |
| 3284 | // image layout transform transfer-dst-optimal @ compute to shader-readonly-optimal @ compute |
| 3285 | { |
| 3286 | VkImageMemoryBarrier barrier; |
| 3287 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3288 | barrier.pNext = 0; |
| 3289 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 3290 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 3291 | barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 3292 | barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 3293 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3294 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3295 | barrier.image = dst.image(); |
| 3296 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3297 | barrier.subresourceRange.baseMipLevel = 0; |
| 3298 | barrier.subresourceRange.levelCount = 1; |
| 3299 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3300 | barrier.subresourceRange.layerCount = 1; |
| 3301 | |
| 3302 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3303 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3304 | |
| 3305 | vkCmdPipelineBarrier(command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, &barrier); |
| 3306 | } |
| 3307 | } |
| 3308 | else |
| 3309 | { |
| 3310 | // queue ownership transfer transfer-write @ transfer to shader-read @ compute |
| 3311 | |
| 3312 | // release |
| 3313 | { |
| 3314 | VkImageMemoryBarrier barrier; |
| 3315 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3316 | barrier.pNext = 0; |
| 3317 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 3318 | barrier.dstAccessMask = 0; |
| 3319 | barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 3320 | barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 3321 | barrier.srcQueueFamilyIndex = vkdev->info.transfer_queue_family_index(); |
| 3322 | barrier.dstQueueFamilyIndex = vkdev->info.compute_queue_family_index(); |
| 3323 | barrier.image = dst.image(); |
| 3324 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3325 | barrier.subresourceRange.baseMipLevel = 0; |
| 3326 | barrier.subresourceRange.levelCount = 1; |
| 3327 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3328 | barrier.subresourceRange.layerCount = 1; |
| 3329 | |
| 3330 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 3331 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 3332 | |
| 3333 | vkCmdPipelineBarrier(d->upload_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, &barrier); |
| 3334 | } |
| 3335 | |
| 3336 | // acquire |
| 3337 | { |
| 3338 | VkImageMemoryBarrier barrier; |
| 3339 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3340 | barrier.pNext = 0; |
| 3341 | barrier.srcAccessMask = 0; |
| 3342 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 3343 | barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 3344 | barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 3345 | barrier.srcQueueFamilyIndex = vkdev->info.transfer_queue_family_index(); |
| 3346 | barrier.dstQueueFamilyIndex = vkdev->info.compute_queue_family_index(); |
| 3347 | barrier.image = dst.image(); |
| 3348 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3349 | barrier.subresourceRange.baseMipLevel = 0; |
| 3350 | barrier.subresourceRange.levelCount = 1; |
| 3351 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3352 | barrier.subresourceRange.layerCount = 1; |
| 3353 | |
| 3354 | VkPipelineStageFlags src_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 3355 | VkPipelineStageFlags dst_stage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3356 | |
| 3357 | vkCmdPipelineBarrier(d->compute_command_buffer, src_stage, dst_stage, 0, 0, 0, 0, 0, 1, &barrier); |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | // mark device shader-readwrite @ compute |
| 3362 | dst.data->access_flags = VK_ACCESS_SHADER_READ_BIT; |
| 3363 | dst.data->image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 3364 | dst.data->stage_flags = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3365 | |
| 3366 | // stash staging |
| 3367 | d->upload_staging_buffers.push_back(dst_staging); |
| 3368 | } |
| 3369 | |
| 3370 | int VkTransfer::submit_and_wait() |
| 3371 | { |
| 3372 | // NCNN_LOGE("submit_and_wait"); |
| 3373 | |
| 3374 | // end command buffer |
| 3375 | { |
| 3376 | d->end_command_buffer(); |
| 3377 | } |
| 3378 | |
| 3379 | VkQueue compute_queue = vkdev->acquire_queue(vkdev->info.compute_queue_family_index()); |
| 3380 | if (compute_queue == 0) |
| 3381 | { |
| 3382 | NCNN_LOGE("out of compute queue"); |
| 3383 | return -1; |
| 3384 | } |
| 3385 | |
| 3386 | if (vkdev->info.unified_compute_transfer_queue()) |
| 3387 | { |
| 3388 | // submit compute |
| 3389 | { |
| 3390 | VkSubmitInfo submitInfo; |
| 3391 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 3392 | submitInfo.pNext = 0; |
| 3393 | submitInfo.waitSemaphoreCount = 0; |
| 3394 | submitInfo.pWaitSemaphores = 0; |
| 3395 | submitInfo.pWaitDstStageMask = 0; |
| 3396 | submitInfo.commandBufferCount = 1; |
| 3397 | submitInfo.pCommandBuffers = &d->compute_command_buffer; |
| 3398 | submitInfo.signalSemaphoreCount = 0; |
| 3399 | submitInfo.pSignalSemaphores = 0; |
| 3400 | |
| 3401 | VkResult ret = vkQueueSubmit(compute_queue, 1, &submitInfo, d->compute_command_fence); |
| 3402 | if (ret != VK_SUCCESS) |
| 3403 | { |
| 3404 | NCNN_LOGE("vkQueueSubmit failed %d", ret); |
| 3405 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 3406 | return -1; |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | else |
| 3411 | { |
| 3412 | VkQueue transfer_queue = vkdev->acquire_queue(vkdev->info.transfer_queue_family_index()); |
| 3413 | if (transfer_queue == 0) |
| 3414 | { |
| 3415 | NCNN_LOGE("out of transfer queue"); |
| 3416 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 3417 | return -1; |
| 3418 | } |
| 3419 | |
| 3420 | // submit upload compute |
| 3421 | { |
| 3422 | VkSubmitInfo submitInfo; |
| 3423 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 3424 | submitInfo.pNext = 0; |
| 3425 | submitInfo.waitSemaphoreCount = 0; |
| 3426 | submitInfo.pWaitSemaphores = 0; |
| 3427 | submitInfo.pWaitDstStageMask = 0; |
| 3428 | submitInfo.commandBufferCount = 1; |
| 3429 | submitInfo.pCommandBuffers = &d->upload_command_buffer; |
| 3430 | submitInfo.signalSemaphoreCount = 1; |
| 3431 | submitInfo.pSignalSemaphores = &d->upload_compute_semaphore; |
| 3432 | |
| 3433 | VkResult ret = vkQueueSubmit(transfer_queue, 1, &submitInfo, d->upload_command_fence); |
| 3434 | if (ret != VK_SUCCESS) |
| 3435 | { |
| 3436 | NCNN_LOGE("vkQueueSubmit failed %d", ret); |
| 3437 | vkdev->reclaim_queue(vkdev->info.transfer_queue_family_index(), transfer_queue); |
| 3438 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 3439 | return -1; |
| 3440 | } |
| 3441 | } |
| 3442 | { |
| 3443 | VkPipelineStageFlags wait_dst_stage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; // FIXME |
| 3444 | |
| 3445 | VkSubmitInfo submitInfo; |
| 3446 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 3447 | submitInfo.pNext = 0; |
| 3448 | submitInfo.waitSemaphoreCount = 1; |
| 3449 | submitInfo.pWaitSemaphores = &d->upload_compute_semaphore; |
| 3450 | submitInfo.pWaitDstStageMask = &wait_dst_stage; |
| 3451 | submitInfo.commandBufferCount = 1; |
| 3452 | submitInfo.pCommandBuffers = &d->compute_command_buffer; |
| 3453 | submitInfo.signalSemaphoreCount = 0; |
| 3454 | submitInfo.pSignalSemaphores = 0; |
| 3455 | |
| 3456 | VkResult ret = vkQueueSubmit(compute_queue, 1, &submitInfo, d->compute_command_fence); |
| 3457 | if (ret != VK_SUCCESS) |
| 3458 | { |
| 3459 | NCNN_LOGE("vkQueueSubmit failed %d", ret); |
| 3460 | vkdev->reclaim_queue(vkdev->info.transfer_queue_family_index(), transfer_queue); |
| 3461 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 3462 | return -1; |
| 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | vkdev->reclaim_queue(vkdev->info.transfer_queue_family_index(), transfer_queue); |
| 3467 | } |
| 3468 | |
| 3469 | vkdev->reclaim_queue(vkdev->info.compute_queue_family_index(), compute_queue); |
| 3470 | |
| 3471 | // wait |
| 3472 | if (vkdev->info.unified_compute_transfer_queue()) |
| 3473 | { |
| 3474 | VkResult ret = vkWaitForFences(vkdev->vkdevice(), 1, &d->compute_command_fence, VK_TRUE, (uint64_t)-1); |
| 3475 | if (ret != VK_SUCCESS) |
| 3476 | { |
| 3477 | NCNN_LOGE("vkWaitForFences failed %d", ret); |
| 3478 | return -1; |
| 3479 | } |
| 3480 | } |
| 3481 | else |
| 3482 | { |
| 3483 | VkFence fences[2] = {d->upload_command_fence, d->compute_command_fence}; |
| 3484 | |
| 3485 | VkResult ret = vkWaitForFences(vkdev->vkdevice(), 2, fences, VK_TRUE, (uint64_t)-1); |
| 3486 | if (ret != VK_SUCCESS) |
| 3487 | { |
| 3488 | NCNN_LOGE("vkWaitForFences failed %d", ret); |
| 3489 | return -1; |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
| 3496 | } // namespace ncnn |
| 3497 | |
| 3498 | #endif // NCNN_VULKAN |