1 /*
2  * Copyright (c) 2024 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 
31 struct InstanceWrapper {
32     VkInstance instance { VK_NULL_HANDLE };
33     bool debugReportSupported { false };
34     bool debugUtilsSupported { false };
35     uint32_t apiMajor { 0u };
36     uint32_t apiMinor { 0u };
37 };
38 
39 struct PhysicalDeviceWrapper {
40     VkPhysicalDevice physicalDevice { VK_NULL_HANDLE };
41     BASE_NS::vector<VkExtensionProperties> physicalDeviceExtensions;
42     PhysicalDevicePropertiesVk physicalDeviceProperties;
43 };
44 
45 struct DeviceWrapper {
46     VkDevice device { VK_NULL_HANDLE };
47     BASE_NS::vector<BASE_NS::string> extensions;
48 };
49 
50 struct QueueProperties {
51     VkQueueFlags requiredFlags { 0 };
52     uint32_t count { 0 };
53     float priority { 1.0f };
54     bool explicitFlags { false };
55     bool canPresent { false };
56 };
57 
58 class CreateFunctionsVk {
59 public:
60     struct Window {
61         // Win: hinstance
62         // Linux: connection
63         // Mac: display
64         uintptr_t instance;
65         uintptr_t window;
66     };
67 
68     static InstanceWrapper CreateInstance(const VersionInfo& engineInfo, const VersionInfo& appInfo);
69     static InstanceWrapper GetWrapper(VkInstance instance);
70     static void DestroyInstance(VkInstance instance);
71 
72     static VkDebugReportCallbackEXT CreateDebugCallback(
73         VkInstance instance, PFN_vkDebugReportCallbackEXT callbackFunction);
74     static void DestroyDebugCallback(VkInstance instance, VkDebugReportCallbackEXT debugReport);
75 
76     static VkDebugUtilsMessengerEXT CreateDebugMessenger(
77         VkInstance instance, PFN_vkDebugUtilsMessengerCallbackEXT callbackFunction);
78     static void DestroyDebugMessenger(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger);
79 
80     static PhysicalDeviceWrapper CreatePhysicalDevice(VkInstance instance, QueueProperties const& queueProperties);
81     static PhysicalDeviceWrapper GetWrapper(VkPhysicalDevice physicalDevice);
82     static bool HasExtension(
83         BASE_NS::array_view<const VkExtensionProperties> physicalDeviceExtensions, BASE_NS::string_view extension);
84 
85     static DeviceWrapper CreateDevice(VkInstance instance, VkPhysicalDevice physicalDevice,
86         const BASE_NS::vector<VkExtensionProperties>& physicalDeviceExtensions,
87         const VkPhysicalDeviceFeatures& featuresToEnable, const VkPhysicalDeviceFeatures2* physicalDeviceFeatures2,
88         const BASE_NS::vector<LowLevelQueueInfo>& availableQueues,
89         const BASE_NS::vector<BASE_NS::string_view>& preferredDeviceExtensions);
90     static void DestroyDevice(VkDevice device);
91 
92     static VkSurfaceKHR CreateSurface(VkInstance instance, Window const& nativeWindow);
93     static void DestroySurface(VkInstance instance, VkSurfaceKHR surface);
94 
95     static void DestroySwapchain(VkDevice device, VkSwapchainKHR swapchain);
96 
97     static BASE_NS::vector<LowLevelQueueInfo> GetAvailableQueues(
98         VkPhysicalDevice physicalDevice, const BASE_NS::vector<QueueProperties>& queueProperties);
99 
100     static VkPipelineCache CreatePipelineCache(VkDevice device, BASE_NS::array_view<const uint8_t> initialData);
101     static void DestroyPipelineCache(VkDevice device, VkPipelineCache cache);
102 };
103 RENDER_END_NAMESPACE()
104 
105 #endif // VULKAN_CREATE_FUNCTIONS_VK_H
106