| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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 | #ifndef NCNN_PIPELINE_H |
| 16 | #define NCNN_PIPELINE_H |
| 17 | |
| 18 | #include "mat.h" |
| 19 | #include "platform.h" |
| 20 | #if NCNN_VULKAN |
| 21 | #include "gpu.h" |
| 22 | #endif // NCNN_VULKAN |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | #if NCNN_VULKAN |
| 27 | class Option; |
| 28 | class PipelinePrivate; |
| 29 | class NCNN_EXPORT Pipeline |
| 30 | { |
| 31 | public: |
| 32 | explicit Pipeline(const VulkanDevice* vkdev); |
| 33 | virtual ~Pipeline(); |
| 34 | |
| 35 | public: |
| 36 | void set_optimal_local_size_xyz(int w = 4, int h = 4, int c = 4); |
| 37 | void set_optimal_local_size_xyz(const Mat& local_size_xyz); |
| 38 | void set_local_size_xyz(int w, int h, int c); |
| 39 | |
| 40 | int create(const uint32_t* spv_data, size_t spv_data_size, const std::vector<vk_specialization_type>& specializations); |
| 41 | |
| 42 | int create(int shader_type_index, const Option& opt, const std::vector<vk_specialization_type>& specializations); |
| 43 | |
| 44 | public: |
| 45 | VkShaderModule shader_module() const; |
| 46 | VkDescriptorSetLayout descriptorset_layout() const; |
| 47 | VkPipelineLayout pipeline_layout() const; |
| 48 | VkPipeline pipeline() const; |
| 49 | VkDescriptorUpdateTemplateKHR descriptor_update_template() const; |
| 50 | |
| 51 | const ShaderInfo& shader_info() const; |
| 52 | |
| 53 | uint32_t local_size_x() const; |
| 54 | uint32_t local_size_y() const; |
| 55 | uint32_t local_size_z() const; |
| 56 | |
| 57 | protected: |
| 58 | void set_shader_module(VkShaderModule shader_module); |
| 59 | void set_descriptorset_layout(VkDescriptorSetLayout descriptorset_layout); |
| 60 | void set_pipeline_layout(VkPipelineLayout pipeline_layout); |
| 61 | void set_pipeline(VkPipeline pipeline); |
| 62 | void set_descriptor_update_template(VkDescriptorUpdateTemplateKHR descriptor_update_template); |
| 63 | |
| 64 | void set_shader_info(const ShaderInfo& shader_info); |
| 65 | |
| 66 | public: |
| 67 | const VulkanDevice* vkdev; |
| 68 | |
| 69 | private: |
| 70 | Pipeline(const Pipeline&); |
| 71 | Pipeline& operator=(const Pipeline&); |
| 72 | |
| 73 | private: |
| 74 | PipelinePrivate* const d; |
| 75 | }; |
| 76 | |
| 77 | #if NCNN_PLATFORM_API |
| 78 | #if __ANDROID_API__ >= 26 |
| 79 | class VkCompute; |
| 80 | class NCNN_EXPORT ImportAndroidHardwareBufferPipeline : private Pipeline |
| 81 | { |
| 82 | public: |
| 83 | explicit ImportAndroidHardwareBufferPipeline(const VulkanDevice* vkdev); |
| 84 | virtual ~ImportAndroidHardwareBufferPipeline(); |
| 85 | |
| 86 | int create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int type_to, int rotate_from, const Option& opt); |
| 87 | int create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int type_to, int rotate_from, int target_width, int target_height, const Option& opt); |
| 88 | void destroy(); |
| 89 | |
| 90 | friend class VkCompute; |
| 91 | |
| 92 | protected: |
| 93 | int create_shader_module(const Option& opt); |
| 94 | int create_sampler(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator); |
| 95 | int create_descriptorset_layout(); |
| 96 | |
| 97 | public: |
| 98 | int type_to; |
| 99 | int rotate_from; |
| 100 | bool need_resize; |
| 101 | |
| 102 | VkSampler sampler; |
| 103 | }; |
| 104 | #endif // __ANDROID_API__ >= 26 |
| 105 | #endif // NCNN_PLATFORM_API |
| 106 | |
| 107 | #endif // NCNN_VULKAN |
| 108 | |
| 109 | } // namespace ncnn |
| 110 | |
| 111 | #endif // NCNN_PIPELINE_H |