command.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef NCNN_COMMAND_H
  15. #define NCNN_COMMAND_H
  16. #include "platform.h"
  17. #if NCNN_VULKAN
  18. #include "mat.h"
  19. #include <vulkan/vulkan.h>
  20. namespace ncnn {
  21. class Pipeline;
  22. #if NCNN_PLATFORM_API
  23. #if __ANDROID_API__ >= 26
  24. class ImportAndroidHardwareBufferPipeline;
  25. #endif // __ANDROID_API__ >= 26
  26. #endif // NCNN_PLATFORM_API
  27. class VkComputePrivate;
  28. class NCNN_EXPORT VkCompute
  29. {
  30. public:
  31. explicit VkCompute(const VulkanDevice* vkdev);
  32. virtual ~VkCompute();
  33. public:
  34. void record_upload(const Mat& src, VkMat& dst, const Option& opt);
  35. void record_upload(const Mat& src, VkImageMat& dst, const Option& opt);
  36. void record_download(const VkMat& src, Mat& dst, const Option& opt);
  37. void record_download(const VkImageMat& src, Mat& dst, const Option& opt);
  38. void record_buffer_to_image(const VkMat& src, VkImageMat& dst, const Option& opt);
  39. void record_image_to_buffer(const VkImageMat& src, VkMat& dst, const Option& opt);
  40. void record_clone(const Mat& src, VkMat& dst, const Option& opt);
  41. void record_clone(const Mat& src, VkImageMat& dst, const Option& opt);
  42. void record_clone(const VkMat& src, Mat& dst, const Option& opt);
  43. void record_clone(const VkImageMat& src, Mat& dst, const Option& opt);
  44. void record_clone(const VkMat& src, VkMat& dst, const Option& opt);
  45. void record_clone(const VkImageMat& src, VkImageMat& dst, const Option& opt);
  46. void record_clone(const VkMat& src, VkImageMat& dst, const Option& opt);
  47. void record_clone(const VkImageMat& src, VkMat& dst, const Option& opt);
  48. void record_pipeline(const Pipeline* pipeline, const std::vector<VkMat>& bindings, const std::vector<vk_constant_type>& constants, const VkMat& dispatcher);
  49. void record_pipeline(const Pipeline* pipeline, const std::vector<VkImageMat>& bindings, const std::vector<vk_constant_type>& constants, const VkImageMat& dispatcher);
  50. void 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);
  51. void 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);
  52. void 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);
  53. #if NCNN_BENCHMARK
  54. void record_write_timestamp(uint32_t query);
  55. #endif // NCNN_BENCHMARK
  56. #if NCNN_PLATFORM_API
  57. #if __ANDROID_API__ >= 26
  58. void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkMat& dst);
  59. void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkImageMat& dst);
  60. #endif // __ANDROID_API__ >= 26
  61. #endif // NCNN_PLATFORM_API
  62. int submit_and_wait();
  63. int reset();
  64. #if NCNN_BENCHMARK
  65. int create_query_pool(uint32_t query_count);
  66. int get_query_pool_results(uint32_t first_query, uint32_t query_count, std::vector<uint64_t>& results);
  67. #endif // NCNN_BENCHMARK
  68. protected:
  69. const VulkanDevice* vkdev;
  70. void barrier_readwrite(const VkMat& binding);
  71. void barrier_readwrite(const VkImageMat& binding);
  72. void barrier_readonly(const VkImageMat& binding);
  73. private:
  74. VkComputePrivate* const d;
  75. };
  76. class VkTransferPrivate;
  77. class NCNN_EXPORT VkTransfer
  78. {
  79. public:
  80. explicit VkTransfer(const VulkanDevice* vkdev);
  81. virtual ~VkTransfer();
  82. public:
  83. void record_upload(const Mat& src, VkMat& dst, const Option& opt, bool flatten = true);
  84. void record_upload(const Mat& src, VkImageMat& dst, const Option& opt);
  85. int submit_and_wait();
  86. protected:
  87. const VulkanDevice* vkdev;
  88. private:
  89. VkTransferPrivate* const d;
  90. };
  91. } // namespace ncnn
  92. #endif // NCNN_VULKAN
  93. #endif // NCNN_COMMAND_H