1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef VULKAN_CREATE_FUNCTIONS_VK_H 17 #define VULKAN_CREATE_FUNCTIONS_VK_H 18 19 #include <cstdint> 20 #include <vulkan/vulkan_core.h> 21 22 #include <base/containers/string.h> 23 #include <base/containers/vector.h> 24 #include <render/namespace.h> 25 #include <render/vulkan/intf_device_vk.h> 26 27 RENDER_BEGIN_NAMESPACE() 28 struct LowLevelQueueInfo; 29 struct VersionInfo; 30 class RenderContext; 31 32 struct InstanceWrapper { 33 VkInstance instance { VK_NULL_HANDLE }; 34 bool debugReportSupported { false }; 35 bool debugUtilsSupported { false }; 36 uint32_t apiMajor { 0u }; 37 uint32_t apiMinor { 0u }; 38 }; 39 40 struct PhysicalDeviceWrapper { 41 VkPhysicalDevice physicalDevice { VK_NULL_HANDLE }; 42 BASE_NS::vector<VkExtensionProperties> physicalDeviceExtensions; 43 PhysicalDevicePropertiesVk physicalDeviceProperties; 44 }; 45 46 struct DeviceWrapper { 47 VkDevice device { VK_NULL_HANDLE }; 48 BASE_NS::vector<BASE_NS::string> extensions; 49 }; 50 51 struct QueueProperties { 52 VkQueueFlags requiredFlags { 0 }; 53 uint32_t count { 0 }; 54 float priority { 1.0f }; 55 bool explicitFlags { false }; 56 bool canPresent { false }; 57 }; 58 59 class CreateFunctionsVk { 60 public: 61 struct Window { 62 // Win: hinstance 63 // Linux: connection 64 // Mac: display 65 uintptr_t instance; 66 uintptr_t window; 67 }; 68 69 static InstanceWrapper CreateInstance(const VersionInfo& engineInfo, const VersionInfo& appInfo); 70 static InstanceWrapper GetWrapper(VkInstance instance); 71 static void DestroyInstance(VkInstance instance); 72 73 static VkDebugReportCallbackEXT CreateDebugCallback( 74 VkInstance instance, PFN_vkDebugReportCallbackEXT callbackFunction); 75 static void DestroyDebugCallback(VkInstance instance, VkDebugReportCallbackEXT debugReport); 76 77 static VkDebugUtilsMessengerEXT CreateDebugMessenger( 78 VkInstance instance, PFN_vkDebugUtilsMessengerCallbackEXT callbackFunction, RenderContext* renderContext); 79 static void DestroyDebugMessenger(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger); 80 81 static PhysicalDeviceWrapper CreatePhysicalDevice(VkInstance instance, QueueProperties const& queueProperties); 82 static PhysicalDeviceWrapper GetWrapper(VkPhysicalDevice physicalDevice); 83 static bool HasExtension( 84 BASE_NS::array_view<const VkExtensionProperties> physicalDeviceExtensions, BASE_NS::string_view extension); 85 86 static DeviceWrapper CreateDevice(VkInstance instance, VkPhysicalDevice physicalDevice, 87 const BASE_NS::vector<VkExtensionProperties>& physicalDeviceExtensions, 88 const VkPhysicalDeviceFeatures& featuresToEnable, const VkPhysicalDeviceFeatures2* physicalDeviceFeatures2, 89 const BASE_NS::vector<LowLevelQueueInfo>& availableQueues, 90 const BASE_NS::vector<BASE_NS::string_view>& preferredDeviceExtensions); 91 static void DestroyDevice(VkDevice device); 92 93 static VkSurfaceKHR CreateSurface(VkInstance instance, Window const& nativeWindow); 94 static void DestroySurface(VkInstance instance, VkSurfaceKHR surface); 95 96 static void DestroySwapchain(VkDevice device, VkSwapchainKHR swapchain); 97 98 static BASE_NS::vector<LowLevelQueueInfo> GetAvailableQueues( 99 VkPhysicalDevice physicalDevice, const BASE_NS::vector<QueueProperties>& queueProperties); 100 101 static VkPipelineCache CreatePipelineCache(VkDevice device, BASE_NS::array_view<const uint8_t> initialData); 102 static void DestroyPipelineCache(VkDevice device, VkPipelineCache cache); 103 }; 104 RENDER_END_NAMESPACE() 105 106 #endif // VULKAN_CREATE_FUNCTIONS_VK_H 107