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 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 /** Platform specific hardware buffer */
125 uintptr_t platformHwBuffer { 0u };
126 };
127
128 /** Low level vk memory access. (Usable only with low level engine use-cases) */
129 struct GpuResourceMemoryVk final {
130 /* Vulkan memory handle */
131 VkDeviceMemory deviceMemory { VK_NULL_HANDLE };
132 /* Offset into deviceMemory object to the beginning of this allocation */
133 VkDeviceSize offset { 0 };
134 /* Size of this allocation */
135 VkDeviceSize size { 0 };
136 /* Null if not mappable */
137 void* mappedData { nullptr };
138
139 /* Memory type */
140 uint32_t memoryTypeIndex { 0 };
141 /* Memory flags */
142 VkMemoryPropertyFlags memoryPropertyFlags { 0 };
143 };
144
145 /** Low level vk buffer access. (Usable only with low level engine use-cases) */
146 struct GpuBufferPlatformDataVk final : public GpuBufferPlatformData {
147 /* Buffer handle */
148 VkBuffer buffer { VK_NULL_HANDLE };
149
150 /* Bindable memory block byte size */
151 uint32_t bindMemoryByteSize { 0u };
152 /* Full byte size of this buffer, i.e. might be 3 x bindMemoryByteSize for dynamic ring buffers.
153 * If no buffering fullByteSize == bindMemoryByteSize.
154 */
155 uint32_t fullByteSize { 0u };
156 /* Current offset with ring buffers (advanced with map), otherwise 0 */
157 uint32_t currentByteOffset { 0u };
158
159 /* Usage flags */
160 VkBufferUsageFlags usage { 0 };
161
162 /* Device address */
163 uint64_t deviceAddress { 0 };
164
165 /* Memory */
166 GpuResourceMemoryVk memory;
167
168 /** Platform specific hardware buffer */
169 uintptr_t platformHwBuffer { 0u };
170 };
171
172 /** Low level vk image access. (Usable only with low level engine use-cases) */
173 struct GpuImagePlatformDataVk final : public GpuImagePlatformData {
174 /* Image handle */
175 VkImage image { VK_NULL_HANDLE };
176 /* Image view */
177 VkImageView imageView { VK_NULL_HANDLE };
178 /* Image view base for mip level 0 and layer 0 for attachments */
179 VkImageView imageViewBase { VK_NULL_HANDLE };
180
181 /* Format */
182 VkFormat format { VK_FORMAT_UNDEFINED };
183 /* Extent */
184 VkExtent3D extent { 0u, 0u, 0u };
185 /* Image type */
186 VkImageType type { VK_IMAGE_TYPE_2D };
187 /* Aspect flags */
188 VkImageAspectFlags aspectFlags { 0 };
189 /* Usage flags */
190 VkImageUsageFlags usage { 0 };
191 /* Sample count flag bits */
192 VkSampleCountFlagBits samples { VK_SAMPLE_COUNT_1_BIT };
193 /* Image tiling */
194 VkImageTiling tiling { VK_IMAGE_TILING_OPTIMAL };
195 /* Mip levels */
196 uint32_t mipLevels { 0u };
197 /* Layer count */
198 uint32_t arrayLayers { 0u };
199
200 /* Memory */
201 GpuResourceMemoryVk memory;
202
203 /** Platform specific hardware buffer */
204 uintptr_t platformHwBuffer { 0u };
205 };
206
207 /** Low level vk sampler access. (Usable only with low level engine use-cases) */
208 struct GpuSamplerPlatformDataVk final : public GpuSamplerPlatformData {
209 /* Sampler handle */
210 VkSampler sampler { VK_NULL_HANDLE };
211 };
212
213 /** Provides interface for low-level access.
214 * Resource access only valid with specific methods in IRenderBackendNode and IRenderDataStore.
215 */
216 class ILowLevelDeviceVk : public ILowLevelDevice {
217 public:
218 virtual const DevicePlatformDataVk& GetPlatformDataVk() const = 0;
219
220 /** Get vulkan buffer. Valid access only during rendering with node and data store methods. */
221 virtual GpuBufferPlatformDataVk GetBuffer(RenderHandle handle) const = 0;
222 /** Get vulkan image. Valid access only during rendering with node and data store methods. */
223 virtual GpuImagePlatformDataVk GetImage(RenderHandle handle) const = 0;
224 /** Get vulkan sampler. Valid access only during rendering with node and data store methods. */
225 virtual GpuSamplerPlatformDataVk GetSampler(RenderHandle handle) const = 0;
226
227 protected:
228 ILowLevelDeviceVk() = default;
229 ~ILowLevelDeviceVk() = default;
230 };
231
232 #endif // RENDER_HAS_VULKAN_BACKEND
233
234 /** Helper for converting between engine and Vulkan handles.
235 * On 32 bit platforms Vulkan handles are uint64_t, but on 64 bit platforms they are pointers. For the engine handles
236 * are always stored as uint64_t regardless of the platform. This helper selects the correct cast for the conversion.
237 * @param handle Handle to convert.
238 * @return Handle cast to the desired type.
239 */
240 template<typename OutHandle, typename InHandle>
VulkanHandleCast(InHandle handle)241 inline OutHandle VulkanHandleCast(InHandle handle)
242 {
243 // based on current use-cases we could assert that is_pointer_v<OutHandle> != is_pointer_v<InHandle> and not cover
244 // the last two cases.
245 if constexpr (BASE_NS::is_same_v<OutHandle, InHandle>) {
246 // engine<->vulkan, on 32 bit platforms
247 return handle;
248 } else if constexpr (BASE_NS::is_pointer_v<OutHandle> && !BASE_NS::is_pointer_v<InHandle>) {
249 // engine -> vulkan, on 64 bit platforms
250 return reinterpret_cast<OutHandle>(static_cast<uintptr_t>(handle));
251 } else if constexpr (!BASE_NS::is_pointer_v<OutHandle> && BASE_NS::is_pointer_v<InHandle>) {
252 // engine <- vulkan, on 64 bit platforms
253 return reinterpret_cast<OutHandle>(handle);
254 } else if constexpr (BASE_NS::is_pointer_v<OutHandle> && BASE_NS::is_pointer_v<InHandle>) {
255 return reinterpret_cast<OutHandle>(handle);
256 } else {
257 return static_cast<OutHandle>(handle);
258 }
259 }
260 /** @} */
261 RENDER_END_NAMESPACE()
262
263 #endif // API_RENDER_VULKAN_IDEVICE_VK_H
264