| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2018 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_GPU_H |
| 16 | #define NCNN_GPU_H |
| 17 | |
| 18 | #include "platform.h" |
| 19 | |
| 20 | #if NCNN_VULKAN |
| 21 | |
| 22 | #include "mat.h" |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | // instance |
| 27 | |
| 28 | // Create VkInstance and initialize some objects that need to be calculated by GPU |
| 29 | // Creates a VkInstance object, Checks the extended attributes supported by the Vulkan instance concerned, |
| 30 | // Initializes, and creates Vulkan validation layers (if ENABLE_VALIDATION_LAYER is enabled), |
| 31 | // Iterates over all supported physical devices, etc. |
| 32 | NCNN_EXPORT int create_gpu_instance(const char* driver_path = 0); |
| 33 | |
| 34 | // Get global VkInstance variable |
| 35 | // Must be called after create_gpu_instance() and before destroy_gpu_instance() |
| 36 | NCNN_EXPORT VkInstance get_gpu_instance(); |
| 37 | |
| 38 | // Destroy VkInstance object and free the memory of the associated object |
| 39 | // Usually called in the destructor of the main program exit |
| 40 | // The function will internally ensure that all vulkan devices are idle before proceeding with destruction. |
| 41 | NCNN_EXPORT void destroy_gpu_instance(); |
| 42 | |
| 43 | // vulkan core |
| 44 | extern PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers; |
| 45 | extern PFN_vkAllocateDescriptorSets vkAllocateDescriptorSets; |
| 46 | extern PFN_vkAllocateMemory vkAllocateMemory; |
| 47 | extern PFN_vkBeginCommandBuffer vkBeginCommandBuffer; |
| 48 | extern PFN_vkBindBufferMemory vkBindBufferMemory; |
| 49 | extern PFN_vkBindImageMemory vkBindImageMemory; |
| 50 | extern PFN_vkCmdBeginQuery vkCmdBeginQuery; |
| 51 | extern PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets; |
| 52 | extern PFN_vkCmdBindIndexBuffer vkCmdBindIndexBuffer; |
| 53 | extern PFN_vkCmdBindPipeline vkCmdBindPipeline; |
| 54 | extern PFN_vkCmdCopyBuffer vkCmdCopyBuffer; |
| 55 | extern PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage; |
| 56 | extern PFN_vkCmdCopyImage vkCmdCopyImage; |
| 57 | extern PFN_vkCmdCopyImageToBuffer vkCmdCopyImageToBuffer; |
| 58 | extern PFN_vkCmdCopyQueryPoolResults vkCmdCopyQueryPoolResults; |
| 59 | extern PFN_vkCmdDispatch vkCmdDispatch; |
| 60 | extern PFN_vkCmdDispatchIndirect vkCmdDispatchIndirect; |
| 61 | extern PFN_vkCmdEndQuery vkCmdEndQuery; |
| 62 | extern PFN_vkCmdExecuteCommands vkCmdExecuteCommands; |
| 63 | extern PFN_vkCmdFillBuffer vkCmdFillBuffer; |
| 64 | extern PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier; |
| 65 | extern PFN_vkCmdPushConstants vkCmdPushConstants; |
| 66 | extern PFN_vkCmdResetQueryPool vkCmdResetQueryPool; |
| 67 | extern PFN_vkCmdResolveImage vkCmdResolveImage; |
| 68 | extern PFN_vkCmdUpdateBuffer vkCmdUpdateBuffer; |
| 69 | extern PFN_vkCmdWriteTimestamp vkCmdWriteTimestamp; |
| 70 | extern PFN_vkCreateBuffer vkCreateBuffer; |
| 71 | extern PFN_vkCreateBufferView vkCreateBufferView; |
| 72 | extern PFN_vkCreateCommandPool vkCreateCommandPool; |
| 73 | extern PFN_vkCreateComputePipelines vkCreateComputePipelines; |
| 74 | extern PFN_vkCreateDescriptorPool vkCreateDescriptorPool; |
| 75 | extern PFN_vkCreateDescriptorSetLayout vkCreateDescriptorSetLayout; |
| 76 | extern PFN_vkCreateDevice vkCreateDevice; |
| 77 | extern PFN_vkCreateFence vkCreateFence; |
| 78 | extern PFN_vkCreateImage vkCreateImage; |
| 79 | extern PFN_vkCreateImageView vkCreateImageView; |
| 80 | extern PFN_vkCreatePipelineCache vkCreatePipelineCache; |
| 81 | extern PFN_vkCreatePipelineLayout vkCreatePipelineLayout; |
| 82 | extern PFN_vkCreateQueryPool vkCreateQueryPool; |
| 83 | extern PFN_vkCreateSampler vkCreateSampler; |
| 84 | extern PFN_vkCreateSemaphore vkCreateSemaphore; |
| 85 | extern PFN_vkCreateShaderModule vkCreateShaderModule; |
| 86 | extern PFN_vkDestroyBuffer vkDestroyBuffer; |
| 87 | extern PFN_vkDestroyBufferView vkDestroyBufferView; |
| 88 | extern PFN_vkDestroyCommandPool vkDestroyCommandPool; |
| 89 | extern PFN_vkDestroyDescriptorPool vkDestroyDescriptorPool; |
| 90 | extern PFN_vkDestroyDescriptorSetLayout vkDestroyDescriptorSetLayout; |
| 91 | extern PFN_vkDestroyDevice vkDestroyDevice; |
| 92 | extern PFN_vkDestroyFence vkDestroyFence; |
| 93 | extern PFN_vkDestroyImage vkDestroyImage; |
| 94 | extern PFN_vkDestroyImageView vkDestroyImageView; |
| 95 | extern PFN_vkDestroyInstance vkDestroyInstance; |
| 96 | extern PFN_vkDestroyPipeline vkDestroyPipeline; |
| 97 | extern PFN_vkDestroyPipelineCache vkDestroyPipelineCache; |
| 98 | extern PFN_vkDestroyPipelineLayout vkDestroyPipelineLayout; |
| 99 | extern PFN_vkDestroyQueryPool vkDestroyQueryPool; |
| 100 | extern PFN_vkDestroySampler vkDestroySampler; |
| 101 | extern PFN_vkDestroySemaphore vkDestroySemaphore; |
| 102 | extern PFN_vkDestroyShaderModule vkDestroyShaderModule; |
| 103 | extern PFN_vkDeviceWaitIdle vkDeviceWaitIdle; |
| 104 | extern PFN_vkEndCommandBuffer vkEndCommandBuffer; |
| 105 | extern PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties; |
| 106 | extern PFN_vkEnumerateDeviceLayerProperties vkEnumerateDeviceLayerProperties; |
| 107 | extern PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices; |
| 108 | extern PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges; |
| 109 | extern PFN_vkFreeCommandBuffers vkFreeCommandBuffers; |
| 110 | extern PFN_vkFreeDescriptorSets vkFreeDescriptorSets; |
| 111 | extern PFN_vkFreeMemory vkFreeMemory; |
| 112 | extern PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements; |
| 113 | extern PFN_vkGetDeviceMemoryCommitment vkGetDeviceMemoryCommitment; |
| 114 | extern PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr; |
| 115 | extern PFN_vkGetDeviceQueue vkGetDeviceQueue; |
| 116 | extern PFN_vkGetFenceStatus vkGetFenceStatus; |
| 117 | extern PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements; |
| 118 | extern PFN_vkGetImageSubresourceLayout vkGetImageSubresourceLayout; |
| 119 | extern PFN_vkGetPhysicalDeviceFeatures vkGetPhysicalDeviceFeatures; |
| 120 | extern PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties; |
| 121 | extern PFN_vkGetPhysicalDeviceImageFormatProperties vkGetPhysicalDeviceImageFormatProperties; |
| 122 | extern PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties; |
| 123 | extern PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties; |
| 124 | extern PFN_vkGetPhysicalDeviceQueueFamilyProperties vkGetPhysicalDeviceQueueFamilyProperties; |
| 125 | extern PFN_vkGetPipelineCacheData vkGetPipelineCacheData; |
| 126 | extern PFN_vkGetQueryPoolResults vkGetQueryPoolResults; |
| 127 | extern PFN_vkInvalidateMappedMemoryRanges vkInvalidateMappedMemoryRanges; |
| 128 | extern PFN_vkMapMemory vkMapMemory; |
| 129 | extern PFN_vkMergePipelineCaches vkMergePipelineCaches; |
| 130 | extern PFN_vkQueueSubmit vkQueueSubmit; |
| 131 | extern PFN_vkQueueWaitIdle vkQueueWaitIdle; |
| 132 | extern PFN_vkResetCommandBuffer vkResetCommandBuffer; |
| 133 | extern PFN_vkResetCommandPool vkResetCommandPool; |
| 134 | extern PFN_vkResetDescriptorPool vkResetDescriptorPool; |
| 135 | extern PFN_vkResetFences vkResetFences; |
| 136 | extern PFN_vkUnmapMemory vkUnmapMemory; |
| 137 | extern PFN_vkUpdateDescriptorSets vkUpdateDescriptorSets; |
| 138 | extern PFN_vkWaitForFences vkWaitForFences; |
| 139 | |
| 140 | // instance extension capability |
| 141 | extern int support_VK_KHR_external_memory_capabilities; |
| 142 | extern int support_VK_KHR_get_physical_device_properties2; |
| 143 | extern int support_VK_KHR_get_surface_capabilities2; |
| 144 | extern int support_VK_KHR_surface; |
| 145 | extern int support_VK_EXT_debug_utils; |
| 146 | extern int support_VK_EXT_validation_features; |
| 147 | extern int support_VK_EXT_validation_flags; |
| 148 | #if __ANDROID_API__ >= 26 |
| 149 | extern int support_VK_KHR_android_surface; |
| 150 | #endif // __ANDROID_API__ >= 26 |
| 151 | |
| 152 | // VK_KHR_cooperative_matrix |
| 153 | extern PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR; |
| 154 | |
| 155 | // VK_KHR_external_memory_capabilities |
| 156 | extern PFN_vkGetPhysicalDeviceExternalBufferPropertiesKHR vkGetPhysicalDeviceExternalBufferPropertiesKHR; |
| 157 | |
| 158 | // VK_KHR_get_physical_device_properties2 |
| 159 | extern PFN_vkGetPhysicalDeviceFeatures2KHR vkGetPhysicalDeviceFeatures2KHR; |
| 160 | extern PFN_vkGetPhysicalDeviceProperties2KHR vkGetPhysicalDeviceProperties2KHR; |
| 161 | extern PFN_vkGetPhysicalDeviceFormatProperties2KHR vkGetPhysicalDeviceFormatProperties2KHR; |
| 162 | extern PFN_vkGetPhysicalDeviceImageFormatProperties2KHR vkGetPhysicalDeviceImageFormatProperties2KHR; |
| 163 | extern PFN_vkGetPhysicalDeviceQueueFamilyProperties2KHR vkGetPhysicalDeviceQueueFamilyProperties2KHR; |
| 164 | extern PFN_vkGetPhysicalDeviceMemoryProperties2KHR vkGetPhysicalDeviceMemoryProperties2KHR; |
| 165 | |
| 166 | // VK_KHR_get_surface_capabilities2 |
| 167 | extern PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR vkGetPhysicalDeviceSurfaceCapabilities2KHR; |
| 168 | extern PFN_vkGetPhysicalDeviceSurfaceFormats2KHR vkGetPhysicalDeviceSurfaceFormats2KHR; |
| 169 | |
| 170 | // VK_KHR_surface |
| 171 | extern PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR; |
| 172 | extern PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR; |
| 173 | extern PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 174 | extern PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR; |
| 175 | extern PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModesKHR; |
| 176 | |
| 177 | #if __ANDROID_API__ >= 26 |
| 178 | // VK_KHR_android_surface |
| 179 | extern PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR; |
| 180 | #endif // __ANDROID_API__ >= 26 |
| 181 | |
| 182 | // VK_NV_cooperative_matrix |
| 183 | extern PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesNV vkGetPhysicalDeviceCooperativeMatrixPropertiesNV; |
| 184 | |
| 185 | // get info |
| 186 | NCNN_EXPORT int get_gpu_count(); |
| 187 | NCNN_EXPORT int get_default_gpu_index(); |
| 188 | |
| 189 | class GpuInfoPrivate; |
| 190 | class NCNN_EXPORT GpuInfo |
| 191 | { |
| 192 | public: |
| 193 | explicit GpuInfo(); |
| 194 | virtual ~GpuInfo(); |
| 195 | |
| 196 | // vulkan physical device |
| 197 | VkPhysicalDevice physical_device() const; |
| 198 | |
| 199 | // memory properties |
| 200 | const VkPhysicalDeviceMemoryProperties& physical_device_memory_properties() const; |
| 201 | |
| 202 | // info |
| 203 | uint32_t api_version() const; |
| 204 | uint32_t driver_version() const; |
| 205 | uint32_t vendor_id() const; |
| 206 | uint32_t device_id() const; |
| 207 | const char* device_name() const; |
| 208 | uint8_t* pipeline_cache_uuid() const; |
| 209 | |
| 210 | // 0 = discrete gpu |
| 211 | // 1 = integrated gpu |
| 212 | // 2 = virtual gpu |
| 213 | // 3 = cpu |
| 214 | int type() const; |
| 215 | |
| 216 | // hardware limit |
| 217 | uint32_t max_shared_memory_size() const; |
| 218 | uint32_t max_workgroup_count_x() const; |
| 219 | uint32_t max_workgroup_count_y() const; |
| 220 | uint32_t max_workgroup_count_z() const; |
| 221 | uint32_t max_workgroup_invocations() const; |
| 222 | uint32_t max_workgroup_size_x() const; |
| 223 | uint32_t max_workgroup_size_y() const; |
| 224 | uint32_t max_workgroup_size_z() const; |
| 225 | size_t memory_map_alignment() const; |
| 226 | size_t buffer_offset_alignment() const; |
| 227 | size_t non_coherent_atom_size() const; |
| 228 | size_t buffer_image_granularity() const; |
| 229 | uint32_t max_image_dimension_1d() const; |
| 230 | uint32_t max_image_dimension_2d() const; |
| 231 | uint32_t max_image_dimension_3d() const; |
| 232 | float timestamp_period() const; |
| 233 | |
| 234 | // runtime |
| 235 | uint32_t compute_queue_family_index() const; |
| 236 | uint32_t graphics_queue_family_index() const; |
| 237 | uint32_t transfer_queue_family_index() const; |
| 238 | |
| 239 | uint32_t compute_queue_count() const; |
| 240 | uint32_t graphics_queue_count() const; |
| 241 | uint32_t transfer_queue_count() const; |
| 242 | |
| 243 | // property |
| 244 | bool unified_compute_transfer_queue() const; |
| 245 | |
| 246 | // subgroup |
| 247 | uint32_t subgroup_size() const; |
| 248 | bool support_subgroup_basic() const; |
| 249 | bool support_subgroup_vote() const; |
| 250 | bool support_subgroup_ballot() const; |
| 251 | bool support_subgroup_shuffle() const; |
| 252 | |
| 253 | // bug is not feature |
| 254 | bool bug_storage_buffer_no_l1() const; |
| 255 | bool bug_corrupted_online_pipeline_cache() const; |
| 256 | bool bug_buffer_image_load_zero() const; |
| 257 | |
| 258 | // but sometimes bug is a feature |
| 259 | bool bug_implicit_fp16_arithmetic() const; |
| 260 | |
| 261 | // fp16 and int8 feature |
| 262 | bool support_fp16_packed() const; |
| 263 | bool support_fp16_storage() const; |
| 264 | bool support_fp16_uniform() const; |
| 265 | bool support_fp16_arithmetic() const; |
| 266 | bool support_int8_packed() const; |
| 267 | bool support_int8_storage() const; |
| 268 | bool support_int8_uniform() const; |
| 269 | bool support_int8_arithmetic() const; |
| 270 | |
| 271 | // ycbcr conversion feature |
| 272 | bool support_ycbcr_conversion() const; |
| 273 | |
| 274 | // cooperative matrix feature |
| 275 | bool support_cooperative_matrix() const; |
| 276 | bool support_cooperative_matrix_8_8_16() const; |
| 277 | bool support_cooperative_matrix_16_8_8() const; |
| 278 | bool support_cooperative_matrix_16_8_16() const; |
| 279 | bool support_cooperative_matrix_16_16_16() const; |
| 280 | |
| 281 | // extension capability |
| 282 | int support_VK_KHR_8bit_storage() const; |
| 283 | int support_VK_KHR_16bit_storage() const; |
| 284 | int support_VK_KHR_bind_memory2() const; |
| 285 | int support_VK_KHR_buffer_device_address() const; |
| 286 | int support_VK_KHR_create_renderpass2() const; |
| 287 | int support_VK_KHR_cooperative_matrix() const; |
| 288 | int support_VK_KHR_dedicated_allocation() const; |
| 289 | int support_VK_KHR_descriptor_update_template() const; |
| 290 | int support_VK_KHR_external_memory() const; |
| 291 | int support_VK_KHR_get_memory_requirements2() const; |
| 292 | int support_VK_KHR_maintenance1() const; |
| 293 | int support_VK_KHR_maintenance2() const; |
| 294 | int support_VK_KHR_maintenance3() const; |
| 295 | int support_VK_KHR_multiview() const; |
| 296 | int support_VK_KHR_portability_subset() const; |
| 297 | int support_VK_KHR_push_descriptor() const; |
| 298 | int support_VK_KHR_sampler_ycbcr_conversion() const; |
| 299 | int support_VK_KHR_shader_float16_int8() const; |
| 300 | int support_VK_KHR_shader_float_controls() const; |
| 301 | int support_VK_KHR_storage_buffer_storage_class() const; |
| 302 | int support_VK_KHR_swapchain() const; |
| 303 | int support_VK_EXT_buffer_device_address() const; |
| 304 | int support_VK_EXT_descriptor_indexing() const; |
| 305 | int support_VK_EXT_memory_budget() const; |
| 306 | int support_VK_EXT_memory_priority() const; |
| 307 | int support_VK_EXT_queue_family_foreign() const; |
| 308 | int support_VK_AMD_device_coherent_memory() const; |
| 309 | #if __ANDROID_API__ >= 26 |
| 310 | int support_VK_ANDROID_external_memory_android_hardware_buffer() const; |
| 311 | #endif // __ANDROID_API__ >= 26 |
| 312 | int support_VK_NV_cooperative_matrix() const; |
| 313 | |
| 314 | private: |
| 315 | GpuInfo(const GpuInfo&); |
| 316 | GpuInfo& operator=(const GpuInfo&); |
| 317 | |
| 318 | private: |
| 319 | friend int create_gpu_instance(const char* driver_path); |
| 320 | GpuInfoPrivate* const d; |
| 321 | }; |
| 322 | |
| 323 | NCNN_EXPORT const GpuInfo& get_gpu_info(int device_index = get_default_gpu_index()); |
| 324 | |
| 325 | class VkAllocator; |
| 326 | class VkCompute; |
| 327 | class Option; |
| 328 | class PipelineCache; |
| 329 | class VulkanDevicePrivate; |
| 330 | class NCNN_EXPORT VulkanDevice |
| 331 | { |
| 332 | public: |
| 333 | VulkanDevice(int device_index = get_default_gpu_index()); |
| 334 | ~VulkanDevice(); |
| 335 | |
| 336 | const GpuInfo& info; |
| 337 | |
| 338 | VkDevice vkdevice() const; |
| 339 | |
| 340 | VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const; |
| 341 | |
| 342 | // with fixed workgroup size |
| 343 | VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size, uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z) const; |
| 344 | |
| 345 | // helper for creating pipeline |
| 346 | int create_descriptorset_layout(int binding_count, const int* binding_types, VkDescriptorSetLayout* descriptorset_layout) const; |
| 347 | int create_pipeline_layout(int push_constant_count, VkDescriptorSetLayout descriptorset_layout, VkPipelineLayout* pipeline_layout) const; |
| 348 | int create_pipeline(VkShaderModule shader_module, VkPipelineLayout pipeline_layout, const std::vector<vk_specialization_type>& specializations, VkPipeline* pipeline) const; |
| 349 | int create_descriptor_update_template(int binding_count, const int* binding_types, VkDescriptorSetLayout descriptorset_layout, VkPipelineLayout pipeline_layout, VkDescriptorUpdateTemplateKHR* descriptor_update_template) const; |
| 350 | |
| 351 | uint32_t find_memory_index(uint32_t memory_type_bits, VkFlags required, VkFlags preferred, VkFlags preferred_not) const; |
| 352 | bool is_mappable(uint32_t memory_type_index) const; |
| 353 | bool is_coherent(uint32_t memory_type_index) const; |
| 354 | |
| 355 | VkQueue acquire_queue(uint32_t queue_family_index) const; |
| 356 | void reclaim_queue(uint32_t queue_family_index, VkQueue queue) const; |
| 357 | |
| 358 | // allocator on this device |
| 359 | VkAllocator* acquire_blob_allocator() const; |
| 360 | void reclaim_blob_allocator(VkAllocator* allocator) const; |
| 361 | |
| 362 | VkAllocator* acquire_staging_allocator() const; |
| 363 | void reclaim_staging_allocator(VkAllocator* allocator) const; |
| 364 | |
| 365 | // immutable sampler for texelfetch |
| 366 | const VkSampler* immutable_texelfetch_sampler() const; |
| 367 | |
| 368 | // dummy buffer image |
| 369 | VkMat get_dummy_buffer() const; |
| 370 | VkImageMat get_dummy_image() const; |
| 371 | VkImageMat get_dummy_image_readonly() const; |
| 372 | |
| 373 | // pipeline cache on this device |
| 374 | const PipelineCache* get_pipeline_cache() const; |
| 375 | |
| 376 | // test image allocation |
| 377 | bool shape_support_image_storage(const Mat& shape) const; |
| 378 | |
| 379 | // current gpu heap memory budget in MB |
| 380 | uint32_t get_heap_budget() const; |
| 381 | |
| 382 | // utility operator |
| 383 | void convert_packing(const VkMat& src, VkMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; |
| 384 | void convert_packing(const VkImageMat& src, VkImageMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; |
| 385 | void convert_packing(const VkMat& src, VkImageMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; |
| 386 | void convert_packing(const VkImageMat& src, VkMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; |
| 387 | |
| 388 | // VK_KHR_bind_memory2 |
| 389 | PFN_vkBindBufferMemory2KHR vkBindBufferMemory2KHR; |
| 390 | PFN_vkBindImageMemory2KHR vkBindImageMemory2KHR; |
| 391 | |
| 392 | // VK_KHR_buffer_device_address |
| 393 | PFN_vkGetBufferDeviceAddressKHR vkGetBufferDeviceAddressKHR; |
| 394 | PFN_vkGetBufferOpaqueCaptureAddressKHR vkGetBufferOpaqueCaptureAddressKHR; |
| 395 | PFN_vkGetDeviceMemoryOpaqueCaptureAddressKHR vkGetDeviceMemoryOpaqueCaptureAddressKHR; |
| 396 | |
| 397 | // VK_KHR_descriptor_update_template |
| 398 | PFN_vkCreateDescriptorUpdateTemplateKHR vkCreateDescriptorUpdateTemplateKHR; |
| 399 | PFN_vkDestroyDescriptorUpdateTemplateKHR vkDestroyDescriptorUpdateTemplateKHR; |
| 400 | PFN_vkUpdateDescriptorSetWithTemplateKHR vkUpdateDescriptorSetWithTemplateKHR; |
| 401 | |
| 402 | // VK_KHR_get_memory_requirements2 |
| 403 | PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2KHR; |
| 404 | PFN_vkGetBufferMemoryRequirements2KHR vkGetBufferMemoryRequirements2KHR; |
| 405 | |
| 406 | // VK_KHR_maintenance1 |
| 407 | PFN_vkTrimCommandPoolKHR vkTrimCommandPoolKHR; |
| 408 | |
| 409 | // VK_KHR_maintenance3 |
| 410 | PFN_vkGetDescriptorSetLayoutSupportKHR vkGetDescriptorSetLayoutSupportKHR; |
| 411 | |
| 412 | // VK_KHR_push_descriptor |
| 413 | PFN_vkCmdPushDescriptorSetWithTemplateKHR vkCmdPushDescriptorSetWithTemplateKHR; |
| 414 | PFN_vkCmdPushDescriptorSetKHR vkCmdPushDescriptorSetKHR; |
| 415 | |
| 416 | // VK_KHR_sampler_ycbcr_conversion |
| 417 | PFN_vkCreateSamplerYcbcrConversionKHR vkCreateSamplerYcbcrConversionKHR; |
| 418 | PFN_vkDestroySamplerYcbcrConversionKHR vkDestroySamplerYcbcrConversionKHR; |
| 419 | |
| 420 | // VK_KHR_swapchain |
| 421 | PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR; |
| 422 | PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR; |
| 423 | PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR; |
| 424 | PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR; |
| 425 | PFN_vkQueuePresentKHR vkQueuePresentKHR; |
| 426 | |
| 427 | // VK_EXT_buffer_device_address |
| 428 | PFN_vkGetBufferDeviceAddressEXT vkGetBufferDeviceAddressEXT; |
| 429 | |
| 430 | #if __ANDROID_API__ >= 26 |
| 431 | // VK_ANDROID_external_memory_android_hardware_buffer |
| 432 | PFN_vkGetAndroidHardwareBufferPropertiesANDROID vkGetAndroidHardwareBufferPropertiesANDROID; |
| 433 | PFN_vkGetMemoryAndroidHardwareBufferANDROID vkGetMemoryAndroidHardwareBufferANDROID; |
| 434 | #endif // __ANDROID_API__ >= 26 |
| 435 | |
| 436 | protected: |
| 437 | // device extension |
| 438 | int init_device_extension(); |
| 439 | |
| 440 | private: |
| 441 | VulkanDevice(const VulkanDevice&); |
| 442 | VulkanDevice& operator=(const VulkanDevice&); |
| 443 | |
| 444 | private: |
| 445 | VulkanDevicePrivate* const d; |
| 446 | }; |
| 447 | |
| 448 | NCNN_EXPORT VulkanDevice* get_gpu_device(int device_index = get_default_gpu_index()); |
| 449 | |
| 450 | // online spirv compilation |
| 451 | NCNN_EXPORT int compile_spirv_module(const char* comp_string, const Option& opt, std::vector<uint32_t>& spirv); |
| 452 | NCNN_EXPORT int compile_spirv_module(const char* comp_data, int comp_data_size, const Option& opt, std::vector<uint32_t>& spirv); |
| 453 | NCNN_EXPORT int compile_spirv_module(int shader_type_index, const Option& opt, std::vector<uint32_t>& spirv); |
| 454 | |
| 455 | // info from spirv |
| 456 | class NCNN_EXPORT ShaderInfo |
| 457 | { |
| 458 | public: |
| 459 | int specialization_count; |
| 460 | int binding_count; |
| 461 | int push_constant_count; |
| 462 | |
| 463 | // 0 = null |
| 464 | // 1 = storage buffer |
| 465 | // 2 = storage image |
| 466 | // 3 = combined image sampler |
| 467 | int binding_types[16]; // 16 is large enough I think ... |
| 468 | |
| 469 | int reserved_0; |
| 470 | int reserved_1; |
| 471 | int reserved_2; |
| 472 | int reserved_3; |
| 473 | }; |
| 474 | |
| 475 | NCNN_EXPORT int resolve_shader_info(const uint32_t* spv_data, size_t spv_data_size, ShaderInfo& shader_info); |
| 476 | |
| 477 | } // namespace ncnn |
| 478 | |
| 479 | #endif // NCNN_VULKAN |
| 480 | |
| 481 | #endif // NCNN_GPU_H |