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 API_RENDER_VULKAN_IDEVICE_VK_H
17 #define API_RENDER_VULKAN_IDEVICE_VK_H
18
19 #include <cstdint>
20
21 #include <base/containers/string_view.h>
22 #include <base/containers/vector.h>
23 #include <render/device/intf_device.h>
24 #include <render/device/intf_gpu_resource_manager.h>
25 #include <render/namespace.h>
26 #include <render/resource_handle.h>
27
28 #if RENDER_HAS_VULKAN_BACKEND
29 // if intf_device_vk.h is included by the app or plugin, vulkan needs to be located
30 #include <vulkan/vulkan_core.h>
31 #endif
32
33 // Platform / Backend specific typedefs.
34 RENDER_BEGIN_NAMESPACE()
35 /** \addtogroup group_gfx_vulkan_idevicevk
36 * @{
37 */
38 #if RENDER_HAS_VULKAN_BACKEND || DOXYGEN
39 /** Backend extra vulkan */
40 struct BackendExtraVk final : public BackendExtra {
41 /* Enable multiple gpu queues for usage */
42 bool enableMultiQueue { false };
43
44 VkInstance instance { VK_NULL_HANDLE };
45 VkPhysicalDevice physicalDevice { VK_NULL_HANDLE };
46 VkDevice device { VK_NULL_HANDLE };
47
48 struct DeviceExtensions {
49 BASE_NS::vector<BASE_NS::string_view> extensionNames;
50 VkPhysicalDeviceFeatures2* physicalDeviceFeaturesToEnable { nullptr };
51 };
52 /* Additional extensions */
53 DeviceExtensions extensions;
54
55 struct GpuMemoryAllocatorSizes {
56 /* Set default allocation block size in bytes, used if the value is not ~0u */
57 uint32_t defaultAllocationBlockSize { ~0u };
58 /* Set custom dynamic (ring buffer) UBO allocation block size in bytes , used if the value is not ~0u */
59 uint32_t customAllocationDynamicUboBlockSize { ~0u };
60 };
61 /* Memory sizes might not be used if the sizes are not valid/sane */
62 GpuMemoryAllocatorSizes gpuMemoryAllocatorSizes;
63 };
64
65 /** Physical device properties vulkan */
66 struct PhysicalDevicePropertiesVk final {
67 /** Physical device properties */
68 VkPhysicalDeviceProperties physicalDeviceProperties;
69 /** Physical device features */
70 VkPhysicalDeviceFeatures physicalDeviceFeatures;
71 /** Physical device memory properties */
72 VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties;
73 };
74
75 /** Device platform data vulkan */
76 struct DevicePlatformDataVk final : DevicePlatformData {
77 /** Instance */
78 VkInstance instance { VK_NULL_HANDLE };
79 /** Physical device */
80 VkPhysicalDevice physicalDevice { VK_NULL_HANDLE };
81 /** Device */
82 VkDevice device { VK_NULL_HANDLE };
83 /** Physical device properties */
84 PhysicalDevicePropertiesVk physicalDeviceProperties {};
85 /** Available physical device extensions */
86 BASE_NS::vector<VkExtensionProperties> physicalDeviceExtensions {};
87
88 /** Enabled physical device features */
89 VkPhysicalDeviceFeatures enabledPhysicalDeviceFeatures {};
90
91 uint32_t deviceApiMajor { 0u };
92 uint32_t deviceApiMinor { 0u };
93
94 VkPipelineCache pipelineCache { VK_NULL_HANDLE };
95 };
96
97 struct RenderBackendRecordingStateVk final : public RenderBackendRecordingState {
98 VkCommandBuffer commandBuffer { VK_NULL_HANDLE };
99
100 VkRenderPass renderPass { VK_NULL_HANDLE };
101 VkFramebuffer framebuffer { VK_NULL_HANDLE };
102 VkExtent2D framebufferSize { 0, 0 };
103 uint32_t subpassIndex { 0u };
104
105 VkPipelineLayout pipelineLayout { VK_NULL_HANDLE };
106 };
107
108 /** Image desc vulkan for creating engine GpuImage based on given data */
109 struct ImageDescVk final : BackendSpecificImageDesc {
110 /** Image */
111 VkImage image { VK_NULL_HANDLE };
112 /** Image view */
113 VkImageView imageView { VK_NULL_HANDLE };
114
115 /** Platform specific hardware buffer */
116 uintptr_t platformHwBuffer { 0u };
117 };
118
119 /** Buffer descriptor vulkan for creating engine GpuBuffer based on given data */
120 struct BufferDescVk : BackendSpecificBufferDesc {
121 /** Buffer */
122 VkBuffer buffer { VK_NULL_HANDLE };
123 };
124
125 /** Low level vk memory access. (Usable only with low level engine use-cases) */
126 struct GpuResourceMemoryVk final {
127 /* Vulkan memory handle */
128 VkDeviceMemory deviceMemory { VK_NULL_HANDLE };
129 /* Offset into deviceMemory object to the beginning of this allocation */
130 VkDeviceSize offset { 0 };
131 /* Size of this allocation */
132 VkDeviceSize size { 0 };
133 /* Null if not mappable */
134 void* mappedData { nullptr };
135
136 /* Memory type */
137 uint32_t memoryTypeIndex { 0 };
138 /* Memory flags */
139 VkMemoryPropertyFlags memoryPropertyFlags { 0 };
140 };
141
142 /** Low level vk buffer access. (Usable only with low level engine use-cases) */
143 struct GpuBufferPlatformDataVk final : public GpuBufferPlatformData {
144 /* Buffer handle */
145 VkBuffer buffer { VK_NULL_HANDLE };
146
147 /* Bindable memory block byte size */
148 uint32_t bindMemoryByteSize { 0u };
149 /* Full byte size of this buffer, i.e. might be 3 x bindMemoryByteSize for dynamic ring buffers.
150 * If no buffering fullByteSize == bindMemoryByteSize.
151 */
152 uint32_t fullByteSize { 0u };
153 /* Current offset with ring buffers (advanced with map), otherwise 0 */
154 uint32_t currentByteOffset { 0u };
155
156 /* Usage flags */
157 VkBufferUsageFlags usage { 0 };
158
159 /* Memory */
160 GpuResourceMemoryVk memory;
161 };
162
163 /** Low level vk image access. (Usable only with low level engine use-cases) */
164 struct GpuImagePlatformDataVk final : public GpuImagePlatformData {
165 /* Image handle */
166 VkImage image { VK_NULL_HANDLE };
167 /* Image view */
168 VkImageView imageView { VK_NULL_HANDLE };
169 /* Image view base for mip level 0 and layer 0 for attachments */
170 VkImageView imageViewBase { VK_NULL_HANDLE };
171
172 /* Format */
173 VkFormat format { VK_FORMAT_UNDEFINED };
174 /* Extent */
175 VkExtent3D extent { 0u, 0u, 0u };
176 /* Image type */
177 VkImageType type { VK_IMAGE_TYPE_2D };
178 /* Aspect flags */
179 VkImageAspectFlags aspectFlags { 0 };
180 /* Usage flags */
181 VkImageUsageFlags usage { 0 };
182 /* Sample count flag bits */
183 VkSampleCountFlagBits samples { VK_SAMPLE_COUNT_1_BIT };
184 /* Image tiling */
185 VkImageTiling tiling { VK_IMAGE_TILING_OPTIMAL };
186 /* Mip levels */
187 uint32_t mipLevels { 0u };
188 /* Layer count */
189 uint32_t arrayLayers { 0u };
190
191 /* Memory */
192 GpuResourceMemoryVk memory;
193 };
194
195 /** Low level vk sampler access. (Usable only with low level engine use-cases) */
196 struct GpuSamplerPlatformDataVk final : public GpuSamplerPlatformData {
197 /* Sampler handle */
198 VkSampler sampler { VK_NULL_HANDLE };
199 };
200
201 /** Provides interface for low-level access.
202 * Resource access only valid with specific methods in IRenderBackendNode and IRenderDataStore.
203 */
204 class ILowLevelDeviceVk : public ILowLevelDevice {
205 public:
206 virtual const DevicePlatformDataVk& GetPlatformDataVk() const = 0;
207
208 /** Get vulkan buffer. Valid access only during rendering with node and data store methods. */
209 virtual GpuBufferPlatformDataVk GetBuffer(RenderHandle handle) const = 0;
210 /** Get vulkan image. Valid access only during rendering with node and data store methods. */
211 virtual GpuImagePlatformDataVk GetImage(RenderHandle handle) const = 0;
212 /** Get vulkan sampler. Valid access only during rendering with node and data store methods. */
213 virtual GpuSamplerPlatformDataVk GetSampler(RenderHandle handle) const = 0;
214
215 protected:
216 ILowLevelDeviceVk() = default;
217 ~ILowLevelDeviceVk() = default;
218 };
219
220 #endif // RENDER_HAS_VULKAN_BACKEND
221
222 /** Helper for converting between engine and Vulkan handles.
223 * On 32 bit platforms Vulkan handles are uint64_t, but on 64 bit platforms they are pointers. For the engine handles
224 * are always stored as uint64_t regardless of the platform. This helper selects the correct cast for the conversion.
225 * @param handle Handle to convert.
226 * @return Handle cast to the desired type.
227 */
228 template<typename OutHandle, typename InHandle>
VulkanHandleCast(InHandle handle)229 inline OutHandle VulkanHandleCast(InHandle handle)
230 {
231 // based on current use-cases we could assert that is_pointer_v<OutHandle> != is_pointer_v<InHandle> and not cover
232 // the last two cases.
233 if constexpr (BASE_NS::is_same_v<OutHandle, InHandle>) {
234 // engine<->vulkan, on 32 bit platforms
235 return handle;
236 } else if constexpr (BASE_NS::is_pointer_v<OutHandle> && !BASE_NS::is_pointer_v<InHandle>) {
237 // engine -> vulkan, on 64 bit platforms
238 return reinterpret_cast<OutHandle>(static_cast<uintptr_t>(handle));
239 } else if constexpr (!BASE_NS::is_pointer_v<OutHandle> && BASE_NS::is_pointer_v<InHandle>) {
240 // engine <- vulkan, on 64 bit platforms
241 return reinterpret_cast<OutHandle>(handle);
242 } else if constexpr (BASE_NS::is_pointer_v<OutHandle> && BASE_NS::is_pointer_v<InHandle>) {
243 return reinterpret_cast<OutHandle>(handle);
244 } else {
245 return static_cast<OutHandle>(handle);
246 }
247 }
248 /** @} */
249 RENDER_END_NAMESPACE()
250
251 #endif // API_RENDER_VULKAN_IDEVICE_VK_H
252