| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qrhivulkan_p.h" |
| 5 | #include <qpa/qplatformvulkaninstance.h> |
| 6 | |
| 7 | #define VMA_IMPLEMENTATION |
| 8 | #define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 |
| 9 | #define VMA_STATIC_VULKAN_FUNCTIONS 0 |
| 10 | #define VMA_RECORDING_ENABLED 0 |
| 11 | #define VMA_DEDICATED_ALLOCATION 0 |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | Q_STATIC_LOGGING_CATEGORY(QRHI_LOG_VMA, "qt.rhi.vma" ) |
| 14 | QT_END_NAMESPACE |
| 15 | #define VMA_ASSERT(expr) Q_ASSERT(expr) |
| 16 | #ifdef QT_DEBUG |
| 17 | #define VMA_DEBUG_INITIALIZE_ALLOCATIONS 1 |
| 18 | #define VMA_DEBUG_LOG(str) QT_PREPEND_NAMESPACE(qDebug)(QT_PREPEND_NAMESPACE(QRHI_LOG_VMA), (str)) |
| 19 | #define VMA_DEBUG_LOG_FORMAT(format, ...) QT_PREPEND_NAMESPACE(qDebug)(QT_PREPEND_NAMESPACE(QRHI_LOG_VMA), format, __VA_ARGS__) |
| 20 | #endif |
| 21 | template<typename... Args> |
| 22 | static void debugVmaLeak(const char *format, Args&&... args) |
| 23 | { |
| 24 | #ifndef QT_NO_DEBUG |
| 25 | // debug builds: just do it always |
| 26 | static bool leakCheck = true; |
| 27 | #else |
| 28 | // release builds: opt-in |
| 29 | static bool leakCheck = QT_PREPEND_NAMESPACE(qEnvironmentVariableIntValue)("QT_RHI_LEAK_CHECK" ); |
| 30 | #endif |
| 31 | if (leakCheck) |
| 32 | QT_PREPEND_NAMESPACE(qWarning)(QT_PREPEND_NAMESPACE(QRHI_LOG_VMA), format, std::forward<Args>(args)...); |
| 33 | } |
| 34 | #define VMA_LEAK_LOG_FORMAT(format, ...) debugVmaLeak(format, __VA_ARGS__) |
| 35 | QT_WARNING_PUSH |
| 36 | QT_WARNING_DISABLE_GCC("-Wsuggest-override" ) |
| 37 | QT_WARNING_DISABLE_GCC("-Wundef" ) |
| 38 | QT_WARNING_DISABLE_CLANG("-Wundef" ) |
| 39 | #if defined(Q_CC_CLANG) && Q_CC_CLANG >= 1100 |
| 40 | QT_WARNING_DISABLE_CLANG("-Wdeprecated-copy" ) |
| 41 | #endif |
| 42 | #include "vk_mem_alloc.h" |
| 43 | QT_WARNING_POP |
| 44 | |
| 45 | #include <qmath.h> |
| 46 | #include <QVulkanFunctions> |
| 47 | #include <QtGui/qwindow.h> |
| 48 | #include <private/qvulkandefaultinstance_p.h> |
| 49 | #include <optional> |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | /* |
| 54 | Vulkan 1.0 backend. Provides a double-buffered swapchain that throttles the |
| 55 | rendering thread to vsync. Textures and "static" buffers are device local, |
| 56 | and a separate, host visible staging buffer is used to upload data to them. |
| 57 | "Dynamic" buffers are in host visible memory and are duplicated (since there |
| 58 | can be 2 frames in flight). This is handled transparently to the application. |
| 59 | |
| 60 | Barriers are generated automatically for each render or compute pass, based |
| 61 | on the resources that are used in that pass (in QRhiShaderResourceBindings, |
| 62 | vertex inputs, etc.). This implies deferring the recording of the command |
| 63 | buffer since the barriers have to be placed at the right place (before the |
| 64 | pass), and that can only be done once we know all the things the pass does. |
| 65 | |
| 66 | This in turn has implications for integrating external commands |
| 67 | (beginExternal() - direct Vulkan calls - endExternal()) because that is |
| 68 | incompatible with this approach by nature. Therefore we support another mode |
| 69 | of operation, where each render or compute pass uses one or more secondary |
| 70 | command buffers (recorded right away), with each beginExternal() leading to |
| 71 | closing the current secondary cb, creating a new secondary cb for the |
| 72 | external content, and then starting yet another one in endExternal() for |
| 73 | whatever comes afterwards in the pass. This way the primary command buffer |
| 74 | only has vkCmdExecuteCommand(s) within a renderpass instance |
| 75 | (Begin-EndRenderPass). (i.e. our only subpass is then |
| 76 | VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS instead of |
| 77 | VK_SUBPASS_CONTENTS_INLINE) |
| 78 | |
| 79 | The command buffer management mode is decided on a per frame basis, |
| 80 | controlled by the ExternalContentsInPass flag of beginFrame(). |
| 81 | */ |
| 82 | |
| 83 | /*! |
| 84 | \class QRhiVulkanInitParams |
| 85 | \inmodule QtGuiPrivate |
| 86 | \inheaderfile rhi/qrhi.h |
| 87 | \since 6.6 |
| 88 | \brief Vulkan specific initialization parameters. |
| 89 | |
| 90 | \note This is a RHI API with limited compatibility guarantees, see \l QRhi |
| 91 | for details. |
| 92 | |
| 93 | A Vulkan-based QRhi needs at minimum a valid QVulkanInstance. It is up to |
| 94 | the user to ensure this is available and initialized. This is typically |
| 95 | done in main() similarly to the following: |
| 96 | |
| 97 | \badcode |
| 98 | int main(int argc, char **argv) |
| 99 | { |
| 100 | ... |
| 101 | |
| 102 | QVulkanInstance inst; |
| 103 | inst.setLayers({ "VK_LAYER_KHRONOS_validation" }); // for debugging only, not for release builds |
| 104 | inst.setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions()); |
| 105 | if (!inst.create()) |
| 106 | qFatal("Vulkan not available"); |
| 107 | |
| 108 | ... |
| 109 | } |
| 110 | \endcode |
| 111 | |
| 112 | This example enables the |
| 113 | \l{https://github.com/KhronosGroup/Vulkan-ValidationLayers}{Vulkan |
| 114 | validation layers}, when they are available, and also enables the |
| 115 | instance-level extensions QRhi reports as desirable (such as, |
| 116 | VK_KHR_get_physical_device_properties2), as long as they are supported by |
| 117 | the Vulkan implementation at run time. |
| 118 | |
| 119 | The former is optional, and is useful during the development phase |
| 120 | QVulkanInstance conveniently redirects messages and warnings to qDebug. |
| 121 | Avoid enabling it in production builds, however. The latter is strongly |
| 122 | recommended, and is important in order to make certain features functional |
| 123 | (for example, QRhi::CustomInstanceStepRate). |
| 124 | |
| 125 | Once this is done, a Vulkan-based QRhi can be created by passing the |
| 126 | instance and a QWindow with its surface type set to |
| 127 | QSurface::VulkanSurface: |
| 128 | |
| 129 | \badcode |
| 130 | QRhiVulkanInitParams params; |
| 131 | params.inst = vulkanInstance; |
| 132 | params.window = window; |
| 133 | rhi = QRhi::create(QRhi::Vulkan, ¶ms); |
| 134 | \endcode |
| 135 | |
| 136 | The window is optional and can be omitted. This is not recommended however |
| 137 | because there is then no way to ensure presenting is supported while |
| 138 | choosing a graphics queue. |
| 139 | |
| 140 | \note Even when a window is specified, QRhiSwapChain objects can be created |
| 141 | for other windows as well, as long as they all have their |
| 142 | QWindow::surfaceType() set to QSurface::VulkanSurface. |
| 143 | |
| 144 | To request additional extensions to be enabled on the Vulkan device, list them |
| 145 | in deviceExtensions. This can be relevant when integrating with native Vulkan |
| 146 | rendering code. |
| 147 | |
| 148 | It is expected that the backend's desired list of instance extensions will |
| 149 | be queried by calling the static function preferredInstanceExtensions() |
| 150 | before initializing a QVulkanInstance. The returned list can be safely |
| 151 | passed to QVulkanInstance::setExtensions() as-is, because unsupported |
| 152 | extensions are filtered out automatically. If this is not done, certain |
| 153 | features, such as QRhi::CustomInstanceStepRate may be reported as |
| 154 | unsupported even when the Vulkan implementation on the system has support |
| 155 | for the relevant functionality. |
| 156 | |
| 157 | For full functionality the QVulkanInstance needs to have API 1.1 enabled, |
| 158 | when available. This means calling QVulkanInstance::setApiVersion() with |
| 159 | 1.1 or higher whenever QVulkanInstance::supportedApiVersion() reports that |
| 160 | at least Vulkan 1.1 is supported. If this is not done, certain features, |
| 161 | such as QRhi::RenderTo3DTextureSlice may be reported as unsupported even |
| 162 | when the Vulkan implementation on the system supports Vulkan 1.1 or newer. |
| 163 | |
| 164 | \section2 Working with existing Vulkan devices |
| 165 | |
| 166 | When interoperating with another graphics engine, it may be necessary to |
| 167 | get a QRhi instance that uses the same Vulkan device. This can be achieved |
| 168 | by passing a pointer to a QRhiVulkanNativeHandles to QRhi::create(). |
| 169 | |
| 170 | The physical device must always be set to a non-null value. If the |
| 171 | intention is to just specify a physical device, but leave the rest of the |
| 172 | VkDevice and queue creation to QRhi, then no other members need to be |
| 173 | filled out in the struct. For example, this is the case when working with |
| 174 | OpenXR. |
| 175 | |
| 176 | To adopt an existing \c VkDevice, the device field must be set to a |
| 177 | non-null value as well. In addition, the graphics queue family index is |
| 178 | required. The queue index is optional, as the default of 0 is often |
| 179 | suitable. |
| 180 | |
| 181 | Optionally, an existing command pool object can be specified as well. Also |
| 182 | optionally, vmemAllocator can be used to share the same |
| 183 | \l{https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator}{Vulkan |
| 184 | memory allocator} between two QRhi instances. |
| 185 | |
| 186 | The QRhi does not take ownership of any of the external objects. |
| 187 | |
| 188 | Applications are encouraged to query the list of desired device extensions |
| 189 | by calling the static function preferredExtensionsForImportedDevice(), and |
| 190 | enable them on the VkDevice. Otherwise certain QRhi features may not be |
| 191 | available. |
| 192 | */ |
| 193 | |
| 194 | /*! |
| 195 | \variable QRhiVulkanInitParams::inst |
| 196 | |
| 197 | The QVulkanInstance that has already been successfully |
| 198 | \l{QVulkanInstance::create()}{created}, required. |
| 199 | */ |
| 200 | |
| 201 | /*! |
| 202 | \variable QRhiVulkanInitParams::window |
| 203 | |
| 204 | Optional, but recommended when targeting a QWindow. |
| 205 | */ |
| 206 | |
| 207 | /*! |
| 208 | \variable QRhiVulkanInitParams::deviceExtensions |
| 209 | |
| 210 | Optional, empty by default. The list of Vulkan device extensions to enable. |
| 211 | Unsupported extensions are ignored gracefully. |
| 212 | */ |
| 213 | |
| 214 | /*! |
| 215 | \class QRhiVulkanNativeHandles |
| 216 | \inmodule QtGuiPrivate |
| 217 | \inheaderfile rhi/qrhi.h |
| 218 | \since 6.6 |
| 219 | \brief Collects device, queue, and other Vulkan objects that are used by the QRhi. |
| 220 | |
| 221 | \note Ownership of the Vulkan objects is never transferred. |
| 222 | |
| 223 | \note This is a RHI API with limited compatibility guarantees, see \l QRhi |
| 224 | for details. |
| 225 | */ |
| 226 | |
| 227 | /*! |
| 228 | \variable QRhiVulkanNativeHandles::physDev |
| 229 | |
| 230 | When different from \nullptr, specifies the Vulkan physical device to use. |
| 231 | */ |
| 232 | |
| 233 | /*! |
| 234 | \variable QRhiVulkanNativeHandles::dev |
| 235 | |
| 236 | When wanting to import not just a physical device, but also use an already |
| 237 | existing VkDevice, set this and the graphics queue index and family index. |
| 238 | */ |
| 239 | |
| 240 | /*! |
| 241 | \variable QRhiVulkanNativeHandles::gfxQueueFamilyIdx |
| 242 | |
| 243 | Graphics queue family index. |
| 244 | */ |
| 245 | |
| 246 | /*! |
| 247 | \variable QRhiVulkanNativeHandles::gfxQueueIdx |
| 248 | |
| 249 | Graphics queue index. |
| 250 | */ |
| 251 | |
| 252 | /*! |
| 253 | \variable QRhiVulkanNativeHandles::vmemAllocator |
| 254 | |
| 255 | Relevant only when importing an existing memory allocator object, |
| 256 | leave it set to \nullptr otherwise. |
| 257 | */ |
| 258 | |
| 259 | /*! |
| 260 | \variable QRhiVulkanNativeHandles::gfxQueue |
| 261 | |
| 262 | Output only, not used by QRhi::create(), only set by the |
| 263 | QRhi::nativeHandles() accessor. The graphics VkQueue used by the QRhi. |
| 264 | */ |
| 265 | |
| 266 | /*! |
| 267 | \variable QRhiVulkanNativeHandles::inst |
| 268 | |
| 269 | Output only, not used by QRhi::create(), only set by the |
| 270 | QRhi::nativeHandles() accessor. The QVulkanInstance used by the QRhi. |
| 271 | */ |
| 272 | |
| 273 | /*! |
| 274 | \class QRhiVulkanCommandBufferNativeHandles |
| 275 | \inmodule QtGuiPrivate |
| 276 | \inheaderfile rhi/qrhi.h |
| 277 | \since 6.6 |
| 278 | \brief Holds the Vulkan command buffer object that is backing a QRhiCommandBuffer. |
| 279 | |
| 280 | \note The Vulkan command buffer object is only guaranteed to be valid, and |
| 281 | in recording state, while recording a frame. That is, between a |
| 282 | \l{QRhi::beginFrame()}{beginFrame()} - \l{QRhi::endFrame()}{endFrame()} or |
| 283 | \l{QRhi::beginOffscreenFrame()}{beginOffscreenFrame()} - |
| 284 | \l{QRhi::endOffscreenFrame()}{endOffscreenFrame()} pair. |
| 285 | |
| 286 | \note This is a RHI API with limited compatibility guarantees, see \l QRhi |
| 287 | for details. |
| 288 | */ |
| 289 | |
| 290 | /*! |
| 291 | \variable QRhiVulkanCommandBufferNativeHandles::commandBuffer |
| 292 | |
| 293 | The VkCommandBuffer object. |
| 294 | */ |
| 295 | |
| 296 | /*! |
| 297 | \class QRhiVulkanRenderPassNativeHandles |
| 298 | \inmodule QtGuiPrivate |
| 299 | \inheaderfile rhi/qrhi.h |
| 300 | \since 6.6 |
| 301 | \brief Holds the Vulkan render pass object backing a QRhiRenderPassDescriptor. |
| 302 | |
| 303 | \note This is a RHI API with limited compatibility guarantees, see \l QRhi |
| 304 | for details. |
| 305 | */ |
| 306 | |
| 307 | /*! |
| 308 | \variable QRhiVulkanRenderPassNativeHandles::renderPass |
| 309 | |
| 310 | The VkRenderPass object. |
| 311 | */ |
| 312 | |
| 313 | /*! |
| 314 | \class QRhiVulkanQueueSubmitParams |
| 315 | \inmodule QtGui |
| 316 | \since 6.9 |
| 317 | \brief References additional Vulkan API objects that get passed to \c vkQueueSubmit(). |
| 318 | |
| 319 | \note This is a RHI API with limited compatibility guarantees, see \l QRhi |
| 320 | for details. |
| 321 | */ |
| 322 | |
| 323 | /*! |
| 324 | \variable QRhiVulkanQueueSubmitParams::waitSemaphoreCount |
| 325 | |
| 326 | See |
| 327 | \l{https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkSubmitInfo.html}{VkSubmitInfo} |
| 328 | for details. |
| 329 | */ |
| 330 | |
| 331 | /*! |
| 332 | \variable QRhiVulkanQueueSubmitParams::waitSemaphores |
| 333 | |
| 334 | See |
| 335 | \l{https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkSubmitInfo.html}{VkSubmitInfo} |
| 336 | for details. |
| 337 | */ |
| 338 | |
| 339 | /*! |
| 340 | \variable QRhiVulkanQueueSubmitParams::signalSemaphoreCount |
| 341 | |
| 342 | See |
| 343 | \l{https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkSubmitInfo.html}{VkSubmitInfo} |
| 344 | for details. |
| 345 | */ |
| 346 | |
| 347 | /*! |
| 348 | \variable QRhiVulkanQueueSubmitParams::signalSemaphores |
| 349 | |
| 350 | See |
| 351 | \l{https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkSubmitInfo.html}{VkSubmitInfo} |
| 352 | for details. |
| 353 | */ |
| 354 | |
| 355 | /*! |
| 356 | \variable QRhiVulkanQueueSubmitParams::presentWaitSemaphoreCount |
| 357 | |
| 358 | When non-zero, this applies to the next \c vkQueuePresentKHR() call. See |
| 359 | \l{https://registry.khronos.org/VulkanSC/specs/1.0-extensions/man/html/VkPresentInfoKHR.html}{VkPresentInfoKHR} |
| 360 | for details. |
| 361 | */ |
| 362 | |
| 363 | /*! |
| 364 | \variable QRhiVulkanQueueSubmitParams::presentWaitSemaphores |
| 365 | |
| 366 | See |
| 367 | \l{https://registry.khronos.org/VulkanSC/specs/1.0-extensions/man/html/VkPresentInfoKHR.html}{VkPresentInfoKHR} |
| 368 | for details. |
| 369 | */ |
| 370 | |
| 371 | template <class Int> |
| 372 | inline Int aligned(Int v, Int byteAlign) |
| 373 | { |
| 374 | return (v + byteAlign - 1) & ~(byteAlign - 1); |
| 375 | } |
| 376 | |
| 377 | static QVulkanInstance *globalVulkanInstance; |
| 378 | |
| 379 | static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL wrap_vkGetInstanceProcAddr(VkInstance, const char *pName) |
| 380 | { |
| 381 | return globalVulkanInstance->getInstanceProcAddr(name: pName); |
| 382 | } |
| 383 | |
| 384 | static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL wrap_vkGetDeviceProcAddr(VkDevice device, const char *pName) |
| 385 | { |
| 386 | return globalVulkanInstance->functions()->vkGetDeviceProcAddr(device, pName); |
| 387 | } |
| 388 | |
| 389 | static inline VmaAllocation toVmaAllocation(QVkAlloc a) |
| 390 | { |
| 391 | return reinterpret_cast<VmaAllocation>(a); |
| 392 | } |
| 393 | |
| 394 | static inline VmaAllocator toVmaAllocator(QVkAllocator a) |
| 395 | { |
| 396 | return reinterpret_cast<VmaAllocator>(a); |
| 397 | } |
| 398 | |
| 399 | /*! |
| 400 | \return the list of instance extensions that are expected to be enabled on |
| 401 | the QVulkanInstance that is used for the Vulkan-based QRhi. |
| 402 | |
| 403 | The returned list can be safely passed to QVulkanInstance::setExtensions() |
| 404 | as-is, because unsupported extensions are filtered out automatically. |
| 405 | */ |
| 406 | QByteArrayList QRhiVulkanInitParams::preferredInstanceExtensions() |
| 407 | { |
| 408 | return { |
| 409 | QByteArrayLiteral("VK_KHR_get_physical_device_properties2" ) |
| 410 | }; |
| 411 | } |
| 412 | |
| 413 | /*! |
| 414 | \return the list of device extensions that are expected to be enabled on the |
| 415 | \c VkDevice when creating a Vulkan-based QRhi with an externally created |
| 416 | \c VkDevice object. |
| 417 | */ |
| 418 | QByteArrayList QRhiVulkanInitParams::preferredExtensionsForImportedDevice() |
| 419 | { |
| 420 | return { |
| 421 | QByteArrayLiteral("VK_KHR_swapchain" ), |
| 422 | QByteArrayLiteral("VK_EXT_vertex_attribute_divisor" ), |
| 423 | QByteArrayLiteral("VK_KHR_create_renderpass2" ), |
| 424 | QByteArrayLiteral("VK_KHR_depth_stencil_resolve" ), |
| 425 | QByteArrayLiteral("VK_KHR_fragment_shading_rate" ) |
| 426 | }; |
| 427 | } |
| 428 | |
| 429 | QRhiVulkan::QRhiVulkan(QRhiVulkanInitParams *params, QRhiVulkanNativeHandles *importParams) |
| 430 | : ofr(this) |
| 431 | { |
| 432 | inst = params->inst; |
| 433 | if (!inst) { |
| 434 | // This builds on the fact that Qt Quick also uses QVulkanDefaultInstance. While |
| 435 | // this way we can support a null inst, it has consequences, so only do it with a |
| 436 | // warning. (e.g. if Qt Quick initializes afterwards, its attempt to set flags on |
| 437 | // QVulkanDefaultInstance will be futile) |
| 438 | qWarning(msg: "QRhi for Vulkan attempted to be initialized without a QVulkanInstance; using QVulkanDefaultInstance." ); |
| 439 | inst = QVulkanDefaultInstance::instance(); |
| 440 | } |
| 441 | |
| 442 | maybeWindow = params->window; // may be null |
| 443 | requestedDeviceExtensions = params->deviceExtensions; |
| 444 | |
| 445 | if (importParams) { |
| 446 | physDev = importParams->physDev; |
| 447 | dev = importParams->dev; |
| 448 | if (dev && physDev) { |
| 449 | importedDevice = true; |
| 450 | gfxQueueFamilyIdx = importParams->gfxQueueFamilyIdx; |
| 451 | gfxQueueIdx = importParams->gfxQueueIdx; |
| 452 | // gfxQueue is output only, no point in accepting it as input |
| 453 | if (importParams->vmemAllocator) { |
| 454 | importedAllocator = true; |
| 455 | allocator = importParams->vmemAllocator; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static bool qvk_debug_filter(QVulkanInstance::DebugMessageSeverityFlags severity, |
| 462 | QVulkanInstance::DebugMessageTypeFlags type, |
| 463 | const void *callbackData) |
| 464 | { |
| 465 | Q_UNUSED(severity); |
| 466 | Q_UNUSED(type); |
| 467 | #ifdef VK_EXT_debug_utils |
| 468 | const VkDebugUtilsMessengerCallbackDataEXT *d = static_cast<const VkDebugUtilsMessengerCallbackDataEXT *>(callbackData); |
| 469 | |
| 470 | // Filter out certain misleading validation layer messages, as per |
| 471 | // VulkanMemoryAllocator documentation. |
| 472 | if (strstr(haystack: d->pMessage, needle: "Mapping an image with layout" ) |
| 473 | && strstr(haystack: d->pMessage, needle: "can result in undefined behavior if this memory is used by the device" )) |
| 474 | { |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | // In certain cases allocateDescriptorSet() will attempt to allocate from a |
| 479 | // pool that does not have enough descriptors of a certain type. This makes |
| 480 | // the validation layer shout. However, this is not an error since we will |
| 481 | // then move on to another pool. If there is a real error, a qWarning |
| 482 | // message is shown by allocateDescriptorSet(), so the validation warning |
| 483 | // does not have any value and is just noise. |
| 484 | if (strstr(haystack: d->pMessage, needle: "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307" )) |
| 485 | return true; |
| 486 | #else |
| 487 | Q_UNUSED(callbackData); |
| 488 | #endif |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | static inline QRhiDriverInfo::DeviceType toRhiDeviceType(VkPhysicalDeviceType type) |
| 493 | { |
| 494 | switch (type) { |
| 495 | case VK_PHYSICAL_DEVICE_TYPE_OTHER: |
| 496 | return QRhiDriverInfo::UnknownDevice; |
| 497 | case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: |
| 498 | return QRhiDriverInfo::IntegratedDevice; |
| 499 | case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: |
| 500 | return QRhiDriverInfo::DiscreteDevice; |
| 501 | case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: |
| 502 | return QRhiDriverInfo::VirtualDevice; |
| 503 | case VK_PHYSICAL_DEVICE_TYPE_CPU: |
| 504 | return QRhiDriverInfo::CpuDevice; |
| 505 | default: |
| 506 | return QRhiDriverInfo::UnknownDevice; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static inline void fillDriverInfo(QRhiDriverInfo *info, const VkPhysicalDeviceProperties &physDevProperties) |
| 511 | { |
| 512 | info->deviceName = QByteArray(physDevProperties.deviceName); |
| 513 | info->deviceId = physDevProperties.deviceID; |
| 514 | info->vendorId = physDevProperties.vendorID; |
| 515 | info->deviceType = toRhiDeviceType(type: physDevProperties.deviceType); |
| 516 | } |
| 517 | |
| 518 | template<typename T> |
| 519 | static inline void addToChain(T *head, void *entry) |
| 520 | { |
| 521 | VkBaseOutStructure *s = reinterpret_cast<VkBaseOutStructure *>(head); |
| 522 | for ( ; ; ) { |
| 523 | VkBaseOutStructure *next = reinterpret_cast<VkBaseOutStructure *>(s->pNext); |
| 524 | if (next) |
| 525 | s = next; |
| 526 | else |
| 527 | break; |
| 528 | } |
| 529 | s->pNext = reinterpret_cast<VkBaseOutStructure *>(entry); |
| 530 | } |
| 531 | |
| 532 | bool QRhiVulkan::create(QRhi::Flags flags) |
| 533 | { |
| 534 | Q_ASSERT(inst); |
| 535 | if (!inst->isValid()) { |
| 536 | qWarning(msg: "Vulkan instance is not valid" ); |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | rhiFlags = flags; |
| 541 | qCDebug(QRHI_LOG_INFO, "Initializing QRhi Vulkan backend %p with flags %d" , this, int(rhiFlags)); |
| 542 | |
| 543 | globalVulkanInstance = inst; // used for function resolving in vkmemalloc callbacks |
| 544 | f = inst->functions(); |
| 545 | if (QRHI_LOG_INFO().isEnabled(type: QtDebugMsg)) { |
| 546 | qCDebug(QRHI_LOG_INFO, "Enabled instance extensions:" ); |
| 547 | for (const char *ext : inst->extensions()) |
| 548 | qCDebug(QRHI_LOG_INFO, " %s" , ext); |
| 549 | } |
| 550 | |
| 551 | caps = {}; |
| 552 | caps.debugUtils = inst->extensions().contains(QByteArrayLiteral("VK_EXT_debug_utils" )); |
| 553 | |
| 554 | QList<VkQueueFamilyProperties> queueFamilyProps; |
| 555 | auto queryQueueFamilyProps = [this, &queueFamilyProps] { |
| 556 | uint32_t queueCount = 0; |
| 557 | f->vkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, nullptr); |
| 558 | queueFamilyProps.resize(size: int(queueCount)); |
| 559 | f->vkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, queueFamilyProps.data()); |
| 560 | }; |
| 561 | |
| 562 | // Choose a physical device, unless one was provided in importParams. |
| 563 | if (!physDev) { |
| 564 | uint32_t physDevCount = 0; |
| 565 | f->vkEnumeratePhysicalDevices(inst->vkInstance(), &physDevCount, nullptr); |
| 566 | if (!physDevCount) { |
| 567 | qWarning(msg: "No physical devices" ); |
| 568 | return false; |
| 569 | } |
| 570 | QVarLengthArray<VkPhysicalDevice, 4> physDevs(physDevCount); |
| 571 | VkResult err = f->vkEnumeratePhysicalDevices(inst->vkInstance(), &physDevCount, physDevs.data()); |
| 572 | if (err != VK_SUCCESS || !physDevCount) { |
| 573 | qWarning(msg: "Failed to enumerate physical devices: %d" , err); |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | int physDevIndex = -1; |
| 578 | int requestedPhysDevIndex = -1; |
| 579 | if (qEnvironmentVariableIsSet(varName: "QT_VK_PHYSICAL_DEVICE_INDEX" )) |
| 580 | requestedPhysDevIndex = qEnvironmentVariableIntValue(varName: "QT_VK_PHYSICAL_DEVICE_INDEX" ); |
| 581 | |
| 582 | if (requestedPhysDevIndex < 0 && requestedRhiAdapter) { |
| 583 | VkPhysicalDevice requestedPhysDev = static_cast<QVulkanAdapter *>(requestedRhiAdapter)->physDev; |
| 584 | for (int i = 0; i < int(physDevCount); ++i) { |
| 585 | if (physDevs[i] == requestedPhysDev) { |
| 586 | requestedPhysDevIndex = i; |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (requestedPhysDevIndex < 0 && flags.testFlag(flag: QRhi::PreferSoftwareRenderer)) { |
| 593 | for (int i = 0; i < int(physDevCount); ++i) { |
| 594 | f->vkGetPhysicalDeviceProperties(physDevs[i], &physDevProperties); |
| 595 | if (physDevProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) { |
| 596 | requestedPhysDevIndex = i; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | for (int i = 0; i < int(physDevCount); ++i) { |
| 603 | f->vkGetPhysicalDeviceProperties(physDevs[i], &physDevProperties); |
| 604 | qCDebug(QRHI_LOG_INFO, "Physical device %d: '%s' %d.%d.%d (api %d.%d.%d vendor 0x%X device 0x%X type %d)" , |
| 605 | i, |
| 606 | physDevProperties.deviceName, |
| 607 | VK_VERSION_MAJOR(physDevProperties.driverVersion), |
| 608 | VK_VERSION_MINOR(physDevProperties.driverVersion), |
| 609 | VK_VERSION_PATCH(physDevProperties.driverVersion), |
| 610 | VK_VERSION_MAJOR(physDevProperties.apiVersion), |
| 611 | VK_VERSION_MINOR(physDevProperties.apiVersion), |
| 612 | VK_VERSION_PATCH(physDevProperties.apiVersion), |
| 613 | physDevProperties.vendorID, |
| 614 | physDevProperties.deviceID, |
| 615 | physDevProperties.deviceType); |
| 616 | if (physDevIndex < 0 && (requestedPhysDevIndex < 0 || requestedPhysDevIndex == int(i))) { |
| 617 | physDevIndex = i; |
| 618 | qCDebug(QRHI_LOG_INFO, " using this physical device" ); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (physDevIndex < 0) { |
| 623 | qWarning(msg: "No matching physical device" ); |
| 624 | return false; |
| 625 | } |
| 626 | physDev = physDevs[physDevIndex]; |
| 627 | f->vkGetPhysicalDeviceProperties(physDev, &physDevProperties); |
| 628 | } else { |
| 629 | f->vkGetPhysicalDeviceProperties(physDev, &physDevProperties); |
| 630 | qCDebug(QRHI_LOG_INFO, "Using imported physical device '%s' %d.%d.%d (api %d.%d.%d vendor 0x%X device 0x%X type %d)" , |
| 631 | physDevProperties.deviceName, |
| 632 | VK_VERSION_MAJOR(physDevProperties.driverVersion), |
| 633 | VK_VERSION_MINOR(physDevProperties.driverVersion), |
| 634 | VK_VERSION_PATCH(physDevProperties.driverVersion), |
| 635 | VK_VERSION_MAJOR(physDevProperties.apiVersion), |
| 636 | VK_VERSION_MINOR(physDevProperties.apiVersion), |
| 637 | VK_VERSION_PATCH(physDevProperties.apiVersion), |
| 638 | physDevProperties.vendorID, |
| 639 | physDevProperties.deviceID, |
| 640 | physDevProperties.deviceType); |
| 641 | } |
| 642 | |
| 643 | caps.apiVersion = inst->apiVersion(); |
| 644 | |
| 645 | // Check the physical device API version against the instance API version, |
| 646 | // they do not have to match, which means whatever version was set in the |
| 647 | // QVulkanInstance may not be legally used with a given device if the |
| 648 | // physical device has a lower version. |
| 649 | const QVersionNumber physDevApiVersion(VK_VERSION_MAJOR(physDevProperties.apiVersion), |
| 650 | VK_VERSION_MINOR(physDevProperties.apiVersion)); // patch version left out intentionally |
| 651 | if (physDevApiVersion < caps.apiVersion) { |
| 652 | qCDebug(QRHI_LOG_INFO) << "Instance has api version" << caps.apiVersion |
| 653 | << "whereas the chosen physical device has" << physDevApiVersion |
| 654 | << "- restricting to the latter" ; |
| 655 | caps.apiVersion = physDevApiVersion; |
| 656 | } |
| 657 | |
| 658 | fillDriverInfo(info: &driverInfoStruct, physDevProperties); |
| 659 | |
| 660 | QVulkanInfoVector<QVulkanExtension> devExts; |
| 661 | uint32_t devExtCount = 0; |
| 662 | f->vkEnumerateDeviceExtensionProperties(physDev, nullptr, &devExtCount, nullptr); |
| 663 | if (devExtCount) { |
| 664 | QList<VkExtensionProperties> extProps(devExtCount); |
| 665 | f->vkEnumerateDeviceExtensionProperties(physDev, nullptr, &devExtCount, extProps.data()); |
| 666 | for (const VkExtensionProperties &p : std::as_const(t&: extProps)) |
| 667 | devExts.append(t: { .name: p.extensionName, .version: p.specVersion }); |
| 668 | } |
| 669 | qCDebug(QRHI_LOG_INFO, "%d device extensions available" , int(devExts.size())); |
| 670 | |
| 671 | bool featuresQueried = false; |
| 672 | #ifdef VK_VERSION_1_1 |
| 673 | VkPhysicalDeviceFeatures2 physDevFeaturesChainable = {}; |
| 674 | physDevFeaturesChainable.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 675 | |
| 676 | // Extensions (that are really extensions in 1.1-1.3, not core) |
| 677 | #ifdef VK_KHR_fragment_shading_rate |
| 678 | VkPhysicalDeviceFragmentShadingRateFeaturesKHR fragmentShadingRateFeatures = {}; |
| 679 | fragmentShadingRateFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_FEATURES_KHR; |
| 680 | if (devExts.contains(name: "VK_KHR_fragment_shading_rate" )) |
| 681 | addToChain(head: &physDevFeaturesChainable, entry: &fragmentShadingRateFeatures); |
| 682 | #endif |
| 683 | #endif |
| 684 | |
| 685 | // Vulkan >=1.2 headers at build time, >=1.2 implementation at run time |
| 686 | #ifdef VK_VERSION_1_2 |
| 687 | if (!featuresQueried) { |
| 688 | // Vulkan11Features, Vulkan12Features, etc. are only in Vulkan 1.2 and newer. |
| 689 | if (caps.apiVersion >= QVersionNumber(1, 2)) { |
| 690 | physDevFeatures11IfApi12OrNewer = {}; |
| 691 | physDevFeatures11IfApi12OrNewer.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES; |
| 692 | physDevFeatures12 = {}; |
| 693 | physDevFeatures12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES; |
| 694 | #ifdef VK_VERSION_1_3 |
| 695 | physDevFeatures13 = {}; |
| 696 | physDevFeatures13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES; |
| 697 | #endif |
| 698 | addToChain(head: &physDevFeaturesChainable, entry: &physDevFeatures11IfApi12OrNewer); |
| 699 | physDevFeatures11IfApi12OrNewer.pNext = &physDevFeatures12; |
| 700 | #ifdef VK_VERSION_1_3 |
| 701 | if (caps.apiVersion >= QVersionNumber(1, 3)) |
| 702 | physDevFeatures12.pNext = &physDevFeatures13; |
| 703 | #endif |
| 704 | f->vkGetPhysicalDeviceFeatures2(physDev, &physDevFeaturesChainable); |
| 705 | memcpy(dest: &physDevFeatures, src: &physDevFeaturesChainable.features, n: sizeof(VkPhysicalDeviceFeatures)); |
| 706 | featuresQueried = true; |
| 707 | } |
| 708 | } |
| 709 | #endif // VK_VERSION_1_2 |
| 710 | |
| 711 | // Vulkan >=1.1 headers at build time, 1.1 implementation at run time |
| 712 | #ifdef VK_VERSION_1_1 |
| 713 | if (!featuresQueried) { |
| 714 | // Vulkan versioning nightmares: if the runtime API version is 1.1, |
| 715 | // there is no Vulkan11Features (introduced in 1.2+, the headers might |
| 716 | // have the types and structs, but the Vulkan implementation version at |
| 717 | // run time is what matters). But there are individual feature structs. |
| 718 | // For multiview, it is important to get this right since at the time of |
| 719 | // writing Quest 3 Android is a Vulkan 1.1 implementation at run time on |
| 720 | // the headset. |
| 721 | if (caps.apiVersion == QVersionNumber(1, 1)) { |
| 722 | multiviewFeaturesIfApi11 = {}; |
| 723 | multiviewFeaturesIfApi11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES; |
| 724 | addToChain(head: &physDevFeaturesChainable, entry: &multiviewFeaturesIfApi11); |
| 725 | f->vkGetPhysicalDeviceFeatures2(physDev, &physDevFeaturesChainable); |
| 726 | memcpy(dest: &physDevFeatures, src: &physDevFeaturesChainable.features, n: sizeof(VkPhysicalDeviceFeatures)); |
| 727 | featuresQueried = true; |
| 728 | } |
| 729 | } |
| 730 | #endif |
| 731 | |
| 732 | if (!featuresQueried) { |
| 733 | // If the API version at run time is 1.0 (or we are building with |
| 734 | // ancient 1.0 headers), then do the Vulkan 1.0 query. |
| 735 | f->vkGetPhysicalDeviceFeatures(physDev, &physDevFeatures); |
| 736 | featuresQueried = true; |
| 737 | } |
| 738 | |
| 739 | // Choose queue and create device, unless the device was specified in importParams. |
| 740 | if (!importedDevice) { |
| 741 | // We only support combined graphics+present queues. When it comes to |
| 742 | // compute, only combined graphics+compute queue is used, compute gets |
| 743 | // disabled otherwise. |
| 744 | std::optional<uint32_t> gfxQueueFamilyIdxOpt; |
| 745 | std::optional<uint32_t> computelessGfxQueueCandidateIdxOpt; |
| 746 | queryQueueFamilyProps(); |
| 747 | const uint32_t queueFamilyCount = uint32_t(queueFamilyProps.size()); |
| 748 | for (uint32_t i = 0; i < queueFamilyCount; ++i) { |
| 749 | qCDebug(QRHI_LOG_INFO, "queue family %u: flags=0x%x count=%u" , |
| 750 | i, queueFamilyProps[i].queueFlags, queueFamilyProps[i].queueCount); |
| 751 | if (!gfxQueueFamilyIdxOpt.has_value() |
| 752 | && (queueFamilyProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) |
| 753 | && (!maybeWindow || inst->supportsPresent(physicalDevice: physDev, queueFamilyIndex: i, window: maybeWindow))) |
| 754 | { |
| 755 | if (queueFamilyProps[i].queueFlags & VK_QUEUE_COMPUTE_BIT) |
| 756 | gfxQueueFamilyIdxOpt = i; |
| 757 | else if (!computelessGfxQueueCandidateIdxOpt.has_value()) |
| 758 | computelessGfxQueueCandidateIdxOpt = i; |
| 759 | } |
| 760 | } |
| 761 | if (gfxQueueFamilyIdxOpt.has_value()) { |
| 762 | gfxQueueFamilyIdx = gfxQueueFamilyIdxOpt.value(); |
| 763 | } else { |
| 764 | if (computelessGfxQueueCandidateIdxOpt.has_value()) { |
| 765 | gfxQueueFamilyIdx = computelessGfxQueueCandidateIdxOpt.value(); |
| 766 | } else { |
| 767 | qWarning(msg: "No graphics (or no graphics+present) queue family found" ); |
| 768 | return false; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | VkDeviceQueueCreateInfo queueInfo = {}; |
| 773 | const float prio[] = { 0 }; |
| 774 | queueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 775 | queueInfo.queueFamilyIndex = gfxQueueFamilyIdx; |
| 776 | queueInfo.queueCount = 1; |
| 777 | queueInfo.pQueuePriorities = prio; |
| 778 | |
| 779 | QList<const char *> devLayers; |
| 780 | if (inst->layers().contains(t: "VK_LAYER_KHRONOS_validation" )) |
| 781 | devLayers.append(t: "VK_LAYER_KHRONOS_validation" ); |
| 782 | |
| 783 | QList<const char *> requestedDevExts; |
| 784 | requestedDevExts.append(t: "VK_KHR_swapchain" ); |
| 785 | |
| 786 | const bool hasPhysDevProp2 = inst->extensions().contains(QByteArrayLiteral("VK_KHR_get_physical_device_properties2" )); |
| 787 | |
| 788 | if (devExts.contains(QByteArrayLiteral("VK_KHR_portability_subset" ))) { |
| 789 | if (hasPhysDevProp2) { |
| 790 | requestedDevExts.append(t: "VK_KHR_portability_subset" ); |
| 791 | } else { |
| 792 | qWarning(msg: "VK_KHR_portability_subset should be enabled on the device " |
| 793 | "but the instance does not have VK_KHR_get_physical_device_properties2 enabled. " |
| 794 | "Expect problems." ); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | #ifdef VK_EXT_vertex_attribute_divisor |
| 799 | if (devExts.contains(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME)) { |
| 800 | if (hasPhysDevProp2) { |
| 801 | requestedDevExts.append(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME); |
| 802 | caps.vertexAttribDivisor = true; |
| 803 | } |
| 804 | } |
| 805 | #endif |
| 806 | |
| 807 | #ifdef VK_KHR_create_renderpass2 |
| 808 | if (devExts.contains(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME)) { |
| 809 | requestedDevExts.append(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME); |
| 810 | caps.renderPass2KHR = true; |
| 811 | } |
| 812 | #endif |
| 813 | |
| 814 | #ifdef VK_KHR_depth_stencil_resolve |
| 815 | if (devExts.contains(VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME)) { |
| 816 | requestedDevExts.append(VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME); |
| 817 | caps.depthStencilResolveKHR = true; |
| 818 | } |
| 819 | #endif |
| 820 | |
| 821 | #ifdef VK_KHR_fragment_shading_rate |
| 822 | if (devExts.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) |
| 823 | requestedDevExts.append(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME); |
| 824 | #endif |
| 825 | |
| 826 | for (const QByteArray &ext : requestedDeviceExtensions) { |
| 827 | if (!ext.isEmpty() && !requestedDevExts.contains(t: ext)) { |
| 828 | if (devExts.contains(name: ext)) { |
| 829 | requestedDevExts.append(t: ext.constData()); |
| 830 | } else { |
| 831 | qWarning(msg: "Device extension %s requested in QRhiVulkanInitParams is not supported" , |
| 832 | ext.constData()); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | QByteArrayList envExtList = qgetenv(varName: "QT_VULKAN_DEVICE_EXTENSIONS" ).split(sep: ';'); |
| 838 | for (const QByteArray &ext : envExtList) { |
| 839 | if (!ext.isEmpty() && !requestedDevExts.contains(t: ext)) { |
| 840 | if (devExts.contains(name: ext)) { |
| 841 | requestedDevExts.append(t: ext.constData()); |
| 842 | } else { |
| 843 | qWarning(msg: "Device extension %s requested in QT_VULKAN_DEVICE_EXTENSIONS is not supported" , |
| 844 | ext.constData()); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if (QRHI_LOG_INFO().isEnabled(type: QtDebugMsg)) { |
| 850 | qCDebug(QRHI_LOG_INFO, "Enabling device extensions:" ); |
| 851 | for (const char *ext : requestedDevExts) |
| 852 | qCDebug(QRHI_LOG_INFO, " %s" , ext); |
| 853 | } |
| 854 | |
| 855 | VkDeviceCreateInfo devInfo = {}; |
| 856 | devInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 857 | devInfo.queueCreateInfoCount = 1; |
| 858 | devInfo.pQueueCreateInfos = &queueInfo; |
| 859 | devInfo.enabledLayerCount = uint32_t(devLayers.size()); |
| 860 | devInfo.ppEnabledLayerNames = devLayers.constData(); |
| 861 | devInfo.enabledExtensionCount = uint32_t(requestedDevExts.size()); |
| 862 | devInfo.ppEnabledExtensionNames = requestedDevExts.constData(); |
| 863 | |
| 864 | // Enable all features that are reported as supported, except |
| 865 | // robustness because that potentially affects performance. |
| 866 | // |
| 867 | // Enabling all features mainly serves third-party renderers that may |
| 868 | // use the VkDevice created here. For the record, the backend here |
| 869 | // optionally relies on the following features, meaning just for our |
| 870 | // (QRhi/Quick/Quick 3D) purposes it would be sufficient to |
| 871 | // enable-if-supported only the following: |
| 872 | // |
| 873 | // wideLines, largePoints, fillModeNonSolid, |
| 874 | // tessellationShader, geometryShader |
| 875 | // textureCompressionETC2, textureCompressionASTC_LDR, textureCompressionBC |
| 876 | |
| 877 | #ifdef VK_VERSION_1_1 |
| 878 | physDevFeaturesChainable.features.robustBufferAccess = VK_FALSE; |
| 879 | #endif |
| 880 | #ifdef VK_VERSION_1_3 |
| 881 | physDevFeatures13.robustImageAccess = VK_FALSE; |
| 882 | #endif |
| 883 | |
| 884 | #ifdef VK_VERSION_1_1 |
| 885 | if (caps.apiVersion >= QVersionNumber(1, 1)) { |
| 886 | // For a >=1.2 implementation at run time, this will enable all |
| 887 | // (1.0-1.3) features reported as supported, except the ones we turn |
| 888 | // off explicitly above. (+extensions) For a 1.1 implementation at |
| 889 | // run time, this only enables the 1.0 and multiview features (+any |
| 890 | // extensions) reported as supported. We will not be bothering with |
| 891 | // the Vulkan 1.1 individual feature struct nonsense. |
| 892 | devInfo.pNext = &physDevFeaturesChainable; |
| 893 | } else |
| 894 | #endif |
| 895 | { |
| 896 | physDevFeatures.robustBufferAccess = VK_FALSE; |
| 897 | devInfo.pEnabledFeatures = &physDevFeatures; |
| 898 | } |
| 899 | |
| 900 | VkResult err = f->vkCreateDevice(physDev, &devInfo, nullptr, &dev); |
| 901 | if (err != VK_SUCCESS) { |
| 902 | qWarning(msg: "Failed to create device: %d" , err); |
| 903 | return false; |
| 904 | } |
| 905 | } else { |
| 906 | qCDebug(QRHI_LOG_INFO, "Using imported device %p" , dev); |
| 907 | |
| 908 | // Here we have no way to tell if the extensions got enabled or not. |
| 909 | // Pretend it's all there and supported. If getProcAddress fails, we'll |
| 910 | // handle that gracefully. |
| 911 | caps.vertexAttribDivisor = true; |
| 912 | caps.renderPass2KHR = true; |
| 913 | caps.depthStencilResolveKHR = true; |
| 914 | } |
| 915 | |
| 916 | vkGetPhysicalDeviceSurfaceCapabilitiesKHR = reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR>( |
| 917 | inst->getInstanceProcAddr(name: "vkGetPhysicalDeviceSurfaceCapabilitiesKHR" )); |
| 918 | vkGetPhysicalDeviceSurfaceFormatsKHR = reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR>( |
| 919 | inst->getInstanceProcAddr(name: "vkGetPhysicalDeviceSurfaceFormatsKHR" )); |
| 920 | vkGetPhysicalDeviceSurfacePresentModesKHR = reinterpret_cast<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR>( |
| 921 | inst->getInstanceProcAddr(name: "vkGetPhysicalDeviceSurfacePresentModesKHR" )); |
| 922 | |
| 923 | df = inst->deviceFunctions(device: dev); |
| 924 | |
| 925 | VkCommandPoolCreateInfo poolInfo = {}; |
| 926 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 927 | poolInfo.queueFamilyIndex = gfxQueueFamilyIdx; |
| 928 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 929 | VkResult err = df->vkCreateCommandPool(dev, &poolInfo, nullptr, &cmdPool[i]); |
| 930 | if (err != VK_SUCCESS) { |
| 931 | qWarning(msg: "Failed to create command pool: %d" , err); |
| 932 | return false; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | qCDebug(QRHI_LOG_INFO, "Using queue family index %u and queue index %u" , |
| 937 | gfxQueueFamilyIdx, gfxQueueIdx); |
| 938 | |
| 939 | df->vkGetDeviceQueue(dev, gfxQueueFamilyIdx, gfxQueueIdx, &gfxQueue); |
| 940 | |
| 941 | if (queueFamilyProps.isEmpty()) |
| 942 | queryQueueFamilyProps(); |
| 943 | |
| 944 | caps.compute = (queueFamilyProps[gfxQueueFamilyIdx].queueFlags & VK_QUEUE_COMPUTE_BIT) != 0; |
| 945 | timestampValidBits = queueFamilyProps[gfxQueueFamilyIdx].timestampValidBits; |
| 946 | |
| 947 | ubufAlign = physDevProperties.limits.minUniformBufferOffsetAlignment; |
| 948 | // helps little with an optimal offset of 1 (on some drivers) when the spec |
| 949 | // elsewhere states that the minimum bufferOffset is 4... |
| 950 | texbufAlign = qMax<VkDeviceSize>(a: 4, b: physDevProperties.limits.optimalBufferCopyOffsetAlignment); |
| 951 | |
| 952 | caps.wideLines = physDevFeatures.wideLines; |
| 953 | |
| 954 | caps.texture3DSliceAs2D = caps.apiVersion >= QVersionNumber(1, 1); |
| 955 | |
| 956 | caps.tessellation = physDevFeatures.tessellationShader; |
| 957 | caps.geometryShader = physDevFeatures.geometryShader; |
| 958 | |
| 959 | caps.nonFillPolygonMode = physDevFeatures.fillModeNonSolid; |
| 960 | |
| 961 | #ifdef VK_VERSION_1_2 |
| 962 | if (caps.apiVersion >= QVersionNumber(1, 2)) |
| 963 | caps.multiView = physDevFeatures11IfApi12OrNewer.multiview; |
| 964 | #endif |
| 965 | |
| 966 | #ifdef VK_VERSION_1_1 |
| 967 | if (caps.apiVersion == QVersionNumber(1, 1)) |
| 968 | caps.multiView = multiviewFeaturesIfApi11.multiview; |
| 969 | #endif |
| 970 | |
| 971 | #ifdef VK_KHR_fragment_shading_rate |
| 972 | fragmentShadingRates.clear(); |
| 973 | if (caps.apiVersion >= QVersionNumber(1, 1)) { |
| 974 | caps.perDrawShadingRate = fragmentShadingRateFeatures.pipelineFragmentShadingRate; |
| 975 | caps.imageBasedShadingRate = fragmentShadingRateFeatures.attachmentFragmentShadingRate; |
| 976 | if (caps.imageBasedShadingRate) { |
| 977 | VkPhysicalDeviceFragmentShadingRatePropertiesKHR shadingRateProps = {}; |
| 978 | shadingRateProps.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR; |
| 979 | VkPhysicalDeviceProperties2 props2 = {}; |
| 980 | props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| 981 | props2.pNext = &shadingRateProps; |
| 982 | f->vkGetPhysicalDeviceProperties2(physDev, &props2); |
| 983 | caps.imageBasedShadingRateTileSize = int(shadingRateProps.maxFragmentShadingRateAttachmentTexelSize.width); |
| 984 | // If it's non-square, there's nothing we can do since it is not compatible with other APIs (D3D12) then. |
| 985 | } |
| 986 | if (caps.perDrawShadingRate) { |
| 987 | PFN_vkGetPhysicalDeviceFragmentShadingRatesKHR vkGetPhysicalDeviceFragmentShadingRatesKHR = |
| 988 | reinterpret_cast<PFN_vkGetPhysicalDeviceFragmentShadingRatesKHR>( |
| 989 | inst->getInstanceProcAddr(name: "vkGetPhysicalDeviceFragmentShadingRatesKHR" )); |
| 990 | if (vkGetPhysicalDeviceFragmentShadingRatesKHR) { |
| 991 | uint32_t count = 0; |
| 992 | vkGetPhysicalDeviceFragmentShadingRatesKHR(physDev, &count, nullptr); |
| 993 | fragmentShadingRates.resize(sz: count); |
| 994 | for (VkPhysicalDeviceFragmentShadingRateKHR &s : fragmentShadingRates) { |
| 995 | s = {}; |
| 996 | s.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_KHR; |
| 997 | } |
| 998 | vkGetPhysicalDeviceFragmentShadingRatesKHR(physDev, &count, fragmentShadingRates.data()); |
| 999 | } |
| 1000 | vkCmdSetFragmentShadingRateKHR = reinterpret_cast<PFN_vkCmdSetFragmentShadingRateKHR>( |
| 1001 | f->vkGetDeviceProcAddr(dev, "vkCmdSetFragmentShadingRateKHR" )); |
| 1002 | } |
| 1003 | } |
| 1004 | #endif |
| 1005 | |
| 1006 | // With Vulkan 1.2 renderpass2 and depth_stencil_resolve are core, but we |
| 1007 | // have to support the case of 1.1 + extensions, in particular for the Quest |
| 1008 | // 3 (Android, Vulkan 1.1 at the time of writing). Therefore, always rely on |
| 1009 | // the KHR extension for now. |
| 1010 | #ifdef VK_KHR_create_renderpass2 |
| 1011 | if (caps.renderPass2KHR) { |
| 1012 | vkCreateRenderPass2KHR = reinterpret_cast<PFN_vkCreateRenderPass2KHR>(f->vkGetDeviceProcAddr(dev, "vkCreateRenderPass2KHR" )); |
| 1013 | if (!vkCreateRenderPass2KHR) // handle it gracefully, the caps flag may be incorrect when using an imported VkDevice |
| 1014 | caps.renderPass2KHR = false; |
| 1015 | } |
| 1016 | #endif |
| 1017 | |
| 1018 | // On Windows, figure out the DXGI adapter LUID. |
| 1019 | #ifdef Q_OS_WIN |
| 1020 | adapterLuidValid = false; |
| 1021 | adapterLuid = {}; |
| 1022 | #ifdef VK_VERSION_1_2 |
| 1023 | if (caps.apiVersion >= QVersionNumber(1, 2)) { |
| 1024 | VkPhysicalDeviceVulkan11Properties v11props = {}; |
| 1025 | v11props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES; |
| 1026 | VkPhysicalDeviceProperties2 props2 = {}; |
| 1027 | props2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| 1028 | props2.pNext = &v11props; |
| 1029 | f->vkGetPhysicalDeviceProperties2(physDev, &props2); |
| 1030 | if (v11props.deviceLUIDValid) { |
| 1031 | const LUID *luid = reinterpret_cast<const LUID *>(v11props.deviceLUID); |
| 1032 | memcpy(&adapterLuid, luid, VK_LUID_SIZE); |
| 1033 | adapterLuidValid = true; |
| 1034 | dxgiHdrInfo = new QDxgiHdrInfo(adapterLuid); |
| 1035 | qCDebug(QRHI_LOG_INFO, "DXGI adapter LUID for physical device is %lu, %lu" , |
| 1036 | adapterLuid.LowPart, adapterLuid.HighPart); |
| 1037 | } |
| 1038 | } |
| 1039 | #endif |
| 1040 | #endif |
| 1041 | |
| 1042 | if (!importedAllocator) { |
| 1043 | VmaVulkanFunctions funcs = {}; |
| 1044 | funcs.vkGetInstanceProcAddr = wrap_vkGetInstanceProcAddr; |
| 1045 | funcs.vkGetDeviceProcAddr = wrap_vkGetDeviceProcAddr; |
| 1046 | |
| 1047 | VmaAllocatorCreateInfo allocatorInfo = {}; |
| 1048 | // A QRhi is supposed to be used from one single thread only. Disable |
| 1049 | // the allocator's own mutexes. This gives a performance boost. |
| 1050 | allocatorInfo.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT; |
| 1051 | allocatorInfo.physicalDevice = physDev; |
| 1052 | allocatorInfo.device = dev; |
| 1053 | allocatorInfo.pVulkanFunctions = &funcs; |
| 1054 | allocatorInfo.instance = inst->vkInstance(); |
| 1055 | |
| 1056 | // Logic would dictate setting allocatorInfo.vulkanApiVersion to caps.apiVersion. |
| 1057 | // However, VMA has asserts to test if the header version Qt was built with is |
| 1058 | // older than the runtime version. This is nice, but a bit unnecessary (in Qt we'd |
| 1059 | // rather prefer losing the affected features automatically, and perhaps printing |
| 1060 | // a warning, instead of aborting the application). Restrict the runtime version |
| 1061 | // passed in based on the preprocessor macro to keep VMA happy. |
| 1062 | #ifdef VK_VERSION_1_4 |
| 1063 | if (caps.apiVersion >= QVersionNumber(1, 4)) |
| 1064 | allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_4; |
| 1065 | else |
| 1066 | #endif |
| 1067 | #ifdef VK_VERSION_1_3 |
| 1068 | if (caps.apiVersion >= QVersionNumber(1, 3)) |
| 1069 | allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_3; |
| 1070 | else |
| 1071 | #endif |
| 1072 | #ifdef VK_VERSION_1_2 |
| 1073 | if (caps.apiVersion >= QVersionNumber(1, 2)) |
| 1074 | allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_2; |
| 1075 | else |
| 1076 | #endif |
| 1077 | #ifdef VK_VERSION_1_1 |
| 1078 | if (caps.apiVersion >= QVersionNumber(1, 1)) |
| 1079 | allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_1; |
| 1080 | else |
| 1081 | #endif |
| 1082 | #ifdef VK_VERSION_1_0 |
| 1083 | allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_0; |
| 1084 | #endif |
| 1085 | |
| 1086 | VmaAllocator vmaallocator; |
| 1087 | VkResult err = vmaCreateAllocator(pCreateInfo: &allocatorInfo, pAllocator: &vmaallocator); |
| 1088 | if (err != VK_SUCCESS) { |
| 1089 | qWarning(msg: "Failed to create allocator: %d" , err); |
| 1090 | return false; |
| 1091 | } |
| 1092 | allocator = vmaallocator; |
| 1093 | } |
| 1094 | |
| 1095 | inst->installDebugOutputFilter(filter: qvk_debug_filter); |
| 1096 | |
| 1097 | VkDescriptorPool pool; |
| 1098 | VkResult err = createDescriptorPool(pool: &pool); |
| 1099 | if (err == VK_SUCCESS) |
| 1100 | descriptorPools.append(t: pool); |
| 1101 | else |
| 1102 | qWarning(msg: "Failed to create initial descriptor pool: %d" , err); |
| 1103 | |
| 1104 | VkQueryPoolCreateInfo timestampQueryPoolInfo = {}; |
| 1105 | timestampQueryPoolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; |
| 1106 | timestampQueryPoolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP; |
| 1107 | timestampQueryPoolInfo.queryCount = QVK_MAX_ACTIVE_TIMESTAMP_PAIRS * 2; |
| 1108 | err = df->vkCreateQueryPool(dev, ×tampQueryPoolInfo, nullptr, ×tampQueryPool); |
| 1109 | if (err != VK_SUCCESS) { |
| 1110 | qWarning(msg: "Failed to create timestamp query pool: %d" , err); |
| 1111 | return false; |
| 1112 | } |
| 1113 | timestampQueryPoolMap.resize(size: QVK_MAX_ACTIVE_TIMESTAMP_PAIRS); // 1 bit per pair |
| 1114 | timestampQueryPoolMap.fill(aval: false); |
| 1115 | |
| 1116 | #ifdef VK_EXT_debug_utils |
| 1117 | if (caps.debugUtils) { |
| 1118 | vkSetDebugUtilsObjectNameEXT = reinterpret_cast<PFN_vkSetDebugUtilsObjectNameEXT>(f->vkGetDeviceProcAddr(dev, "vkSetDebugUtilsObjectNameEXT" )); |
| 1119 | vkCmdBeginDebugUtilsLabelEXT = reinterpret_cast<PFN_vkCmdBeginDebugUtilsLabelEXT>(f->vkGetDeviceProcAddr(dev, "vkCmdBeginDebugUtilsLabelEXT" )); |
| 1120 | vkCmdEndDebugUtilsLabelEXT = reinterpret_cast<PFN_vkCmdEndDebugUtilsLabelEXT>(f->vkGetDeviceProcAddr(dev, "vkCmdEndDebugUtilsLabelEXT" )); |
| 1121 | vkCmdInsertDebugUtilsLabelEXT = reinterpret_cast<PFN_vkCmdInsertDebugUtilsLabelEXT>(f->vkGetDeviceProcAddr(dev, "vkCmdInsertDebugUtilsLabelEXT" )); |
| 1122 | } |
| 1123 | #endif |
| 1124 | |
| 1125 | deviceLost = false; |
| 1126 | |
| 1127 | nativeHandlesStruct.physDev = physDev; |
| 1128 | nativeHandlesStruct.dev = dev; |
| 1129 | nativeHandlesStruct.gfxQueueFamilyIdx = gfxQueueFamilyIdx; |
| 1130 | nativeHandlesStruct.gfxQueueIdx = gfxQueueIdx; |
| 1131 | nativeHandlesStruct.gfxQueue = gfxQueue; |
| 1132 | nativeHandlesStruct.vmemAllocator = allocator; |
| 1133 | nativeHandlesStruct.inst = inst; |
| 1134 | |
| 1135 | return true; |
| 1136 | } |
| 1137 | |
| 1138 | void QRhiVulkan::destroy() |
| 1139 | { |
| 1140 | if (!df) |
| 1141 | return; |
| 1142 | |
| 1143 | if (!deviceLost) |
| 1144 | df->vkDeviceWaitIdle(dev); |
| 1145 | |
| 1146 | executeDeferredReleases(forced: true); |
| 1147 | finishActiveReadbacks(forced: true); |
| 1148 | |
| 1149 | #ifdef Q_OS_WIN |
| 1150 | delete dxgiHdrInfo; |
| 1151 | dxgiHdrInfo = nullptr; |
| 1152 | #endif |
| 1153 | |
| 1154 | if (ofr.cmdFence) { |
| 1155 | df->vkDestroyFence(dev, ofr.cmdFence, nullptr); |
| 1156 | ofr.cmdFence = VK_NULL_HANDLE; |
| 1157 | } |
| 1158 | |
| 1159 | if (pipelineCache) { |
| 1160 | df->vkDestroyPipelineCache(dev, pipelineCache, nullptr); |
| 1161 | pipelineCache = VK_NULL_HANDLE; |
| 1162 | } |
| 1163 | |
| 1164 | for (const DescriptorPoolData &pool : descriptorPools) |
| 1165 | df->vkDestroyDescriptorPool(dev, pool.pool, nullptr); |
| 1166 | |
| 1167 | descriptorPools.clear(); |
| 1168 | |
| 1169 | if (timestampQueryPool) { |
| 1170 | df->vkDestroyQueryPool(dev, timestampQueryPool, nullptr); |
| 1171 | timestampQueryPool = VK_NULL_HANDLE; |
| 1172 | } |
| 1173 | |
| 1174 | if (!importedAllocator && allocator) { |
| 1175 | vmaDestroyAllocator(allocator: toVmaAllocator(a: allocator)); |
| 1176 | allocator = nullptr; |
| 1177 | } |
| 1178 | |
| 1179 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 1180 | if (cmdPool[i]) { |
| 1181 | df->vkDestroyCommandPool(dev, cmdPool[i], nullptr); |
| 1182 | cmdPool[i] = VK_NULL_HANDLE; |
| 1183 | } |
| 1184 | freeSecondaryCbs[i].clear(); |
| 1185 | ofr.cbWrapper[i]->cb = VK_NULL_HANDLE; |
| 1186 | } |
| 1187 | |
| 1188 | if (!importedDevice && dev) { |
| 1189 | df->vkDestroyDevice(dev, nullptr); |
| 1190 | inst->resetDeviceFunctions(device: dev); |
| 1191 | dev = VK_NULL_HANDLE; |
| 1192 | } |
| 1193 | |
| 1194 | f = nullptr; |
| 1195 | df = nullptr; |
| 1196 | |
| 1197 | importedDevice = false; |
| 1198 | importedAllocator = false; |
| 1199 | } |
| 1200 | |
| 1201 | QRhi::AdapterList QRhiVulkan::enumerateAdaptersBeforeCreate(QRhiNativeHandles *nativeHandles) const |
| 1202 | { |
| 1203 | VkPhysicalDevice requestedPhysDev = VK_NULL_HANDLE; |
| 1204 | if (nativeHandles) { |
| 1205 | QRhiVulkanNativeHandles *h = static_cast<QRhiVulkanNativeHandles *>(nativeHandles); |
| 1206 | requestedPhysDev = h->physDev; |
| 1207 | } |
| 1208 | |
| 1209 | QRhi::AdapterList list; |
| 1210 | QVulkanFunctions *f = inst->functions(); |
| 1211 | uint32_t physDevCount = 0; |
| 1212 | f->vkEnumeratePhysicalDevices(inst->vkInstance(), &physDevCount, nullptr); |
| 1213 | if (!physDevCount) |
| 1214 | return {}; |
| 1215 | |
| 1216 | QVarLengthArray<VkPhysicalDevice, 4> physDevs(physDevCount); |
| 1217 | VkResult err = f->vkEnumeratePhysicalDevices(inst->vkInstance(), &physDevCount, physDevs.data()); |
| 1218 | if (err != VK_SUCCESS || !physDevCount) |
| 1219 | return {}; |
| 1220 | |
| 1221 | VkPhysicalDeviceProperties physDevProperties = {}; |
| 1222 | for (uint32_t i = 0; i < physDevCount; ++i) { |
| 1223 | if (requestedPhysDev && physDevs[i] != requestedPhysDev) |
| 1224 | continue; |
| 1225 | |
| 1226 | f->vkGetPhysicalDeviceProperties(physDevs[i], &physDevProperties); |
| 1227 | QVulkanAdapter *a = new QVulkanAdapter; |
| 1228 | a->physDev = physDevs[i]; |
| 1229 | fillDriverInfo(info: &a->adapterInfo, physDevProperties); |
| 1230 | list.append(t: a); |
| 1231 | } |
| 1232 | |
| 1233 | return list; |
| 1234 | } |
| 1235 | |
| 1236 | QRhiDriverInfo QVulkanAdapter::info() const |
| 1237 | { |
| 1238 | return adapterInfo; |
| 1239 | } |
| 1240 | |
| 1241 | VkResult QRhiVulkan::createDescriptorPool(VkDescriptorPool *pool) |
| 1242 | { |
| 1243 | VkDescriptorPoolSize descPoolSizes[] = { |
| 1244 | { .type: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .descriptorCount: QVK_UNIFORM_BUFFERS_PER_POOL }, |
| 1245 | { .type: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, .descriptorCount: QVK_UNIFORM_BUFFERS_PER_POOL }, |
| 1246 | { .type: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .descriptorCount: QVK_COMBINED_IMAGE_SAMPLERS_PER_POOL }, |
| 1247 | { .type: VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount: QVK_STORAGE_BUFFERS_PER_POOL }, |
| 1248 | { .type: VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .descriptorCount: QVK_STORAGE_IMAGES_PER_POOL } |
| 1249 | }; |
| 1250 | VkDescriptorPoolCreateInfo descPoolInfo = {}; |
| 1251 | descPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 1252 | // Do not enable vkFreeDescriptorSets - sets are never freed on their own |
| 1253 | // (good so no trouble with fragmentation), they just deref their pool |
| 1254 | // which is then reset at some point (or not). |
| 1255 | descPoolInfo.flags = 0; |
| 1256 | descPoolInfo.maxSets = QVK_DESC_SETS_PER_POOL; |
| 1257 | descPoolInfo.poolSizeCount = sizeof(descPoolSizes) / sizeof(descPoolSizes[0]); |
| 1258 | descPoolInfo.pPoolSizes = descPoolSizes; |
| 1259 | return df->vkCreateDescriptorPool(dev, &descPoolInfo, nullptr, pool); |
| 1260 | } |
| 1261 | |
| 1262 | bool QRhiVulkan::allocateDescriptorSet(VkDescriptorSetAllocateInfo *allocInfo, VkDescriptorSet *result, int *resultPoolIndex) |
| 1263 | { |
| 1264 | auto tryAllocate = [this, allocInfo, result](int poolIndex) { |
| 1265 | allocInfo->descriptorPool = descriptorPools[poolIndex].pool; |
| 1266 | VkResult r = df->vkAllocateDescriptorSets(dev, allocInfo, result); |
| 1267 | if (r == VK_SUCCESS) |
| 1268 | descriptorPools[poolIndex].refCount += 1; |
| 1269 | return r; |
| 1270 | }; |
| 1271 | |
| 1272 | int lastPoolIdx = descriptorPools.size() - 1; |
| 1273 | for (int i = lastPoolIdx; i >= 0; --i) { |
| 1274 | if (descriptorPools[i].refCount == 0) { |
| 1275 | df->vkResetDescriptorPool(dev, descriptorPools[i].pool, 0); |
| 1276 | descriptorPools[i].allocedDescSets = 0; |
| 1277 | } |
| 1278 | if (descriptorPools[i].allocedDescSets + int(allocInfo->descriptorSetCount) <= QVK_DESC_SETS_PER_POOL) { |
| 1279 | VkResult err = tryAllocate(i); |
| 1280 | if (err == VK_SUCCESS) { |
| 1281 | descriptorPools[i].allocedDescSets += allocInfo->descriptorSetCount; |
| 1282 | *resultPoolIndex = i; |
| 1283 | return true; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | VkDescriptorPool newPool; |
| 1289 | VkResult poolErr = createDescriptorPool(pool: &newPool); |
| 1290 | if (poolErr == VK_SUCCESS) { |
| 1291 | descriptorPools.append(t: newPool); |
| 1292 | lastPoolIdx = descriptorPools.size() - 1; |
| 1293 | VkResult err = tryAllocate(lastPoolIdx); |
| 1294 | if (err != VK_SUCCESS) { |
| 1295 | qWarning(msg: "Failed to allocate descriptor set from new pool too, giving up: %d" , err); |
| 1296 | return false; |
| 1297 | } |
| 1298 | descriptorPools[lastPoolIdx].allocedDescSets += allocInfo->descriptorSetCount; |
| 1299 | *resultPoolIndex = lastPoolIdx; |
| 1300 | return true; |
| 1301 | } else { |
| 1302 | qWarning(msg: "Failed to allocate new descriptor pool: %d" , poolErr); |
| 1303 | return false; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | static inline VkFormat toVkTextureFormat(QRhiTexture::Format format, QRhiTexture::Flags flags) |
| 1308 | { |
| 1309 | const bool srgb = flags.testFlag(flag: QRhiTexture::sRGB); |
| 1310 | switch (format) { |
| 1311 | case QRhiTexture::RGBA8: |
| 1312 | return srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM; |
| 1313 | case QRhiTexture::BGRA8: |
| 1314 | return srgb ? VK_FORMAT_B8G8R8A8_SRGB : VK_FORMAT_B8G8R8A8_UNORM; |
| 1315 | case QRhiTexture::R8: |
| 1316 | return srgb ? VK_FORMAT_R8_SRGB : VK_FORMAT_R8_UNORM; |
| 1317 | case QRhiTexture::RG8: |
| 1318 | return srgb ? VK_FORMAT_R8G8_SRGB : VK_FORMAT_R8G8_UNORM; |
| 1319 | case QRhiTexture::R16: |
| 1320 | return VK_FORMAT_R16_UNORM; |
| 1321 | case QRhiTexture::RG16: |
| 1322 | return VK_FORMAT_R16G16_UNORM; |
| 1323 | case QRhiTexture::RED_OR_ALPHA8: |
| 1324 | return VK_FORMAT_R8_UNORM; |
| 1325 | |
| 1326 | case QRhiTexture::RGBA16F: |
| 1327 | return VK_FORMAT_R16G16B16A16_SFLOAT; |
| 1328 | case QRhiTexture::RGBA32F: |
| 1329 | return VK_FORMAT_R32G32B32A32_SFLOAT; |
| 1330 | case QRhiTexture::R16F: |
| 1331 | return VK_FORMAT_R16_SFLOAT; |
| 1332 | case QRhiTexture::R32F: |
| 1333 | return VK_FORMAT_R32_SFLOAT; |
| 1334 | |
| 1335 | case QRhiTexture::RGB10A2: |
| 1336 | // intentionally A2B10G10R10, not A2R10G10B10 |
| 1337 | return VK_FORMAT_A2B10G10R10_UNORM_PACK32; |
| 1338 | |
| 1339 | case QRhiTexture::R8SI: |
| 1340 | return VK_FORMAT_R8_SINT; |
| 1341 | case QRhiTexture::R32SI: |
| 1342 | return VK_FORMAT_R32_SINT; |
| 1343 | case QRhiTexture::RG32SI: |
| 1344 | return VK_FORMAT_R32G32_SINT; |
| 1345 | case QRhiTexture::RGBA32SI: |
| 1346 | return VK_FORMAT_R32G32B32A32_SINT; |
| 1347 | |
| 1348 | case QRhiTexture::R8UI: |
| 1349 | return VK_FORMAT_R8_UINT; |
| 1350 | case QRhiTexture::R32UI: |
| 1351 | return VK_FORMAT_R32_UINT; |
| 1352 | case QRhiTexture::RG32UI: |
| 1353 | return VK_FORMAT_R32G32_UINT; |
| 1354 | case QRhiTexture::RGBA32UI: |
| 1355 | return VK_FORMAT_R32G32B32A32_UINT; |
| 1356 | |
| 1357 | case QRhiTexture::D16: |
| 1358 | return VK_FORMAT_D16_UNORM; |
| 1359 | case QRhiTexture::D24: |
| 1360 | return VK_FORMAT_X8_D24_UNORM_PACK32; |
| 1361 | case QRhiTexture::D24S8: |
| 1362 | return VK_FORMAT_D24_UNORM_S8_UINT; |
| 1363 | case QRhiTexture::D32F: |
| 1364 | return VK_FORMAT_D32_SFLOAT; |
| 1365 | case QRhiTexture::D32FS8: |
| 1366 | return VK_FORMAT_D32_SFLOAT_S8_UINT; |
| 1367 | |
| 1368 | case QRhiTexture::BC1: |
| 1369 | return srgb ? VK_FORMAT_BC1_RGB_SRGB_BLOCK : VK_FORMAT_BC1_RGB_UNORM_BLOCK; |
| 1370 | case QRhiTexture::BC2: |
| 1371 | return srgb ? VK_FORMAT_BC2_SRGB_BLOCK : VK_FORMAT_BC2_UNORM_BLOCK; |
| 1372 | case QRhiTexture::BC3: |
| 1373 | return srgb ? VK_FORMAT_BC3_SRGB_BLOCK : VK_FORMAT_BC3_UNORM_BLOCK; |
| 1374 | case QRhiTexture::BC4: |
| 1375 | return VK_FORMAT_BC4_UNORM_BLOCK; |
| 1376 | case QRhiTexture::BC5: |
| 1377 | return VK_FORMAT_BC5_UNORM_BLOCK; |
| 1378 | case QRhiTexture::BC6H: |
| 1379 | return VK_FORMAT_BC6H_UFLOAT_BLOCK; |
| 1380 | case QRhiTexture::BC7: |
| 1381 | return srgb ? VK_FORMAT_BC7_SRGB_BLOCK : VK_FORMAT_BC7_UNORM_BLOCK; |
| 1382 | |
| 1383 | case QRhiTexture::ETC2_RGB8: |
| 1384 | return srgb ? VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK; |
| 1385 | case QRhiTexture::ETC2_RGB8A1: |
| 1386 | return srgb ? VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK; |
| 1387 | case QRhiTexture::ETC2_RGBA8: |
| 1388 | return srgb ? VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK; |
| 1389 | |
| 1390 | case QRhiTexture::ASTC_4x4: |
| 1391 | return srgb ? VK_FORMAT_ASTC_4x4_SRGB_BLOCK : VK_FORMAT_ASTC_4x4_UNORM_BLOCK; |
| 1392 | case QRhiTexture::ASTC_5x4: |
| 1393 | return srgb ? VK_FORMAT_ASTC_5x4_SRGB_BLOCK : VK_FORMAT_ASTC_5x4_UNORM_BLOCK; |
| 1394 | case QRhiTexture::ASTC_5x5: |
| 1395 | return srgb ? VK_FORMAT_ASTC_5x5_SRGB_BLOCK : VK_FORMAT_ASTC_5x5_UNORM_BLOCK; |
| 1396 | case QRhiTexture::ASTC_6x5: |
| 1397 | return srgb ? VK_FORMAT_ASTC_6x5_SRGB_BLOCK : VK_FORMAT_ASTC_6x5_UNORM_BLOCK; |
| 1398 | case QRhiTexture::ASTC_6x6: |
| 1399 | return srgb ? VK_FORMAT_ASTC_6x6_SRGB_BLOCK : VK_FORMAT_ASTC_6x6_UNORM_BLOCK; |
| 1400 | case QRhiTexture::ASTC_8x5: |
| 1401 | return srgb ? VK_FORMAT_ASTC_8x5_SRGB_BLOCK : VK_FORMAT_ASTC_8x5_UNORM_BLOCK; |
| 1402 | case QRhiTexture::ASTC_8x6: |
| 1403 | return srgb ? VK_FORMAT_ASTC_8x6_SRGB_BLOCK : VK_FORMAT_ASTC_8x6_UNORM_BLOCK; |
| 1404 | case QRhiTexture::ASTC_8x8: |
| 1405 | return srgb ? VK_FORMAT_ASTC_8x8_SRGB_BLOCK : VK_FORMAT_ASTC_8x8_UNORM_BLOCK; |
| 1406 | case QRhiTexture::ASTC_10x5: |
| 1407 | return srgb ? VK_FORMAT_ASTC_10x5_SRGB_BLOCK : VK_FORMAT_ASTC_10x5_UNORM_BLOCK; |
| 1408 | case QRhiTexture::ASTC_10x6: |
| 1409 | return srgb ? VK_FORMAT_ASTC_10x6_SRGB_BLOCK : VK_FORMAT_ASTC_10x6_UNORM_BLOCK; |
| 1410 | case QRhiTexture::ASTC_10x8: |
| 1411 | return srgb ? VK_FORMAT_ASTC_10x8_SRGB_BLOCK : VK_FORMAT_ASTC_10x8_UNORM_BLOCK; |
| 1412 | case QRhiTexture::ASTC_10x10: |
| 1413 | return srgb ? VK_FORMAT_ASTC_10x10_SRGB_BLOCK : VK_FORMAT_ASTC_10x10_UNORM_BLOCK; |
| 1414 | case QRhiTexture::ASTC_12x10: |
| 1415 | return srgb ? VK_FORMAT_ASTC_12x10_SRGB_BLOCK : VK_FORMAT_ASTC_12x10_UNORM_BLOCK; |
| 1416 | case QRhiTexture::ASTC_12x12: |
| 1417 | return srgb ? VK_FORMAT_ASTC_12x12_SRGB_BLOCK : VK_FORMAT_ASTC_12x12_UNORM_BLOCK; |
| 1418 | |
| 1419 | default: |
| 1420 | Q_UNREACHABLE_RETURN(VK_FORMAT_R8G8B8A8_UNORM); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | static inline QRhiTexture::Format swapchainReadbackTextureFormat(VkFormat format, QRhiTexture::Flags *flags) |
| 1425 | { |
| 1426 | switch (format) { |
| 1427 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 1428 | return QRhiTexture::RGBA8; |
| 1429 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 1430 | if (flags) |
| 1431 | (*flags) |= QRhiTexture::sRGB; |
| 1432 | return QRhiTexture::RGBA8; |
| 1433 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 1434 | return QRhiTexture::BGRA8; |
| 1435 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 1436 | if (flags) |
| 1437 | (*flags) |= QRhiTexture::sRGB; |
| 1438 | return QRhiTexture::BGRA8; |
| 1439 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 1440 | return QRhiTexture::RGBA16F; |
| 1441 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 1442 | return QRhiTexture::RGBA32F; |
| 1443 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
| 1444 | return QRhiTexture::RGB10A2; |
| 1445 | default: |
| 1446 | qWarning(msg: "VkFormat %d cannot be read back" , format); |
| 1447 | break; |
| 1448 | } |
| 1449 | return QRhiTexture::UnknownFormat; |
| 1450 | } |
| 1451 | |
| 1452 | static constexpr inline bool isDepthTextureFormat(QRhiTexture::Format format) |
| 1453 | { |
| 1454 | switch (format) { |
| 1455 | case QRhiTexture::Format::D16: |
| 1456 | case QRhiTexture::Format::D24: |
| 1457 | case QRhiTexture::Format::D24S8: |
| 1458 | case QRhiTexture::Format::D32F: |
| 1459 | case QRhiTexture::Format::D32FS8: |
| 1460 | return true; |
| 1461 | |
| 1462 | default: |
| 1463 | return false; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | static constexpr inline bool isStencilTextureFormat(QRhiTexture::Format format) |
| 1468 | { |
| 1469 | switch (format) { |
| 1470 | case QRhiTexture::Format::D24S8: |
| 1471 | case QRhiTexture::Format::D32FS8: |
| 1472 | return true; |
| 1473 | |
| 1474 | default: |
| 1475 | return false; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | static constexpr inline VkImageAspectFlags aspectMaskForTextureFormat(QRhiTexture::Format format) |
| 1480 | { |
| 1481 | if (isDepthTextureFormat(format)) { |
| 1482 | if (isStencilTextureFormat(format)) |
| 1483 | return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1484 | else |
| 1485 | return VK_IMAGE_ASPECT_DEPTH_BIT; |
| 1486 | } else { |
| 1487 | return VK_IMAGE_ASPECT_COLOR_BIT; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // Transient images ("render buffers") backed by lazily allocated memory are |
| 1492 | // managed manually without going through vk_mem_alloc since it does not offer |
| 1493 | // any support for such images. This should be ok since in practice there |
| 1494 | // should be very few of such images. |
| 1495 | |
| 1496 | uint32_t QRhiVulkan::chooseTransientImageMemType(VkImage img, uint32_t startIndex) |
| 1497 | { |
| 1498 | VkPhysicalDeviceMemoryProperties physDevMemProps; |
| 1499 | f->vkGetPhysicalDeviceMemoryProperties(physDev, &physDevMemProps); |
| 1500 | |
| 1501 | VkMemoryRequirements memReq; |
| 1502 | df->vkGetImageMemoryRequirements(dev, img, &memReq); |
| 1503 | uint32_t memTypeIndex = uint32_t(-1); |
| 1504 | |
| 1505 | if (memReq.memoryTypeBits) { |
| 1506 | // Find a device local + lazily allocated, or at least device local memtype. |
| 1507 | const VkMemoryType *memType = physDevMemProps.memoryTypes; |
| 1508 | bool foundDevLocal = false; |
| 1509 | for (uint32_t i = startIndex; i < physDevMemProps.memoryTypeCount; ++i) { |
| 1510 | if (memReq.memoryTypeBits & (1 << i)) { |
| 1511 | if (memType[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { |
| 1512 | if (!foundDevLocal) { |
| 1513 | foundDevLocal = true; |
| 1514 | memTypeIndex = i; |
| 1515 | } |
| 1516 | if (memType[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { |
| 1517 | memTypeIndex = i; |
| 1518 | break; |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | return memTypeIndex; |
| 1526 | } |
| 1527 | |
| 1528 | bool QRhiVulkan::createTransientImage(VkFormat format, |
| 1529 | const QSize &pixelSize, |
| 1530 | VkImageUsageFlags usage, |
| 1531 | VkImageAspectFlags aspectMask, |
| 1532 | VkSampleCountFlagBits samples, |
| 1533 | VkDeviceMemory *mem, |
| 1534 | VkImage *images, |
| 1535 | VkImageView *views, |
| 1536 | int count) |
| 1537 | { |
| 1538 | VkMemoryRequirements memReq; |
| 1539 | VkResult err; |
| 1540 | |
| 1541 | for (int i = 0; i < count; ++i) { |
| 1542 | VkImageCreateInfo imgInfo = {}; |
| 1543 | imgInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 1544 | imgInfo.imageType = VK_IMAGE_TYPE_2D; |
| 1545 | imgInfo.format = format; |
| 1546 | imgInfo.extent.width = uint32_t(pixelSize.width()); |
| 1547 | imgInfo.extent.height = uint32_t(pixelSize.height()); |
| 1548 | imgInfo.extent.depth = 1; |
| 1549 | imgInfo.mipLevels = imgInfo.arrayLayers = 1; |
| 1550 | imgInfo.samples = samples; |
| 1551 | imgInfo.tiling = VK_IMAGE_TILING_OPTIMAL; |
| 1552 | imgInfo.usage = usage | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT; |
| 1553 | imgInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1554 | |
| 1555 | err = df->vkCreateImage(dev, &imgInfo, nullptr, images + i); |
| 1556 | if (err != VK_SUCCESS) { |
| 1557 | qWarning(msg: "Failed to create image: %d" , err); |
| 1558 | return false; |
| 1559 | } |
| 1560 | |
| 1561 | // Assume the reqs are the same since the images are same in every way. |
| 1562 | // Still, call GetImageMemReq for every image, in order to prevent the |
| 1563 | // validation layer from complaining. |
| 1564 | df->vkGetImageMemoryRequirements(dev, images[i], &memReq); |
| 1565 | } |
| 1566 | |
| 1567 | VkMemoryAllocateInfo memInfo = {}; |
| 1568 | memInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 1569 | memInfo.allocationSize = aligned(v: memReq.size, byteAlign: memReq.alignment) * VkDeviceSize(count); |
| 1570 | |
| 1571 | uint32_t startIndex = 0; |
| 1572 | do { |
| 1573 | memInfo.memoryTypeIndex = chooseTransientImageMemType(img: images[0], startIndex); |
| 1574 | if (memInfo.memoryTypeIndex == uint32_t(-1)) { |
| 1575 | qWarning(msg: "No suitable memory type found" ); |
| 1576 | return false; |
| 1577 | } |
| 1578 | startIndex = memInfo.memoryTypeIndex + 1; |
| 1579 | err = df->vkAllocateMemory(dev, &memInfo, nullptr, mem); |
| 1580 | if (err != VK_SUCCESS && err != VK_ERROR_OUT_OF_DEVICE_MEMORY) { |
| 1581 | qWarning(msg: "Failed to allocate image memory: %d" , err); |
| 1582 | return false; |
| 1583 | } |
| 1584 | } while (err != VK_SUCCESS); |
| 1585 | |
| 1586 | VkDeviceSize ofs = 0; |
| 1587 | for (int i = 0; i < count; ++i) { |
| 1588 | err = df->vkBindImageMemory(dev, images[i], *mem, ofs); |
| 1589 | if (err != VK_SUCCESS) { |
| 1590 | qWarning(msg: "Failed to bind image memory: %d" , err); |
| 1591 | return false; |
| 1592 | } |
| 1593 | ofs += aligned(v: memReq.size, byteAlign: memReq.alignment); |
| 1594 | |
| 1595 | VkImageViewCreateInfo imgViewInfo = {}; |
| 1596 | imgViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 1597 | imgViewInfo.image = images[i]; |
| 1598 | imgViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; |
| 1599 | imgViewInfo.format = format; |
| 1600 | imgViewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 1601 | imgViewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 1602 | imgViewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 1603 | imgViewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 1604 | imgViewInfo.subresourceRange.aspectMask = aspectMask; |
| 1605 | imgViewInfo.subresourceRange.levelCount = imgViewInfo.subresourceRange.layerCount = 1; |
| 1606 | |
| 1607 | err = df->vkCreateImageView(dev, &imgViewInfo, nullptr, views + i); |
| 1608 | if (err != VK_SUCCESS) { |
| 1609 | qWarning(msg: "Failed to create image view: %d" , err); |
| 1610 | return false; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | return true; |
| 1615 | } |
| 1616 | |
| 1617 | VkFormat QRhiVulkan::optimalDepthStencilFormat() |
| 1618 | { |
| 1619 | if (optimalDsFormat != VK_FORMAT_UNDEFINED) |
| 1620 | return optimalDsFormat; |
| 1621 | |
| 1622 | const VkFormat dsFormatCandidates[] = { |
| 1623 | VK_FORMAT_D24_UNORM_S8_UINT, |
| 1624 | VK_FORMAT_D32_SFLOAT_S8_UINT, |
| 1625 | VK_FORMAT_D16_UNORM_S8_UINT |
| 1626 | }; |
| 1627 | const int dsFormatCandidateCount = sizeof(dsFormatCandidates) / sizeof(VkFormat); |
| 1628 | int dsFormatIdx = 0; |
| 1629 | while (dsFormatIdx < dsFormatCandidateCount) { |
| 1630 | optimalDsFormat = dsFormatCandidates[dsFormatIdx]; |
| 1631 | VkFormatProperties fmtProp; |
| 1632 | f->vkGetPhysicalDeviceFormatProperties(physDev, optimalDsFormat, &fmtProp); |
| 1633 | if (fmtProp.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) |
| 1634 | break; |
| 1635 | ++dsFormatIdx; |
| 1636 | } |
| 1637 | if (dsFormatIdx == dsFormatCandidateCount) |
| 1638 | qWarning(msg: "Failed to find an optimal depth-stencil format" ); |
| 1639 | |
| 1640 | return optimalDsFormat; |
| 1641 | } |
| 1642 | |
| 1643 | struct MultiViewRenderPassSetupHelper |
| 1644 | { |
| 1645 | bool prepare(VkRenderPassCreateInfo *rpInfo, int multiViewCount, bool multiViewCap) |
| 1646 | { |
| 1647 | if (multiViewCount < 2) |
| 1648 | return true; |
| 1649 | if (!multiViewCap) { |
| 1650 | qWarning(msg: "Cannot create multiview render pass without support for the Vulkan 1.1 multiview feature" ); |
| 1651 | return false; |
| 1652 | } |
| 1653 | #ifdef VK_VERSION_1_1 |
| 1654 | uint32_t allViewsMask = 0; |
| 1655 | for (uint32_t i = 0; i < uint32_t(multiViewCount); ++i) |
| 1656 | allViewsMask |= (1 << i); |
| 1657 | multiViewMask = allViewsMask; |
| 1658 | multiViewCorrelationMask = allViewsMask; |
| 1659 | multiViewInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO; |
| 1660 | multiViewInfo.subpassCount = 1; |
| 1661 | multiViewInfo.pViewMasks = &multiViewMask; |
| 1662 | multiViewInfo.correlationMaskCount = 1; |
| 1663 | multiViewInfo.pCorrelationMasks = &multiViewCorrelationMask; |
| 1664 | rpInfo->pNext = &multiViewInfo; |
| 1665 | #endif |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
| 1669 | #ifdef VK_VERSION_1_1 |
| 1670 | VkRenderPassMultiviewCreateInfo multiViewInfo = {}; |
| 1671 | uint32_t multiViewMask = 0; |
| 1672 | uint32_t multiViewCorrelationMask = 0; |
| 1673 | #endif |
| 1674 | }; |
| 1675 | |
| 1676 | #ifdef VK_KHR_create_renderpass2 |
| 1677 | // Effectively converts a VkRenderPassCreateInfo into a VkRenderPassCreateInfo2, |
| 1678 | // adding depth-stencil resolve and VRS support. Incorporates multiview into the |
| 1679 | // info structs (no chaining needed). Assumes a single subpass. |
| 1680 | struct RenderPass2SetupHelper |
| 1681 | { |
| 1682 | RenderPass2SetupHelper(QRhiVulkan *rhiD) : rhiD(rhiD) { } |
| 1683 | |
| 1684 | bool prepare(VkRenderPassCreateInfo2 *rpInfo2, const VkRenderPassCreateInfo *rpInfo, const QVkRenderPassDescriptor *rpD, int multiViewCount) { |
| 1685 | *rpInfo2 = {}; |
| 1686 | |
| 1687 | viewMask = 0; |
| 1688 | if (multiViewCount >= 2) { |
| 1689 | for (uint32_t i = 0; i < uint32_t(multiViewCount); ++i) |
| 1690 | viewMask |= (1 << i); |
| 1691 | } |
| 1692 | |
| 1693 | attDescs2.resize(sz: rpInfo->attachmentCount); |
| 1694 | for (qsizetype i = 0; i < attDescs2.count(); ++i) { |
| 1695 | VkAttachmentDescription2KHR &att2(attDescs2[i]); |
| 1696 | const VkAttachmentDescription &att(rpInfo->pAttachments[i]); |
| 1697 | att2 = {}; |
| 1698 | att2.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2; |
| 1699 | att2.flags = att.flags; |
| 1700 | att2.format = att.format; |
| 1701 | att2.samples = att.samples; |
| 1702 | att2.loadOp = att.loadOp; |
| 1703 | att2.storeOp = att.storeOp; |
| 1704 | att2.stencilLoadOp = att.stencilLoadOp; |
| 1705 | att2.stencilStoreOp = att.stencilStoreOp; |
| 1706 | att2.initialLayout = att.initialLayout; |
| 1707 | att2.finalLayout = att.finalLayout; |
| 1708 | } |
| 1709 | |
| 1710 | attRefs2.clear(); |
| 1711 | subpass2 = {}; |
| 1712 | subpass2.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2_KHR; |
| 1713 | const VkSubpassDescription &subpassDesc(rpInfo->pSubpasses[0]); |
| 1714 | subpass2.flags = subpassDesc.flags; |
| 1715 | subpass2.pipelineBindPoint = subpassDesc.pipelineBindPoint; |
| 1716 | if (multiViewCount >= 2) |
| 1717 | subpass2.viewMask = viewMask; |
| 1718 | |
| 1719 | // color attachment refs |
| 1720 | qsizetype startIndex = attRefs2.count(); |
| 1721 | for (uint32_t j = 0; j < subpassDesc.colorAttachmentCount; ++j) { |
| 1722 | attRefs2.append(t: {}); |
| 1723 | VkAttachmentReference2KHR &attref2(attRefs2.last()); |
| 1724 | const VkAttachmentReference &attref(subpassDesc.pColorAttachments[j]); |
| 1725 | attref2.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR; |
| 1726 | attref2.attachment = attref.attachment; |
| 1727 | attref2.layout = attref.layout; |
| 1728 | attref2.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1729 | } |
| 1730 | subpass2.colorAttachmentCount = subpassDesc.colorAttachmentCount; |
| 1731 | subpass2.pColorAttachments = attRefs2.constData() + startIndex; |
| 1732 | |
| 1733 | // color resolve refs |
| 1734 | if (subpassDesc.pResolveAttachments) { |
| 1735 | startIndex = attRefs2.count(); |
| 1736 | for (uint32_t j = 0; j < subpassDesc.colorAttachmentCount; ++j) { |
| 1737 | attRefs2.append(t: {}); |
| 1738 | VkAttachmentReference2KHR &attref2(attRefs2.last()); |
| 1739 | const VkAttachmentReference &attref(subpassDesc.pResolveAttachments[j]); |
| 1740 | attref2.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR; |
| 1741 | attref2.attachment = attref.attachment; |
| 1742 | attref2.layout = attref.layout; |
| 1743 | attref2.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1744 | } |
| 1745 | subpass2.pResolveAttachments = attRefs2.constData() + startIndex; |
| 1746 | } |
| 1747 | |
| 1748 | // depth-stencil ref |
| 1749 | if (subpassDesc.pDepthStencilAttachment) { |
| 1750 | startIndex = attRefs2.count(); |
| 1751 | attRefs2.append(t: {}); |
| 1752 | VkAttachmentReference2KHR &attref2(attRefs2.last()); |
| 1753 | const VkAttachmentReference &attref(*subpassDesc.pDepthStencilAttachment); |
| 1754 | attref2.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR; |
| 1755 | attref2.attachment = attref.attachment; |
| 1756 | attref2.layout = attref.layout; |
| 1757 | attref2.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1758 | subpass2.pDepthStencilAttachment = attRefs2.constData() + startIndex; |
| 1759 | } |
| 1760 | |
| 1761 | // depth-stencil resolve ref |
| 1762 | #ifdef VK_KHR_depth_stencil_resolve |
| 1763 | dsResolveDesc = {}; |
| 1764 | if (rpD->hasDepthStencilResolve) { |
| 1765 | startIndex = attRefs2.count(); |
| 1766 | attRefs2.append(t: {}); |
| 1767 | VkAttachmentReference2KHR &attref2(attRefs2.last()); |
| 1768 | attref2.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR; |
| 1769 | attref2.attachment = rpD->dsResolveRef.attachment; |
| 1770 | attref2.layout = rpD->dsResolveRef.layout; |
| 1771 | attref2.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1772 | dsResolveDesc.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE_KHR; |
| 1773 | dsResolveDesc.depthResolveMode = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT; |
| 1774 | dsResolveDesc.stencilResolveMode = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT; |
| 1775 | dsResolveDesc.pDepthStencilResolveAttachment = attRefs2.constData() + startIndex; |
| 1776 | addToChain(head: &subpass2, entry: &dsResolveDesc); |
| 1777 | } |
| 1778 | #endif |
| 1779 | |
| 1780 | #ifdef VK_KHR_fragment_shading_rate |
| 1781 | shadingRateAttInfo = {}; |
| 1782 | if (rpD->hasShadingRateMap) { |
| 1783 | startIndex = attRefs2.count(); |
| 1784 | attRefs2.append(t: {}); |
| 1785 | VkAttachmentReference2KHR &attref2(attRefs2.last()); |
| 1786 | attref2.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR; |
| 1787 | attref2.attachment = rpD->shadingRateRef.attachment; |
| 1788 | attref2.layout = rpD->shadingRateRef.layout; |
| 1789 | shadingRateAttInfo.sType = VK_STRUCTURE_TYPE_FRAGMENT_SHADING_RATE_ATTACHMENT_INFO_KHR; |
| 1790 | shadingRateAttInfo.pFragmentShadingRateAttachment = attRefs2.constData() + startIndex; |
| 1791 | shadingRateAttInfo.shadingRateAttachmentTexelSize.width = rhiD->caps.imageBasedShadingRateTileSize; |
| 1792 | shadingRateAttInfo.shadingRateAttachmentTexelSize.height = rhiD->caps.imageBasedShadingRateTileSize; |
| 1793 | addToChain(head: &subpass2, entry: &shadingRateAttInfo); |
| 1794 | } |
| 1795 | #endif |
| 1796 | |
| 1797 | // subpass dependencies, typically 0, 1, 2 of them, |
| 1798 | // depending on targeting swapchain or texture |
| 1799 | subpassDeps2.clear(); |
| 1800 | for (uint32_t i = 0; i < rpInfo->dependencyCount; ++i) { |
| 1801 | const VkSubpassDependency &dep(rpInfo->pDependencies[i]); |
| 1802 | subpassDeps2.append(t: {}); |
| 1803 | VkSubpassDependency2 &dep2(subpassDeps2.last()); |
| 1804 | dep2.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2_KHR; |
| 1805 | dep2.srcSubpass = dep.srcSubpass; |
| 1806 | dep2.dstSubpass = dep.dstSubpass; |
| 1807 | dep2.srcStageMask = dep.srcStageMask; |
| 1808 | dep2.dstStageMask = dep.dstStageMask; |
| 1809 | dep2.srcAccessMask = dep.srcAccessMask; |
| 1810 | dep2.dstAccessMask = dep.dstAccessMask; |
| 1811 | dep2.dependencyFlags = dep.dependencyFlags; |
| 1812 | } |
| 1813 | |
| 1814 | rpInfo2->sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR; |
| 1815 | rpInfo2->pNext = nullptr; // the 1.1 VkRenderPassMultiviewCreateInfo is part of the '2' structs |
| 1816 | rpInfo2->flags = rpInfo->flags; |
| 1817 | rpInfo2->attachmentCount = rpInfo->attachmentCount; |
| 1818 | rpInfo2->pAttachments = attDescs2.constData(); |
| 1819 | rpInfo2->subpassCount = 1; |
| 1820 | rpInfo2->pSubpasses = &subpass2; |
| 1821 | rpInfo2->dependencyCount = subpassDeps2.count(); |
| 1822 | rpInfo2->pDependencies = !subpassDeps2.isEmpty() ? subpassDeps2.constData() : nullptr; |
| 1823 | if (multiViewCount >= 2) { |
| 1824 | rpInfo2->correlatedViewMaskCount = 1; |
| 1825 | rpInfo2->pCorrelatedViewMasks = &viewMask; |
| 1826 | } |
| 1827 | return true; |
| 1828 | } |
| 1829 | |
| 1830 | QRhiVulkan *rhiD; |
| 1831 | QVarLengthArray<VkAttachmentDescription2KHR, 8> attDescs2; |
| 1832 | QVarLengthArray<VkAttachmentReference2KHR, 8> attRefs2; |
| 1833 | VkSubpassDescription2KHR subpass2; |
| 1834 | QVarLengthArray<VkSubpassDependency2KHR, 4> subpassDeps2; |
| 1835 | #ifdef VK_KHR_depth_stencil_resolve |
| 1836 | VkSubpassDescriptionDepthStencilResolveKHR dsResolveDesc; |
| 1837 | #endif |
| 1838 | #ifdef VK_KHR_fragment_shading_rate |
| 1839 | VkFragmentShadingRateAttachmentInfoKHR shadingRateAttInfo; |
| 1840 | #endif |
| 1841 | uint32_t viewMask; |
| 1842 | }; |
| 1843 | #endif // VK_KHR_create_renderpass2 |
| 1844 | |
| 1845 | static void fillRenderPassCreateInfo(VkRenderPassCreateInfo *rpInfo, |
| 1846 | VkSubpassDescription *subpassDesc, |
| 1847 | QVkRenderPassDescriptor *rpD) |
| 1848 | { |
| 1849 | memset(s: subpassDesc, c: 0, n: sizeof(VkSubpassDescription)); |
| 1850 | subpassDesc->pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 1851 | subpassDesc->colorAttachmentCount = uint32_t(rpD->colorRefs.size()); |
| 1852 | subpassDesc->pColorAttachments = !rpD->colorRefs.isEmpty() ? rpD->colorRefs.constData() : nullptr; |
| 1853 | subpassDesc->pDepthStencilAttachment = rpD->hasDepthStencil ? &rpD->dsRef : nullptr; |
| 1854 | subpassDesc->pResolveAttachments = !rpD->resolveRefs.isEmpty() ? rpD->resolveRefs.constData() : nullptr; |
| 1855 | |
| 1856 | memset(s: rpInfo, c: 0, n: sizeof(VkRenderPassCreateInfo)); |
| 1857 | rpInfo->sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
| 1858 | rpInfo->attachmentCount = uint32_t(rpD->attDescs.size()); |
| 1859 | rpInfo->pAttachments = rpD->attDescs.constData(); |
| 1860 | rpInfo->subpassCount = 1; |
| 1861 | rpInfo->pSubpasses = subpassDesc; |
| 1862 | rpInfo->dependencyCount = uint32_t(rpD->subpassDeps.size()); |
| 1863 | rpInfo->pDependencies = !rpD->subpassDeps.isEmpty() ? rpD->subpassDeps.constData() : nullptr; |
| 1864 | } |
| 1865 | |
| 1866 | bool QRhiVulkan::createDefaultRenderPass(QVkRenderPassDescriptor *rpD, |
| 1867 | bool hasDepthStencil, |
| 1868 | VkSampleCountFlagBits samples, |
| 1869 | VkFormat colorFormat, |
| 1870 | QRhiShadingRateMap *shadingRateMap) |
| 1871 | { |
| 1872 | // attachment list layout is color (1), ds (0-1), resolve (0-1), shading rate (0-1) |
| 1873 | |
| 1874 | VkAttachmentDescription attDesc = {}; |
| 1875 | attDesc.format = colorFormat; |
| 1876 | attDesc.samples = samples; |
| 1877 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 1878 | attDesc.storeOp = samples > VK_SAMPLE_COUNT_1_BIT ? VK_ATTACHMENT_STORE_OP_DONT_CARE : VK_ATTACHMENT_STORE_OP_STORE; |
| 1879 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 1880 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 1881 | attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1882 | attDesc.finalLayout = samples > VK_SAMPLE_COUNT_1_BIT ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
| 1883 | rpD->attDescs.append(t: attDesc); |
| 1884 | |
| 1885 | rpD->colorRefs.append(t: { .attachment: 0, .layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }); |
| 1886 | |
| 1887 | rpD->hasDepthStencil = hasDepthStencil; |
| 1888 | rpD->hasDepthStencilResolve = false; |
| 1889 | rpD->hasShadingRateMap = shadingRateMap != nullptr; |
| 1890 | rpD->multiViewCount = 0; |
| 1891 | |
| 1892 | if (hasDepthStencil) { |
| 1893 | // clear on load + no store + lazy alloc + transient image should play |
| 1894 | // nicely with tiled GPUs (no physical backing necessary for ds buffer) |
| 1895 | attDesc = {}; |
| 1896 | attDesc.format = optimalDepthStencilFormat(); |
| 1897 | attDesc.samples = samples; |
| 1898 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 1899 | attDesc.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 1900 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 1901 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 1902 | attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1903 | attDesc.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 1904 | rpD->attDescs.append(t: attDesc); |
| 1905 | |
| 1906 | rpD->dsRef = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL }; |
| 1907 | } else { |
| 1908 | rpD->dsRef = {}; |
| 1909 | } |
| 1910 | |
| 1911 | if (samples > VK_SAMPLE_COUNT_1_BIT) { |
| 1912 | attDesc = {}; |
| 1913 | attDesc.format = colorFormat; |
| 1914 | attDesc.samples = VK_SAMPLE_COUNT_1_BIT; |
| 1915 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 1916 | attDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 1917 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 1918 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 1919 | attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1920 | attDesc.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
| 1921 | rpD->attDescs.append(t: attDesc); |
| 1922 | |
| 1923 | rpD->resolveRefs.append(t: { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }); |
| 1924 | } |
| 1925 | |
| 1926 | rpD->dsResolveRef = {}; |
| 1927 | |
| 1928 | rpD->shadingRateRef = {}; |
| 1929 | #ifdef VK_KHR_fragment_shading_rate |
| 1930 | if (shadingRateMap) { |
| 1931 | attDesc = {}; |
| 1932 | attDesc.format = VK_FORMAT_R8_UINT; |
| 1933 | attDesc.samples = VK_SAMPLE_COUNT_1_BIT; |
| 1934 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
| 1935 | attDesc.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 1936 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 1937 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 1938 | attDesc.initialLayout = VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR; |
| 1939 | attDesc.finalLayout = VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR; |
| 1940 | rpD->attDescs.append(t: attDesc); |
| 1941 | |
| 1942 | rpD->shadingRateRef = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR }; |
| 1943 | } |
| 1944 | #endif |
| 1945 | |
| 1946 | // Replace the first implicit dep (TOP_OF_PIPE / ALL_COMMANDS) with our own. |
| 1947 | VkSubpassDependency subpassDep = {}; |
| 1948 | subpassDep.srcSubpass = VK_SUBPASS_EXTERNAL; |
| 1949 | subpassDep.dstSubpass = 0; |
| 1950 | subpassDep.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 1951 | subpassDep.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 1952 | subpassDep.srcAccessMask = 0; |
| 1953 | subpassDep.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 1954 | rpD->subpassDeps.append(t: subpassDep); |
| 1955 | if (hasDepthStencil) { |
| 1956 | memset(s: &subpassDep, c: 0, n: sizeof(subpassDep)); |
| 1957 | subpassDep.srcSubpass = VK_SUBPASS_EXTERNAL; |
| 1958 | subpassDep.dstSubpass = 0; |
| 1959 | subpassDep.srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
| 1960 | | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 1961 | subpassDep.dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
| 1962 | | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 1963 | subpassDep.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 1964 | subpassDep.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
| 1965 | | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 1966 | rpD->subpassDeps.append(t: subpassDep); |
| 1967 | } |
| 1968 | |
| 1969 | VkRenderPassCreateInfo rpInfo; |
| 1970 | VkSubpassDescription subpassDesc; |
| 1971 | fillRenderPassCreateInfo(rpInfo: &rpInfo, subpassDesc: &subpassDesc, rpD); |
| 1972 | |
| 1973 | #ifdef VK_KHR_create_renderpass2 |
| 1974 | if (caps.renderPass2KHR) { |
| 1975 | // Use the KHR extension, not the 1.2 core API, in order to support Vulkan 1.1. |
| 1976 | VkRenderPassCreateInfo2KHR rpInfo2; |
| 1977 | RenderPass2SetupHelper rp2Helper(this); |
| 1978 | if (!rp2Helper.prepare(rpInfo2: &rpInfo2, rpInfo: &rpInfo, rpD, multiViewCount: 0)) |
| 1979 | return false; |
| 1980 | VkResult err = vkCreateRenderPass2KHR(dev, &rpInfo2, nullptr, &rpD->rp); |
| 1981 | if (err != VK_SUCCESS) { |
| 1982 | qWarning(msg: "Failed to create renderpass (using VkRenderPassCreateInfo2KHR): %d" , err); |
| 1983 | return false; |
| 1984 | } |
| 1985 | } else |
| 1986 | #endif |
| 1987 | { |
| 1988 | if (rpD->hasShadingRateMap) |
| 1989 | qWarning(msg: "Variable rate shading with image is not supported without VK_KHR_create_renderpass2" ); |
| 1990 | VkResult err = df->vkCreateRenderPass(dev, &rpInfo, nullptr, &rpD->rp); |
| 1991 | if (err != VK_SUCCESS) { |
| 1992 | qWarning(msg: "Failed to create renderpass: %d" , err); |
| 1993 | return false; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | return true; |
| 1998 | } |
| 1999 | |
| 2000 | bool QRhiVulkan::createOffscreenRenderPass(QVkRenderPassDescriptor *rpD, |
| 2001 | const QRhiColorAttachment *colorAttachmentsBegin, |
| 2002 | const QRhiColorAttachment *colorAttachmentsEnd, |
| 2003 | bool preserveColor, |
| 2004 | bool preserveDs, |
| 2005 | bool storeDs, |
| 2006 | QRhiRenderBuffer *depthStencilBuffer, |
| 2007 | QRhiTexture *depthTexture, |
| 2008 | QRhiTexture *depthResolveTexture, |
| 2009 | QRhiShadingRateMap *shadingRateMap) |
| 2010 | { |
| 2011 | // attachment list layout is color (0-8), ds (0-1), resolve (0-8), ds resolve (0-1) |
| 2012 | |
| 2013 | int multiViewCount = 0; |
| 2014 | for (auto it = colorAttachmentsBegin; it != colorAttachmentsEnd; ++it) { |
| 2015 | QVkTexture *texD = QRHI_RES(QVkTexture, it->texture()); |
| 2016 | QVkRenderBuffer *rbD = QRHI_RES(QVkRenderBuffer, it->renderBuffer()); |
| 2017 | Q_ASSERT(texD || rbD); |
| 2018 | const VkFormat vkformat = texD ? texD->viewFormat : rbD->vkformat; |
| 2019 | const VkSampleCountFlagBits samples = texD ? texD->samples : rbD->samples; |
| 2020 | |
| 2021 | VkAttachmentDescription attDesc = {}; |
| 2022 | attDesc.format = vkformat; |
| 2023 | attDesc.samples = samples; |
| 2024 | attDesc.loadOp = preserveColor ? VK_ATTACHMENT_LOAD_OP_LOAD : VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 2025 | attDesc.storeOp = (it->resolveTexture() && !preserveColor) ? VK_ATTACHMENT_STORE_OP_DONT_CARE : VK_ATTACHMENT_STORE_OP_STORE; |
| 2026 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2027 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 2028 | // this has to interact correctly with activateTextureRenderTarget(), hence leaving in COLOR_ATT |
| 2029 | attDesc.initialLayout = preserveColor ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED; |
| 2030 | attDesc.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 2031 | rpD->attDescs.append(t: attDesc); |
| 2032 | |
| 2033 | const VkAttachmentReference ref = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; |
| 2034 | rpD->colorRefs.append(t: ref); |
| 2035 | |
| 2036 | if (it->multiViewCount() >= 2) { |
| 2037 | if (multiViewCount > 0 && multiViewCount != it->multiViewCount()) |
| 2038 | qWarning(msg: "Inconsistent multiViewCount in color attachment set" ); |
| 2039 | else |
| 2040 | multiViewCount = it->multiViewCount(); |
| 2041 | } else if (multiViewCount > 0) { |
| 2042 | qWarning(msg: "Mixing non-multiview color attachments within a multiview render pass" ); |
| 2043 | } |
| 2044 | } |
| 2045 | Q_ASSERT(multiViewCount == 0 || multiViewCount >= 2); |
| 2046 | rpD->multiViewCount = uint32_t(multiViewCount); |
| 2047 | |
| 2048 | rpD->hasDepthStencil = depthStencilBuffer || depthTexture; |
| 2049 | if (rpD->hasDepthStencil) { |
| 2050 | const VkFormat dsFormat = depthTexture ? QRHI_RES(QVkTexture, depthTexture)->viewFormat |
| 2051 | : QRHI_RES(QVkRenderBuffer, depthStencilBuffer)->vkformat; |
| 2052 | const VkSampleCountFlagBits samples = depthTexture ? QRHI_RES(QVkTexture, depthTexture)->samples |
| 2053 | : QRHI_RES(QVkRenderBuffer, depthStencilBuffer)->samples; |
| 2054 | const VkAttachmentLoadOp loadOp = preserveDs ? VK_ATTACHMENT_LOAD_OP_LOAD : VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 2055 | const VkAttachmentStoreOp storeOp = storeDs ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 2056 | VkAttachmentDescription attDesc = {}; |
| 2057 | attDesc.format = dsFormat; |
| 2058 | attDesc.samples = samples; |
| 2059 | attDesc.loadOp = loadOp; |
| 2060 | attDesc.storeOp = storeOp; |
| 2061 | attDesc.stencilLoadOp = loadOp; |
| 2062 | attDesc.stencilStoreOp = storeOp; |
| 2063 | attDesc.initialLayout = preserveDs ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED; |
| 2064 | attDesc.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2065 | rpD->attDescs.append(t: attDesc); |
| 2066 | if (depthTexture && depthTexture->arraySize() >= 2 && colorAttachmentsBegin == colorAttachmentsEnd) { |
| 2067 | multiViewCount = depthTexture->arraySize(); |
| 2068 | rpD->multiViewCount = multiViewCount; |
| 2069 | } |
| 2070 | rpD->dsRef = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL }; |
| 2071 | } else { |
| 2072 | rpD->dsRef = {}; |
| 2073 | } |
| 2074 | |
| 2075 | for (auto it = colorAttachmentsBegin; it != colorAttachmentsEnd; ++it) { |
| 2076 | if (it->resolveTexture()) { |
| 2077 | QVkTexture *rtexD = QRHI_RES(QVkTexture, it->resolveTexture()); |
| 2078 | const VkFormat dstFormat = rtexD->vkformat; |
| 2079 | if (rtexD->samples > VK_SAMPLE_COUNT_1_BIT) |
| 2080 | qWarning(msg: "Resolving into a multisample texture is not supported" ); |
| 2081 | |
| 2082 | QVkTexture *texD = QRHI_RES(QVkTexture, it->texture()); |
| 2083 | QVkRenderBuffer *rbD = QRHI_RES(QVkRenderBuffer, it->renderBuffer()); |
| 2084 | const VkFormat srcFormat = texD ? texD->vkformat : rbD->vkformat; |
| 2085 | if (srcFormat != dstFormat) { |
| 2086 | // This is a validation error. But some implementations survive, |
| 2087 | // actually. Warn about it however, because it's an error with |
| 2088 | // some other backends (like D3D) as well. |
| 2089 | qWarning(msg: "Multisample resolve between different formats (%d and %d) is not supported." , |
| 2090 | int(srcFormat), int(dstFormat)); |
| 2091 | } |
| 2092 | |
| 2093 | VkAttachmentDescription attDesc = {}; |
| 2094 | attDesc.format = rtexD->viewFormat; |
| 2095 | attDesc.samples = VK_SAMPLE_COUNT_1_BIT; |
| 2096 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; // ignored |
| 2097 | attDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 2098 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2099 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 2100 | attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 2101 | attDesc.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 2102 | rpD->attDescs.append(t: attDesc); |
| 2103 | |
| 2104 | const VkAttachmentReference ref = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; |
| 2105 | rpD->resolveRefs.append(t: ref); |
| 2106 | } else { |
| 2107 | const VkAttachmentReference ref = { VK_ATTACHMENT_UNUSED, .layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; |
| 2108 | rpD->resolveRefs.append(t: ref); |
| 2109 | } |
| 2110 | } |
| 2111 | Q_ASSERT(rpD->colorRefs.size() == rpD->resolveRefs.size()); |
| 2112 | |
| 2113 | rpD->hasDepthStencilResolve = rpD->hasDepthStencil && depthResolveTexture; |
| 2114 | if (rpD->hasDepthStencilResolve) { |
| 2115 | QVkTexture *rtexD = QRHI_RES(QVkTexture, depthResolveTexture); |
| 2116 | if (rtexD->samples > VK_SAMPLE_COUNT_1_BIT) |
| 2117 | qWarning(msg: "Resolving into a multisample depth texture is not supported" ); |
| 2118 | |
| 2119 | QVkTexture *texD = QRHI_RES(QVkTexture, depthResolveTexture); |
| 2120 | if (texD->vkformat != rtexD->vkformat) { |
| 2121 | qWarning(msg: "Multisample resolve between different depth-stencil formats (%d and %d) is not supported." , |
| 2122 | int(texD->vkformat), int(rtexD->vkformat)); |
| 2123 | } |
| 2124 | |
| 2125 | VkAttachmentDescription attDesc = {}; |
| 2126 | attDesc.format = rtexD->viewFormat; |
| 2127 | attDesc.samples = VK_SAMPLE_COUNT_1_BIT; |
| 2128 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; // ignored |
| 2129 | attDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 2130 | attDesc.stencilLoadOp = attDesc.loadOp; |
| 2131 | attDesc.stencilStoreOp = attDesc.storeOp; |
| 2132 | attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 2133 | attDesc.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 2134 | rpD->attDescs.append(t: attDesc); |
| 2135 | rpD->dsResolveRef = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL }; |
| 2136 | } else { |
| 2137 | rpD->dsResolveRef = {}; |
| 2138 | } |
| 2139 | |
| 2140 | rpD->hasShadingRateMap = shadingRateMap != nullptr; |
| 2141 | rpD->shadingRateRef = {}; |
| 2142 | #ifdef VK_KHR_fragment_shading_rate |
| 2143 | if (shadingRateMap) { |
| 2144 | VkAttachmentDescription attDesc = {}; |
| 2145 | attDesc.format = VK_FORMAT_R8_UINT; |
| 2146 | attDesc.samples = VK_SAMPLE_COUNT_1_BIT; |
| 2147 | attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
| 2148 | attDesc.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 2149 | attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 2150 | attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 2151 | attDesc.initialLayout = VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR; |
| 2152 | attDesc.finalLayout = VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR; |
| 2153 | rpD->attDescs.append(t: attDesc); |
| 2154 | rpD->shadingRateRef = { .attachment: uint32_t(rpD->attDescs.size() - 1), .layout: VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR }; |
| 2155 | } |
| 2156 | #endif |
| 2157 | |
| 2158 | // rpD->subpassDeps stays empty: don't yet know the correct initial/final |
| 2159 | // access and stage stuff for the implicit deps at this point, so leave it |
| 2160 | // to the resource tracking and activateTextureRenderTarget() to generate |
| 2161 | // barriers. |
| 2162 | |
| 2163 | VkRenderPassCreateInfo rpInfo; |
| 2164 | VkSubpassDescription subpassDesc; |
| 2165 | fillRenderPassCreateInfo(rpInfo: &rpInfo, subpassDesc: &subpassDesc, rpD); |
| 2166 | |
| 2167 | MultiViewRenderPassSetupHelper multiViewHelper; |
| 2168 | if (!multiViewHelper.prepare(rpInfo: &rpInfo, multiViewCount, multiViewCap: caps.multiView)) |
| 2169 | return false; |
| 2170 | |
| 2171 | #ifdef VK_KHR_create_renderpass2 |
| 2172 | if (caps.renderPass2KHR) { |
| 2173 | // Use the KHR extension, not the 1.2 core API, in order to support Vulkan 1.1. |
| 2174 | VkRenderPassCreateInfo2KHR rpInfo2; |
| 2175 | RenderPass2SetupHelper rp2Helper(this); |
| 2176 | if (!rp2Helper.prepare(rpInfo2: &rpInfo2, rpInfo: &rpInfo, rpD, multiViewCount)) |
| 2177 | return false; |
| 2178 | |
| 2179 | VkResult err = vkCreateRenderPass2KHR(dev, &rpInfo2, nullptr, &rpD->rp); |
| 2180 | if (err != VK_SUCCESS) { |
| 2181 | qWarning(msg: "Failed to create renderpass (using VkRenderPassCreateInfo2KHR): %d" , err); |
| 2182 | return false; |
| 2183 | } |
| 2184 | } else |
| 2185 | #endif |
| 2186 | { |
| 2187 | if (rpD->hasDepthStencilResolve) { |
| 2188 | qWarning(msg: "Resolving multisample depth-stencil buffers is not supported without " |
| 2189 | "VK_KHR_depth_stencil_resolve and VK_KHR_create_renderpass2" ); |
| 2190 | } |
| 2191 | if (rpD->hasShadingRateMap) |
| 2192 | qWarning(msg: "Variable rate shading with image is not supported without VK_KHR_create_renderpass2" ); |
| 2193 | VkResult err = df->vkCreateRenderPass(dev, &rpInfo, nullptr, &rpD->rp); |
| 2194 | if (err != VK_SUCCESS) { |
| 2195 | qWarning(msg: "Failed to create renderpass: %d" , err); |
| 2196 | return false; |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | return true; |
| 2201 | } |
| 2202 | |
| 2203 | bool QRhiVulkan::recreateSwapChain(QRhiSwapChain *swapChain) |
| 2204 | { |
| 2205 | QVkSwapChain *swapChainD = QRHI_RES(QVkSwapChain, swapChain); |
| 2206 | if (swapChainD->pixelSize.isEmpty()) { |
| 2207 | qWarning(msg: "Surface size is 0, cannot create swapchain" ); |
| 2208 | return false; |
| 2209 | } |
| 2210 | |
| 2211 | df->vkDeviceWaitIdle(dev); |
| 2212 | |
| 2213 | if (!vkCreateSwapchainKHR) { |
| 2214 | vkCreateSwapchainKHR = reinterpret_cast<PFN_vkCreateSwapchainKHR>(f->vkGetDeviceProcAddr(dev, "vkCreateSwapchainKHR" )); |
| 2215 | vkDestroySwapchainKHR = reinterpret_cast<PFN_vkDestroySwapchainKHR>(f->vkGetDeviceProcAddr(dev, "vkDestroySwapchainKHR" )); |
| 2216 | vkGetSwapchainImagesKHR = reinterpret_cast<PFN_vkGetSwapchainImagesKHR>(f->vkGetDeviceProcAddr(dev, "vkGetSwapchainImagesKHR" )); |
| 2217 | vkAcquireNextImageKHR = reinterpret_cast<PFN_vkAcquireNextImageKHR>(f->vkGetDeviceProcAddr(dev, "vkAcquireNextImageKHR" )); |
| 2218 | vkQueuePresentKHR = reinterpret_cast<PFN_vkQueuePresentKHR>(f->vkGetDeviceProcAddr(dev, "vkQueuePresentKHR" )); |
| 2219 | if (!vkCreateSwapchainKHR || !vkDestroySwapchainKHR || !vkGetSwapchainImagesKHR || !vkAcquireNextImageKHR || !vkQueuePresentKHR) { |
| 2220 | qWarning(msg: "Swapchain functions not available" ); |
| 2221 | return false; |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | VkSurfaceCapabilitiesKHR surfaceCaps; |
| 2226 | vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physDev, swapChainD->surface, &surfaceCaps); |
| 2227 | quint32 reqBufferCount; |
| 2228 | if (swapChainD->m_flags.testFlag(flag: QRhiSwapChain::MinimalBufferCount) || surfaceCaps.maxImageCount == 0) { |
| 2229 | reqBufferCount = qMax<quint32>(a: 2, b: surfaceCaps.minImageCount); |
| 2230 | } else { |
| 2231 | reqBufferCount = qMax(a: qMin<quint32>(a: surfaceCaps.maxImageCount, b: 3), b: surfaceCaps.minImageCount); |
| 2232 | } |
| 2233 | VkSurfaceTransformFlagBitsKHR preTransform = |
| 2234 | (surfaceCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) |
| 2235 | ? VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
| 2236 | : surfaceCaps.currentTransform; |
| 2237 | |
| 2238 | // This looks odd but matches how platforms work in practice. |
| 2239 | // |
| 2240 | // On Windows with NVIDIA for example, the only supportedCompositeAlpha |
| 2241 | // value reported is OPAQUE, nothing else. Yet transparency works |
| 2242 | // regardless, as long as the native window is set up correctly, so that's |
| 2243 | // not something we need to handle here. |
| 2244 | // |
| 2245 | // On Linux with Intel and Mesa and running on xcb reports, on one |
| 2246 | // particular system, INHERIT+PRE_MULTIPLIED. Tranparency works, regardless, |
| 2247 | // presumably due to setting INHERIT. |
| 2248 | // |
| 2249 | // On the same setup with Wayland instead of xcb we see |
| 2250 | // OPAQUE+PRE_MULTIPLIED reported. Here transparency won't work unless |
| 2251 | // PRE_MULTIPLIED is set. |
| 2252 | // |
| 2253 | // Therefore our rules are: |
| 2254 | // - Prefer INHERIT over OPAQUE. |
| 2255 | // - Then based on the request, try the requested alpha mode, but if |
| 2256 | // that's not reported as supported, try also the other (PRE/POST, |
| 2257 | // POST/PRE) as that is better than nothing. This is not different from |
| 2258 | // some other backends, e.g. D3D11 with DirectComposition there is also |
| 2259 | // no control over being straight or pre-multiplied. Whereas with |
| 2260 | // WGL/GLX/EGL we never had that sort of control. |
| 2261 | |
| 2262 | VkCompositeAlphaFlagBitsKHR compositeAlpha = |
| 2263 | (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) |
| 2264 | ? VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
| 2265 | : VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 2266 | |
| 2267 | if (swapChainD->m_flags.testFlag(flag: QRhiSwapChain::SurfaceHasPreMulAlpha)) { |
| 2268 | if (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR) |
| 2269 | compositeAlpha = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR; |
| 2270 | else if (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR) |
| 2271 | compositeAlpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR; |
| 2272 | } else if (swapChainD->m_flags.testFlag(flag: QRhiSwapChain::SurfaceHasNonPreMulAlpha)) { |
| 2273 | if (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR) |
| 2274 | compositeAlpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR; |
| 2275 | else if (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR) |
| 2276 | compositeAlpha = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR; |
| 2277 | } |
| 2278 | |
| 2279 | VkImageUsageFlags usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 2280 | swapChainD->supportsReadback = (surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT); |
| 2281 | if (swapChainD->supportsReadback && swapChainD->m_flags.testFlag(flag: QRhiSwapChain::UsedAsTransferSource)) |
| 2282 | usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 2283 | |
| 2284 | const bool stereo = bool(swapChainD->m_window) && (swapChainD->m_window->format().stereo()) |
| 2285 | && surfaceCaps.maxImageArrayLayers > 1; |
| 2286 | swapChainD->stereo = stereo; |
| 2287 | |
| 2288 | VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR; |
| 2289 | if (swapChainD->m_flags.testFlag(flag: QRhiSwapChain::NoVSync)) { |
| 2290 | // Stereo has a weird bug, when using VK_PRESENT_MODE_MAILBOX_KHR, |
| 2291 | // black screen is shown, but there is no validation error. |
| 2292 | // Detected on Windows, with NVidia RTX A series (at least 4000 and 6000) driver 535.98 |
| 2293 | if (swapChainD->supportedPresentationModes.contains(t: VK_PRESENT_MODE_MAILBOX_KHR) && !stereo) |
| 2294 | presentMode = VK_PRESENT_MODE_MAILBOX_KHR; |
| 2295 | else if (swapChainD->supportedPresentationModes.contains(t: VK_PRESENT_MODE_IMMEDIATE_KHR)) |
| 2296 | presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; |
| 2297 | } |
| 2298 | |
| 2299 | // If the surface is different than before, then passing in the old |
| 2300 | // swapchain associated with the old surface can fail the swapchain |
| 2301 | // creation. (for example, Android loses the surface when backgrounding and |
| 2302 | // restoring applications, and it also enforces failing swapchain creation |
| 2303 | // with VK_ERROR_NATIVE_WINDOW_IN_USE_KHR if the old swapchain is provided) |
| 2304 | const bool reuseExisting = swapChainD->sc && swapChainD->lastConnectedSurface == swapChainD->surface; |
| 2305 | |
| 2306 | qCDebug(QRHI_LOG_INFO, "Creating %s swapchain of %u buffers, size %dx%d, presentation mode %d" , |
| 2307 | reuseExisting ? "recycled" : "new" , |
| 2308 | reqBufferCount, swapChainD->pixelSize.width(), swapChainD->pixelSize.height(), presentMode); |
| 2309 | |
| 2310 | VkSwapchainCreateInfoKHR swapChainInfo = {}; |
| 2311 | swapChainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 2312 | swapChainInfo.surface = swapChainD->surface; |
| 2313 | swapChainInfo.minImageCount = reqBufferCount; |
| 2314 | swapChainInfo.imageFormat = swapChainD->colorFormat; |
| 2315 | swapChainInfo.imageColorSpace = swapChainD->colorSpace; |
| 2316 | swapChainInfo.imageExtent = VkExtent2D { .width: uint32_t(swapChainD->pixelSize.width()), .height: uint32_t(swapChainD->pixelSize.height()) }; |
| 2317 | swapChainInfo.imageArrayLayers = stereo ? 2u : 1u; |
| 2318 | swapChainInfo.imageUsage = usage; |
| 2319 | swapChainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 2320 | swapChainInfo.preTransform = preTransform; |
| 2321 | swapChainInfo.compositeAlpha = compositeAlpha; |
| 2322 | swapChainInfo.presentMode = presentMode; |
| 2323 | swapChainInfo.clipped = true; |
| 2324 | swapChainInfo.oldSwapchain = reuseExisting ? swapChainD->sc : VK_NULL_HANDLE; |
| 2325 | |
| 2326 | VkSwapchainKHR newSwapChain; |
| 2327 | VkResult err = vkCreateSwapchainKHR(dev, &swapChainInfo, nullptr, &newSwapChain); |
| 2328 | if (err != VK_SUCCESS) { |
| 2329 | qWarning(msg: "Failed to create swapchain: %d" , err); |
| 2330 | return false; |
| 2331 | } |
| 2332 | |
| 2333 | if (swapChainD->sc) |
| 2334 | releaseSwapChainResources(swapChain); |
| 2335 | |
| 2336 | swapChainD->sc = newSwapChain; |
| 2337 | swapChainD->lastConnectedSurface = swapChainD->surface; |
| 2338 | |
| 2339 | quint32 actualSwapChainBufferCount = 0; |
| 2340 | err = vkGetSwapchainImagesKHR(dev, swapChainD->sc, &actualSwapChainBufferCount, nullptr); |
| 2341 | if (err != VK_SUCCESS || actualSwapChainBufferCount == 0) { |
| 2342 | qWarning(msg: "Failed to get swapchain images: %d" , err); |
| 2343 | return false; |
| 2344 | } |
| 2345 | |
| 2346 | if (actualSwapChainBufferCount != reqBufferCount) |
| 2347 | qCDebug(QRHI_LOG_INFO, "Actual swapchain buffer count is %u" , actualSwapChainBufferCount); |
| 2348 | swapChainD->bufferCount = int(actualSwapChainBufferCount); |
| 2349 | |
| 2350 | QVarLengthArray<VkImage, QVkSwapChain::EXPECTED_MAX_BUFFER_COUNT> swapChainImages(actualSwapChainBufferCount); |
| 2351 | err = vkGetSwapchainImagesKHR(dev, swapChainD->sc, &actualSwapChainBufferCount, swapChainImages.data()); |
| 2352 | if (err != VK_SUCCESS) { |
| 2353 | qWarning(msg: "Failed to get swapchain images: %d" , err); |
| 2354 | return false; |
| 2355 | } |
| 2356 | |
| 2357 | QVarLengthArray<VkImage, QVkSwapChain::EXPECTED_MAX_BUFFER_COUNT> msaaImages(swapChainD->bufferCount); |
| 2358 | QVarLengthArray<VkImageView, QVkSwapChain::EXPECTED_MAX_BUFFER_COUNT> msaaViews(swapChainD->bufferCount); |
| 2359 | if (swapChainD->samples > VK_SAMPLE_COUNT_1_BIT) { |
| 2360 | if (!createTransientImage(format: swapChainD->colorFormat, |
| 2361 | pixelSize: swapChainD->pixelSize, |
| 2362 | usage: VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 2363 | aspectMask: VK_IMAGE_ASPECT_COLOR_BIT, |
| 2364 | samples: swapChainD->samples, |
| 2365 | mem: &swapChainD->msaaImageMem, |
| 2366 | images: msaaImages.data(), |
| 2367 | views: msaaViews.data(), |
| 2368 | count: swapChainD->bufferCount)) |
| 2369 | { |
| 2370 | qWarning(msg: "Failed to create transient image for MSAA color buffer" ); |
| 2371 | return false; |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | VkFenceCreateInfo fenceInfo = {}; |
| 2376 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 2377 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
| 2378 | |
| 2379 | // Double up for stereo |
| 2380 | swapChainD->imageRes.resize(sz: swapChainD->bufferCount * (stereo ? 2u : 1u)); |
| 2381 | |
| 2382 | for (int i = 0; i < swapChainD->bufferCount; ++i) { |
| 2383 | QVkSwapChain::ImageResources &image(swapChainD->imageRes[i]); |
| 2384 | image.image = swapChainImages[i]; |
| 2385 | if (swapChainD->samples > VK_SAMPLE_COUNT_1_BIT) { |
| 2386 | image.msaaImage = msaaImages[i]; |
| 2387 | image.msaaImageView = msaaViews[i]; |
| 2388 | } |
| 2389 | |
| 2390 | VkImageViewCreateInfo imgViewInfo = {}; |
| 2391 | imgViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 2392 | imgViewInfo.image = swapChainImages[i]; |
| 2393 | imgViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; |
| 2394 | imgViewInfo.format = swapChainD->colorFormat; |
| 2395 | imgViewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 2396 | imgViewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 2397 | imgViewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 2398 | imgViewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 2399 | imgViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2400 | imgViewInfo.subresourceRange.levelCount = imgViewInfo.subresourceRange.layerCount = 1; |
| 2401 | err = df->vkCreateImageView(dev, &imgViewInfo, nullptr, &image.imageView); |
| 2402 | if (err != VK_SUCCESS) { |
| 2403 | qWarning(msg: "Failed to create swapchain image view %d: %d" , i, err); |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
| 2407 | image.lastUse = QVkSwapChain::ImageResources::ScImageUseNone; |
| 2408 | |
| 2409 | VkSemaphoreCreateInfo semInfo = {}; |
| 2410 | semInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 2411 | df->vkCreateSemaphore(dev, &semInfo, nullptr, &image.drawSem); |
| 2412 | } |
| 2413 | if (stereo) { |
| 2414 | for (int i = 0; i < swapChainD->bufferCount; ++i) { |
| 2415 | QVkSwapChain::ImageResources &image(swapChainD->imageRes[i + swapChainD->bufferCount]); |
| 2416 | image.image = swapChainImages[i]; |
| 2417 | if (swapChainD->samples > VK_SAMPLE_COUNT_1_BIT) { |
| 2418 | image.msaaImage = msaaImages[i]; |
| 2419 | image.msaaImageView = msaaViews[i]; |
| 2420 | } |
| 2421 | |
| 2422 | VkImageViewCreateInfo imgViewInfo = {}; |
| 2423 | imgViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 2424 | imgViewInfo.image = swapChainImages[i]; |
| 2425 | imgViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; |
| 2426 | imgViewInfo.format = swapChainD->colorFormat; |
| 2427 | imgViewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 2428 | imgViewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 2429 | imgViewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 2430 | imgViewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 2431 | imgViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2432 | imgViewInfo.subresourceRange.baseArrayLayer = 1; |
| 2433 | imgViewInfo.subresourceRange.levelCount = imgViewInfo.subresourceRange.layerCount = 1; |
| 2434 | err = df->vkCreateImageView(dev, &imgViewInfo, nullptr, &image.imageView); |
| 2435 | if (err != VK_SUCCESS) { |
| 2436 | qWarning(msg: "Failed to create swapchain image view %d: %d" , i, err); |
| 2437 | return false; |
| 2438 | } |
| 2439 | |
| 2440 | VkSemaphoreCreateInfo semInfo = {}; |
| 2441 | semInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 2442 | df->vkCreateSemaphore(dev, &semInfo, nullptr, &image.drawSem); |
| 2443 | |
| 2444 | image.lastUse = QVkSwapChain::ImageResources::ScImageUseNone; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | swapChainD->currentImageIndex = 0; |
| 2449 | |
| 2450 | if (swapChainD->shadingRateMap() && caps.renderPass2KHR && caps.imageBasedShadingRate) { |
| 2451 | QVkTexture *texD = QRHI_RES(QVkShadingRateMap, swapChainD->shadingRateMap())->texture; |
| 2452 | Q_ASSERT(texD->flags().testFlag(QRhiTexture::UsedAsShadingRateMap)); |
| 2453 | VkImageViewCreateInfo viewInfo = {}; |
| 2454 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 2455 | viewInfo.image = texD->image; |
| 2456 | viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; |
| 2457 | viewInfo.format = texD->viewFormat; |
| 2458 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 2459 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 2460 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 2461 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 2462 | viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2463 | viewInfo.subresourceRange.baseMipLevel = 0; |
| 2464 | viewInfo.subresourceRange.levelCount = 1; |
| 2465 | viewInfo.subresourceRange.baseArrayLayer = 0; |
| 2466 | viewInfo.subresourceRange.layerCount = 1; |
| 2467 | VkResult err = df->vkCreateImageView(dev, &viewInfo, nullptr, &swapChainD->shadingRateMapView); |
| 2468 | if (err != VK_SUCCESS) { |
| 2469 | qWarning(msg: "Failed to create swapchain shading rate map view: %d" , err); |
| 2470 | return false; |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | VkSemaphoreCreateInfo semInfo = {}; |
| 2475 | semInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 2476 | |
| 2477 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 2478 | QVkSwapChain::FrameResources &frame(swapChainD->frameRes[i]); |
| 2479 | |
| 2480 | frame.imageAcquired = false; |
| 2481 | frame.imageSemWaitable = false; |
| 2482 | |
| 2483 | df->vkCreateSemaphore(dev, &semInfo, nullptr, &frame.imageSem); |
| 2484 | |
| 2485 | err = df->vkCreateFence(dev, &fenceInfo, nullptr, &frame.cmdFence); |
| 2486 | if (err != VK_SUCCESS) { |
| 2487 | qWarning(msg: "Failed to create command buffer fence: %d" , err); |
| 2488 | return false; |
| 2489 | } |
| 2490 | frame.cmdFenceWaitable = true; // fence was created in signaled state |
| 2491 | } |
| 2492 | |
| 2493 | swapChainD->currentFrameSlot = 0; |
| 2494 | |
| 2495 | return true; |
| 2496 | } |
| 2497 | |
| 2498 | void QRhiVulkan::releaseSwapChainResources(QRhiSwapChain *swapChain) |
| 2499 | { |
| 2500 | QVkSwapChain *swapChainD = QRHI_RES(QVkSwapChain, swapChain); |
| 2501 | |
| 2502 | if (swapChainD->sc == VK_NULL_HANDLE) |
| 2503 | return; |
| 2504 | |
| 2505 | if (!deviceLost) |
| 2506 | df->vkDeviceWaitIdle(dev); |
| 2507 | |
| 2508 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 2509 | QVkSwapChain::FrameResources &frame(swapChainD->frameRes[i]); |
| 2510 | if (frame.cmdFence) { |
| 2511 | if (!deviceLost && frame.cmdFenceWaitable) |
| 2512 | df->vkWaitForFences(dev, 1, &frame.cmdFence, VK_TRUE, UINT64_MAX); |
| 2513 | df->vkDestroyFence(dev, frame.cmdFence, nullptr); |
| 2514 | frame.cmdFence = VK_NULL_HANDLE; |
| 2515 | frame.cmdFenceWaitable = false; |
| 2516 | } |
| 2517 | if (frame.imageSem) { |
| 2518 | df->vkDestroySemaphore(dev, frame.imageSem, nullptr); |
| 2519 | frame.imageSem = VK_NULL_HANDLE; |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | for (int i = 0; i < swapChainD->bufferCount * (swapChainD->stereo ? 2 : 1); ++i) { |
| 2524 | QVkSwapChain::ImageResources &image(swapChainD->imageRes[i]); |
| 2525 | if (image.fb) { |
| 2526 | df->vkDestroyFramebuffer(dev, image.fb, nullptr); |
| 2527 | image.fb = VK_NULL_HANDLE; |
| 2528 | } |
| 2529 | if (image.imageView) { |
| 2530 | df->vkDestroyImageView(dev, image.imageView, nullptr); |
| 2531 | image.imageView = VK_NULL_HANDLE; |
| 2532 | } |
| 2533 | if (image.msaaImageView) { |
| 2534 | df->vkDestroyImageView(dev, image.msaaImageView, nullptr); |
| 2535 | image.msaaImageView = VK_NULL_HANDLE; |
| 2536 | } |
| 2537 | if (image.msaaImage) { |
| 2538 | df->vkDestroyImage(dev, image.msaaImage, nullptr); |
| 2539 | image.msaaImage = VK_NULL_HANDLE; |
| 2540 | } |
| 2541 | if (image.drawSem) { |
| 2542 | df->vkDestroySemaphore(dev, image.drawSem, nullptr); |
| 2543 | image.drawSem = VK_NULL_HANDLE; |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | if (swapChainD->msaaImageMem) { |
| 2548 | df->vkFreeMemory(dev, swapChainD->msaaImageMem, nullptr); |
| 2549 | swapChainD->msaaImageMem = VK_NULL_HANDLE; |
| 2550 | } |
| 2551 | |
| 2552 | if (swapChainD->shadingRateMapView) { |
| 2553 | df->vkDestroyImageView(dev, swapChainD->shadingRateMapView, nullptr); |
| 2554 | swapChainD->shadingRateMapView = VK_NULL_HANDLE; |
| 2555 | } |
| 2556 | |
| 2557 | vkDestroySwapchainKHR(dev, swapChainD->sc, nullptr); |
| 2558 | swapChainD->sc = VK_NULL_HANDLE; |
| 2559 | |
| 2560 | // NB! surface and similar must remain intact |
| 2561 | } |
| 2562 | |
| 2563 | void QRhiVulkan::ensureCommandPoolForNewFrame() |
| 2564 | { |
| 2565 | VkCommandPoolResetFlags flags = 0; |
| 2566 | |
| 2567 | // While not clear what "recycles all of the resources from the command |
| 2568 | // pool back to the system" really means in practice, set it when there was |
| 2569 | // a call to releaseCachedResources() recently. |
| 2570 | if (releaseCachedResourcesCalledBeforeFrameStart) |
| 2571 | flags |= VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT; |
| 2572 | |
| 2573 | // put all command buffers allocated from this slot's pool to initial state |
| 2574 | df->vkResetCommandPool(dev, cmdPool[currentFrameSlot], flags); |
| 2575 | } |
| 2576 | |
| 2577 | double QRhiVulkan::elapsedSecondsFromTimestamp(quint64 timestamp[2], bool *ok) |
| 2578 | { |
| 2579 | quint64 mask = 0; |
| 2580 | for (quint64 i = 0; i < timestampValidBits; i += 8) |
| 2581 | mask |= 0xFFULL << i; |
| 2582 | const quint64 ts0 = timestamp[0] & mask; |
| 2583 | const quint64 ts1 = timestamp[1] & mask; |
| 2584 | const float nsecsPerTick = physDevProperties.limits.timestampPeriod; |
| 2585 | if (!qFuzzyIsNull(f: nsecsPerTick)) { |
| 2586 | const float elapsedMs = float(ts1 - ts0) * nsecsPerTick / 1000000.0f; |
| 2587 | const double elapsedSec = elapsedMs / 1000.0; |
| 2588 | *ok = true; |
| 2589 | return elapsedSec; |
| 2590 | } |
| 2591 | *ok = false; |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
| 2595 | QRhi::FrameOpResult QRhiVulkan::beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags) |
| 2596 | { |
| 2597 | QVkSwapChain *swapChainD = QRHI_RES(QVkSwapChain, swapChain); |
| 2598 | const int frameResIndex = swapChainD->bufferCount > 1 ? swapChainD->currentFrameSlot : 0; |
| 2599 | QVkSwapChain::FrameResources &frame(swapChainD->frameRes[frameResIndex]); |
| 2600 | |
| 2601 | inst->handle()->beginFrame(window: swapChainD->window); |
| 2602 | |
| 2603 | // Make sure the previous commands for the same frame slot have finished. |
| 2604 | // |
| 2605 | // Do this also for any other swapchain's commands with the same frame slot |
| 2606 | // While this reduces concurrency, it keeps resource usage safe: swapchain |
| 2607 | // A starting its frame 0, followed by swapchain B starting its own frame 0 |
| 2608 | // will make B wait for A's frame 0 commands, so if a resource is written |
| 2609 | // in B's frame or when B checks for pending resource releases, that won't |
| 2610 | // mess up A's in-flight commands (as they are not in flight anymore). |
| 2611 | QRhi::FrameOpResult waitResult = waitCommandCompletion(frameSlot: frameResIndex); |
| 2612 | if (waitResult != QRhi::FrameOpSuccess) |
| 2613 | return waitResult; |
| 2614 | |
| 2615 | if (!frame.imageAcquired) { |
| 2616 | // move on to next swapchain image |
| 2617 | uint32_t imageIndex = 0; |
| 2618 | VkResult err = vkAcquireNextImageKHR(dev, swapChainD->sc, UINT64_MAX, |
| 2619 | frame.imageSem, VK_NULL_HANDLE, &imageIndex); |
| 2620 | |
| 2621 | if (err == VK_SUCCESS || err == VK_SUBOPTIMAL_KHR) { |
| 2622 | swapChainD->currentImageIndex = imageIndex; |
| 2623 | frame.imageSemWaitable = true; |
| 2624 | frame.imageAcquired = true; |
| 2625 | } else if (err == VK_ERROR_OUT_OF_DATE_KHR) { |
| 2626 | return QRhi::FrameOpSwapChainOutOfDate; |
| 2627 | } else { |
| 2628 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2629 | qWarning(msg: "Device loss detected in vkAcquireNextImageKHR()" ); |
| 2630 | deviceLost = true; |
| 2631 | return QRhi::FrameOpDeviceLost; |
| 2632 | } |
| 2633 | qWarning(msg: "Failed to acquire next swapchain image: %d" , err); |
| 2634 | return QRhi::FrameOpError; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | currentFrameSlot = int(swapChainD->currentFrameSlot); |
| 2639 | currentSwapChain = swapChainD; |
| 2640 | if (swapChainD->ds) |
| 2641 | swapChainD->ds->lastActiveFrameSlot = currentFrameSlot; |
| 2642 | |
| 2643 | // reset the command pool |
| 2644 | ensureCommandPoolForNewFrame(); |
| 2645 | |
| 2646 | // start recording to this frame's command buffer |
| 2647 | QRhi::FrameOpResult cbres = startPrimaryCommandBuffer(cb: &frame.cmdBuf); |
| 2648 | if (cbres != QRhi::FrameOpSuccess) |
| 2649 | return cbres; |
| 2650 | |
| 2651 | swapChainD->cbWrapper.cb = frame.cmdBuf; |
| 2652 | |
| 2653 | QVkSwapChain::ImageResources &image(swapChainD->imageRes[swapChainD->currentImageIndex]); |
| 2654 | swapChainD->rtWrapper.d.fb = image.fb; |
| 2655 | |
| 2656 | if (swapChainD->stereo) { |
| 2657 | QVkSwapChain::ImageResources &image( |
| 2658 | swapChainD->imageRes[swapChainD->currentImageIndex + swapChainD->bufferCount]); |
| 2659 | swapChainD->rtWrapperRight.d.fb = image.fb; |
| 2660 | } |
| 2661 | |
| 2662 | prepareNewFrame(cb: &swapChainD->cbWrapper); |
| 2663 | |
| 2664 | // Read the timestamps for the previous frame for this slot. |
| 2665 | if (frame.timestampQueryIndex >= 0) { |
| 2666 | quint64 timestamp[2] = { 0, 0 }; |
| 2667 | VkResult err = df->vkGetQueryPoolResults(dev, timestampQueryPool, uint32_t(frame.timestampQueryIndex), 2, |
| 2668 | 2 * sizeof(quint64), timestamp, sizeof(quint64), |
| 2669 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 2670 | timestampQueryPoolMap.clearBit(i: frame.timestampQueryIndex / 2); |
| 2671 | frame.timestampQueryIndex = -1; |
| 2672 | if (err == VK_SUCCESS) { |
| 2673 | bool ok = false; |
| 2674 | const double elapsedSec = elapsedSecondsFromTimestamp(timestamp, ok: &ok); |
| 2675 | if (ok) |
| 2676 | swapChainD->cbWrapper.lastGpuTime = elapsedSec; |
| 2677 | } else { |
| 2678 | qWarning(msg: "Failed to query timestamp: %d" , err); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | // No timestamps if the client did not opt in, or when not having at least 2 frames in flight. |
| 2683 | if (rhiFlags.testFlag(flag: QRhi::EnableTimestamps) && swapChainD->bufferCount > 1) { |
| 2684 | int timestampQueryIdx = -1; |
| 2685 | for (int i = 0; i < timestampQueryPoolMap.size(); ++i) { |
| 2686 | if (!timestampQueryPoolMap.testBit(i)) { |
| 2687 | timestampQueryPoolMap.setBit(i); |
| 2688 | timestampQueryIdx = i * 2; |
| 2689 | break; |
| 2690 | } |
| 2691 | } |
| 2692 | if (timestampQueryIdx >= 0) { |
| 2693 | df->vkCmdResetQueryPool(frame.cmdBuf, timestampQueryPool, uint32_t(timestampQueryIdx), 2); |
| 2694 | // record timestamp at the start of the command buffer |
| 2695 | df->vkCmdWriteTimestamp(frame.cmdBuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
| 2696 | timestampQueryPool, uint32_t(timestampQueryIdx)); |
| 2697 | frame.timestampQueryIndex = timestampQueryIdx; |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | return QRhi::FrameOpSuccess; |
| 2702 | } |
| 2703 | |
| 2704 | QRhi::FrameOpResult QRhiVulkan::endFrame(QRhiSwapChain *swapChain, QRhi::EndFrameFlags flags) |
| 2705 | { |
| 2706 | QVkSwapChain *swapChainD = QRHI_RES(QVkSwapChain, swapChain); |
| 2707 | Q_ASSERT(currentSwapChain == swapChainD); |
| 2708 | |
| 2709 | auto cleanup = qScopeGuard(f: [this, swapChainD] { |
| 2710 | inst->handle()->endFrame(window: swapChainD->window); |
| 2711 | }); |
| 2712 | |
| 2713 | recordPrimaryCommandBuffer(cbD: &swapChainD->cbWrapper); |
| 2714 | |
| 2715 | int frameResIndex = swapChainD->bufferCount > 1 ? swapChainD->currentFrameSlot : 0; |
| 2716 | QVkSwapChain::FrameResources &frame(swapChainD->frameRes[frameResIndex]); |
| 2717 | QVkSwapChain::ImageResources &image(swapChainD->imageRes[swapChainD->currentImageIndex]); |
| 2718 | |
| 2719 | if (image.lastUse != QVkSwapChain::ImageResources::ScImageUseRender) { |
| 2720 | VkImageMemoryBarrier presTrans = {}; |
| 2721 | presTrans.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 2722 | presTrans.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 2723 | presTrans.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
| 2724 | presTrans.image = image.image; |
| 2725 | presTrans.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 2726 | presTrans.subresourceRange.levelCount = presTrans.subresourceRange.layerCount = 1; |
| 2727 | |
| 2728 | if (image.lastUse == QVkSwapChain::ImageResources::ScImageUseNone) { |
| 2729 | // was not used at all (no render pass), just transition from undefined to presentable |
| 2730 | presTrans.srcAccessMask = 0; |
| 2731 | presTrans.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 2732 | df->vkCmdPipelineBarrier(frame.cmdBuf, |
| 2733 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 2734 | 0, 0, nullptr, 0, nullptr, |
| 2735 | 1, &presTrans); |
| 2736 | } else if (image.lastUse == QVkSwapChain::ImageResources::ScImageUseTransferSource) { |
| 2737 | // was used in a readback as transfer source, go back to presentable layout |
| 2738 | presTrans.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 2739 | presTrans.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 2740 | df->vkCmdPipelineBarrier(frame.cmdBuf, |
| 2741 | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 2742 | 0, 0, nullptr, 0, nullptr, |
| 2743 | 1, &presTrans); |
| 2744 | } |
| 2745 | image.lastUse = QVkSwapChain::ImageResources::ScImageUseRender; |
| 2746 | } |
| 2747 | |
| 2748 | // record another timestamp, when enabled |
| 2749 | if (frame.timestampQueryIndex >= 0) { |
| 2750 | df->vkCmdWriteTimestamp(frame.cmdBuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 2751 | timestampQueryPool, uint32_t(frame.timestampQueryIndex + 1)); |
| 2752 | } |
| 2753 | |
| 2754 | // stop recording and submit to the queue |
| 2755 | Q_ASSERT(!frame.cmdFenceWaitable); |
| 2756 | const bool needsPresent = !flags.testFlag(flag: QRhi::SkipPresent); |
| 2757 | QRhi::FrameOpResult submitres = endAndSubmitPrimaryCommandBuffer(cb: frame.cmdBuf, |
| 2758 | cmdFence: frame.cmdFence, |
| 2759 | waitSem: frame.imageSemWaitable ? &frame.imageSem : nullptr, |
| 2760 | signalSem: needsPresent ? &image.drawSem : nullptr); |
| 2761 | if (submitres != QRhi::FrameOpSuccess) |
| 2762 | return submitres; |
| 2763 | |
| 2764 | frame.imageSemWaitable = false; |
| 2765 | frame.cmdFenceWaitable = true; |
| 2766 | |
| 2767 | if (needsPresent) { |
| 2768 | // add the Present to the queue |
| 2769 | VkPresentInfoKHR presInfo = {}; |
| 2770 | presInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
| 2771 | presInfo.swapchainCount = 1; |
| 2772 | presInfo.pSwapchains = &swapChainD->sc; |
| 2773 | presInfo.pImageIndices = &swapChainD->currentImageIndex; |
| 2774 | waitSemaphoresForPresent.append(t: image.drawSem); |
| 2775 | presInfo.waitSemaphoreCount = uint32_t(waitSemaphoresForPresent.count());; |
| 2776 | presInfo.pWaitSemaphores = waitSemaphoresForPresent.constData(); |
| 2777 | |
| 2778 | // Do platform-specific WM notification. F.ex. essential on Wayland in |
| 2779 | // order to circumvent driver frame callbacks |
| 2780 | inst->presentAboutToBeQueued(window: swapChainD->window); |
| 2781 | |
| 2782 | VkResult err = vkQueuePresentKHR(gfxQueue, &presInfo); |
| 2783 | waitSemaphoresForPresent.clear(); |
| 2784 | if (err != VK_SUCCESS) { |
| 2785 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { |
| 2786 | return QRhi::FrameOpSwapChainOutOfDate; |
| 2787 | } else if (err != VK_SUBOPTIMAL_KHR) { |
| 2788 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2789 | qWarning(msg: "Device loss detected in vkQueuePresentKHR()" ); |
| 2790 | deviceLost = true; |
| 2791 | return QRhi::FrameOpDeviceLost; |
| 2792 | } |
| 2793 | qWarning(msg: "Failed to present: %d" , err); |
| 2794 | return QRhi::FrameOpError; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | // Do platform-specific WM notification. F.ex. essential on X11 in |
| 2799 | // order to prevent glitches on resizing the window. |
| 2800 | inst->presentQueued(window: swapChainD->window); |
| 2801 | |
| 2802 | // mark the current swapchain buffer as unused from our side |
| 2803 | frame.imageAcquired = false; |
| 2804 | // and move on to the next buffer |
| 2805 | swapChainD->currentFrameSlot = (swapChainD->currentFrameSlot + 1) % QVK_FRAMES_IN_FLIGHT; |
| 2806 | } |
| 2807 | |
| 2808 | swapChainD->frameCount += 1; |
| 2809 | currentSwapChain = nullptr; |
| 2810 | return QRhi::FrameOpSuccess; |
| 2811 | } |
| 2812 | |
| 2813 | void QRhiVulkan::prepareNewFrame(QRhiCommandBuffer *cb) |
| 2814 | { |
| 2815 | // Now is the time to do things for frame N-F, where N is the current one, |
| 2816 | // F is QVK_FRAMES_IN_FLIGHT, because only here it is guaranteed that that |
| 2817 | // frame has completed on the GPU (due to the fence wait in beginFrame). To |
| 2818 | // decide if something is safe to handle now a simple "lastActiveFrameSlot |
| 2819 | // == currentFrameSlot" is sufficient (remember that e.g. with F==2 |
| 2820 | // currentFrameSlot goes 0, 1, 0, 1, 0, ...) |
| 2821 | // |
| 2822 | // With multiple swapchains on the same QRhi things get more convoluted |
| 2823 | // (and currentFrameSlot strictly alternating is not true anymore) but |
| 2824 | // begin(Offscreen)Frame() blocks anyway waiting for its current frame |
| 2825 | // slot's previous commands to complete so this here is safe regardless. |
| 2826 | |
| 2827 | executeDeferredReleases(); |
| 2828 | |
| 2829 | QRHI_RES(QVkCommandBuffer, cb)->resetState(); |
| 2830 | |
| 2831 | finishActiveReadbacks(); // last, in case the readback-completed callback issues rhi calls |
| 2832 | |
| 2833 | releaseCachedResourcesCalledBeforeFrameStart = false; |
| 2834 | } |
| 2835 | |
| 2836 | QRhi::FrameOpResult QRhiVulkan::startPrimaryCommandBuffer(VkCommandBuffer *cb) |
| 2837 | { |
| 2838 | if (!*cb) { |
| 2839 | VkCommandBufferAllocateInfo cmdBufInfo = {}; |
| 2840 | cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 2841 | cmdBufInfo.commandPool = cmdPool[currentFrameSlot]; |
| 2842 | cmdBufInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 2843 | cmdBufInfo.commandBufferCount = 1; |
| 2844 | |
| 2845 | VkResult err = df->vkAllocateCommandBuffers(dev, &cmdBufInfo, cb); |
| 2846 | if (err != VK_SUCCESS) { |
| 2847 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2848 | qWarning(msg: "Device loss detected in vkAllocateCommandBuffers()" ); |
| 2849 | deviceLost = true; |
| 2850 | return QRhi::FrameOpDeviceLost; |
| 2851 | } |
| 2852 | qWarning(msg: "Failed to allocate frame command buffer: %d" , err); |
| 2853 | return QRhi::FrameOpError; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | VkCommandBufferBeginInfo cmdBufBeginInfo = {}; |
| 2858 | cmdBufBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 2859 | |
| 2860 | VkResult err = df->vkBeginCommandBuffer(*cb, &cmdBufBeginInfo); |
| 2861 | if (err != VK_SUCCESS) { |
| 2862 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2863 | qWarning(msg: "Device loss detected in vkBeginCommandBuffer()" ); |
| 2864 | deviceLost = true; |
| 2865 | return QRhi::FrameOpDeviceLost; |
| 2866 | } |
| 2867 | qWarning(msg: "Failed to begin frame command buffer: %d" , err); |
| 2868 | return QRhi::FrameOpError; |
| 2869 | } |
| 2870 | |
| 2871 | return QRhi::FrameOpSuccess; |
| 2872 | } |
| 2873 | |
| 2874 | QRhi::FrameOpResult QRhiVulkan::endAndSubmitPrimaryCommandBuffer(VkCommandBuffer cb, VkFence cmdFence, |
| 2875 | VkSemaphore *waitSem, VkSemaphore *signalSem) |
| 2876 | { |
| 2877 | VkResult err = df->vkEndCommandBuffer(cb); |
| 2878 | if (err != VK_SUCCESS) { |
| 2879 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2880 | qWarning(msg: "Device loss detected in vkEndCommandBuffer()" ); |
| 2881 | deviceLost = true; |
| 2882 | return QRhi::FrameOpDeviceLost; |
| 2883 | } |
| 2884 | qWarning(msg: "Failed to end frame command buffer: %d" , err); |
| 2885 | return QRhi::FrameOpError; |
| 2886 | } |
| 2887 | |
| 2888 | VkSubmitInfo submitInfo = {}; |
| 2889 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 2890 | submitInfo.commandBufferCount = 1; |
| 2891 | submitInfo.pCommandBuffers = &cb; |
| 2892 | |
| 2893 | if (waitSem) |
| 2894 | waitSemaphoresForQueueSubmit.append(t: *waitSem); |
| 2895 | if (signalSem) |
| 2896 | signalSemaphoresForQueueSubmit.append(t: *signalSem); |
| 2897 | |
| 2898 | submitInfo.waitSemaphoreCount = uint32_t(waitSemaphoresForQueueSubmit.count()); |
| 2899 | if (!waitSemaphoresForQueueSubmit.isEmpty()) { |
| 2900 | submitInfo.pWaitSemaphores = waitSemaphoresForQueueSubmit.constData(); |
| 2901 | semaphoresWaitMasksForQueueSubmit.resize(sz: waitSemaphoresForQueueSubmit.count(), v: VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT); |
| 2902 | submitInfo.pWaitDstStageMask = semaphoresWaitMasksForQueueSubmit.constData(); |
| 2903 | } |
| 2904 | submitInfo.signalSemaphoreCount = uint32_t(signalSemaphoresForQueueSubmit.count()); |
| 2905 | if (!signalSemaphoresForQueueSubmit.isEmpty()) { |
| 2906 | submitInfo.pSignalSemaphores = signalSemaphoresForQueueSubmit.constData(); |
| 2907 | } |
| 2908 | |
| 2909 | err = df->vkQueueSubmit(gfxQueue, 1, &submitInfo, cmdFence); |
| 2910 | |
| 2911 | waitSemaphoresForQueueSubmit.clear(); |
| 2912 | signalSemaphoresForQueueSubmit.clear(); |
| 2913 | |
| 2914 | if (err != VK_SUCCESS) { |
| 2915 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2916 | qWarning(msg: "Device loss detected in vkQueueSubmit()" ); |
| 2917 | deviceLost = true; |
| 2918 | return QRhi::FrameOpDeviceLost; |
| 2919 | } |
| 2920 | qWarning(msg: "Failed to submit to graphics queue: %d" , err); |
| 2921 | return QRhi::FrameOpError; |
| 2922 | } |
| 2923 | |
| 2924 | return QRhi::FrameOpSuccess; |
| 2925 | } |
| 2926 | |
| 2927 | QRhi::FrameOpResult QRhiVulkan::waitCommandCompletion(int frameSlot) |
| 2928 | { |
| 2929 | for (QVkSwapChain *sc : std::as_const(t&: swapchains)) { |
| 2930 | const int frameResIndex = sc->bufferCount > 1 ? frameSlot : 0; |
| 2931 | QVkSwapChain::FrameResources &frame(sc->frameRes[frameResIndex]); |
| 2932 | if (frame.cmdFenceWaitable) { |
| 2933 | VkResult err = df->vkWaitForFences(dev, 1, &frame.cmdFence, VK_TRUE, UINT64_MAX); |
| 2934 | |
| 2935 | if (err != VK_SUCCESS) { |
| 2936 | if (err == VK_ERROR_DEVICE_LOST) { |
| 2937 | qWarning(msg: "Device loss detected in vkWaitForFences()" ); |
| 2938 | deviceLost = true; |
| 2939 | return QRhi::FrameOpDeviceLost; |
| 2940 | } |
| 2941 | qWarning(msg: "Failed to wait for fence: %d" , err); |
| 2942 | return QRhi::FrameOpError; |
| 2943 | } |
| 2944 | |
| 2945 | df->vkResetFences(dev, 1, &frame.cmdFence); |
| 2946 | frame.cmdFenceWaitable = false; |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | return QRhi::FrameOpSuccess; |
| 2951 | } |
| 2952 | |
| 2953 | QRhi::FrameOpResult QRhiVulkan::beginOffscreenFrame(QRhiCommandBuffer **cb, QRhi::BeginFrameFlags) |
| 2954 | { |
| 2955 | // Switch to the next slot manually. Swapchains do not know about this |
| 2956 | // which is good. So for example an onscreen, onscreen, offscreen, |
| 2957 | // onscreen, onscreen, onscreen sequence of frames leads to 0, 1, 0, 0, 1, |
| 2958 | // 0. (no strict alternation anymore) But this is not different from what |
| 2959 | // happens when multiple swapchains are involved. Offscreen frames are |
| 2960 | // synchronous anyway in the sense that they wait for execution to complete |
| 2961 | // in endOffscreenFrame, so no resources used in that frame are busy |
| 2962 | // anymore in the next frame. |
| 2963 | |
| 2964 | currentFrameSlot = (currentFrameSlot + 1) % QVK_FRAMES_IN_FLIGHT; |
| 2965 | |
| 2966 | QRhi::FrameOpResult waitResult = waitCommandCompletion(frameSlot: currentFrameSlot); |
| 2967 | if (waitResult != QRhi::FrameOpSuccess) |
| 2968 | return waitResult; |
| 2969 | |
| 2970 | ensureCommandPoolForNewFrame(); |
| 2971 | |
| 2972 | QVkCommandBuffer *cbWrapper = ofr.cbWrapper[currentFrameSlot]; |
| 2973 | QRhi::FrameOpResult cbres = startPrimaryCommandBuffer(cb: &cbWrapper->cb); |
| 2974 | if (cbres != QRhi::FrameOpSuccess) |
| 2975 | return cbres; |
| 2976 | |
| 2977 | prepareNewFrame(cb: cbWrapper); |
| 2978 | ofr.active = true; |
| 2979 | |
| 2980 | if (rhiFlags.testFlag(flag: QRhi::EnableTimestamps)) { |
| 2981 | int timestampQueryIdx = -1; |
| 2982 | for (int i = 0; i < timestampQueryPoolMap.size(); ++i) { |
| 2983 | if (!timestampQueryPoolMap.testBit(i)) { |
| 2984 | timestampQueryPoolMap.setBit(i); |
| 2985 | timestampQueryIdx = i * 2; |
| 2986 | break; |
| 2987 | } |
| 2988 | } |
| 2989 | if (timestampQueryIdx >= 0) { |
| 2990 | df->vkCmdResetQueryPool(cbWrapper->cb, timestampQueryPool, uint32_t(timestampQueryIdx), 2); |
| 2991 | // record timestamp at the start of the command buffer |
| 2992 | df->vkCmdWriteTimestamp(cbWrapper->cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
| 2993 | timestampQueryPool, uint32_t(timestampQueryIdx)); |
| 2994 | ofr.timestampQueryIndex = timestampQueryIdx; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | *cb = cbWrapper; |
| 2999 | return QRhi::FrameOpSuccess; |
| 3000 | } |
| 3001 | |
| 3002 | QRhi::FrameOpResult QRhiVulkan::endOffscreenFrame(QRhi::EndFrameFlags flags) |
| 3003 | { |
| 3004 | Q_UNUSED(flags); |
| 3005 | Q_ASSERT(ofr.active); |
| 3006 | ofr.active = false; |
| 3007 | |
| 3008 | QVkCommandBuffer *cbWrapper(ofr.cbWrapper[currentFrameSlot]); |
| 3009 | recordPrimaryCommandBuffer(cbD: cbWrapper); |
| 3010 | |
| 3011 | // record another timestamp, when enabled |
| 3012 | if (ofr.timestampQueryIndex >= 0) { |
| 3013 | df->vkCmdWriteTimestamp(cbWrapper->cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 3014 | timestampQueryPool, uint32_t(ofr.timestampQueryIndex + 1)); |
| 3015 | } |
| 3016 | |
| 3017 | if (!ofr.cmdFence) { |
| 3018 | VkFenceCreateInfo fenceInfo = {}; |
| 3019 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 3020 | VkResult err = df->vkCreateFence(dev, &fenceInfo, nullptr, &ofr.cmdFence); |
| 3021 | if (err != VK_SUCCESS) { |
| 3022 | qWarning(msg: "Failed to create command buffer fence: %d" , err); |
| 3023 | return QRhi::FrameOpError; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | QRhi::FrameOpResult submitres = endAndSubmitPrimaryCommandBuffer(cb: cbWrapper->cb, cmdFence: ofr.cmdFence, waitSem: nullptr, signalSem: nullptr); |
| 3028 | if (submitres != QRhi::FrameOpSuccess) |
| 3029 | return submitres; |
| 3030 | |
| 3031 | // wait for completion |
| 3032 | df->vkWaitForFences(dev, 1, &ofr.cmdFence, VK_TRUE, UINT64_MAX); |
| 3033 | df->vkResetFences(dev, 1, &ofr.cmdFence); |
| 3034 | |
| 3035 | // Here we know that executing the host-side reads for this (or any |
| 3036 | // previous) frame is safe since we waited for completion above. |
| 3037 | finishActiveReadbacks(forced: true); |
| 3038 | |
| 3039 | // Read the timestamps, if we wrote them. |
| 3040 | if (ofr.timestampQueryIndex >= 0) { |
| 3041 | quint64 timestamp[2] = { 0, 0 }; |
| 3042 | VkResult err = df->vkGetQueryPoolResults(dev, timestampQueryPool, uint32_t(ofr.timestampQueryIndex), 2, |
| 3043 | 2 * sizeof(quint64), timestamp, sizeof(quint64), |
| 3044 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 3045 | timestampQueryPoolMap.clearBit(i: ofr.timestampQueryIndex / 2); |
| 3046 | ofr.timestampQueryIndex = -1; |
| 3047 | if (err == VK_SUCCESS) { |
| 3048 | bool ok = false; |
| 3049 | const double elapsedSec = elapsedSecondsFromTimestamp(timestamp, ok: &ok); |
| 3050 | if (ok) |
| 3051 | cbWrapper->lastGpuTime = elapsedSec; |
| 3052 | } else { |
| 3053 | qWarning(msg: "Failed to query timestamp: %d" , err); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | return QRhi::FrameOpSuccess; |
| 3058 | } |
| 3059 | |
| 3060 | QRhi::FrameOpResult QRhiVulkan::finish() |
| 3061 | { |
| 3062 | QVkSwapChain *swapChainD = nullptr; |
| 3063 | if (inFrame) { |
| 3064 | // There is either a swapchain or an offscreen frame on-going. |
| 3065 | // End command recording and submit what we have. |
| 3066 | VkCommandBuffer cb; |
| 3067 | if (ofr.active) { |
| 3068 | Q_ASSERT(!currentSwapChain); |
| 3069 | QVkCommandBuffer *cbWrapper(ofr.cbWrapper[currentFrameSlot]); |
| 3070 | Q_ASSERT(cbWrapper->recordingPass == QVkCommandBuffer::NoPass); |
| 3071 | recordPrimaryCommandBuffer(cbD: cbWrapper); |
| 3072 | cbWrapper->resetCommands(); |
| 3073 | cb = cbWrapper->cb; |
| 3074 | } else { |
| 3075 | Q_ASSERT(currentSwapChain); |
| 3076 | Q_ASSERT(currentSwapChain->cbWrapper.recordingPass == QVkCommandBuffer::NoPass); |
| 3077 | swapChainD = currentSwapChain; |
| 3078 | recordPrimaryCommandBuffer(cbD: &swapChainD->cbWrapper); |
| 3079 | swapChainD->cbWrapper.resetCommands(); |
| 3080 | cb = swapChainD->cbWrapper.cb; |
| 3081 | } |
| 3082 | QRhi::FrameOpResult submitres = endAndSubmitPrimaryCommandBuffer(cb, VK_NULL_HANDLE, waitSem: nullptr, signalSem: nullptr); |
| 3083 | if (submitres != QRhi::FrameOpSuccess) |
| 3084 | return submitres; |
| 3085 | } |
| 3086 | |
| 3087 | df->vkQueueWaitIdle(gfxQueue); |
| 3088 | |
| 3089 | if (inFrame) { |
| 3090 | // The current frame slot's command pool needs to be reset. |
| 3091 | ensureCommandPoolForNewFrame(); |
| 3092 | // Allocate and begin recording on a new command buffer. |
| 3093 | if (ofr.active) { |
| 3094 | startPrimaryCommandBuffer(cb: &ofr.cbWrapper[currentFrameSlot]->cb); |
| 3095 | } else { |
| 3096 | QVkSwapChain::FrameResources &frame(swapChainD->frameRes[swapChainD->currentFrameSlot]); |
| 3097 | startPrimaryCommandBuffer(cb: &frame.cmdBuf); |
| 3098 | swapChainD->cbWrapper.cb = frame.cmdBuf; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | executeDeferredReleases(forced: true); |
| 3103 | finishActiveReadbacks(forced: true); |
| 3104 | |
| 3105 | return QRhi::FrameOpSuccess; |
| 3106 | } |
| 3107 | |
| 3108 | static inline QRhiPassResourceTracker::UsageState toPassTrackerUsageState(const QVkBuffer::UsageState &bufUsage) |
| 3109 | { |
| 3110 | QRhiPassResourceTracker::UsageState u; |
| 3111 | u.layout = 0; // unused with buffers |
| 3112 | u.access = int(bufUsage.access); |
| 3113 | u.stage = int(bufUsage.stage); |
| 3114 | return u; |
| 3115 | } |
| 3116 | |
| 3117 | static inline QRhiPassResourceTracker::UsageState toPassTrackerUsageState(const QVkTexture::UsageState &texUsage) |
| 3118 | { |
| 3119 | QRhiPassResourceTracker::UsageState u; |
| 3120 | u.layout = texUsage.layout; |
| 3121 | u.access = int(texUsage.access); |
| 3122 | u.stage = int(texUsage.stage); |
| 3123 | return u; |
| 3124 | } |
| 3125 | |
| 3126 | void QRhiVulkan::activateTextureRenderTarget(QVkCommandBuffer *cbD, QVkTextureRenderTarget *rtD) |
| 3127 | { |
| 3128 | if (!QRhiRenderTargetAttachmentTracker::isUpToDate<QVkTexture, QVkRenderBuffer>(desc: rtD->description(), currentResIdList: rtD->d.currentResIdList)) |
| 3129 | rtD->create(); |
| 3130 | |
| 3131 | rtD->lastActiveFrameSlot = currentFrameSlot; |
| 3132 | rtD->d.rp->lastActiveFrameSlot = currentFrameSlot; |
| 3133 | QRhiPassResourceTracker &passResTracker(cbD->passResTrackers[cbD->currentPassResTrackerIndex]); |
| 3134 | for (auto it = rtD->m_desc.cbeginColorAttachments(), itEnd = rtD->m_desc.cendColorAttachments(); it != itEnd; ++it) { |
| 3135 | QVkTexture *texD = QRHI_RES(QVkTexture, it->texture()); |
| 3136 | QVkTexture *resolveTexD = QRHI_RES(QVkTexture, it->resolveTexture()); |
| 3137 | QVkRenderBuffer *rbD = QRHI_RES(QVkRenderBuffer, it->renderBuffer()); |
| 3138 | if (texD) { |
| 3139 | trackedRegisterTexture(passResTracker: &passResTracker, texD, |
| 3140 | access: QRhiPassResourceTracker::TexColorOutput, |
| 3141 | stage: QRhiPassResourceTracker::TexColorOutputStage); |
| 3142 | texD->lastActiveFrameSlot = currentFrameSlot; |
| 3143 | } else if (rbD) { |
| 3144 | // Won't register rbD->backingTexture because it cannot be used for |
| 3145 | // anything in a renderpass, its use makes only sense in |
| 3146 | // combination with a resolveTexture. |
| 3147 | rbD->lastActiveFrameSlot = currentFrameSlot; |
| 3148 | } |
| 3149 | if (resolveTexD) { |
| 3150 | trackedRegisterTexture(passResTracker: &passResTracker, texD: resolveTexD, |
| 3151 | access: QRhiPassResourceTracker::TexColorOutput, |
| 3152 | stage: QRhiPassResourceTracker::TexColorOutputStage); |
| 3153 | resolveTexD->lastActiveFrameSlot = currentFrameSlot; |
| 3154 | } |
| 3155 | } |
| 3156 | if (rtD->m_desc.depthStencilBuffer()) { |
| 3157 | QVkRenderBuffer *rbD = QRHI_RES(QVkRenderBuffer, rtD->m_desc.depthStencilBuffer()); |
| 3158 | Q_ASSERT(rbD->m_type == QRhiRenderBuffer::DepthStencil); |
| 3159 | // We specify no explicit VkSubpassDependency for an offscreen render |
| 3160 | // target, meaning we need an explicit barrier for the depth-stencil |
| 3161 | // buffer to avoid a write-after-write hazard (as the implicit one is |
| 3162 | // not sufficient). Textures are taken care of by the resource tracking |
| 3163 | // but that excludes the (content-wise) throwaway depth-stencil buffer. |
| 3164 | depthStencilExplicitBarrier(cbD, rbD); |
| 3165 | rbD->lastActiveFrameSlot = currentFrameSlot; |
| 3166 | } |
| 3167 | if (rtD->m_desc.depthTexture()) { |
| 3168 | QVkTexture *depthTexD = QRHI_RES(QVkTexture, rtD->m_desc.depthTexture()); |
| 3169 | trackedRegisterTexture(passResTracker: &passResTracker, texD: depthTexD, |
| 3170 | access: QRhiPassResourceTracker::TexDepthOutput, |
| 3171 | stage: QRhiPassResourceTracker::TexDepthOutputStage); |
| 3172 | depthTexD->lastActiveFrameSlot = currentFrameSlot; |
| 3173 | } |
| 3174 | if (rtD->m_desc.depthResolveTexture()) { |
| 3175 | QVkTexture *depthResolveTexD = QRHI_RES(QVkTexture, rtD->m_desc.depthResolveTexture()); |
| 3176 | trackedRegisterTexture(passResTracker: &passResTracker, texD: depthResolveTexD, |
| 3177 | access: QRhiPassResourceTracker::TexDepthOutput, |
| 3178 | stage: QRhiPassResourceTracker::TexDepthOutputStage); |
| 3179 | depthResolveTexD->lastActiveFrameSlot = currentFrameSlot; |
| 3180 | } |
| 3181 | if (rtD->m_desc.shadingRateMap()) { |
| 3182 | QVkTexture *texD = QRHI_RES(QVkShadingRateMap, rtD->m_desc.shadingRateMap())->texture; |
| 3183 | trackedRegisterTexture(passResTracker: &passResTracker, texD, |
| 3184 | access: QRhiPassResourceTracker::TexShadingRate, |
| 3185 | stage: QRhiPassResourceTracker::TexColorOutputStage); |
| 3186 | texD->lastActiveFrameSlot = currentFrameSlot; |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | void QRhiVulkan::resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) |
| 3191 | { |
| 3192 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3193 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3194 | |
| 3195 | enqueueResourceUpdates(cbD, resourceUpdates); |
| 3196 | } |
| 3197 | |
| 3198 | VkCommandBuffer QRhiVulkan::startSecondaryCommandBuffer(QVkRenderTargetData *rtD) |
| 3199 | { |
| 3200 | VkCommandBuffer secondaryCb; |
| 3201 | |
| 3202 | if (!freeSecondaryCbs[currentFrameSlot].isEmpty()) { |
| 3203 | secondaryCb = freeSecondaryCbs[currentFrameSlot].last(); |
| 3204 | freeSecondaryCbs[currentFrameSlot].removeLast(); |
| 3205 | } else { |
| 3206 | VkCommandBufferAllocateInfo cmdBufInfo = {}; |
| 3207 | cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 3208 | cmdBufInfo.commandPool = cmdPool[currentFrameSlot]; |
| 3209 | cmdBufInfo.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY; |
| 3210 | cmdBufInfo.commandBufferCount = 1; |
| 3211 | |
| 3212 | VkResult err = df->vkAllocateCommandBuffers(dev, &cmdBufInfo, &secondaryCb); |
| 3213 | if (err != VK_SUCCESS) { |
| 3214 | qWarning(msg: "Failed to create secondary command buffer: %d" , err); |
| 3215 | return VK_NULL_HANDLE; |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | VkCommandBufferBeginInfo cmdBufBeginInfo = {}; |
| 3220 | cmdBufBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 3221 | cmdBufBeginInfo.flags = rtD ? VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT : 0; |
| 3222 | VkCommandBufferInheritanceInfo cmdBufInheritInfo = {}; |
| 3223 | cmdBufInheritInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
| 3224 | cmdBufInheritInfo.subpass = 0; |
| 3225 | if (rtD) { |
| 3226 | cmdBufInheritInfo.renderPass = rtD->rp->rp; |
| 3227 | cmdBufInheritInfo.framebuffer = rtD->fb; |
| 3228 | } |
| 3229 | cmdBufBeginInfo.pInheritanceInfo = &cmdBufInheritInfo; |
| 3230 | |
| 3231 | VkResult err = df->vkBeginCommandBuffer(secondaryCb, &cmdBufBeginInfo); |
| 3232 | if (err != VK_SUCCESS) { |
| 3233 | qWarning(msg: "Failed to begin secondary command buffer: %d" , err); |
| 3234 | return VK_NULL_HANDLE; |
| 3235 | } |
| 3236 | |
| 3237 | return secondaryCb; |
| 3238 | } |
| 3239 | |
| 3240 | void QRhiVulkan::endAndEnqueueSecondaryCommandBuffer(VkCommandBuffer cb, QVkCommandBuffer *cbD) |
| 3241 | { |
| 3242 | VkResult err = df->vkEndCommandBuffer(cb); |
| 3243 | if (err != VK_SUCCESS) |
| 3244 | qWarning(msg: "Failed to end secondary command buffer: %d" , err); |
| 3245 | |
| 3246 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3247 | cmd.cmd = QVkCommandBuffer::Command::ExecuteSecondary; |
| 3248 | cmd.args.executeSecondary.cb = cb; |
| 3249 | |
| 3250 | QRhiVulkan::DeferredReleaseEntry e; |
| 3251 | e.type = QRhiVulkan::DeferredReleaseEntry::SecondaryCommandBuffer; |
| 3252 | e.lastActiveFrameSlot = currentFrameSlot; |
| 3253 | e.secondaryCommandBuffer.cb = cb; |
| 3254 | releaseQueue.append(t: e); |
| 3255 | } |
| 3256 | |
| 3257 | void QRhiVulkan::beginPass(QRhiCommandBuffer *cb, |
| 3258 | QRhiRenderTarget *rt, |
| 3259 | const QColor &colorClearValue, |
| 3260 | const QRhiDepthStencilClearValue &depthStencilClearValue, |
| 3261 | QRhiResourceUpdateBatch *resourceUpdates, |
| 3262 | QRhiCommandBuffer::BeginPassFlags flags) |
| 3263 | { |
| 3264 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3265 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3266 | |
| 3267 | if (resourceUpdates) |
| 3268 | enqueueResourceUpdates(cbD, resourceUpdates); |
| 3269 | |
| 3270 | // Insert a TransitionPassResources into the command stream, pointing to |
| 3271 | // the tracker this pass is going to use. That's how we generate the |
| 3272 | // barriers later during recording the real VkCommandBuffer, right before |
| 3273 | // the vkCmdBeginRenderPass. |
| 3274 | enqueueTransitionPassResources(cbD); |
| 3275 | |
| 3276 | QVkRenderTargetData *rtD = nullptr; |
| 3277 | switch (rt->resourceType()) { |
| 3278 | case QRhiResource::SwapChainRenderTarget: |
| 3279 | rtD = &QRHI_RES(QVkSwapChainRenderTarget, rt)->d; |
| 3280 | rtD->rp->lastActiveFrameSlot = currentFrameSlot; |
| 3281 | Q_ASSERT(currentSwapChain); |
| 3282 | currentSwapChain->imageRes[currentSwapChain->currentImageIndex].lastUse = |
| 3283 | QVkSwapChain::ImageResources::ScImageUseRender; |
| 3284 | if (currentSwapChain->shadingRateMapView) { |
| 3285 | QVkTexture *texD = QRHI_RES(QVkShadingRateMap, currentSwapChain->shadingRateMap())->texture; |
| 3286 | QRhiPassResourceTracker &passResTracker(cbD->passResTrackers[cbD->currentPassResTrackerIndex]); |
| 3287 | trackedRegisterTexture(passResTracker: &passResTracker, texD, |
| 3288 | access: QRhiPassResourceTracker::TexShadingRate, |
| 3289 | stage: QRhiPassResourceTracker::TexColorOutputStage); |
| 3290 | texD->lastActiveFrameSlot = currentFrameSlot; |
| 3291 | } |
| 3292 | break; |
| 3293 | case QRhiResource::TextureRenderTarget: |
| 3294 | { |
| 3295 | QVkTextureRenderTarget *rtTex = QRHI_RES(QVkTextureRenderTarget, rt); |
| 3296 | rtD = &rtTex->d; |
| 3297 | activateTextureRenderTarget(cbD, rtD: rtTex); |
| 3298 | } |
| 3299 | break; |
| 3300 | default: |
| 3301 | Q_UNREACHABLE(); |
| 3302 | break; |
| 3303 | } |
| 3304 | |
| 3305 | cbD->recordingPass = QVkCommandBuffer::RenderPass; |
| 3306 | cbD->passUsesSecondaryCb = flags.testFlag(flag: QRhiCommandBuffer::ExternalContent); |
| 3307 | cbD->currentTarget = rt; |
| 3308 | |
| 3309 | // No copy operations or image layout transitions allowed after this point |
| 3310 | // (up until endPass) as we are going to begin the renderpass. |
| 3311 | |
| 3312 | VkRenderPassBeginInfo rpBeginInfo = {}; |
| 3313 | rpBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
| 3314 | rpBeginInfo.renderPass = rtD->rp->rp; |
| 3315 | rpBeginInfo.framebuffer = rtD->fb; |
| 3316 | rpBeginInfo.renderArea.extent.width = uint32_t(rtD->pixelSize.width()); |
| 3317 | rpBeginInfo.renderArea.extent.height = uint32_t(rtD->pixelSize.height()); |
| 3318 | |
| 3319 | QVarLengthArray<VkClearValue, (QVkRenderTargetData::MAX_COLOR_ATTACHMENTS + 1) * 2 + 1> cvs; |
| 3320 | for (int i = 0; i < rtD->colorAttCount; ++i) { |
| 3321 | VkClearValue cv; |
| 3322 | cv.color = { .float32: { float(colorClearValue.redF()), float(colorClearValue.greenF()), float(colorClearValue.blueF()), |
| 3323 | float(colorClearValue.alphaF()) } }; |
| 3324 | cvs.append(t: cv); |
| 3325 | } |
| 3326 | for (int i = 0; i < rtD->dsAttCount; ++i) { |
| 3327 | VkClearValue cv; |
| 3328 | cv.depthStencil = { .depth: depthStencilClearValue.depthClearValue(), .stencil: depthStencilClearValue.stencilClearValue() }; |
| 3329 | cvs.append(t: cv); |
| 3330 | } |
| 3331 | for (int i = 0; i < rtD->resolveAttCount; ++i) { |
| 3332 | VkClearValue cv; |
| 3333 | cv.color = { .float32: { float(colorClearValue.redF()), float(colorClearValue.greenF()), float(colorClearValue.blueF()), |
| 3334 | float(colorClearValue.alphaF()) } }; |
| 3335 | cvs.append(t: cv); |
| 3336 | } |
| 3337 | for (int i = 0; i < rtD->dsResolveAttCount; ++i) { |
| 3338 | VkClearValue cv; |
| 3339 | cv.depthStencil = { .depth: depthStencilClearValue.depthClearValue(), .stencil: depthStencilClearValue.stencilClearValue() }; |
| 3340 | cvs.append(t: cv); |
| 3341 | } |
| 3342 | for (int i = 0; i < rtD->shadingRateAttCount; ++i) { |
| 3343 | VkClearValue cv; |
| 3344 | cv.color = { .float32: { 0.0f, 0.0f, 0.0f, 0.0f } }; |
| 3345 | cvs.append(t: cv); |
| 3346 | } |
| 3347 | rpBeginInfo.clearValueCount = uint32_t(cvs.size()); |
| 3348 | |
| 3349 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3350 | cmd.cmd = QVkCommandBuffer::Command::BeginRenderPass; |
| 3351 | cmd.args.beginRenderPass.desc = rpBeginInfo; |
| 3352 | cmd.args.beginRenderPass.clearValueIndex = cbD->pools.clearValue.size(); |
| 3353 | cmd.args.beginRenderPass.useSecondaryCb = cbD->passUsesSecondaryCb; |
| 3354 | cbD->pools.clearValue.append(buf: cvs.constData(), sz: cvs.size()); |
| 3355 | |
| 3356 | if (cbD->passUsesSecondaryCb) |
| 3357 | cbD->activeSecondaryCbStack.append(t: startSecondaryCommandBuffer(rtD)); |
| 3358 | |
| 3359 | if (cbD->hasShadingRateSet) { |
| 3360 | QVkCommandBuffer::Command &rateCmd(cbD->commands.get()); |
| 3361 | rateCmd.cmd = QVkCommandBuffer::Command::SetShadingRate; |
| 3362 | rateCmd.args.setShadingRate.w = 1; |
| 3363 | rateCmd.args.setShadingRate.h = 1; |
| 3364 | } |
| 3365 | |
| 3366 | cbD->resetPerPassState(); |
| 3367 | } |
| 3368 | |
| 3369 | void QRhiVulkan::endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) |
| 3370 | { |
| 3371 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3372 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 3373 | |
| 3374 | if (cbD->passUsesSecondaryCb) { |
| 3375 | VkCommandBuffer secondaryCb = cbD->activeSecondaryCbStack.last(); |
| 3376 | cbD->activeSecondaryCbStack.removeLast(); |
| 3377 | endAndEnqueueSecondaryCommandBuffer(cb: secondaryCb, cbD); |
| 3378 | } |
| 3379 | |
| 3380 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3381 | cmd.cmd = QVkCommandBuffer::Command::EndRenderPass; |
| 3382 | |
| 3383 | cbD->recordingPass = QVkCommandBuffer::NoPass; |
| 3384 | cbD->currentTarget = nullptr; |
| 3385 | |
| 3386 | if (resourceUpdates) |
| 3387 | enqueueResourceUpdates(cbD, resourceUpdates); |
| 3388 | } |
| 3389 | |
| 3390 | void QRhiVulkan::beginComputePass(QRhiCommandBuffer *cb, |
| 3391 | QRhiResourceUpdateBatch *resourceUpdates, |
| 3392 | QRhiCommandBuffer::BeginPassFlags flags) |
| 3393 | { |
| 3394 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3395 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3396 | |
| 3397 | if (resourceUpdates) |
| 3398 | enqueueResourceUpdates(cbD, resourceUpdates); |
| 3399 | |
| 3400 | enqueueTransitionPassResources(cbD); |
| 3401 | |
| 3402 | cbD->recordingPass = QVkCommandBuffer::ComputePass; |
| 3403 | cbD->passUsesSecondaryCb = flags.testFlag(flag: QRhiCommandBuffer::ExternalContent); |
| 3404 | |
| 3405 | cbD->computePassState.reset(); |
| 3406 | |
| 3407 | if (cbD->passUsesSecondaryCb) |
| 3408 | cbD->activeSecondaryCbStack.append(t: startSecondaryCommandBuffer()); |
| 3409 | |
| 3410 | cbD->resetPerPassState(); |
| 3411 | } |
| 3412 | |
| 3413 | void QRhiVulkan::endComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) |
| 3414 | { |
| 3415 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3416 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::ComputePass); |
| 3417 | |
| 3418 | if (cbD->passUsesSecondaryCb) { |
| 3419 | VkCommandBuffer secondaryCb = cbD->activeSecondaryCbStack.last(); |
| 3420 | cbD->activeSecondaryCbStack.removeLast(); |
| 3421 | endAndEnqueueSecondaryCommandBuffer(cb: secondaryCb, cbD); |
| 3422 | } |
| 3423 | |
| 3424 | cbD->recordingPass = QVkCommandBuffer::NoPass; |
| 3425 | |
| 3426 | if (resourceUpdates) |
| 3427 | enqueueResourceUpdates(cbD, resourceUpdates); |
| 3428 | } |
| 3429 | |
| 3430 | void QRhiVulkan::setComputePipeline(QRhiCommandBuffer *cb, QRhiComputePipeline *ps) |
| 3431 | { |
| 3432 | QVkComputePipeline *psD = QRHI_RES(QVkComputePipeline, ps); |
| 3433 | Q_ASSERT(psD->pipeline); |
| 3434 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3435 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::ComputePass); |
| 3436 | |
| 3437 | if (cbD->currentComputePipeline != ps || cbD->currentPipelineGeneration != psD->generation) { |
| 3438 | if (cbD->passUsesSecondaryCb) { |
| 3439 | df->vkCmdBindPipeline(cbD->activeSecondaryCbStack.last(), VK_PIPELINE_BIND_POINT_COMPUTE, psD->pipeline); |
| 3440 | } else { |
| 3441 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3442 | cmd.cmd = QVkCommandBuffer::Command::BindPipeline; |
| 3443 | cmd.args.bindPipeline.bindPoint = VK_PIPELINE_BIND_POINT_COMPUTE; |
| 3444 | cmd.args.bindPipeline.pipeline = psD->pipeline; |
| 3445 | } |
| 3446 | |
| 3447 | cbD->currentGraphicsPipeline = nullptr; |
| 3448 | cbD->currentComputePipeline = ps; |
| 3449 | cbD->currentPipelineGeneration = psD->generation; |
| 3450 | } |
| 3451 | |
| 3452 | psD->lastActiveFrameSlot = currentFrameSlot; |
| 3453 | } |
| 3454 | |
| 3455 | template<typename T> |
| 3456 | inline void qrhivk_accumulateComputeResource(T *writtenResources, QRhiResource *resource, |
| 3457 | QRhiShaderResourceBinding::Type bindingType, |
| 3458 | int loadTypeVal, int storeTypeVal, int loadStoreTypeVal) |
| 3459 | { |
| 3460 | VkAccessFlags access = 0; |
| 3461 | if (bindingType == loadTypeVal) { |
| 3462 | access = VK_ACCESS_SHADER_READ_BIT; |
| 3463 | } else { |
| 3464 | access = VK_ACCESS_SHADER_WRITE_BIT; |
| 3465 | if (bindingType == loadStoreTypeVal) |
| 3466 | access |= VK_ACCESS_SHADER_READ_BIT; |
| 3467 | } |
| 3468 | auto it = writtenResources->find(resource); |
| 3469 | if (it != writtenResources->end()) |
| 3470 | it->first |= access; |
| 3471 | else if (bindingType == storeTypeVal || bindingType == loadStoreTypeVal) |
| 3472 | writtenResources->insert(resource, { access, true }); |
| 3473 | } |
| 3474 | |
| 3475 | void QRhiVulkan::dispatch(QRhiCommandBuffer *cb, int x, int y, int z) |
| 3476 | { |
| 3477 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 3478 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::ComputePass); |
| 3479 | |
| 3480 | // When there are multiple dispatches, read-after-write and |
| 3481 | // write-after-write need a barrier. |
| 3482 | QVarLengthArray<VkImageMemoryBarrier, 8> imageBarriers; |
| 3483 | QVarLengthArray<VkBufferMemoryBarrier, 8> bufferBarriers; |
| 3484 | if (cbD->currentComputeSrb) { |
| 3485 | // The key in the writtenResources map indicates that the resource was |
| 3486 | // written in a previous dispatch, whereas the value accumulates the |
| 3487 | // access mask in the current one. |
| 3488 | for (auto &accessAndIsNewFlag : cbD->computePassState.writtenResources) |
| 3489 | accessAndIsNewFlag = { 0, false }; |
| 3490 | |
| 3491 | QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, cbD->currentComputeSrb); |
| 3492 | const int bindingCount = srbD->m_bindings.size(); |
| 3493 | for (int i = 0; i < bindingCount; ++i) { |
| 3494 | const QRhiShaderResourceBinding::Data *b = shaderResourceBindingData(binding: srbD->m_bindings.at(idx: i)); |
| 3495 | switch (b->type) { |
| 3496 | case QRhiShaderResourceBinding::ImageLoad: |
| 3497 | case QRhiShaderResourceBinding::ImageStore: |
| 3498 | case QRhiShaderResourceBinding::ImageLoadStore: |
| 3499 | qrhivk_accumulateComputeResource(writtenResources: &cbD->computePassState.writtenResources, |
| 3500 | resource: b->u.simage.tex, |
| 3501 | bindingType: b->type, |
| 3502 | loadTypeVal: QRhiShaderResourceBinding::ImageLoad, |
| 3503 | storeTypeVal: QRhiShaderResourceBinding::ImageStore, |
| 3504 | loadStoreTypeVal: QRhiShaderResourceBinding::ImageLoadStore); |
| 3505 | break; |
| 3506 | case QRhiShaderResourceBinding::BufferLoad: |
| 3507 | case QRhiShaderResourceBinding::BufferStore: |
| 3508 | case QRhiShaderResourceBinding::BufferLoadStore: |
| 3509 | qrhivk_accumulateComputeResource(writtenResources: &cbD->computePassState.writtenResources, |
| 3510 | resource: b->u.sbuf.buf, |
| 3511 | bindingType: b->type, |
| 3512 | loadTypeVal: QRhiShaderResourceBinding::BufferLoad, |
| 3513 | storeTypeVal: QRhiShaderResourceBinding::BufferStore, |
| 3514 | loadStoreTypeVal: QRhiShaderResourceBinding::BufferLoadStore); |
| 3515 | break; |
| 3516 | default: |
| 3517 | break; |
| 3518 | } |
| 3519 | } |
| 3520 | |
| 3521 | for (auto it = cbD->computePassState.writtenResources.begin(); it != cbD->computePassState.writtenResources.end(); ) { |
| 3522 | const int accessInThisDispatch = it->first; |
| 3523 | const bool isNewInThisDispatch = it->second; |
| 3524 | if (accessInThisDispatch && !isNewInThisDispatch) { |
| 3525 | if (it.key()->resourceType() == QRhiResource::Texture) { |
| 3526 | QVkTexture *texD = QRHI_RES(QVkTexture, it.key()); |
| 3527 | VkImageMemoryBarrier barrier = {}; |
| 3528 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3529 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3530 | // won't care about subresources, pretend the whole resource was written |
| 3531 | barrier.subresourceRange.baseMipLevel = 0; |
| 3532 | barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; |
| 3533 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3534 | barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS; |
| 3535 | barrier.oldLayout = texD->usageState.layout; |
| 3536 | barrier.newLayout = texD->usageState.layout; |
| 3537 | barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 3538 | barrier.dstAccessMask = accessInThisDispatch; |
| 3539 | barrier.image = texD->image; |
| 3540 | imageBarriers.append(t: barrier); |
| 3541 | } else { |
| 3542 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, it.key()); |
| 3543 | VkBufferMemoryBarrier barrier = {}; |
| 3544 | barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3545 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3546 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3547 | barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 3548 | barrier.dstAccessMask = accessInThisDispatch; |
| 3549 | barrier.buffer = bufD->buffers[bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0]; |
| 3550 | barrier.size = VK_WHOLE_SIZE; |
| 3551 | bufferBarriers.append(t: barrier); |
| 3552 | } |
| 3553 | } |
| 3554 | // Anything that was previously written, but is only read now, can be |
| 3555 | // removed from the written list (because that previous write got a |
| 3556 | // corresponding barrier now). |
| 3557 | if (accessInThisDispatch == VK_ACCESS_SHADER_READ_BIT) |
| 3558 | it = cbD->computePassState.writtenResources.erase(it); |
| 3559 | else |
| 3560 | ++it; |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | if (cbD->passUsesSecondaryCb) { |
| 3565 | VkCommandBuffer secondaryCb = cbD->activeSecondaryCbStack.last(); |
| 3566 | if (!imageBarriers.isEmpty()) { |
| 3567 | df->vkCmdPipelineBarrier(secondaryCb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, |
| 3568 | 0, 0, nullptr, |
| 3569 | 0, nullptr, |
| 3570 | imageBarriers.size(), imageBarriers.constData()); |
| 3571 | } |
| 3572 | if (!bufferBarriers.isEmpty()) { |
| 3573 | df->vkCmdPipelineBarrier(secondaryCb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, |
| 3574 | 0, 0, nullptr, |
| 3575 | bufferBarriers.size(), bufferBarriers.constData(), |
| 3576 | 0, nullptr); |
| 3577 | } |
| 3578 | df->vkCmdDispatch(secondaryCb, uint32_t(x), uint32_t(y), uint32_t(z)); |
| 3579 | } else { |
| 3580 | if (!imageBarriers.isEmpty()) { |
| 3581 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3582 | cmd.cmd = QVkCommandBuffer::Command::ImageBarrier; |
| 3583 | cmd.args.imageBarrier.srcStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3584 | cmd.args.imageBarrier.dstStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3585 | cmd.args.imageBarrier.count = imageBarriers.size(); |
| 3586 | cmd.args.imageBarrier.index = cbD->pools.imageBarrier.size(); |
| 3587 | cbD->pools.imageBarrier.append(buf: imageBarriers.constData(), sz: imageBarriers.size()); |
| 3588 | } |
| 3589 | if (!bufferBarriers.isEmpty()) { |
| 3590 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3591 | cmd.cmd = QVkCommandBuffer::Command::BufferBarrier; |
| 3592 | cmd.args.bufferBarrier.srcStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3593 | cmd.args.bufferBarrier.dstStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 3594 | cmd.args.bufferBarrier.count = bufferBarriers.size(); |
| 3595 | cmd.args.bufferBarrier.index = cbD->pools.bufferBarrier.size(); |
| 3596 | cbD->pools.bufferBarrier.append(buf: bufferBarriers.constData(), sz: bufferBarriers.size()); |
| 3597 | } |
| 3598 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3599 | cmd.cmd = QVkCommandBuffer::Command::Dispatch; |
| 3600 | cmd.args.dispatch.x = x; |
| 3601 | cmd.args.dispatch.y = y; |
| 3602 | cmd.args.dispatch.z = z; |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | VkShaderModule QRhiVulkan::createShader(const QByteArray &spirv) |
| 3607 | { |
| 3608 | VkShaderModuleCreateInfo shaderInfo = {}; |
| 3609 | shaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 3610 | shaderInfo.codeSize = size_t(spirv.size()); |
| 3611 | shaderInfo.pCode = reinterpret_cast<const quint32 *>(spirv.constData()); |
| 3612 | VkShaderModule shaderModule; |
| 3613 | VkResult err = df->vkCreateShaderModule(dev, &shaderInfo, nullptr, &shaderModule); |
| 3614 | if (err != VK_SUCCESS) { |
| 3615 | qWarning(msg: "Failed to create shader module: %d" , err); |
| 3616 | return VK_NULL_HANDLE; |
| 3617 | } |
| 3618 | return shaderModule; |
| 3619 | } |
| 3620 | |
| 3621 | bool QRhiVulkan::ensurePipelineCache(const void *initialData, size_t initialDataSize) |
| 3622 | { |
| 3623 | if (pipelineCache) |
| 3624 | return true; |
| 3625 | |
| 3626 | VkPipelineCacheCreateInfo pipelineCacheInfo = {}; |
| 3627 | pipelineCacheInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 3628 | pipelineCacheInfo.initialDataSize = initialDataSize; |
| 3629 | pipelineCacheInfo.pInitialData = initialData; |
| 3630 | VkResult err = df->vkCreatePipelineCache(dev, &pipelineCacheInfo, nullptr, &pipelineCache); |
| 3631 | if (err != VK_SUCCESS) { |
| 3632 | qWarning(msg: "Failed to create pipeline cache: %d" , err); |
| 3633 | return false; |
| 3634 | } |
| 3635 | return true; |
| 3636 | } |
| 3637 | |
| 3638 | void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb) |
| 3639 | { |
| 3640 | QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, srb); |
| 3641 | |
| 3642 | QVarLengthArray<VkDescriptorBufferInfo, 8> bufferInfos; |
| 3643 | using ArrayOfImageDesc = QVarLengthArray<VkDescriptorImageInfo, 8>; |
| 3644 | QVarLengthArray<ArrayOfImageDesc, 8> imageInfos; |
| 3645 | QVarLengthArray<VkWriteDescriptorSet, 12> writeInfos; |
| 3646 | QVarLengthArray<std::pair<int, int>, 12> infoIndices; |
| 3647 | |
| 3648 | for (int i = 0, ie = srbD->sortedBindings.size(); i != ie; ++i) { |
| 3649 | const QRhiShaderResourceBinding::Data *b = shaderResourceBindingData(binding: srbD->sortedBindings.at(idx: i)); |
| 3650 | QVkShaderResourceBindings::BoundResourceData &bd(srbD->boundResourceData[currentFrameSlot][i]); |
| 3651 | |
| 3652 | VkWriteDescriptorSet writeInfo = {}; |
| 3653 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 3654 | writeInfo.dstSet = srbD->descSets[currentFrameSlot]; |
| 3655 | writeInfo.dstBinding = uint32_t(b->binding); |
| 3656 | writeInfo.descriptorCount = 1; |
| 3657 | |
| 3658 | int bufferInfoIndex = -1; |
| 3659 | int imageInfoIndex = -1; |
| 3660 | |
| 3661 | switch (b->type) { |
| 3662 | case QRhiShaderResourceBinding::UniformBuffer: |
| 3663 | { |
| 3664 | writeInfo.descriptorType = b->u.ubuf.hasDynamicOffset ? VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC |
| 3665 | : VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 3666 | QRhiBuffer *buf = b->u.ubuf.buf; |
| 3667 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, buf); |
| 3668 | bd.ubuf.id = bufD->m_id; |
| 3669 | bd.ubuf.generation = bufD->generation; |
| 3670 | VkDescriptorBufferInfo bufInfo; |
| 3671 | bufInfo.buffer = bufD->m_type == QRhiBuffer::Dynamic ? bufD->buffers[currentFrameSlot] : bufD->buffers[0]; |
| 3672 | bufInfo.offset = b->u.ubuf.offset; |
| 3673 | bufInfo.range = b->u.ubuf.maybeSize ? b->u.ubuf.maybeSize : VK_WHOLE_SIZE; |
| 3674 | // be nice and assert when we know the vulkan device would die a horrible death due to non-aligned reads |
| 3675 | Q_ASSERT(aligned(bufInfo.offset, ubufAlign) == bufInfo.offset); |
| 3676 | bufferInfoIndex = bufferInfos.size(); |
| 3677 | bufferInfos.append(t: bufInfo); |
| 3678 | } |
| 3679 | break; |
| 3680 | case QRhiShaderResourceBinding::SampledTexture: |
| 3681 | { |
| 3682 | const QRhiShaderResourceBinding::Data::TextureAndOrSamplerData *data = &b->u.stex; |
| 3683 | writeInfo.descriptorCount = data->count; // arrays of combined image samplers are supported |
| 3684 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 3685 | ArrayOfImageDesc imageInfo(data->count); |
| 3686 | for (int elem = 0; elem < data->count; ++elem) { |
| 3687 | QVkTexture *texD = QRHI_RES(QVkTexture, data->texSamplers[elem].tex); |
| 3688 | QVkSampler *samplerD = QRHI_RES(QVkSampler, data->texSamplers[elem].sampler); |
| 3689 | bd.stex.d[elem].texId = texD->m_id; |
| 3690 | bd.stex.d[elem].texGeneration = texD->generation; |
| 3691 | bd.stex.d[elem].samplerId = samplerD->m_id; |
| 3692 | bd.stex.d[elem].samplerGeneration = samplerD->generation; |
| 3693 | imageInfo[elem].sampler = samplerD->sampler; |
| 3694 | imageInfo[elem].imageView = texD->imageView; |
| 3695 | imageInfo[elem].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 3696 | } |
| 3697 | bd.stex.count = data->count; |
| 3698 | imageInfoIndex = imageInfos.size(); |
| 3699 | imageInfos.append(t: imageInfo); |
| 3700 | } |
| 3701 | break; |
| 3702 | case QRhiShaderResourceBinding::Texture: |
| 3703 | { |
| 3704 | const QRhiShaderResourceBinding::Data::TextureAndOrSamplerData *data = &b->u.stex; |
| 3705 | writeInfo.descriptorCount = data->count; // arrays of (separate) images are supported |
| 3706 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
| 3707 | ArrayOfImageDesc imageInfo(data->count); |
| 3708 | for (int elem = 0; elem < data->count; ++elem) { |
| 3709 | QVkTexture *texD = QRHI_RES(QVkTexture, data->texSamplers[elem].tex); |
| 3710 | bd.stex.d[elem].texId = texD->m_id; |
| 3711 | bd.stex.d[elem].texGeneration = texD->generation; |
| 3712 | bd.stex.d[elem].samplerId = 0; |
| 3713 | bd.stex.d[elem].samplerGeneration = 0; |
| 3714 | imageInfo[elem].sampler = VK_NULL_HANDLE; |
| 3715 | imageInfo[elem].imageView = texD->imageView; |
| 3716 | imageInfo[elem].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 3717 | } |
| 3718 | bd.stex.count = data->count; |
| 3719 | imageInfoIndex = imageInfos.size(); |
| 3720 | imageInfos.append(t: imageInfo); |
| 3721 | } |
| 3722 | break; |
| 3723 | case QRhiShaderResourceBinding::Sampler: |
| 3724 | { |
| 3725 | QVkSampler *samplerD = QRHI_RES(QVkSampler, b->u.stex.texSamplers[0].sampler); |
| 3726 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; |
| 3727 | bd.stex.d[0].texId = 0; |
| 3728 | bd.stex.d[0].texGeneration = 0; |
| 3729 | bd.stex.d[0].samplerId = samplerD->m_id; |
| 3730 | bd.stex.d[0].samplerGeneration = samplerD->generation; |
| 3731 | ArrayOfImageDesc imageInfo(1); |
| 3732 | imageInfo[0].sampler = samplerD->sampler; |
| 3733 | imageInfo[0].imageView = VK_NULL_HANDLE; |
| 3734 | imageInfo[0].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 3735 | imageInfoIndex = imageInfos.size(); |
| 3736 | imageInfos.append(t: imageInfo); |
| 3737 | } |
| 3738 | break; |
| 3739 | case QRhiShaderResourceBinding::ImageLoad: |
| 3740 | case QRhiShaderResourceBinding::ImageStore: |
| 3741 | case QRhiShaderResourceBinding::ImageLoadStore: |
| 3742 | { |
| 3743 | QVkTexture *texD = QRHI_RES(QVkTexture, b->u.simage.tex); |
| 3744 | VkImageView view = texD->perLevelImageViewForLoadStore(level: b->u.simage.level); |
| 3745 | if (view) { |
| 3746 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 3747 | bd.simage.id = texD->m_id; |
| 3748 | bd.simage.generation = texD->generation; |
| 3749 | ArrayOfImageDesc imageInfo(1); |
| 3750 | imageInfo[0].sampler = VK_NULL_HANDLE; |
| 3751 | imageInfo[0].imageView = view; |
| 3752 | imageInfo[0].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 3753 | imageInfoIndex = imageInfos.size(); |
| 3754 | imageInfos.append(t: imageInfo); |
| 3755 | } |
| 3756 | } |
| 3757 | break; |
| 3758 | case QRhiShaderResourceBinding::BufferLoad: |
| 3759 | case QRhiShaderResourceBinding::BufferStore: |
| 3760 | case QRhiShaderResourceBinding::BufferLoadStore: |
| 3761 | { |
| 3762 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, b->u.sbuf.buf); |
| 3763 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 3764 | bd.sbuf.id = bufD->m_id; |
| 3765 | bd.sbuf.generation = bufD->generation; |
| 3766 | VkDescriptorBufferInfo bufInfo; |
| 3767 | bufInfo.buffer = bufD->m_type == QRhiBuffer::Dynamic ? bufD->buffers[currentFrameSlot] : bufD->buffers[0]; |
| 3768 | bufInfo.offset = b->u.sbuf.offset; |
| 3769 | bufInfo.range = b->u.sbuf.maybeSize ? b->u.sbuf.maybeSize : VK_WHOLE_SIZE; |
| 3770 | bufferInfoIndex = bufferInfos.size(); |
| 3771 | bufferInfos.append(t: bufInfo); |
| 3772 | } |
| 3773 | break; |
| 3774 | default: |
| 3775 | continue; |
| 3776 | } |
| 3777 | |
| 3778 | writeInfos.append(t: writeInfo); |
| 3779 | infoIndices.append(t: { bufferInfoIndex, imageInfoIndex }); |
| 3780 | } |
| 3781 | |
| 3782 | for (int i = 0, writeInfoCount = writeInfos.size(); i < writeInfoCount; ++i) { |
| 3783 | const int bufferInfoIndex = infoIndices[i].first; |
| 3784 | const int imageInfoIndex = infoIndices[i].second; |
| 3785 | if (bufferInfoIndex >= 0) |
| 3786 | writeInfos[i].pBufferInfo = &bufferInfos[bufferInfoIndex]; |
| 3787 | else if (imageInfoIndex >= 0) |
| 3788 | writeInfos[i].pImageInfo = imageInfos[imageInfoIndex].constData(); |
| 3789 | } |
| 3790 | |
| 3791 | df->vkUpdateDescriptorSets(dev, uint32_t(writeInfos.size()), writeInfos.constData(), 0, nullptr); |
| 3792 | } |
| 3793 | |
| 3794 | static inline bool accessIsWrite(VkAccessFlags access) |
| 3795 | { |
| 3796 | return (access & VK_ACCESS_SHADER_WRITE_BIT) != 0 |
| 3797 | || (access & VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT) != 0 |
| 3798 | || (access & VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT) != 0 |
| 3799 | || (access & VK_ACCESS_TRANSFER_WRITE_BIT) != 0 |
| 3800 | || (access & VK_ACCESS_HOST_WRITE_BIT) != 0 |
| 3801 | || (access & VK_ACCESS_MEMORY_WRITE_BIT) != 0; |
| 3802 | } |
| 3803 | |
| 3804 | void QRhiVulkan::trackedBufferBarrier(QVkCommandBuffer *cbD, QVkBuffer *bufD, int slot, |
| 3805 | VkAccessFlags access, VkPipelineStageFlags stage) |
| 3806 | { |
| 3807 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3808 | Q_ASSERT(access && stage); |
| 3809 | QVkBuffer::UsageState &s(bufD->usageState[slot]); |
| 3810 | if (!s.stage) { |
| 3811 | s.access = access; |
| 3812 | s.stage = stage; |
| 3813 | return; |
| 3814 | } |
| 3815 | |
| 3816 | if (s.access == access && s.stage == stage) { |
| 3817 | // No need to flood with unnecessary read-after-read barriers. |
| 3818 | // Write-after-write is a different matter, however. |
| 3819 | if (!accessIsWrite(access)) |
| 3820 | return; |
| 3821 | } |
| 3822 | |
| 3823 | VkBufferMemoryBarrier bufMemBarrier = {}; |
| 3824 | bufMemBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 3825 | bufMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3826 | bufMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 3827 | bufMemBarrier.srcAccessMask = s.access; |
| 3828 | bufMemBarrier.dstAccessMask = access; |
| 3829 | bufMemBarrier.buffer = bufD->buffers[slot]; |
| 3830 | bufMemBarrier.size = VK_WHOLE_SIZE; |
| 3831 | |
| 3832 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3833 | cmd.cmd = QVkCommandBuffer::Command::BufferBarrier; |
| 3834 | cmd.args.bufferBarrier.srcStageMask = s.stage; |
| 3835 | cmd.args.bufferBarrier.dstStageMask = stage; |
| 3836 | cmd.args.bufferBarrier.count = 1; |
| 3837 | cmd.args.bufferBarrier.index = cbD->pools.bufferBarrier.size(); |
| 3838 | cbD->pools.bufferBarrier.append(t: bufMemBarrier); |
| 3839 | |
| 3840 | s.access = access; |
| 3841 | s.stage = stage; |
| 3842 | } |
| 3843 | |
| 3844 | void QRhiVulkan::trackedImageBarrier(QVkCommandBuffer *cbD, QVkTexture *texD, |
| 3845 | VkImageLayout layout, VkAccessFlags access, VkPipelineStageFlags stage) |
| 3846 | { |
| 3847 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3848 | Q_ASSERT(layout && access && stage); |
| 3849 | QVkTexture::UsageState &s(texD->usageState); |
| 3850 | if (s.access == access && s.stage == stage && s.layout == layout) { |
| 3851 | if (!accessIsWrite(access)) |
| 3852 | return; |
| 3853 | } |
| 3854 | |
| 3855 | VkImageMemoryBarrier barrier = {}; |
| 3856 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3857 | barrier.subresourceRange.aspectMask = aspectMaskForTextureFormat(format: texD->m_format); |
| 3858 | barrier.subresourceRange.baseMipLevel = 0; |
| 3859 | barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; |
| 3860 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3861 | barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS; |
| 3862 | barrier.oldLayout = s.layout; // new textures have this set to PREINITIALIZED |
| 3863 | barrier.newLayout = layout; |
| 3864 | barrier.srcAccessMask = s.access; // may be 0 but that's fine |
| 3865 | barrier.dstAccessMask = access; |
| 3866 | barrier.image = texD->image; |
| 3867 | |
| 3868 | VkPipelineStageFlags srcStage = s.stage; |
| 3869 | // stage mask cannot be 0 |
| 3870 | if (!srcStage) |
| 3871 | srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 3872 | |
| 3873 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3874 | cmd.cmd = QVkCommandBuffer::Command::ImageBarrier; |
| 3875 | cmd.args.imageBarrier.srcStageMask = srcStage; |
| 3876 | cmd.args.imageBarrier.dstStageMask = stage; |
| 3877 | cmd.args.imageBarrier.count = 1; |
| 3878 | cmd.args.imageBarrier.index = cbD->pools.imageBarrier.size(); |
| 3879 | cbD->pools.imageBarrier.append(t: barrier); |
| 3880 | |
| 3881 | s.layout = layout; |
| 3882 | s.access = access; |
| 3883 | s.stage = stage; |
| 3884 | } |
| 3885 | |
| 3886 | void QRhiVulkan::depthStencilExplicitBarrier(QVkCommandBuffer *cbD, QVkRenderBuffer *rbD) |
| 3887 | { |
| 3888 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3889 | |
| 3890 | VkImageMemoryBarrier barrier = {}; |
| 3891 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3892 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 3893 | barrier.subresourceRange.baseMipLevel = 0; |
| 3894 | barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; |
| 3895 | barrier.subresourceRange.baseArrayLayer = 0; |
| 3896 | barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS; |
| 3897 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 3898 | barrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 3899 | barrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 3900 | barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
| 3901 | | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 3902 | barrier.image = rbD->image; |
| 3903 | |
| 3904 | const VkPipelineStageFlags stages = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
| 3905 | | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 3906 | |
| 3907 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3908 | cmd.cmd = QVkCommandBuffer::Command::ImageBarrier; |
| 3909 | cmd.args.imageBarrier.srcStageMask = stages; |
| 3910 | cmd.args.imageBarrier.dstStageMask = stages; |
| 3911 | cmd.args.imageBarrier.count = 1; |
| 3912 | cmd.args.imageBarrier.index = cbD->pools.imageBarrier.size(); |
| 3913 | cbD->pools.imageBarrier.append(t: barrier); |
| 3914 | } |
| 3915 | |
| 3916 | void QRhiVulkan::subresourceBarrier(QVkCommandBuffer *cbD, VkImage image, |
| 3917 | VkImageLayout oldLayout, VkImageLayout newLayout, |
| 3918 | VkAccessFlags srcAccess, VkAccessFlags dstAccess, |
| 3919 | VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage, |
| 3920 | int startLayer, int layerCount, |
| 3921 | int startLevel, int levelCount) |
| 3922 | { |
| 3923 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 3924 | VkImageMemoryBarrier barrier = {}; |
| 3925 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 3926 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3927 | barrier.subresourceRange.baseMipLevel = uint32_t(startLevel); |
| 3928 | barrier.subresourceRange.levelCount = uint32_t(levelCount); |
| 3929 | barrier.subresourceRange.baseArrayLayer = uint32_t(startLayer); |
| 3930 | barrier.subresourceRange.layerCount = uint32_t(layerCount); |
| 3931 | barrier.oldLayout = oldLayout; |
| 3932 | barrier.newLayout = newLayout; |
| 3933 | barrier.srcAccessMask = srcAccess; |
| 3934 | barrier.dstAccessMask = dstAccess; |
| 3935 | barrier.image = image; |
| 3936 | |
| 3937 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 3938 | cmd.cmd = QVkCommandBuffer::Command::ImageBarrier; |
| 3939 | cmd.args.imageBarrier.srcStageMask = srcStage; |
| 3940 | cmd.args.imageBarrier.dstStageMask = dstStage; |
| 3941 | cmd.args.imageBarrier.count = 1; |
| 3942 | cmd.args.imageBarrier.index = cbD->pools.imageBarrier.size(); |
| 3943 | cbD->pools.imageBarrier.append(t: barrier); |
| 3944 | } |
| 3945 | |
| 3946 | VkDeviceSize QRhiVulkan::subresUploadByteSize(const QRhiTextureSubresourceUploadDescription &subresDesc) const |
| 3947 | { |
| 3948 | VkDeviceSize size = 0; |
| 3949 | const qsizetype imageSizeBytes = subresDesc.image().isNull() ? |
| 3950 | subresDesc.data().size() : subresDesc.image().sizeInBytes(); |
| 3951 | if (imageSizeBytes > 0) |
| 3952 | size += aligned(v: VkDeviceSize(imageSizeBytes), byteAlign: texbufAlign); |
| 3953 | return size; |
| 3954 | } |
| 3955 | |
| 3956 | void QRhiVulkan::prepareUploadSubres(QVkTexture *texD, int layer, int level, |
| 3957 | const QRhiTextureSubresourceUploadDescription &subresDesc, |
| 3958 | size_t *curOfs, void *mp, |
| 3959 | BufferImageCopyList *copyInfos) |
| 3960 | { |
| 3961 | qsizetype copySizeBytes = 0; |
| 3962 | qsizetype imageSizeBytes = 0; |
| 3963 | const void *src = nullptr; |
| 3964 | const bool is3D = texD->m_flags.testFlag(flag: QRhiTexture::ThreeDimensional); |
| 3965 | const bool is1D = texD->m_flags.testFlag(flag: QRhiTexture::OneDimensional); |
| 3966 | |
| 3967 | VkBufferImageCopy copyInfo = {}; |
| 3968 | copyInfo.bufferOffset = *curOfs; |
| 3969 | copyInfo.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 3970 | copyInfo.imageSubresource.mipLevel = uint32_t(level); |
| 3971 | copyInfo.imageSubresource.baseArrayLayer = is3D ? 0 : uint32_t(layer); |
| 3972 | copyInfo.imageSubresource.layerCount = 1; |
| 3973 | copyInfo.imageExtent.depth = 1; |
| 3974 | if (is3D) |
| 3975 | copyInfo.imageOffset.z = uint32_t(layer); |
| 3976 | if (is1D) |
| 3977 | copyInfo.imageOffset.y = uint32_t(layer); |
| 3978 | |
| 3979 | const QByteArray rawData = subresDesc.data(); |
| 3980 | const QPoint dp = subresDesc.destinationTopLeft(); |
| 3981 | QImage image = subresDesc.image(); |
| 3982 | if (!image.isNull()) { |
| 3983 | copySizeBytes = imageSizeBytes = image.sizeInBytes(); |
| 3984 | QSize size = image.size(); |
| 3985 | src = image.constBits(); |
| 3986 | // Scanlines in QImage are 4 byte aligned so bpl must |
| 3987 | // be taken into account for bufferRowLength. |
| 3988 | int bpc = qMax(a: 1, b: image.depth() / 8); |
| 3989 | // this is in pixels, not bytes, to make it more complicated... |
| 3990 | copyInfo.bufferRowLength = uint32_t(image.bytesPerLine() / bpc); |
| 3991 | if (!subresDesc.sourceSize().isEmpty() || !subresDesc.sourceTopLeft().isNull()) { |
| 3992 | const int sx = subresDesc.sourceTopLeft().x(); |
| 3993 | const int sy = subresDesc.sourceTopLeft().y(); |
| 3994 | if (!subresDesc.sourceSize().isEmpty()) |
| 3995 | size = subresDesc.sourceSize(); |
| 3996 | |
| 3997 | if (size.width() == image.width()) { |
| 3998 | // No need to make a QImage copy here, can copy from the source |
| 3999 | // QImage into staging directly. |
| 4000 | src = image.constBits() + sy * image.bytesPerLine() + sx * bpc; |
| 4001 | copySizeBytes = size.height() * image.bytesPerLine(); |
| 4002 | } else { |
| 4003 | image = image.copy(x: sx, y: sy, w: size.width(), h: size.height()); |
| 4004 | src = image.constBits(); |
| 4005 | // The staging buffer gets the slice only. The rest of the |
| 4006 | // space reserved for this mip will be unused. |
| 4007 | copySizeBytes = image.sizeInBytes(); |
| 4008 | bpc = qMax(a: 1, b: image.depth() / 8); |
| 4009 | copyInfo.bufferRowLength = uint32_t(image.bytesPerLine() / bpc); |
| 4010 | } |
| 4011 | } |
| 4012 | copyInfo.imageOffset.x = dp.x(); |
| 4013 | copyInfo.imageOffset.y = dp.y(); |
| 4014 | copyInfo.imageExtent.width = uint32_t(size.width()); |
| 4015 | copyInfo.imageExtent.height = uint32_t(size.height()); |
| 4016 | copyInfos->append(t: copyInfo); |
| 4017 | } else if (!rawData.isEmpty() && isCompressedFormat(format: texD->m_format)) { |
| 4018 | copySizeBytes = imageSizeBytes = rawData.size(); |
| 4019 | src = rawData.constData(); |
| 4020 | QSize size = q->sizeForMipLevel(mipLevel: level, baseLevelSize: texD->m_pixelSize); |
| 4021 | const int subresw = size.width(); |
| 4022 | const int subresh = size.height(); |
| 4023 | if (!subresDesc.sourceSize().isEmpty()) |
| 4024 | size = subresDesc.sourceSize(); |
| 4025 | const int w = size.width(); |
| 4026 | const int h = size.height(); |
| 4027 | QSize blockDim; |
| 4028 | compressedFormatInfo(format: texD->m_format, size: QSize(w, h), bpl: nullptr, byteSize: nullptr, blockDim: &blockDim); |
| 4029 | // x and y must be multiples of the block width and height |
| 4030 | copyInfo.imageOffset.x = aligned(v: dp.x(), byteAlign: blockDim.width()); |
| 4031 | copyInfo.imageOffset.y = aligned(v: dp.y(), byteAlign: blockDim.height()); |
| 4032 | // width and height must be multiples of the block width and height |
| 4033 | // or x + width and y + height must equal the subresource width and height |
| 4034 | copyInfo.imageExtent.width = uint32_t(dp.x() + w == subresw ? w : aligned(v: w, byteAlign: blockDim.width())); |
| 4035 | copyInfo.imageExtent.height = uint32_t(dp.y() + h == subresh ? h : aligned(v: h, byteAlign: blockDim.height())); |
| 4036 | copyInfos->append(t: copyInfo); |
| 4037 | } else if (!rawData.isEmpty()) { |
| 4038 | copySizeBytes = imageSizeBytes = rawData.size(); |
| 4039 | src = rawData.constData(); |
| 4040 | QSize size = q->sizeForMipLevel(mipLevel: level, baseLevelSize: texD->m_pixelSize); |
| 4041 | if (subresDesc.dataStride()) { |
| 4042 | quint32 bytesPerPixel = 0; |
| 4043 | textureFormatInfo(format: texD->m_format, size, bpl: nullptr, byteSize: nullptr, bytesPerPixel: &bytesPerPixel); |
| 4044 | if (bytesPerPixel) |
| 4045 | copyInfo.bufferRowLength = subresDesc.dataStride() / bytesPerPixel; |
| 4046 | } |
| 4047 | if (!subresDesc.sourceSize().isEmpty()) |
| 4048 | size = subresDesc.sourceSize(); |
| 4049 | copyInfo.imageOffset.x = dp.x(); |
| 4050 | copyInfo.imageOffset.y = dp.y(); |
| 4051 | copyInfo.imageExtent.width = uint32_t(size.width()); |
| 4052 | copyInfo.imageExtent.height = uint32_t(size.height()); |
| 4053 | copyInfos->append(t: copyInfo); |
| 4054 | } else { |
| 4055 | qWarning(msg: "Invalid texture upload for %p layer=%d mip=%d" , texD, layer, level); |
| 4056 | } |
| 4057 | |
| 4058 | if (src) { |
| 4059 | memcpy(dest: reinterpret_cast<char *>(mp) + *curOfs, src: src, n: size_t(copySizeBytes)); |
| 4060 | *curOfs += aligned(v: VkDeviceSize(imageSizeBytes), byteAlign: texbufAlign); |
| 4061 | } |
| 4062 | } |
| 4063 | |
| 4064 | void QRhiVulkan::(VkResult err) |
| 4065 | { |
| 4066 | if (err == VK_ERROR_OUT_OF_DEVICE_MEMORY) |
| 4067 | qWarning() << "Out of device memory, current allocator statistics are" << statistics(); |
| 4068 | } |
| 4069 | |
| 4070 | void QRhiVulkan::enqueueResourceUpdates(QVkCommandBuffer *cbD, QRhiResourceUpdateBatch *resourceUpdates) |
| 4071 | { |
| 4072 | QRhiResourceUpdateBatchPrivate *ud = QRhiResourceUpdateBatchPrivate::get(b: resourceUpdates); |
| 4073 | |
| 4074 | for (int opIdx = 0; opIdx < ud->activeBufferOpCount; ++opIdx) { |
| 4075 | const QRhiResourceUpdateBatchPrivate::BufferOp &u(ud->bufferOps[opIdx]); |
| 4076 | if (u.type == QRhiResourceUpdateBatchPrivate::BufferOp::DynamicUpdate) { |
| 4077 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, u.buf); |
| 4078 | Q_ASSERT(bufD->m_type == QRhiBuffer::Dynamic); |
| 4079 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 4080 | if (u.offset == 0 && u.data.size() == bufD->m_size) |
| 4081 | bufD->pendingDynamicUpdates[i].clear(); |
| 4082 | bufD->pendingDynamicUpdates[i].append(t: { .offset: u.offset, .data: u.data }); |
| 4083 | } |
| 4084 | } else if (u.type == QRhiResourceUpdateBatchPrivate::BufferOp::StaticUpload) { |
| 4085 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, u.buf); |
| 4086 | Q_ASSERT(bufD->m_type != QRhiBuffer::Dynamic); |
| 4087 | Q_ASSERT(u.offset + u.data.size() <= bufD->m_size); |
| 4088 | |
| 4089 | if (!bufD->stagingBuffers[currentFrameSlot]) { |
| 4090 | VkBufferCreateInfo bufferInfo = {}; |
| 4091 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 4092 | // must cover the entire buffer - this way multiple, partial updates per frame |
| 4093 | // are supported even when the staging buffer is reused (Static) |
| 4094 | bufferInfo.size = bufD->m_size; |
| 4095 | bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
| 4096 | |
| 4097 | VmaAllocationCreateInfo allocInfo = {}; |
| 4098 | allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY; |
| 4099 | |
| 4100 | VmaAllocation allocation; |
| 4101 | VkResult err = vmaCreateBuffer(allocator: toVmaAllocator(a: allocator), pBufferCreateInfo: &bufferInfo, pAllocationCreateInfo: &allocInfo, |
| 4102 | pBuffer: &bufD->stagingBuffers[currentFrameSlot], pAllocation: &allocation, pAllocationInfo: nullptr); |
| 4103 | if (err == VK_SUCCESS) { |
| 4104 | bufD->stagingAllocations[currentFrameSlot] = allocation; |
| 4105 | setAllocationName(allocation, name: bufD->name()); |
| 4106 | } else { |
| 4107 | qWarning(msg: "Failed to create staging buffer of size %u: %d" , bufD->m_size, err); |
| 4108 | printExtraErrorInfo(err); |
| 4109 | continue; |
| 4110 | } |
| 4111 | } |
| 4112 | |
| 4113 | VkResult err = vmaCopyMemoryToAllocation(allocator: toVmaAllocator(a: allocator), pSrcHostPointer: u.data.constData(), |
| 4114 | dstAllocation: toVmaAllocation(a: bufD->stagingAllocations[currentFrameSlot]), |
| 4115 | dstAllocationLocalOffset: u.offset, size: u.data.size()); |
| 4116 | if (err != VK_SUCCESS) { |
| 4117 | qWarning(msg: "Failed to copy memory to buffer: %d" , err); |
| 4118 | continue; |
| 4119 | } |
| 4120 | |
| 4121 | trackedBufferBarrier(cbD, bufD, slot: 0, |
| 4122 | access: VK_ACCESS_TRANSFER_WRITE_BIT, stage: VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 4123 | |
| 4124 | VkBufferCopy copyInfo = {}; |
| 4125 | copyInfo.srcOffset = u.offset; |
| 4126 | copyInfo.dstOffset = u.offset; |
| 4127 | copyInfo.size = u.data.size(); |
| 4128 | |
| 4129 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4130 | cmd.cmd = QVkCommandBuffer::Command::CopyBuffer; |
| 4131 | cmd.args.copyBuffer.src = bufD->stagingBuffers[currentFrameSlot]; |
| 4132 | cmd.args.copyBuffer.dst = bufD->buffers[0]; |
| 4133 | cmd.args.copyBuffer.desc = copyInfo; |
| 4134 | |
| 4135 | // Where's the barrier for read-after-write? (assuming the common case |
| 4136 | // of binding this buffer as vertex/index, or, less likely, as uniform |
| 4137 | // buffer, in a renderpass later on) That is handled by the pass |
| 4138 | // resource tracking: the appropriate pipeline barrier will be |
| 4139 | // generated and recorded right before the renderpass, that binds this |
| 4140 | // buffer in one of its commands, gets its BeginRenderPass recorded. |
| 4141 | |
| 4142 | bufD->lastActiveFrameSlot = currentFrameSlot; |
| 4143 | |
| 4144 | if (bufD->m_type == QRhiBuffer::Immutable) { |
| 4145 | QRhiVulkan::DeferredReleaseEntry e; |
| 4146 | e.type = QRhiVulkan::DeferredReleaseEntry::StagingBuffer; |
| 4147 | e.lastActiveFrameSlot = currentFrameSlot; |
| 4148 | e.stagingBuffer.stagingBuffer = bufD->stagingBuffers[currentFrameSlot]; |
| 4149 | e.stagingBuffer.stagingAllocation = bufD->stagingAllocations[currentFrameSlot]; |
| 4150 | bufD->stagingBuffers[currentFrameSlot] = VK_NULL_HANDLE; |
| 4151 | bufD->stagingAllocations[currentFrameSlot] = nullptr; |
| 4152 | releaseQueue.append(t: e); |
| 4153 | } |
| 4154 | } else if (u.type == QRhiResourceUpdateBatchPrivate::BufferOp::Read) { |
| 4155 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, u.buf); |
| 4156 | if (bufD->m_type == QRhiBuffer::Dynamic) { |
| 4157 | executeBufferHostWritesForSlot(bufD, slot: currentFrameSlot); |
| 4158 | u.result->data.resizeForOverwrite(size: u.readSize); |
| 4159 | VkResult err = vmaCopyAllocationToMemory(allocator: toVmaAllocator(a: allocator), |
| 4160 | srcAllocation: toVmaAllocation(a: bufD->allocations[currentFrameSlot]), |
| 4161 | srcAllocationLocalOffset: u.offset, pDstHostPointer: u.result->data.data(), size: u.readSize); |
| 4162 | if (err != VK_SUCCESS) { |
| 4163 | qWarning(msg: "Failed to copy memory from buffer: %d" , err); |
| 4164 | u.result->data.clear(); |
| 4165 | } |
| 4166 | if (u.result->completed) |
| 4167 | u.result->completed(); |
| 4168 | } else { |
| 4169 | // Non-Dynamic buffers may not be host visible, so have to |
| 4170 | // create a readback buffer, enqueue a copy from |
| 4171 | // bufD->buffers[0] to this buffer, and then once the command |
| 4172 | // buffer completes, copy the data out of the host visible |
| 4173 | // readback buffer. Quite similar to what we do for texture |
| 4174 | // readbacks. |
| 4175 | BufferReadback readback; |
| 4176 | readback.activeFrameSlot = currentFrameSlot; |
| 4177 | readback.result = u.result; |
| 4178 | readback.byteSize = u.readSize; |
| 4179 | |
| 4180 | VkBufferCreateInfo bufferInfo = {}; |
| 4181 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 4182 | bufferInfo.size = readback.byteSize; |
| 4183 | bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT; |
| 4184 | |
| 4185 | VmaAllocationCreateInfo allocInfo = {}; |
| 4186 | allocInfo.usage = VMA_MEMORY_USAGE_GPU_TO_CPU; |
| 4187 | |
| 4188 | VmaAllocation allocation; |
| 4189 | VkResult err = vmaCreateBuffer(allocator: toVmaAllocator(a: allocator), pBufferCreateInfo: &bufferInfo, pAllocationCreateInfo: &allocInfo, pBuffer: &readback.stagingBuf, pAllocation: &allocation, pAllocationInfo: nullptr); |
| 4190 | if (err == VK_SUCCESS) { |
| 4191 | readback.stagingAlloc = allocation; |
| 4192 | setAllocationName(allocation, name: bufD->name()); |
| 4193 | } else { |
| 4194 | qWarning(msg: "Failed to create readback buffer of size %u: %d" , readback.byteSize, err); |
| 4195 | printExtraErrorInfo(err); |
| 4196 | continue; |
| 4197 | } |
| 4198 | |
| 4199 | trackedBufferBarrier(cbD, bufD, slot: 0, access: VK_ACCESS_TRANSFER_READ_BIT, stage: VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 4200 | |
| 4201 | VkBufferCopy copyInfo = {}; |
| 4202 | copyInfo.srcOffset = u.offset; |
| 4203 | copyInfo.size = u.readSize; |
| 4204 | |
| 4205 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4206 | cmd.cmd = QVkCommandBuffer::Command::CopyBuffer; |
| 4207 | cmd.args.copyBuffer.src = bufD->buffers[0]; |
| 4208 | cmd.args.copyBuffer.dst = readback.stagingBuf; |
| 4209 | cmd.args.copyBuffer.desc = copyInfo; |
| 4210 | |
| 4211 | bufD->lastActiveFrameSlot = currentFrameSlot; |
| 4212 | |
| 4213 | activeBufferReadbacks.append(t: readback); |
| 4214 | } |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | for (int opIdx = 0; opIdx < ud->activeTextureOpCount; ++opIdx) { |
| 4219 | const QRhiResourceUpdateBatchPrivate::TextureOp &u(ud->textureOps[opIdx]); |
| 4220 | if (u.type == QRhiResourceUpdateBatchPrivate::TextureOp::Upload) { |
| 4221 | QVkTexture *utexD = QRHI_RES(QVkTexture, u.dst); |
| 4222 | // batch into a single staging buffer and a single CopyBufferToImage with multiple copyInfos |
| 4223 | VkDeviceSize stagingSize = 0; |
| 4224 | for (int layer = 0, maxLayer = u.subresDesc.size(); layer < maxLayer; ++layer) { |
| 4225 | for (int level = 0; level < QRhi::MAX_MIP_LEVELS; ++level) { |
| 4226 | for (const QRhiTextureSubresourceUploadDescription &subresDesc : std::as_const(t: u.subresDesc[layer][level])) |
| 4227 | stagingSize += subresUploadByteSize(subresDesc); |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | Q_ASSERT(!utexD->stagingBuffers[currentFrameSlot]); |
| 4232 | VkBufferCreateInfo bufferInfo = {}; |
| 4233 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 4234 | bufferInfo.size = stagingSize; |
| 4235 | bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
| 4236 | |
| 4237 | VmaAllocationCreateInfo allocInfo = {}; |
| 4238 | allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 4239 | |
| 4240 | VmaAllocation allocation; |
| 4241 | VkResult err = vmaCreateBuffer(allocator: toVmaAllocator(a: allocator), pBufferCreateInfo: &bufferInfo, pAllocationCreateInfo: &allocInfo, |
| 4242 | pBuffer: &utexD->stagingBuffers[currentFrameSlot], pAllocation: &allocation, pAllocationInfo: nullptr); |
| 4243 | if (err != VK_SUCCESS) { |
| 4244 | qWarning(msg: "Failed to create image staging buffer of size %d: %d" , int(stagingSize), err); |
| 4245 | printExtraErrorInfo(err); |
| 4246 | continue; |
| 4247 | } |
| 4248 | utexD->stagingAllocations[currentFrameSlot] = allocation; |
| 4249 | setAllocationName(allocation, name: utexD->name()); |
| 4250 | |
| 4251 | BufferImageCopyList copyInfos; |
| 4252 | size_t curOfs = 0; |
| 4253 | void *mp = nullptr; |
| 4254 | VmaAllocation a = toVmaAllocation(a: utexD->stagingAllocations[currentFrameSlot]); |
| 4255 | err = vmaMapMemory(allocator: toVmaAllocator(a: allocator), allocation: a, ppData: &mp); |
| 4256 | if (err != VK_SUCCESS) { |
| 4257 | qWarning(msg: "Failed to map image data: %d" , err); |
| 4258 | continue; |
| 4259 | } |
| 4260 | |
| 4261 | for (int layer = 0, maxLayer = u.subresDesc.size(); layer < maxLayer; ++layer) { |
| 4262 | for (int level = 0; level < QRhi::MAX_MIP_LEVELS; ++level) { |
| 4263 | const QList<QRhiTextureSubresourceUploadDescription> &srd(u.subresDesc[layer][level]); |
| 4264 | if (srd.isEmpty()) |
| 4265 | continue; |
| 4266 | for (const QRhiTextureSubresourceUploadDescription &subresDesc : std::as_const(t: srd)) { |
| 4267 | prepareUploadSubres(texD: utexD, layer, level, |
| 4268 | subresDesc, curOfs: &curOfs, mp, copyInfos: ©Infos); |
| 4269 | } |
| 4270 | } |
| 4271 | } |
| 4272 | vmaFlushAllocation(allocator: toVmaAllocator(a: allocator), allocation: a, offset: 0, size: stagingSize); |
| 4273 | vmaUnmapMemory(allocator: toVmaAllocator(a: allocator), allocation: a); |
| 4274 | |
| 4275 | trackedImageBarrier(cbD, texD: utexD, layout: VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 4276 | access: VK_ACCESS_TRANSFER_WRITE_BIT, stage: VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 4277 | |
| 4278 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4279 | cmd.cmd = QVkCommandBuffer::Command::CopyBufferToImage; |
| 4280 | cmd.args.copyBufferToImage.src = utexD->stagingBuffers[currentFrameSlot]; |
| 4281 | cmd.args.copyBufferToImage.dst = utexD->image; |
| 4282 | cmd.args.copyBufferToImage.dstLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 4283 | cmd.args.copyBufferToImage.count = copyInfos.size(); |
| 4284 | cmd.args.copyBufferToImage.bufferImageCopyIndex = cbD->pools.bufferImageCopy.size(); |
| 4285 | cbD->pools.bufferImageCopy.append(buf: copyInfos.constData(), sz: copyInfos.size()); |
| 4286 | |
| 4287 | // no reuse of staging, this is intentional |
| 4288 | QRhiVulkan::DeferredReleaseEntry e; |
| 4289 | e.type = QRhiVulkan::DeferredReleaseEntry::StagingBuffer; |
| 4290 | e.lastActiveFrameSlot = currentFrameSlot; |
| 4291 | e.stagingBuffer.stagingBuffer = utexD->stagingBuffers[currentFrameSlot]; |
| 4292 | e.stagingBuffer.stagingAllocation = utexD->stagingAllocations[currentFrameSlot]; |
| 4293 | utexD->stagingBuffers[currentFrameSlot] = VK_NULL_HANDLE; |
| 4294 | utexD->stagingAllocations[currentFrameSlot] = nullptr; |
| 4295 | releaseQueue.append(t: e); |
| 4296 | |
| 4297 | // Similarly to buffers, transitioning away from DST is done later, |
| 4298 | // when a renderpass using the texture is encountered. |
| 4299 | |
| 4300 | utexD->lastActiveFrameSlot = currentFrameSlot; |
| 4301 | } else if (u.type == QRhiResourceUpdateBatchPrivate::TextureOp::Copy) { |
| 4302 | Q_ASSERT(u.src && u.dst); |
| 4303 | if (u.src == u.dst) { |
| 4304 | qWarning(msg: "Texture copy with matching source and destination is not supported" ); |
| 4305 | continue; |
| 4306 | } |
| 4307 | QVkTexture *srcD = QRHI_RES(QVkTexture, u.src); |
| 4308 | QVkTexture *dstD = QRHI_RES(QVkTexture, u.dst); |
| 4309 | const bool srcIs3D = srcD->m_flags.testFlag(flag: QRhiTexture::ThreeDimensional); |
| 4310 | const bool dstIs3D = dstD->m_flags.testFlag(flag: QRhiTexture::ThreeDimensional); |
| 4311 | |
| 4312 | VkImageCopy region = {}; |
| 4313 | region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 4314 | region.srcSubresource.mipLevel = uint32_t(u.desc.sourceLevel()); |
| 4315 | region.srcSubresource.baseArrayLayer = srcIs3D ? 0 : uint32_t(u.desc.sourceLayer()); |
| 4316 | region.srcSubresource.layerCount = 1; |
| 4317 | |
| 4318 | region.srcOffset.x = u.desc.sourceTopLeft().x(); |
| 4319 | region.srcOffset.y = u.desc.sourceTopLeft().y(); |
| 4320 | if (srcIs3D) |
| 4321 | region.srcOffset.z = uint32_t(u.desc.sourceLayer()); |
| 4322 | |
| 4323 | region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 4324 | region.dstSubresource.mipLevel = uint32_t(u.desc.destinationLevel()); |
| 4325 | region.dstSubresource.baseArrayLayer = dstIs3D ? 0 : uint32_t(u.desc.destinationLayer()); |
| 4326 | region.dstSubresource.layerCount = 1; |
| 4327 | |
| 4328 | region.dstOffset.x = u.desc.destinationTopLeft().x(); |
| 4329 | region.dstOffset.y = u.desc.destinationTopLeft().y(); |
| 4330 | if (dstIs3D) |
| 4331 | region.dstOffset.z = uint32_t(u.desc.destinationLayer()); |
| 4332 | |
| 4333 | const QSize mipSize = q->sizeForMipLevel(mipLevel: u.desc.sourceLevel(), baseLevelSize: srcD->m_pixelSize); |
| 4334 | const QSize copySize = u.desc.pixelSize().isEmpty() ? mipSize : u.desc.pixelSize(); |
| 4335 | region.extent.width = uint32_t(copySize.width()); |
| 4336 | region.extent.height = uint32_t(copySize.height()); |
| 4337 | region.extent.depth = 1; |
| 4338 | |
| 4339 | trackedImageBarrier(cbD, texD: srcD, layout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 4340 | access: VK_ACCESS_TRANSFER_READ_BIT, stage: VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 4341 | trackedImageBarrier(cbD, texD: dstD, layout: VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 4342 | access: VK_ACCESS_TRANSFER_WRITE_BIT, stage: VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 4343 | |
| 4344 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4345 | cmd.cmd = QVkCommandBuffer::Command::CopyImage; |
| 4346 | cmd.args.copyImage.src = srcD->image; |
| 4347 | cmd.args.copyImage.srcLayout = srcD->usageState.layout; |
| 4348 | cmd.args.copyImage.dst = dstD->image; |
| 4349 | cmd.args.copyImage.dstLayout = dstD->usageState.layout; |
| 4350 | cmd.args.copyImage.desc = region; |
| 4351 | |
| 4352 | srcD->lastActiveFrameSlot = dstD->lastActiveFrameSlot = currentFrameSlot; |
| 4353 | } else if (u.type == QRhiResourceUpdateBatchPrivate::TextureOp::Read) { |
| 4354 | TextureReadback readback; |
| 4355 | readback.activeFrameSlot = currentFrameSlot; |
| 4356 | readback.desc = u.rb; |
| 4357 | readback.result = u.result; |
| 4358 | |
| 4359 | QVkTexture *texD = QRHI_RES(QVkTexture, u.rb.texture()); |
| 4360 | QVkSwapChain *swapChainD = nullptr; |
| 4361 | bool is3D = false; |
| 4362 | if (texD) { |
| 4363 | if (texD->samples > VK_SAMPLE_COUNT_1_BIT) { |
| 4364 | qWarning(msg: "Multisample texture cannot be read back" ); |
| 4365 | continue; |
| 4366 | } |
| 4367 | is3D = texD->m_flags.testFlag(flag: QRhiTexture::ThreeDimensional); |
| 4368 | if (u.rb.rect().isValid()) |
| 4369 | readback.rect = u.rb.rect(); |
| 4370 | else |
| 4371 | readback.rect = QRect({0, 0}, q->sizeForMipLevel(mipLevel: u.rb.level(), baseLevelSize: texD->m_pixelSize)); |
| 4372 | readback.format = texD->m_format; |
| 4373 | texD->lastActiveFrameSlot = currentFrameSlot; |
| 4374 | } else { |
| 4375 | Q_ASSERT(currentSwapChain); |
| 4376 | swapChainD = QRHI_RES(QVkSwapChain, currentSwapChain); |
| 4377 | if (!swapChainD->supportsReadback) { |
| 4378 | qWarning(msg: "Swapchain does not support readback" ); |
| 4379 | continue; |
| 4380 | } |
| 4381 | if (u.rb.rect().isValid()) |
| 4382 | readback.rect = u.rb.rect(); |
| 4383 | else |
| 4384 | readback.rect = QRect({0, 0}, swapChainD->pixelSize); |
| 4385 | readback.format = swapchainReadbackTextureFormat(format: swapChainD->colorFormat, flags: nullptr); |
| 4386 | if (readback.format == QRhiTexture::UnknownFormat) |
| 4387 | continue; |
| 4388 | |
| 4389 | // Multisample swapchains need nothing special since resolving |
| 4390 | // happens when ending a renderpass. |
| 4391 | } |
| 4392 | textureFormatInfo(format: readback.format, size: readback.rect.size(), bpl: nullptr, byteSize: &readback.byteSize, bytesPerPixel: nullptr); |
| 4393 | |
| 4394 | // Create a host visible readback buffer. |
| 4395 | VkBufferCreateInfo bufferInfo = {}; |
| 4396 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 4397 | bufferInfo.size = readback.byteSize; |
| 4398 | bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT; |
| 4399 | |
| 4400 | VmaAllocationCreateInfo allocInfo = {}; |
| 4401 | allocInfo.usage = VMA_MEMORY_USAGE_GPU_TO_CPU; |
| 4402 | |
| 4403 | VmaAllocation allocation; |
| 4404 | VkResult err = vmaCreateBuffer(allocator: toVmaAllocator(a: allocator), pBufferCreateInfo: &bufferInfo, pAllocationCreateInfo: &allocInfo, pBuffer: &readback.stagingBuf, pAllocation: &allocation, pAllocationInfo: nullptr); |
| 4405 | if (err == VK_SUCCESS) { |
| 4406 | readback.stagingAlloc = allocation; |
| 4407 | setAllocationName(allocation, name: texD ? texD->name() : swapChainD->name()); |
| 4408 | } else { |
| 4409 | qWarning(msg: "Failed to create readback buffer of size %u: %d" , readback.byteSize, err); |
| 4410 | printExtraErrorInfo(err); |
| 4411 | continue; |
| 4412 | } |
| 4413 | |
| 4414 | // Copy from the (optimal and not host visible) image into the buffer. |
| 4415 | VkBufferImageCopy copyDesc = {}; |
| 4416 | copyDesc.bufferOffset = 0; |
| 4417 | copyDesc.imageSubresource.aspectMask = aspectMaskForTextureFormat(format: readback.format); |
| 4418 | copyDesc.imageSubresource.mipLevel = uint32_t(u.rb.level()); |
| 4419 | copyDesc.imageSubresource.baseArrayLayer = is3D ? 0 : uint32_t(u.rb.layer()); |
| 4420 | copyDesc.imageSubresource.layerCount = 1; |
| 4421 | copyDesc.imageOffset.x = readback.rect.x(); |
| 4422 | copyDesc.imageOffset.y = readback.rect.y(); |
| 4423 | if (is3D) |
| 4424 | copyDesc.imageOffset.z = u.rb.layer(); |
| 4425 | copyDesc.imageExtent.width = uint32_t(readback.rect.width()); |
| 4426 | copyDesc.imageExtent.height = uint32_t(readback.rect.height()); |
| 4427 | copyDesc.imageExtent.depth = 1; |
| 4428 | |
| 4429 | if (texD) { |
| 4430 | trackedImageBarrier(cbD, texD, layout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 4431 | access: VK_ACCESS_TRANSFER_READ_BIT, stage: VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 4432 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4433 | cmd.cmd = QVkCommandBuffer::Command::CopyImageToBuffer; |
| 4434 | cmd.args.copyImageToBuffer.src = texD->image; |
| 4435 | cmd.args.copyImageToBuffer.srcLayout = texD->usageState.layout; |
| 4436 | cmd.args.copyImageToBuffer.dst = readback.stagingBuf; |
| 4437 | cmd.args.copyImageToBuffer.desc = copyDesc; |
| 4438 | } else { |
| 4439 | // use the swapchain image |
| 4440 | QVkSwapChain::ImageResources &imageRes(swapChainD->imageRes[swapChainD->currentImageIndex]); |
| 4441 | VkImage image = imageRes.image; |
| 4442 | if (imageRes.lastUse != QVkSwapChain::ImageResources::ScImageUseTransferSource) { |
| 4443 | if (imageRes.lastUse != QVkSwapChain::ImageResources::ScImageUseRender) { |
| 4444 | qWarning(msg: "Attempted to read back undefined swapchain image content, " |
| 4445 | "results are undefined. (do a render pass first)" ); |
| 4446 | } |
| 4447 | subresourceBarrier(cbD, image, |
| 4448 | oldLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, newLayout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 4449 | srcAccess: VK_ACCESS_MEMORY_READ_BIT, dstAccess: VK_ACCESS_TRANSFER_READ_BIT, |
| 4450 | srcStage: VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, dstStage: VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 4451 | startLayer: 0, layerCount: 1, |
| 4452 | startLevel: 0, levelCount: 1); |
| 4453 | imageRes.lastUse = QVkSwapChain::ImageResources::ScImageUseTransferSource; |
| 4454 | } |
| 4455 | |
| 4456 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4457 | cmd.cmd = QVkCommandBuffer::Command::CopyImageToBuffer; |
| 4458 | cmd.args.copyImageToBuffer.src = image; |
| 4459 | cmd.args.copyImageToBuffer.srcLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 4460 | cmd.args.copyImageToBuffer.dst = readback.stagingBuf; |
| 4461 | cmd.args.copyImageToBuffer.desc = copyDesc; |
| 4462 | } |
| 4463 | |
| 4464 | activeTextureReadbacks.append(t: readback); |
| 4465 | } else if (u.type == QRhiResourceUpdateBatchPrivate::TextureOp::GenMips) { |
| 4466 | QVkTexture *utexD = QRHI_RES(QVkTexture, u.dst); |
| 4467 | Q_ASSERT(utexD->m_flags.testFlag(QRhiTexture::UsedWithGenerateMips)); |
| 4468 | const bool isCube = utexD->m_flags.testFlag(flag: QRhiTexture::CubeMap); |
| 4469 | const bool isArray = utexD->m_flags.testFlag(flag: QRhiTexture::TextureArray); |
| 4470 | const bool is3D = utexD->m_flags.testFlag(flag: QRhiTexture::ThreeDimensional); |
| 4471 | |
| 4472 | VkImageLayout origLayout = utexD->usageState.layout; |
| 4473 | VkAccessFlags origAccess = utexD->usageState.access; |
| 4474 | VkPipelineStageFlags origStage = utexD->usageState.stage; |
| 4475 | if (!origStage) |
| 4476 | origStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 4477 | |
| 4478 | for (int layer = 0; layer < (isCube ? 6 : (isArray ? qMax(a: 0, b: utexD->m_arraySize) : 1)); ++layer) { |
| 4479 | int w = utexD->m_pixelSize.width(); |
| 4480 | int h = utexD->m_pixelSize.height(); |
| 4481 | int depth = is3D ? qMax(a: 1, b: utexD->m_depth) : 1; |
| 4482 | for (int level = 1; level < int(utexD->mipLevelCount); ++level) { |
| 4483 | if (level == 1) { |
| 4484 | subresourceBarrier(cbD, image: utexD->image, |
| 4485 | oldLayout: origLayout, newLayout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 4486 | srcAccess: origAccess, dstAccess: VK_ACCESS_TRANSFER_READ_BIT, |
| 4487 | srcStage: origStage, dstStage: VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 4488 | startLayer: layer, layerCount: 1, |
| 4489 | startLevel: level - 1, levelCount: 1); |
| 4490 | } else { |
| 4491 | subresourceBarrier(cbD, image: utexD->image, |
| 4492 | oldLayout: VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, newLayout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 4493 | srcAccess: VK_ACCESS_TRANSFER_WRITE_BIT, dstAccess: VK_ACCESS_TRANSFER_READ_BIT, |
| 4494 | srcStage: VK_PIPELINE_STAGE_TRANSFER_BIT, dstStage: VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 4495 | startLayer: layer, layerCount: 1, |
| 4496 | startLevel: level - 1, levelCount: 1); |
| 4497 | } |
| 4498 | |
| 4499 | subresourceBarrier(cbD, image: utexD->image, |
| 4500 | oldLayout: origLayout, newLayout: VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 4501 | srcAccess: origAccess, dstAccess: VK_ACCESS_TRANSFER_WRITE_BIT, |
| 4502 | srcStage: origStage, dstStage: VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 4503 | startLayer: layer, layerCount: 1, |
| 4504 | startLevel: level, levelCount: 1); |
| 4505 | |
| 4506 | VkImageBlit region = {}; |
| 4507 | region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 4508 | region.srcSubresource.mipLevel = uint32_t(level) - 1; |
| 4509 | region.srcSubresource.baseArrayLayer = uint32_t(layer); |
| 4510 | region.srcSubresource.layerCount = 1; |
| 4511 | |
| 4512 | region.srcOffsets[1].x = qMax(a: 1, b: w); |
| 4513 | region.srcOffsets[1].y = qMax(a: 1, b: h); |
| 4514 | region.srcOffsets[1].z = qMax(a: 1, b: depth); |
| 4515 | |
| 4516 | region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 4517 | region.dstSubresource.mipLevel = uint32_t(level); |
| 4518 | region.dstSubresource.baseArrayLayer = uint32_t(layer); |
| 4519 | region.dstSubresource.layerCount = 1; |
| 4520 | |
| 4521 | region.dstOffsets[1].x = qMax(a: 1, b: w >> 1); |
| 4522 | region.dstOffsets[1].y = qMax(a: 1, b: h >> 1); |
| 4523 | region.dstOffsets[1].z = qMax(a: 1, b: depth >> 1); |
| 4524 | |
| 4525 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4526 | cmd.cmd = QVkCommandBuffer::Command::BlitImage; |
| 4527 | cmd.args.blitImage.src = utexD->image; |
| 4528 | cmd.args.blitImage.srcLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 4529 | cmd.args.blitImage.dst = utexD->image; |
| 4530 | cmd.args.blitImage.dstLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 4531 | cmd.args.blitImage.filter = VK_FILTER_LINEAR; |
| 4532 | cmd.args.blitImage.desc = region; |
| 4533 | |
| 4534 | w >>= 1; |
| 4535 | h >>= 1; |
| 4536 | depth >>= 1; |
| 4537 | } |
| 4538 | |
| 4539 | if (utexD->mipLevelCount > 1) { |
| 4540 | subresourceBarrier(cbD, image: utexD->image, |
| 4541 | oldLayout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, newLayout: origLayout, |
| 4542 | srcAccess: VK_ACCESS_TRANSFER_READ_BIT, dstAccess: origAccess, |
| 4543 | srcStage: VK_PIPELINE_STAGE_TRANSFER_BIT, dstStage: origStage, |
| 4544 | startLayer: layer, layerCount: 1, |
| 4545 | startLevel: 0, levelCount: int(utexD->mipLevelCount) - 1); |
| 4546 | subresourceBarrier(cbD, image: utexD->image, |
| 4547 | oldLayout: VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, newLayout: origLayout, |
| 4548 | srcAccess: VK_ACCESS_TRANSFER_WRITE_BIT, dstAccess: origAccess, |
| 4549 | srcStage: VK_PIPELINE_STAGE_TRANSFER_BIT, dstStage: origStage, |
| 4550 | startLayer: layer, layerCount: 1, |
| 4551 | startLevel: int(utexD->mipLevelCount) - 1, levelCount: 1); |
| 4552 | } |
| 4553 | } |
| 4554 | utexD->lastActiveFrameSlot = currentFrameSlot; |
| 4555 | } |
| 4556 | } |
| 4557 | |
| 4558 | ud->free(); |
| 4559 | } |
| 4560 | |
| 4561 | void QRhiVulkan::executeBufferHostWritesForSlot(QVkBuffer *bufD, int slot) |
| 4562 | { |
| 4563 | if (bufD->pendingDynamicUpdates[slot].isEmpty()) |
| 4564 | return; |
| 4565 | |
| 4566 | Q_ASSERT(bufD->m_type == QRhiBuffer::Dynamic); |
| 4567 | void *p = nullptr; |
| 4568 | VmaAllocation a = toVmaAllocation(a: bufD->allocations[slot]); |
| 4569 | // The vmaMap/Unmap are basically a no-op when persistently mapped since it |
| 4570 | // refcounts; this is great because we don't need to care if the allocation |
| 4571 | // was created as persistently mapped or not. |
| 4572 | VkResult err = vmaMapMemory(allocator: toVmaAllocator(a: allocator), allocation: a, ppData: &p); |
| 4573 | if (err != VK_SUCCESS) { |
| 4574 | qWarning(msg: "Failed to map buffer: %d" , err); |
| 4575 | return; |
| 4576 | } |
| 4577 | quint32 changeBegin = UINT32_MAX; |
| 4578 | quint32 changeEnd = 0; |
| 4579 | for (const QVkBuffer::DynamicUpdate &u : std::as_const(t&: bufD->pendingDynamicUpdates[slot])) { |
| 4580 | memcpy(dest: static_cast<char *>(p) + u.offset, src: u.data.constData(), n: u.data.size()); |
| 4581 | if (u.offset < changeBegin) |
| 4582 | changeBegin = u.offset; |
| 4583 | if (u.offset + u.data.size() > changeEnd) |
| 4584 | changeEnd = u.offset + u.data.size(); |
| 4585 | } |
| 4586 | if (changeBegin < UINT32_MAX && changeBegin < changeEnd) |
| 4587 | vmaFlushAllocation(allocator: toVmaAllocator(a: allocator), allocation: a, offset: changeBegin, size: changeEnd - changeBegin); |
| 4588 | vmaUnmapMemory(allocator: toVmaAllocator(a: allocator), allocation: a); |
| 4589 | |
| 4590 | bufD->pendingDynamicUpdates[slot].clear(); |
| 4591 | } |
| 4592 | |
| 4593 | static void qrhivk_releaseBuffer(const QRhiVulkan::DeferredReleaseEntry &e, void *allocator) |
| 4594 | { |
| 4595 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 4596 | vmaDestroyBuffer(allocator: toVmaAllocator(a: allocator), buffer: e.buffer.buffers[i], allocation: toVmaAllocation(a: e.buffer.allocations[i])); |
| 4597 | vmaDestroyBuffer(allocator: toVmaAllocator(a: allocator), buffer: e.buffer.stagingBuffers[i], allocation: toVmaAllocation(a: e.buffer.stagingAllocations[i])); |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | static void qrhivk_releaseRenderBuffer(const QRhiVulkan::DeferredReleaseEntry &e, VkDevice dev, QVulkanDeviceFunctions *df) |
| 4602 | { |
| 4603 | df->vkDestroyImageView(dev, e.renderBuffer.imageView, nullptr); |
| 4604 | df->vkDestroyImage(dev, e.renderBuffer.image, nullptr); |
| 4605 | df->vkFreeMemory(dev, e.renderBuffer.memory, nullptr); |
| 4606 | } |
| 4607 | |
| 4608 | static void qrhivk_releaseTexture(const QRhiVulkan::DeferredReleaseEntry &e, VkDevice dev, QVulkanDeviceFunctions *df, void *allocator) |
| 4609 | { |
| 4610 | df->vkDestroyImageView(dev, e.texture.imageView, nullptr); |
| 4611 | vmaDestroyImage(allocator: toVmaAllocator(a: allocator), image: e.texture.image, allocation: toVmaAllocation(a: e.texture.allocation)); |
| 4612 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) |
| 4613 | vmaDestroyBuffer(allocator: toVmaAllocator(a: allocator), buffer: e.texture.stagingBuffers[i], allocation: toVmaAllocation(a: e.texture.stagingAllocations[i])); |
| 4614 | for (int i = 0; i < QRhi::MAX_MIP_LEVELS; ++i) { |
| 4615 | if (e.texture.extraImageViews[i]) |
| 4616 | df->vkDestroyImageView(dev, e.texture.extraImageViews[i], nullptr); |
| 4617 | } |
| 4618 | } |
| 4619 | |
| 4620 | static void qrhivk_releaseSampler(const QRhiVulkan::DeferredReleaseEntry &e, VkDevice dev, QVulkanDeviceFunctions *df) |
| 4621 | { |
| 4622 | df->vkDestroySampler(dev, e.sampler.sampler, nullptr); |
| 4623 | } |
| 4624 | |
| 4625 | void QRhiVulkan::executeDeferredReleases(bool forced) |
| 4626 | { |
| 4627 | for (int i = releaseQueue.size() - 1; i >= 0; --i) { |
| 4628 | const QRhiVulkan::DeferredReleaseEntry &e(releaseQueue[i]); |
| 4629 | if (forced || currentFrameSlot == e.lastActiveFrameSlot || e.lastActiveFrameSlot < 0) { |
| 4630 | switch (e.type) { |
| 4631 | case QRhiVulkan::DeferredReleaseEntry::Pipeline: |
| 4632 | df->vkDestroyPipeline(dev, e.pipelineState.pipeline, nullptr); |
| 4633 | df->vkDestroyPipelineLayout(dev, e.pipelineState.layout, nullptr); |
| 4634 | break; |
| 4635 | case QRhiVulkan::DeferredReleaseEntry::ShaderResourceBindings: |
| 4636 | df->vkDestroyDescriptorSetLayout(dev, e.shaderResourceBindings.layout, nullptr); |
| 4637 | if (e.shaderResourceBindings.poolIndex >= 0) { |
| 4638 | descriptorPools[e.shaderResourceBindings.poolIndex].refCount -= 1; |
| 4639 | Q_ASSERT(descriptorPools[e.shaderResourceBindings.poolIndex].refCount >= 0); |
| 4640 | } |
| 4641 | break; |
| 4642 | case QRhiVulkan::DeferredReleaseEntry::Buffer: |
| 4643 | qrhivk_releaseBuffer(e, allocator); |
| 4644 | break; |
| 4645 | case QRhiVulkan::DeferredReleaseEntry::RenderBuffer: |
| 4646 | qrhivk_releaseRenderBuffer(e, dev, df); |
| 4647 | break; |
| 4648 | case QRhiVulkan::DeferredReleaseEntry::Texture: |
| 4649 | qrhivk_releaseTexture(e, dev, df, allocator); |
| 4650 | break; |
| 4651 | case QRhiVulkan::DeferredReleaseEntry::Sampler: |
| 4652 | qrhivk_releaseSampler(e, dev, df); |
| 4653 | break; |
| 4654 | case QRhiVulkan::DeferredReleaseEntry::TextureRenderTarget: |
| 4655 | df->vkDestroyFramebuffer(dev, e.textureRenderTarget.fb, nullptr); |
| 4656 | for (int att = 0; att < QVkRenderTargetData::MAX_COLOR_ATTACHMENTS; ++att) { |
| 4657 | df->vkDestroyImageView(dev, e.textureRenderTarget.rtv[att], nullptr); |
| 4658 | df->vkDestroyImageView(dev, e.textureRenderTarget.resrtv[att], nullptr); |
| 4659 | } |
| 4660 | df->vkDestroyImageView(dev, e.textureRenderTarget.dsv, nullptr); |
| 4661 | df->vkDestroyImageView(dev, e.textureRenderTarget.resdsv, nullptr); |
| 4662 | df->vkDestroyImageView(dev, e.textureRenderTarget.shadingRateMapView, nullptr); |
| 4663 | break; |
| 4664 | case QRhiVulkan::DeferredReleaseEntry::RenderPass: |
| 4665 | df->vkDestroyRenderPass(dev, e.renderPass.rp, nullptr); |
| 4666 | break; |
| 4667 | case QRhiVulkan::DeferredReleaseEntry::StagingBuffer: |
| 4668 | vmaDestroyBuffer(allocator: toVmaAllocator(a: allocator), buffer: e.stagingBuffer.stagingBuffer, allocation: toVmaAllocation(a: e.stagingBuffer.stagingAllocation)); |
| 4669 | break; |
| 4670 | case QRhiVulkan::DeferredReleaseEntry::SecondaryCommandBuffer: |
| 4671 | freeSecondaryCbs[e.lastActiveFrameSlot].append(t: e.secondaryCommandBuffer.cb); |
| 4672 | break; |
| 4673 | default: |
| 4674 | Q_UNREACHABLE(); |
| 4675 | break; |
| 4676 | } |
| 4677 | releaseQueue.removeAt(i); |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | |
| 4682 | void QRhiVulkan::finishActiveReadbacks(bool forced) |
| 4683 | { |
| 4684 | QVarLengthArray<std::function<void()>, 4> completedCallbacks; |
| 4685 | |
| 4686 | for (int i = activeTextureReadbacks.size() - 1; i >= 0; --i) { |
| 4687 | const QRhiVulkan::TextureReadback &readback(activeTextureReadbacks[i]); |
| 4688 | if (forced || currentFrameSlot == readback.activeFrameSlot || readback.activeFrameSlot < 0) { |
| 4689 | readback.result->format = readback.format; |
| 4690 | readback.result->pixelSize = readback.rect.size(); |
| 4691 | readback.result->data.resizeForOverwrite(size: readback.byteSize); |
| 4692 | VkResult err = vmaCopyAllocationToMemory(allocator: toVmaAllocator(a: allocator), |
| 4693 | srcAllocation: toVmaAllocation(a: readback.stagingAlloc), |
| 4694 | srcAllocationLocalOffset: 0, pDstHostPointer: readback.result->data.data(), size: readback.byteSize); |
| 4695 | if (err != VK_SUCCESS) { |
| 4696 | qWarning(msg: "Failed to copy texture readback buffer of size %u: %d" , readback.byteSize, err); |
| 4697 | readback.result->data.clear(); |
| 4698 | } |
| 4699 | |
| 4700 | vmaDestroyBuffer(allocator: toVmaAllocator(a: allocator), buffer: readback.stagingBuf, allocation: toVmaAllocation(a: readback.stagingAlloc)); |
| 4701 | |
| 4702 | if (readback.result->completed) |
| 4703 | completedCallbacks.append(t: readback.result->completed); |
| 4704 | |
| 4705 | activeTextureReadbacks.remove(i); |
| 4706 | } |
| 4707 | } |
| 4708 | |
| 4709 | for (int i = activeBufferReadbacks.size() - 1; i >= 0; --i) { |
| 4710 | const QRhiVulkan::BufferReadback &readback(activeBufferReadbacks[i]); |
| 4711 | if (forced || currentFrameSlot == readback.activeFrameSlot || readback.activeFrameSlot < 0) { |
| 4712 | readback.result->data.resizeForOverwrite(size: readback.byteSize); |
| 4713 | VkResult err = vmaCopyAllocationToMemory(allocator: toVmaAllocator(a: allocator), |
| 4714 | srcAllocation: toVmaAllocation(a: readback.stagingAlloc), |
| 4715 | srcAllocationLocalOffset: 0, pDstHostPointer: readback.result->data.data(), size: readback.byteSize); |
| 4716 | if (err != VK_SUCCESS) { |
| 4717 | qWarning(msg: "Failed to copy buffer readback buffer of size %d: %d" , readback.byteSize, err); |
| 4718 | readback.result->data.clear(); |
| 4719 | } |
| 4720 | |
| 4721 | vmaDestroyBuffer(allocator: toVmaAllocator(a: allocator), buffer: readback.stagingBuf, allocation: toVmaAllocation(a: readback.stagingAlloc)); |
| 4722 | |
| 4723 | if (readback.result->completed) |
| 4724 | completedCallbacks.append(t: readback.result->completed); |
| 4725 | |
| 4726 | activeBufferReadbacks.remove(i); |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | for (auto f : completedCallbacks) |
| 4731 | f(); |
| 4732 | } |
| 4733 | |
| 4734 | static struct { |
| 4735 | VkSampleCountFlagBits mask; |
| 4736 | int count; |
| 4737 | } qvk_sampleCounts[] = { |
| 4738 | // keep this sorted by 'count' |
| 4739 | { .mask: VK_SAMPLE_COUNT_1_BIT, .count: 1 }, |
| 4740 | { .mask: VK_SAMPLE_COUNT_2_BIT, .count: 2 }, |
| 4741 | { .mask: VK_SAMPLE_COUNT_4_BIT, .count: 4 }, |
| 4742 | { .mask: VK_SAMPLE_COUNT_8_BIT, .count: 8 }, |
| 4743 | { .mask: VK_SAMPLE_COUNT_16_BIT, .count: 16 }, |
| 4744 | { .mask: VK_SAMPLE_COUNT_32_BIT, .count: 32 }, |
| 4745 | { .mask: VK_SAMPLE_COUNT_64_BIT, .count: 64 } |
| 4746 | }; |
| 4747 | |
| 4748 | QList<int> QRhiVulkan::supportedSampleCounts() const |
| 4749 | { |
| 4750 | const VkPhysicalDeviceLimits *limits = &physDevProperties.limits; |
| 4751 | VkSampleCountFlags color = limits->framebufferColorSampleCounts; |
| 4752 | VkSampleCountFlags depth = limits->framebufferDepthSampleCounts; |
| 4753 | VkSampleCountFlags stencil = limits->framebufferStencilSampleCounts; |
| 4754 | QList<int> result; |
| 4755 | |
| 4756 | for (const auto &qvk_sampleCount : qvk_sampleCounts) { |
| 4757 | if ((color & qvk_sampleCount.mask) |
| 4758 | && (depth & qvk_sampleCount.mask) |
| 4759 | && (stencil & qvk_sampleCount.mask)) |
| 4760 | { |
| 4761 | result.append(t: qvk_sampleCount.count); |
| 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | return result; |
| 4766 | } |
| 4767 | |
| 4768 | VkSampleCountFlagBits QRhiVulkan::effectiveSampleCountBits(int sampleCount) |
| 4769 | { |
| 4770 | const int s = effectiveSampleCount(sampleCount); |
| 4771 | |
| 4772 | for (const auto &qvk_sampleCount : qvk_sampleCounts) { |
| 4773 | if (qvk_sampleCount.count == s) |
| 4774 | return qvk_sampleCount.mask; |
| 4775 | } |
| 4776 | |
| 4777 | Q_UNREACHABLE_RETURN(VK_SAMPLE_COUNT_1_BIT); |
| 4778 | } |
| 4779 | |
| 4780 | QList<QSize> QRhiVulkan::supportedShadingRates(int sampleCount) const |
| 4781 | { |
| 4782 | QList<QSize> result; |
| 4783 | #ifdef VK_KHR_fragment_shading_rate |
| 4784 | sampleCount = qMax(a: 1, b: sampleCount); |
| 4785 | VkSampleCountFlagBits mask = VK_SAMPLE_COUNT_1_BIT; |
| 4786 | for (const auto &qvk_sampleCount : qvk_sampleCounts) { |
| 4787 | if (qvk_sampleCount.count == sampleCount) { |
| 4788 | mask = qvk_sampleCount.mask; |
| 4789 | break; |
| 4790 | } |
| 4791 | } |
| 4792 | for (const VkPhysicalDeviceFragmentShadingRateKHR &s : fragmentShadingRates) { |
| 4793 | if (s.sampleCounts & mask) |
| 4794 | result.append(t: QSize(int(s.fragmentSize.width), int(s.fragmentSize.height))); |
| 4795 | } |
| 4796 | #else |
| 4797 | Q_UNUSED(sampleCount); |
| 4798 | result.append(QSize(1, 1)); |
| 4799 | #endif |
| 4800 | return result; |
| 4801 | } |
| 4802 | |
| 4803 | void QRhiVulkan::enqueueTransitionPassResources(QVkCommandBuffer *cbD) |
| 4804 | { |
| 4805 | cbD->passResTrackers.append(t: QRhiPassResourceTracker()); |
| 4806 | cbD->currentPassResTrackerIndex = cbD->passResTrackers.size() - 1; |
| 4807 | |
| 4808 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 4809 | cmd.cmd = QVkCommandBuffer::Command::TransitionPassResources; |
| 4810 | cmd.args.transitionResources.trackerIndex = cbD->passResTrackers.size() - 1; |
| 4811 | } |
| 4812 | |
| 4813 | void QRhiVulkan::recordPrimaryCommandBuffer(QVkCommandBuffer *cbD) |
| 4814 | { |
| 4815 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::NoPass); |
| 4816 | |
| 4817 | for (auto it = cbD->commands.begin(), end = cbD->commands.end(); it != end; ++it) { |
| 4818 | QVkCommandBuffer::Command &cmd(*it); |
| 4819 | switch (cmd.cmd) { |
| 4820 | case QVkCommandBuffer::Command::CopyBuffer: |
| 4821 | df->vkCmdCopyBuffer(cbD->cb, cmd.args.copyBuffer.src, cmd.args.copyBuffer.dst, |
| 4822 | 1, &cmd.args.copyBuffer.desc); |
| 4823 | break; |
| 4824 | case QVkCommandBuffer::Command::CopyBufferToImage: |
| 4825 | df->vkCmdCopyBufferToImage(cbD->cb, cmd.args.copyBufferToImage.src, cmd.args.copyBufferToImage.dst, |
| 4826 | cmd.args.copyBufferToImage.dstLayout, |
| 4827 | uint32_t(cmd.args.copyBufferToImage.count), |
| 4828 | cbD->pools.bufferImageCopy.constData() + cmd.args.copyBufferToImage.bufferImageCopyIndex); |
| 4829 | break; |
| 4830 | case QVkCommandBuffer::Command::CopyImage: |
| 4831 | df->vkCmdCopyImage(cbD->cb, cmd.args.copyImage.src, cmd.args.copyImage.srcLayout, |
| 4832 | cmd.args.copyImage.dst, cmd.args.copyImage.dstLayout, |
| 4833 | 1, &cmd.args.copyImage.desc); |
| 4834 | break; |
| 4835 | case QVkCommandBuffer::Command::CopyImageToBuffer: |
| 4836 | df->vkCmdCopyImageToBuffer(cbD->cb, cmd.args.copyImageToBuffer.src, cmd.args.copyImageToBuffer.srcLayout, |
| 4837 | cmd.args.copyImageToBuffer.dst, |
| 4838 | 1, &cmd.args.copyImageToBuffer.desc); |
| 4839 | break; |
| 4840 | case QVkCommandBuffer::Command::ImageBarrier: |
| 4841 | df->vkCmdPipelineBarrier(cbD->cb, cmd.args.imageBarrier.srcStageMask, cmd.args.imageBarrier.dstStageMask, |
| 4842 | 0, 0, nullptr, 0, nullptr, |
| 4843 | cmd.args.imageBarrier.count, cbD->pools.imageBarrier.constData() + cmd.args.imageBarrier.index); |
| 4844 | break; |
| 4845 | case QVkCommandBuffer::Command::BufferBarrier: |
| 4846 | df->vkCmdPipelineBarrier(cbD->cb, cmd.args.bufferBarrier.srcStageMask, cmd.args.bufferBarrier.dstStageMask, |
| 4847 | 0, 0, nullptr, |
| 4848 | cmd.args.bufferBarrier.count, cbD->pools.bufferBarrier.constData() + cmd.args.bufferBarrier.index, |
| 4849 | 0, nullptr); |
| 4850 | break; |
| 4851 | case QVkCommandBuffer::Command::BlitImage: |
| 4852 | df->vkCmdBlitImage(cbD->cb, cmd.args.blitImage.src, cmd.args.blitImage.srcLayout, |
| 4853 | cmd.args.blitImage.dst, cmd.args.blitImage.dstLayout, |
| 4854 | 1, &cmd.args.blitImage.desc, |
| 4855 | cmd.args.blitImage.filter); |
| 4856 | break; |
| 4857 | case QVkCommandBuffer::Command::BeginRenderPass: |
| 4858 | cmd.args.beginRenderPass.desc.pClearValues = cbD->pools.clearValue.constData() + cmd.args.beginRenderPass.clearValueIndex; |
| 4859 | df->vkCmdBeginRenderPass(cbD->cb, &cmd.args.beginRenderPass.desc, |
| 4860 | cmd.args.beginRenderPass.useSecondaryCb ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS |
| 4861 | : VK_SUBPASS_CONTENTS_INLINE); |
| 4862 | break; |
| 4863 | case QVkCommandBuffer::Command::EndRenderPass: |
| 4864 | df->vkCmdEndRenderPass(cbD->cb); |
| 4865 | break; |
| 4866 | case QVkCommandBuffer::Command::BindPipeline: |
| 4867 | df->vkCmdBindPipeline(cbD->cb, cmd.args.bindPipeline.bindPoint, cmd.args.bindPipeline.pipeline); |
| 4868 | break; |
| 4869 | case QVkCommandBuffer::Command::BindDescriptorSet: |
| 4870 | { |
| 4871 | const uint32_t *offsets = nullptr; |
| 4872 | if (cmd.args.bindDescriptorSet.dynamicOffsetCount > 0) |
| 4873 | offsets = cbD->pools.dynamicOffset.constData() + cmd.args.bindDescriptorSet.dynamicOffsetIndex; |
| 4874 | df->vkCmdBindDescriptorSets(cbD->cb, cmd.args.bindDescriptorSet.bindPoint, |
| 4875 | cmd.args.bindDescriptorSet.pipelineLayout, |
| 4876 | 0, 1, &cmd.args.bindDescriptorSet.descSet, |
| 4877 | uint32_t(cmd.args.bindDescriptorSet.dynamicOffsetCount), |
| 4878 | offsets); |
| 4879 | } |
| 4880 | break; |
| 4881 | case QVkCommandBuffer::Command::BindVertexBuffer: |
| 4882 | df->vkCmdBindVertexBuffers(cbD->cb, uint32_t(cmd.args.bindVertexBuffer.startBinding), |
| 4883 | uint32_t(cmd.args.bindVertexBuffer.count), |
| 4884 | cbD->pools.vertexBuffer.constData() + cmd.args.bindVertexBuffer.vertexBufferIndex, |
| 4885 | cbD->pools.vertexBufferOffset.constData() + cmd.args.bindVertexBuffer.vertexBufferOffsetIndex); |
| 4886 | break; |
| 4887 | case QVkCommandBuffer::Command::BindIndexBuffer: |
| 4888 | df->vkCmdBindIndexBuffer(cbD->cb, cmd.args.bindIndexBuffer.buf, |
| 4889 | cmd.args.bindIndexBuffer.ofs, cmd.args.bindIndexBuffer.type); |
| 4890 | break; |
| 4891 | case QVkCommandBuffer::Command::SetViewport: |
| 4892 | df->vkCmdSetViewport(cbD->cb, 0, 1, &cmd.args.setViewport.viewport); |
| 4893 | break; |
| 4894 | case QVkCommandBuffer::Command::SetScissor: |
| 4895 | df->vkCmdSetScissor(cbD->cb, 0, 1, &cmd.args.setScissor.scissor); |
| 4896 | break; |
| 4897 | case QVkCommandBuffer::Command::SetBlendConstants: |
| 4898 | df->vkCmdSetBlendConstants(cbD->cb, cmd.args.setBlendConstants.c); |
| 4899 | break; |
| 4900 | case QVkCommandBuffer::Command::SetStencilRef: |
| 4901 | df->vkCmdSetStencilReference(cbD->cb, VK_STENCIL_FRONT_AND_BACK, cmd.args.setStencilRef.ref); |
| 4902 | break; |
| 4903 | case QVkCommandBuffer::Command::Draw: |
| 4904 | df->vkCmdDraw(cbD->cb, cmd.args.draw.vertexCount, cmd.args.draw.instanceCount, |
| 4905 | cmd.args.draw.firstVertex, cmd.args.draw.firstInstance); |
| 4906 | break; |
| 4907 | case QVkCommandBuffer::Command::DrawIndexed: |
| 4908 | df->vkCmdDrawIndexed(cbD->cb, cmd.args.drawIndexed.indexCount, cmd.args.drawIndexed.instanceCount, |
| 4909 | cmd.args.drawIndexed.firstIndex, cmd.args.drawIndexed.vertexOffset, |
| 4910 | cmd.args.drawIndexed.firstInstance); |
| 4911 | break; |
| 4912 | case QVkCommandBuffer::Command::DebugMarkerBegin: |
| 4913 | #ifdef VK_EXT_debug_utils |
| 4914 | cmd.args.debugMarkerBegin.label.pLabelName = |
| 4915 | cbD->pools.debugMarkerData[cmd.args.debugMarkerBegin.labelNameIndex].constData(); |
| 4916 | vkCmdBeginDebugUtilsLabelEXT(cbD->cb, &cmd.args.debugMarkerBegin.label); |
| 4917 | #endif |
| 4918 | break; |
| 4919 | case QVkCommandBuffer::Command::DebugMarkerEnd: |
| 4920 | #ifdef VK_EXT_debug_utils |
| 4921 | vkCmdEndDebugUtilsLabelEXT(cbD->cb); |
| 4922 | #endif |
| 4923 | break; |
| 4924 | case QVkCommandBuffer::Command::DebugMarkerInsert: |
| 4925 | #ifdef VK_EXT_debug_utils |
| 4926 | cmd.args.debugMarkerInsert.label.pLabelName = |
| 4927 | cbD->pools.debugMarkerData[cmd.args.debugMarkerInsert.labelNameIndex].constData(); |
| 4928 | vkCmdInsertDebugUtilsLabelEXT(cbD->cb, &cmd.args.debugMarkerInsert.label); |
| 4929 | #endif |
| 4930 | break; |
| 4931 | case QVkCommandBuffer::Command::TransitionPassResources: |
| 4932 | recordTransitionPassResources(cbD, tracker: cbD->passResTrackers[cmd.args.transitionResources.trackerIndex]); |
| 4933 | break; |
| 4934 | case QVkCommandBuffer::Command::Dispatch: |
| 4935 | df->vkCmdDispatch(cbD->cb, uint32_t(cmd.args.dispatch.x), uint32_t(cmd.args.dispatch.y), uint32_t(cmd.args.dispatch.z)); |
| 4936 | break; |
| 4937 | case QVkCommandBuffer::Command::ExecuteSecondary: |
| 4938 | df->vkCmdExecuteCommands(cbD->cb, 1, &cmd.args.executeSecondary.cb); |
| 4939 | break; |
| 4940 | case QVkCommandBuffer::Command::SetShadingRate: |
| 4941 | { |
| 4942 | #ifdef VK_KHR_fragment_shading_rate |
| 4943 | VkFragmentShadingRateCombinerOpKHR op[2] = { |
| 4944 | VK_FRAGMENT_SHADING_RATE_COMBINER_OP_MAX_KHR, |
| 4945 | VK_FRAGMENT_SHADING_RATE_COMBINER_OP_MAX_KHR |
| 4946 | }; |
| 4947 | VkExtent2D size = { .width: cmd.args.setShadingRate.w, .height: cmd.args.setShadingRate.h }; |
| 4948 | vkCmdSetFragmentShadingRateKHR(cbD->cb, &size, op); |
| 4949 | #endif |
| 4950 | } |
| 4951 | break; |
| 4952 | default: |
| 4953 | break; |
| 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | static inline VkAccessFlags toVkAccess(QRhiPassResourceTracker::BufferAccess access) |
| 4959 | { |
| 4960 | switch (access) { |
| 4961 | case QRhiPassResourceTracker::BufVertexInput: |
| 4962 | return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; |
| 4963 | case QRhiPassResourceTracker::BufIndexRead: |
| 4964 | return VK_ACCESS_INDEX_READ_BIT; |
| 4965 | case QRhiPassResourceTracker::BufUniformRead: |
| 4966 | return VK_ACCESS_UNIFORM_READ_BIT; |
| 4967 | case QRhiPassResourceTracker::BufStorageLoad: |
| 4968 | return VK_ACCESS_SHADER_READ_BIT; |
| 4969 | case QRhiPassResourceTracker::BufStorageStore: |
| 4970 | return VK_ACCESS_SHADER_WRITE_BIT; |
| 4971 | case QRhiPassResourceTracker::BufStorageLoadStore: |
| 4972 | return VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 4973 | default: |
| 4974 | Q_UNREACHABLE(); |
| 4975 | break; |
| 4976 | } |
| 4977 | return 0; |
| 4978 | } |
| 4979 | |
| 4980 | static inline VkPipelineStageFlags toVkPipelineStage(QRhiPassResourceTracker::BufferStage stage) |
| 4981 | { |
| 4982 | switch (stage) { |
| 4983 | case QRhiPassResourceTracker::BufVertexInputStage: |
| 4984 | return VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; |
| 4985 | case QRhiPassResourceTracker::BufVertexStage: |
| 4986 | return VK_PIPELINE_STAGE_VERTEX_SHADER_BIT; |
| 4987 | case QRhiPassResourceTracker::BufTCStage: |
| 4988 | return VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT; |
| 4989 | case QRhiPassResourceTracker::BufTEStage: |
| 4990 | return VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT; |
| 4991 | case QRhiPassResourceTracker::BufFragmentStage: |
| 4992 | return VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 4993 | case QRhiPassResourceTracker::BufComputeStage: |
| 4994 | return VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 4995 | case QRhiPassResourceTracker::BufGeometryStage: |
| 4996 | return VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT; |
| 4997 | default: |
| 4998 | Q_UNREACHABLE(); |
| 4999 | break; |
| 5000 | } |
| 5001 | return 0; |
| 5002 | } |
| 5003 | |
| 5004 | static inline QVkBuffer::UsageState toVkBufferUsageState(QRhiPassResourceTracker::UsageState usage) |
| 5005 | { |
| 5006 | QVkBuffer::UsageState u; |
| 5007 | u.access = VkAccessFlags(usage.access); |
| 5008 | u.stage = VkPipelineStageFlags(usage.stage); |
| 5009 | return u; |
| 5010 | } |
| 5011 | |
| 5012 | static inline VkImageLayout toVkLayout(QRhiPassResourceTracker::TextureAccess access) |
| 5013 | { |
| 5014 | switch (access) { |
| 5015 | case QRhiPassResourceTracker::TexSample: |
| 5016 | return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 5017 | case QRhiPassResourceTracker::TexColorOutput: |
| 5018 | return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 5019 | case QRhiPassResourceTracker::TexDepthOutput: |
| 5020 | return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 5021 | case QRhiPassResourceTracker::TexStorageLoad: |
| 5022 | case QRhiPassResourceTracker::TexStorageStore: |
| 5023 | case QRhiPassResourceTracker::TexStorageLoadStore: |
| 5024 | return VK_IMAGE_LAYOUT_GENERAL; |
| 5025 | case QRhiPassResourceTracker::TexShadingRate: |
| 5026 | #ifdef VK_KHR_fragment_shading_rate |
| 5027 | return VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR; |
| 5028 | #else |
| 5029 | return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 5030 | #endif |
| 5031 | default: |
| 5032 | Q_UNREACHABLE(); |
| 5033 | break; |
| 5034 | } |
| 5035 | return VK_IMAGE_LAYOUT_GENERAL; |
| 5036 | } |
| 5037 | |
| 5038 | static inline VkAccessFlags toVkAccess(QRhiPassResourceTracker::TextureAccess access) |
| 5039 | { |
| 5040 | switch (access) { |
| 5041 | case QRhiPassResourceTracker::TexSample: |
| 5042 | return VK_ACCESS_SHADER_READ_BIT; |
| 5043 | case QRhiPassResourceTracker::TexColorOutput: |
| 5044 | return VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 5045 | case QRhiPassResourceTracker::TexDepthOutput: |
| 5046 | return VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 5047 | case QRhiPassResourceTracker::TexStorageLoad: |
| 5048 | return VK_ACCESS_SHADER_READ_BIT; |
| 5049 | case QRhiPassResourceTracker::TexStorageStore: |
| 5050 | return VK_ACCESS_SHADER_WRITE_BIT; |
| 5051 | case QRhiPassResourceTracker::TexStorageLoadStore: |
| 5052 | return VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 5053 | case QRhiPassResourceTracker::TexShadingRate: |
| 5054 | return 0; |
| 5055 | default: |
| 5056 | Q_UNREACHABLE(); |
| 5057 | break; |
| 5058 | } |
| 5059 | return 0; |
| 5060 | } |
| 5061 | |
| 5062 | static inline VkPipelineStageFlags toVkPipelineStage(QRhiPassResourceTracker::TextureStage stage) |
| 5063 | { |
| 5064 | switch (stage) { |
| 5065 | case QRhiPassResourceTracker::TexVertexStage: |
| 5066 | return VK_PIPELINE_STAGE_VERTEX_SHADER_BIT; |
| 5067 | case QRhiPassResourceTracker::TexTCStage: |
| 5068 | return VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT; |
| 5069 | case QRhiPassResourceTracker::TexTEStage: |
| 5070 | return VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT; |
| 5071 | case QRhiPassResourceTracker::TexFragmentStage: |
| 5072 | return VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 5073 | case QRhiPassResourceTracker::TexColorOutputStage: |
| 5074 | return VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 5075 | case QRhiPassResourceTracker::TexDepthOutputStage: |
| 5076 | return VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 5077 | case QRhiPassResourceTracker::TexComputeStage: |
| 5078 | return VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 5079 | case QRhiPassResourceTracker::TexGeometryStage: |
| 5080 | return VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT; |
| 5081 | default: |
| 5082 | Q_UNREACHABLE(); |
| 5083 | break; |
| 5084 | } |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
| 5088 | static inline QVkTexture::UsageState toVkTextureUsageState(QRhiPassResourceTracker::UsageState usage) |
| 5089 | { |
| 5090 | QVkTexture::UsageState u; |
| 5091 | u.layout = VkImageLayout(usage.layout); |
| 5092 | u.access = VkAccessFlags(usage.access); |
| 5093 | u.stage = VkPipelineStageFlags(usage.stage); |
| 5094 | return u; |
| 5095 | } |
| 5096 | |
| 5097 | void QRhiVulkan::trackedRegisterBuffer(QRhiPassResourceTracker *passResTracker, |
| 5098 | QVkBuffer *bufD, |
| 5099 | int slot, |
| 5100 | QRhiPassResourceTracker::BufferAccess access, |
| 5101 | QRhiPassResourceTracker::BufferStage stage) |
| 5102 | { |
| 5103 | QVkBuffer::UsageState &u(bufD->usageState[slot]); |
| 5104 | const VkAccessFlags newAccess = toVkAccess(access); |
| 5105 | const VkPipelineStageFlags newStage = toVkPipelineStage(stage); |
| 5106 | if (u.access == newAccess && u.stage == newStage) { |
| 5107 | if (!accessIsWrite(access)) |
| 5108 | return; |
| 5109 | } |
| 5110 | passResTracker->registerBuffer(buf: bufD, slot, access: &access, stage: &stage, state: toPassTrackerUsageState(bufUsage: u)); |
| 5111 | u.access = newAccess; |
| 5112 | u.stage = newStage; |
| 5113 | } |
| 5114 | |
| 5115 | void QRhiVulkan::trackedRegisterTexture(QRhiPassResourceTracker *passResTracker, |
| 5116 | QVkTexture *texD, |
| 5117 | QRhiPassResourceTracker::TextureAccess access, |
| 5118 | QRhiPassResourceTracker::TextureStage stage) |
| 5119 | { |
| 5120 | QVkTexture::UsageState &u(texD->usageState); |
| 5121 | const VkAccessFlags newAccess = toVkAccess(access); |
| 5122 | const VkPipelineStageFlags newStage = toVkPipelineStage(stage); |
| 5123 | const VkImageLayout newLayout = toVkLayout(access); |
| 5124 | if (u.access == newAccess && u.stage == newStage && u.layout == newLayout) { |
| 5125 | if (!accessIsWrite(access)) |
| 5126 | return; |
| 5127 | } |
| 5128 | passResTracker->registerTexture(tex: texD, access: &access, stage: &stage, state: toPassTrackerUsageState(texUsage: u)); |
| 5129 | u.layout = newLayout; |
| 5130 | u.access = newAccess; |
| 5131 | u.stage = newStage; |
| 5132 | } |
| 5133 | |
| 5134 | void QRhiVulkan::recordTransitionPassResources(QVkCommandBuffer *cbD, const QRhiPassResourceTracker &tracker) |
| 5135 | { |
| 5136 | if (tracker.isEmpty()) |
| 5137 | return; |
| 5138 | |
| 5139 | for (auto it = tracker.cbeginBuffers(), itEnd = tracker.cendBuffers(); it != itEnd; ++it) { |
| 5140 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, it.key()); |
| 5141 | VkAccessFlags access = toVkAccess(access: it->access); |
| 5142 | VkPipelineStageFlags stage = toVkPipelineStage(stage: it->stage); |
| 5143 | QVkBuffer::UsageState s = toVkBufferUsageState(usage: it->stateAtPassBegin); |
| 5144 | if (!s.stage) |
| 5145 | continue; |
| 5146 | if (s.access == access && s.stage == stage) { |
| 5147 | if (!accessIsWrite(access)) |
| 5148 | continue; |
| 5149 | } |
| 5150 | VkBufferMemoryBarrier bufMemBarrier = {}; |
| 5151 | bufMemBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; |
| 5152 | bufMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 5153 | bufMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 5154 | bufMemBarrier.srcAccessMask = s.access; |
| 5155 | bufMemBarrier.dstAccessMask = access; |
| 5156 | bufMemBarrier.buffer = bufD->buffers[it->slot]; |
| 5157 | bufMemBarrier.size = VK_WHOLE_SIZE; |
| 5158 | df->vkCmdPipelineBarrier(cbD->cb, s.stage, stage, 0, |
| 5159 | 0, nullptr, |
| 5160 | 1, &bufMemBarrier, |
| 5161 | 0, nullptr); |
| 5162 | } |
| 5163 | |
| 5164 | for (auto it = tracker.cbeginTextures(), itEnd = tracker.cendTextures(); it != itEnd; ++it) { |
| 5165 | QVkTexture *texD = QRHI_RES(QVkTexture, it.key()); |
| 5166 | VkImageLayout layout = toVkLayout(access: it->access); |
| 5167 | VkAccessFlags access = toVkAccess(access: it->access); |
| 5168 | VkPipelineStageFlags stage = toVkPipelineStage(stage: it->stage); |
| 5169 | QVkTexture::UsageState s = toVkTextureUsageState(usage: it->stateAtPassBegin); |
| 5170 | if (s.access == access && s.stage == stage && s.layout == layout) { |
| 5171 | if (!accessIsWrite(access)) |
| 5172 | continue; |
| 5173 | } |
| 5174 | VkImageMemoryBarrier barrier = {}; |
| 5175 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 5176 | barrier.subresourceRange.aspectMask = aspectMaskForTextureFormat(format: texD->m_format); |
| 5177 | barrier.subresourceRange.baseMipLevel = 0; |
| 5178 | barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; |
| 5179 | barrier.subresourceRange.baseArrayLayer = 0; |
| 5180 | barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS; |
| 5181 | barrier.oldLayout = s.layout; // new textures have this set to PREINITIALIZED |
| 5182 | barrier.newLayout = layout; |
| 5183 | barrier.srcAccessMask = s.access; // may be 0 but that's fine |
| 5184 | barrier.dstAccessMask = access; |
| 5185 | barrier.image = texD->image; |
| 5186 | VkPipelineStageFlags srcStage = s.stage; |
| 5187 | // stage mask cannot be 0 |
| 5188 | if (!srcStage) |
| 5189 | srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 5190 | df->vkCmdPipelineBarrier(cbD->cb, srcStage, stage, 0, |
| 5191 | 0, nullptr, |
| 5192 | 0, nullptr, |
| 5193 | 1, &barrier); |
| 5194 | } |
| 5195 | } |
| 5196 | |
| 5197 | QRhiSwapChain *QRhiVulkan::createSwapChain() |
| 5198 | { |
| 5199 | if (!vkGetPhysicalDeviceSurfaceCapabilitiesKHR |
| 5200 | || !vkGetPhysicalDeviceSurfaceFormatsKHR |
| 5201 | || !vkGetPhysicalDeviceSurfacePresentModesKHR) |
| 5202 | { |
| 5203 | qWarning(msg: "Physical device surface queries not available" ); |
| 5204 | return nullptr; |
| 5205 | } |
| 5206 | |
| 5207 | return new QVkSwapChain(this); |
| 5208 | } |
| 5209 | |
| 5210 | QRhiBuffer *QRhiVulkan::createBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, quint32 size) |
| 5211 | { |
| 5212 | return new QVkBuffer(this, type, usage, size); |
| 5213 | } |
| 5214 | |
| 5215 | int QRhiVulkan::ubufAlignment() const |
| 5216 | { |
| 5217 | return int(ubufAlign); // typically 256 (bytes) |
| 5218 | } |
| 5219 | |
| 5220 | bool QRhiVulkan::isYUpInFramebuffer() const |
| 5221 | { |
| 5222 | return false; |
| 5223 | } |
| 5224 | |
| 5225 | bool QRhiVulkan::isYUpInNDC() const |
| 5226 | { |
| 5227 | return false; |
| 5228 | } |
| 5229 | |
| 5230 | bool QRhiVulkan::isClipDepthZeroToOne() const |
| 5231 | { |
| 5232 | return true; |
| 5233 | } |
| 5234 | |
| 5235 | QMatrix4x4 QRhiVulkan::clipSpaceCorrMatrix() const |
| 5236 | { |
| 5237 | // See https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/ |
| 5238 | |
| 5239 | static QMatrix4x4 m; |
| 5240 | if (m.isIdentity()) { |
| 5241 | // NB the ctor takes row-major |
| 5242 | m = QMatrix4x4(1.0f, 0.0f, 0.0f, 0.0f, |
| 5243 | 0.0f, -1.0f, 0.0f, 0.0f, |
| 5244 | 0.0f, 0.0f, 0.5f, 0.5f, |
| 5245 | 0.0f, 0.0f, 0.0f, 1.0f); |
| 5246 | } |
| 5247 | return m; |
| 5248 | } |
| 5249 | |
| 5250 | bool QRhiVulkan::isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const |
| 5251 | { |
| 5252 | // Note that with some SDKs the validation layer gives an odd warning about |
| 5253 | // BC not being supported, even when our check here succeeds. Not much we |
| 5254 | // can do about that. |
| 5255 | if (format >= QRhiTexture::BC1 && format <= QRhiTexture::BC7) { |
| 5256 | if (!physDevFeatures.textureCompressionBC) |
| 5257 | return false; |
| 5258 | } |
| 5259 | |
| 5260 | if (format >= QRhiTexture::ETC2_RGB8 && format <= QRhiTexture::ETC2_RGBA8) { |
| 5261 | if (!physDevFeatures.textureCompressionETC2) |
| 5262 | return false; |
| 5263 | } |
| 5264 | |
| 5265 | if (format >= QRhiTexture::ASTC_4x4 && format <= QRhiTexture::ASTC_12x12) { |
| 5266 | if (!physDevFeatures.textureCompressionASTC_LDR) |
| 5267 | return false; |
| 5268 | } |
| 5269 | |
| 5270 | VkFormat vkformat = toVkTextureFormat(format, flags); |
| 5271 | VkFormatProperties props; |
| 5272 | f->vkGetPhysicalDeviceFormatProperties(physDev, vkformat, &props); |
| 5273 | return (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) != 0; |
| 5274 | } |
| 5275 | |
| 5276 | bool QRhiVulkan::isFeatureSupported(QRhi::Feature feature) const |
| 5277 | { |
| 5278 | switch (feature) { |
| 5279 | case QRhi::MultisampleTexture: |
| 5280 | return true; |
| 5281 | case QRhi::MultisampleRenderBuffer: |
| 5282 | return true; |
| 5283 | case QRhi::DebugMarkers: |
| 5284 | return caps.debugUtils; |
| 5285 | case QRhi::Timestamps: |
| 5286 | return timestampValidBits != 0; |
| 5287 | case QRhi::Instancing: |
| 5288 | return true; |
| 5289 | case QRhi::CustomInstanceStepRate: |
| 5290 | return caps.vertexAttribDivisor; |
| 5291 | case QRhi::PrimitiveRestart: |
| 5292 | return true; |
| 5293 | case QRhi::NonDynamicUniformBuffers: |
| 5294 | return true; |
| 5295 | case QRhi::NonFourAlignedEffectiveIndexBufferOffset: |
| 5296 | return true; |
| 5297 | case QRhi::NPOTTextureRepeat: |
| 5298 | return true; |
| 5299 | case QRhi::RedOrAlpha8IsRed: |
| 5300 | return true; |
| 5301 | case QRhi::ElementIndexUint: |
| 5302 | return true; |
| 5303 | case QRhi::Compute: |
| 5304 | return caps.compute; |
| 5305 | case QRhi::WideLines: |
| 5306 | return caps.wideLines; |
| 5307 | case QRhi::VertexShaderPointSize: |
| 5308 | return true; |
| 5309 | case QRhi::BaseVertex: |
| 5310 | return true; |
| 5311 | case QRhi::BaseInstance: |
| 5312 | return true; |
| 5313 | case QRhi::TriangleFanTopology: |
| 5314 | return true; |
| 5315 | case QRhi::ReadBackNonUniformBuffer: |
| 5316 | return true; |
| 5317 | case QRhi::ReadBackNonBaseMipLevel: |
| 5318 | return true; |
| 5319 | case QRhi::TexelFetch: |
| 5320 | return true; |
| 5321 | case QRhi::RenderToNonBaseMipLevel: |
| 5322 | return true; |
| 5323 | case QRhi::IntAttributes: |
| 5324 | return true; |
| 5325 | case QRhi::ScreenSpaceDerivatives: |
| 5326 | return true; |
| 5327 | case QRhi::ReadBackAnyTextureFormat: |
| 5328 | return true; |
| 5329 | case QRhi::PipelineCacheDataLoadSave: |
| 5330 | return true; |
| 5331 | case QRhi::ImageDataStride: |
| 5332 | return true; |
| 5333 | case QRhi::RenderBufferImport: |
| 5334 | return false; |
| 5335 | case QRhi::ThreeDimensionalTextures: |
| 5336 | return true; |
| 5337 | case QRhi::RenderTo3DTextureSlice: |
| 5338 | return caps.texture3DSliceAs2D; |
| 5339 | case QRhi::TextureArrays: |
| 5340 | return true; |
| 5341 | case QRhi::Tessellation: |
| 5342 | return caps.tessellation; |
| 5343 | case QRhi::GeometryShader: |
| 5344 | return caps.geometryShader; |
| 5345 | case QRhi::TextureArrayRange: |
| 5346 | return true; |
| 5347 | case QRhi::NonFillPolygonMode: |
| 5348 | return caps.nonFillPolygonMode; |
| 5349 | case QRhi::OneDimensionalTextures: |
| 5350 | return true; |
| 5351 | case QRhi::OneDimensionalTextureMipmaps: |
| 5352 | return true; |
| 5353 | case QRhi::HalfAttributes: |
| 5354 | return true; |
| 5355 | case QRhi::RenderToOneDimensionalTexture: |
| 5356 | return true; |
| 5357 | case QRhi::ThreeDimensionalTextureMipmaps: |
| 5358 | return true; |
| 5359 | case QRhi::MultiView: |
| 5360 | return caps.multiView; |
| 5361 | case QRhi::TextureViewFormat: |
| 5362 | return true; |
| 5363 | case QRhi::ResolveDepthStencil: |
| 5364 | return caps.renderPass2KHR && caps.depthStencilResolveKHR; |
| 5365 | case QRhi::VariableRateShading: |
| 5366 | return caps.renderPass2KHR && caps.perDrawShadingRate; |
| 5367 | case QRhi::VariableRateShadingMap: |
| 5368 | case QRhi::VariableRateShadingMapWithTexture: |
| 5369 | return caps.renderPass2KHR && caps.imageBasedShadingRate; |
| 5370 | case QRhi::PerRenderTargetBlending: |
| 5371 | case QRhi::SampleVariables: |
| 5372 | return true; |
| 5373 | default: |
| 5374 | Q_UNREACHABLE_RETURN(false); |
| 5375 | } |
| 5376 | } |
| 5377 | |
| 5378 | int QRhiVulkan::resourceLimit(QRhi::ResourceLimit limit) const |
| 5379 | { |
| 5380 | switch (limit) { |
| 5381 | case QRhi::TextureSizeMin: |
| 5382 | return 1; |
| 5383 | case QRhi::TextureSizeMax: |
| 5384 | return int(physDevProperties.limits.maxImageDimension2D); |
| 5385 | case QRhi::MaxColorAttachments: |
| 5386 | return int(physDevProperties.limits.maxColorAttachments); |
| 5387 | case QRhi::FramesInFlight: |
| 5388 | return QVK_FRAMES_IN_FLIGHT; |
| 5389 | case QRhi::MaxAsyncReadbackFrames: |
| 5390 | return QVK_FRAMES_IN_FLIGHT; |
| 5391 | case QRhi::MaxThreadGroupsPerDimension: |
| 5392 | return int(qMin(a: physDevProperties.limits.maxComputeWorkGroupCount[0], |
| 5393 | b: qMin(a: physDevProperties.limits.maxComputeWorkGroupCount[1], |
| 5394 | b: physDevProperties.limits.maxComputeWorkGroupCount[2]))); |
| 5395 | case QRhi::MaxThreadsPerThreadGroup: |
| 5396 | return int(physDevProperties.limits.maxComputeWorkGroupInvocations); |
| 5397 | case QRhi::MaxThreadGroupX: |
| 5398 | return int(physDevProperties.limits.maxComputeWorkGroupSize[0]); |
| 5399 | case QRhi::MaxThreadGroupY: |
| 5400 | return int(physDevProperties.limits.maxComputeWorkGroupSize[1]); |
| 5401 | case QRhi::MaxThreadGroupZ: |
| 5402 | return int(physDevProperties.limits.maxComputeWorkGroupSize[2]); |
| 5403 | case QRhi::TextureArraySizeMax: |
| 5404 | return int(physDevProperties.limits.maxImageArrayLayers); |
| 5405 | case QRhi::MaxUniformBufferRange: |
| 5406 | return int(qMin<uint32_t>(INT_MAX, b: physDevProperties.limits.maxUniformBufferRange)); |
| 5407 | case QRhi::MaxVertexInputs: |
| 5408 | return physDevProperties.limits.maxVertexInputAttributes; |
| 5409 | case QRhi::MaxVertexOutputs: |
| 5410 | return physDevProperties.limits.maxVertexOutputComponents / 4; |
| 5411 | case QRhi::ShadingRateImageTileSize: |
| 5412 | return caps.imageBasedShadingRateTileSize; |
| 5413 | default: |
| 5414 | Q_UNREACHABLE_RETURN(0); |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | const QRhiNativeHandles *QRhiVulkan::nativeHandles() |
| 5419 | { |
| 5420 | return &nativeHandlesStruct; |
| 5421 | } |
| 5422 | |
| 5423 | QRhiDriverInfo QRhiVulkan::driverInfo() const |
| 5424 | { |
| 5425 | return driverInfoStruct; |
| 5426 | } |
| 5427 | |
| 5428 | QRhiStats QRhiVulkan::statistics() |
| 5429 | { |
| 5430 | QRhiStats result; |
| 5431 | result.totalPipelineCreationTime = totalPipelineCreationTime(); |
| 5432 | |
| 5433 | VmaBudget budgets[VK_MAX_MEMORY_HEAPS]; |
| 5434 | vmaGetHeapBudgets(allocator: toVmaAllocator(a: allocator), pBudgets: budgets); |
| 5435 | |
| 5436 | uint32_t count = toVmaAllocator(a: allocator)->GetMemoryHeapCount(); |
| 5437 | for (uint32_t i = 0; i < count; ++i) { |
| 5438 | const VmaStatistics &stats(budgets[i].statistics); |
| 5439 | result.blockCount += stats.blockCount; |
| 5440 | result.allocCount += stats.allocationCount; |
| 5441 | result.usedBytes += stats.allocationBytes; |
| 5442 | result.unusedBytes += stats.blockBytes - stats.allocationBytes; |
| 5443 | } |
| 5444 | |
| 5445 | return result; |
| 5446 | } |
| 5447 | |
| 5448 | bool QRhiVulkan::makeThreadLocalNativeContextCurrent() |
| 5449 | { |
| 5450 | // not applicable |
| 5451 | return false; |
| 5452 | } |
| 5453 | |
| 5454 | void QRhiVulkan::setQueueSubmitParams(QRhiNativeHandles *params) |
| 5455 | { |
| 5456 | QRhiVulkanQueueSubmitParams *sp = static_cast<QRhiVulkanQueueSubmitParams *>(params); |
| 5457 | if (!sp) |
| 5458 | return; |
| 5459 | |
| 5460 | waitSemaphoresForQueueSubmit.clear(); |
| 5461 | if (sp->waitSemaphoreCount) |
| 5462 | waitSemaphoresForQueueSubmit.append(buf: sp->waitSemaphores, sz: sp->waitSemaphoreCount); |
| 5463 | |
| 5464 | signalSemaphoresForQueueSubmit.clear(); |
| 5465 | if (sp->signalSemaphoreCount) |
| 5466 | signalSemaphoresForQueueSubmit.append(buf: sp->signalSemaphores, sz: sp->signalSemaphoreCount); |
| 5467 | |
| 5468 | waitSemaphoresForPresent.clear(); |
| 5469 | if (sp->presentWaitSemaphoreCount) |
| 5470 | waitSemaphoresForPresent.append(buf: sp->presentWaitSemaphores, sz: sp->presentWaitSemaphoreCount); |
| 5471 | } |
| 5472 | |
| 5473 | void QRhiVulkan::releaseCachedResources() |
| 5474 | { |
| 5475 | releaseCachedResourcesCalledBeforeFrameStart = true; |
| 5476 | } |
| 5477 | |
| 5478 | bool QRhiVulkan::isDeviceLost() const |
| 5479 | { |
| 5480 | return deviceLost; |
| 5481 | } |
| 5482 | |
| 5483 | struct |
| 5484 | { |
| 5485 | quint32 ; |
| 5486 | quint32 ; |
| 5487 | quint32 ; |
| 5488 | quint32 ; |
| 5489 | quint32 ; |
| 5490 | quint32 ; |
| 5491 | quint32 ; |
| 5492 | quint32 ; |
| 5493 | }; |
| 5494 | |
| 5495 | QByteArray QRhiVulkan::pipelineCacheData() |
| 5496 | { |
| 5497 | Q_STATIC_ASSERT(sizeof(QVkPipelineCacheDataHeader) == 32); |
| 5498 | |
| 5499 | QByteArray data; |
| 5500 | if (!pipelineCache || !rhiFlags.testFlag(flag: QRhi::EnablePipelineCacheDataSave)) |
| 5501 | return data; |
| 5502 | |
| 5503 | size_t dataSize = 0; |
| 5504 | VkResult err = df->vkGetPipelineCacheData(dev, pipelineCache, &dataSize, nullptr); |
| 5505 | if (err != VK_SUCCESS) { |
| 5506 | qCDebug(QRHI_LOG_INFO, "Failed to get pipeline cache data size: %d" , err); |
| 5507 | return QByteArray(); |
| 5508 | } |
| 5509 | const size_t = sizeof(QVkPipelineCacheDataHeader); |
| 5510 | const size_t dataOffset = headerSize + VK_UUID_SIZE; |
| 5511 | data.resize(size: dataOffset + dataSize); |
| 5512 | err = df->vkGetPipelineCacheData(dev, pipelineCache, &dataSize, data.data() + dataOffset); |
| 5513 | if (err != VK_SUCCESS) { |
| 5514 | qCDebug(QRHI_LOG_INFO, "Failed to get pipeline cache data of %d bytes: %d" , int(dataSize), err); |
| 5515 | return QByteArray(); |
| 5516 | } |
| 5517 | |
| 5518 | QVkPipelineCacheDataHeader ; |
| 5519 | header.rhiId = pipelineCacheRhiId(); |
| 5520 | header.arch = quint32(sizeof(void*)); |
| 5521 | header.driverVersion = physDevProperties.driverVersion; |
| 5522 | header.vendorId = physDevProperties.vendorID; |
| 5523 | header.deviceId = physDevProperties.deviceID; |
| 5524 | header.dataSize = quint32(dataSize); |
| 5525 | header.uuidSize = VK_UUID_SIZE; |
| 5526 | header.reserved = 0; |
| 5527 | memcpy(dest: data.data(), src: &header, n: headerSize); |
| 5528 | memcpy(dest: data.data() + headerSize, src: physDevProperties.pipelineCacheUUID, VK_UUID_SIZE); |
| 5529 | |
| 5530 | return data; |
| 5531 | } |
| 5532 | |
| 5533 | void QRhiVulkan::setPipelineCacheData(const QByteArray &data) |
| 5534 | { |
| 5535 | if (data.isEmpty()) |
| 5536 | return; |
| 5537 | |
| 5538 | const size_t = sizeof(QVkPipelineCacheDataHeader); |
| 5539 | if (data.size() < qsizetype(headerSize)) { |
| 5540 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: Invalid blob size" ); |
| 5541 | return; |
| 5542 | } |
| 5543 | QVkPipelineCacheDataHeader ; |
| 5544 | memcpy(dest: &header, src: data.constData(), n: headerSize); |
| 5545 | |
| 5546 | const quint32 rhiId = pipelineCacheRhiId(); |
| 5547 | if (header.rhiId != rhiId) { |
| 5548 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: The data is for a different QRhi version or backend (%u, %u)" , |
| 5549 | rhiId, header.rhiId); |
| 5550 | return; |
| 5551 | } |
| 5552 | const quint32 arch = quint32(sizeof(void*)); |
| 5553 | if (header.arch != arch) { |
| 5554 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: Architecture does not match (%u, %u)" , |
| 5555 | arch, header.arch); |
| 5556 | return; |
| 5557 | } |
| 5558 | if (header.driverVersion != physDevProperties.driverVersion) { |
| 5559 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: driverVersion does not match (%u, %u)" , |
| 5560 | physDevProperties.driverVersion, header.driverVersion); |
| 5561 | return; |
| 5562 | } |
| 5563 | if (header.vendorId != physDevProperties.vendorID) { |
| 5564 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: vendorID does not match (%u, %u)" , |
| 5565 | physDevProperties.vendorID, header.vendorId); |
| 5566 | return; |
| 5567 | } |
| 5568 | if (header.deviceId != physDevProperties.deviceID) { |
| 5569 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: deviceID does not match (%u, %u)" , |
| 5570 | physDevProperties.deviceID, header.deviceId); |
| 5571 | return; |
| 5572 | } |
| 5573 | if (header.uuidSize != VK_UUID_SIZE) { |
| 5574 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: VK_UUID_SIZE does not match (%u, %u)" , |
| 5575 | quint32(VK_UUID_SIZE), header.uuidSize); |
| 5576 | return; |
| 5577 | } |
| 5578 | |
| 5579 | if (data.size() < qsizetype(headerSize + VK_UUID_SIZE)) { |
| 5580 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: Invalid blob, no uuid" ); |
| 5581 | return; |
| 5582 | } |
| 5583 | if (memcmp(s1: data.constData() + headerSize, s2: physDevProperties.pipelineCacheUUID, VK_UUID_SIZE)) { |
| 5584 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: pipelineCacheUUID does not match" ); |
| 5585 | return; |
| 5586 | } |
| 5587 | |
| 5588 | const size_t dataOffset = headerSize + VK_UUID_SIZE; |
| 5589 | if (data.size() < qsizetype(dataOffset + header.dataSize)) { |
| 5590 | qCDebug(QRHI_LOG_INFO, "setPipelineCacheData: Invalid blob, data missing" ); |
| 5591 | return; |
| 5592 | } |
| 5593 | |
| 5594 | if (pipelineCache) { |
| 5595 | df->vkDestroyPipelineCache(dev, pipelineCache, nullptr); |
| 5596 | pipelineCache = VK_NULL_HANDLE; |
| 5597 | } |
| 5598 | |
| 5599 | if (ensurePipelineCache(initialData: data.constData() + dataOffset, initialDataSize: header.dataSize)) { |
| 5600 | qCDebug(QRHI_LOG_INFO, "Created pipeline cache with initial data of %d bytes" , |
| 5601 | int(header.dataSize)); |
| 5602 | } else { |
| 5603 | qCDebug(QRHI_LOG_INFO, "Failed to create pipeline cache with initial data specified" ); |
| 5604 | } |
| 5605 | } |
| 5606 | |
| 5607 | QRhiRenderBuffer *QRhiVulkan::createRenderBuffer(QRhiRenderBuffer::Type type, const QSize &pixelSize, |
| 5608 | int sampleCount, QRhiRenderBuffer::Flags flags, |
| 5609 | QRhiTexture::Format backingFormatHint) |
| 5610 | { |
| 5611 | return new QVkRenderBuffer(this, type, pixelSize, sampleCount, flags, backingFormatHint); |
| 5612 | } |
| 5613 | |
| 5614 | QRhiTexture *QRhiVulkan::createTexture(QRhiTexture::Format format, |
| 5615 | const QSize &pixelSize, int depth, int arraySize, |
| 5616 | int sampleCount, QRhiTexture::Flags flags) |
| 5617 | { |
| 5618 | return new QVkTexture(this, format, pixelSize, depth, arraySize, sampleCount, flags); |
| 5619 | } |
| 5620 | |
| 5621 | QRhiSampler *QRhiVulkan::createSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter, |
| 5622 | QRhiSampler::Filter mipmapMode, |
| 5623 | QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w) |
| 5624 | { |
| 5625 | return new QVkSampler(this, magFilter, minFilter, mipmapMode, u, v, w); |
| 5626 | } |
| 5627 | |
| 5628 | QRhiShadingRateMap *QRhiVulkan::createShadingRateMap() |
| 5629 | { |
| 5630 | return new QVkShadingRateMap(this); |
| 5631 | } |
| 5632 | |
| 5633 | QRhiTextureRenderTarget *QRhiVulkan::createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc, |
| 5634 | QRhiTextureRenderTarget::Flags flags) |
| 5635 | { |
| 5636 | return new QVkTextureRenderTarget(this, desc, flags); |
| 5637 | } |
| 5638 | |
| 5639 | QRhiGraphicsPipeline *QRhiVulkan::createGraphicsPipeline() |
| 5640 | { |
| 5641 | return new QVkGraphicsPipeline(this); |
| 5642 | } |
| 5643 | |
| 5644 | QRhiComputePipeline *QRhiVulkan::createComputePipeline() |
| 5645 | { |
| 5646 | return new QVkComputePipeline(this); |
| 5647 | } |
| 5648 | |
| 5649 | QRhiShaderResourceBindings *QRhiVulkan::createShaderResourceBindings() |
| 5650 | { |
| 5651 | return new QVkShaderResourceBindings(this); |
| 5652 | } |
| 5653 | |
| 5654 | void QRhiVulkan::setGraphicsPipeline(QRhiCommandBuffer *cb, QRhiGraphicsPipeline *ps) |
| 5655 | { |
| 5656 | QVkGraphicsPipeline *psD = QRHI_RES(QVkGraphicsPipeline, ps); |
| 5657 | Q_ASSERT(psD->pipeline); |
| 5658 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 5659 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 5660 | |
| 5661 | if (cbD->currentGraphicsPipeline != ps || cbD->currentPipelineGeneration != psD->generation) { |
| 5662 | if (cbD->passUsesSecondaryCb) { |
| 5663 | df->vkCmdBindPipeline(cbD->activeSecondaryCbStack.last(), VK_PIPELINE_BIND_POINT_GRAPHICS, psD->pipeline); |
| 5664 | } else { |
| 5665 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 5666 | cmd.cmd = QVkCommandBuffer::Command::BindPipeline; |
| 5667 | cmd.args.bindPipeline.bindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 5668 | cmd.args.bindPipeline.pipeline = psD->pipeline; |
| 5669 | } |
| 5670 | |
| 5671 | cbD->currentGraphicsPipeline = ps; |
| 5672 | cbD->currentComputePipeline = nullptr; |
| 5673 | cbD->currentPipelineGeneration = psD->generation; |
| 5674 | } |
| 5675 | |
| 5676 | psD->lastActiveFrameSlot = currentFrameSlot; |
| 5677 | } |
| 5678 | |
| 5679 | void QRhiVulkan::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBindings *srb, |
| 5680 | int dynamicOffsetCount, |
| 5681 | const QRhiCommandBuffer::DynamicOffset *dynamicOffsets) |
| 5682 | { |
| 5683 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 5684 | Q_ASSERT(cbD->recordingPass != QVkCommandBuffer::NoPass); |
| 5685 | QRhiPassResourceTracker &passResTracker(cbD->passResTrackers[cbD->currentPassResTrackerIndex]); |
| 5686 | QVkGraphicsPipeline *gfxPsD = QRHI_RES(QVkGraphicsPipeline, cbD->currentGraphicsPipeline); |
| 5687 | QVkComputePipeline *compPsD = QRHI_RES(QVkComputePipeline, cbD->currentComputePipeline); |
| 5688 | |
| 5689 | if (!srb) { |
| 5690 | if (gfxPsD) |
| 5691 | srb = gfxPsD->m_shaderResourceBindings; |
| 5692 | else |
| 5693 | srb = compPsD->m_shaderResourceBindings; |
| 5694 | } |
| 5695 | |
| 5696 | QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, srb); |
| 5697 | auto &descSetBd(srbD->boundResourceData[currentFrameSlot]); |
| 5698 | bool rewriteDescSet = false; |
| 5699 | |
| 5700 | // Do host writes and mark referenced shader resources as in-use. |
| 5701 | // Also prepare to ensure the descriptor set we are going to bind refers to up-to-date Vk objects. |
| 5702 | for (int i = 0, ie = srbD->sortedBindings.size(); i != ie; ++i) { |
| 5703 | const QRhiShaderResourceBinding::Data *b = shaderResourceBindingData(binding&: srbD->sortedBindings[i]); |
| 5704 | QVkShaderResourceBindings::BoundResourceData &bd(descSetBd[i]); |
| 5705 | switch (b->type) { |
| 5706 | case QRhiShaderResourceBinding::UniformBuffer: |
| 5707 | { |
| 5708 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, b->u.ubuf.buf); |
| 5709 | Q_ASSERT(bufD->m_usage.testFlag(QRhiBuffer::UniformBuffer)); |
| 5710 | |
| 5711 | if (bufD->m_type == QRhiBuffer::Dynamic) |
| 5712 | executeBufferHostWritesForSlot(bufD, slot: currentFrameSlot); |
| 5713 | |
| 5714 | bufD->lastActiveFrameSlot = currentFrameSlot; |
| 5715 | trackedRegisterBuffer(passResTracker: &passResTracker, bufD, slot: bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0, |
| 5716 | access: QRhiPassResourceTracker::BufUniformRead, |
| 5717 | stage: QRhiPassResourceTracker::toPassTrackerBufferStage(stages: b->stage)); |
| 5718 | |
| 5719 | // Check both the "local" id (the generation counter) and the |
| 5720 | // global id. The latter is relevant when a newly allocated |
| 5721 | // QRhiResource ends up with the same pointer as a previous one. |
| 5722 | // (and that previous one could have been in an srb...) |
| 5723 | if (bufD->generation != bd.ubuf.generation || bufD->m_id != bd.ubuf.id) { |
| 5724 | rewriteDescSet = true; |
| 5725 | bd.ubuf.id = bufD->m_id; |
| 5726 | bd.ubuf.generation = bufD->generation; |
| 5727 | } |
| 5728 | } |
| 5729 | break; |
| 5730 | case QRhiShaderResourceBinding::SampledTexture: |
| 5731 | case QRhiShaderResourceBinding::Texture: |
| 5732 | case QRhiShaderResourceBinding::Sampler: |
| 5733 | { |
| 5734 | const QRhiShaderResourceBinding::Data::TextureAndOrSamplerData *data = &b->u.stex; |
| 5735 | if (bd.stex.count != data->count) { |
| 5736 | bd.stex.count = data->count; |
| 5737 | rewriteDescSet = true; |
| 5738 | } |
| 5739 | for (int elem = 0; elem < data->count; ++elem) { |
| 5740 | QVkTexture *texD = QRHI_RES(QVkTexture, data->texSamplers[elem].tex); |
| 5741 | QVkSampler *samplerD = QRHI_RES(QVkSampler, data->texSamplers[elem].sampler); |
| 5742 | // We use the same code path for both combined and separate |
| 5743 | // images and samplers, so tex or sampler (but not both) can be |
| 5744 | // null here. |
| 5745 | Q_ASSERT(texD || samplerD); |
| 5746 | if (texD) { |
| 5747 | texD->lastActiveFrameSlot = currentFrameSlot; |
| 5748 | trackedRegisterTexture(passResTracker: &passResTracker, texD, |
| 5749 | access: QRhiPassResourceTracker::TexSample, |
| 5750 | stage: QRhiPassResourceTracker::toPassTrackerTextureStage(stages: b->stage)); |
| 5751 | } |
| 5752 | if (samplerD) |
| 5753 | samplerD->lastActiveFrameSlot = currentFrameSlot; |
| 5754 | const quint64 texId = texD ? texD->m_id : 0; |
| 5755 | const uint texGen = texD ? texD->generation : 0; |
| 5756 | const quint64 samplerId = samplerD ? samplerD->m_id : 0; |
| 5757 | const uint samplerGen = samplerD ? samplerD->generation : 0; |
| 5758 | if (texGen != bd.stex.d[elem].texGeneration |
| 5759 | || texId != bd.stex.d[elem].texId |
| 5760 | || samplerGen != bd.stex.d[elem].samplerGeneration |
| 5761 | || samplerId != bd.stex.d[elem].samplerId) |
| 5762 | { |
| 5763 | rewriteDescSet = true; |
| 5764 | bd.stex.d[elem].texId = texId; |
| 5765 | bd.stex.d[elem].texGeneration = texGen; |
| 5766 | bd.stex.d[elem].samplerId = samplerId; |
| 5767 | bd.stex.d[elem].samplerGeneration = samplerGen; |
| 5768 | } |
| 5769 | } |
| 5770 | } |
| 5771 | break; |
| 5772 | case QRhiShaderResourceBinding::ImageLoad: |
| 5773 | case QRhiShaderResourceBinding::ImageStore: |
| 5774 | case QRhiShaderResourceBinding::ImageLoadStore: |
| 5775 | { |
| 5776 | QVkTexture *texD = QRHI_RES(QVkTexture, b->u.simage.tex); |
| 5777 | Q_ASSERT(texD->m_flags.testFlag(QRhiTexture::UsedWithLoadStore)); |
| 5778 | texD->lastActiveFrameSlot = currentFrameSlot; |
| 5779 | QRhiPassResourceTracker::TextureAccess access; |
| 5780 | if (b->type == QRhiShaderResourceBinding::ImageLoad) |
| 5781 | access = QRhiPassResourceTracker::TexStorageLoad; |
| 5782 | else if (b->type == QRhiShaderResourceBinding::ImageStore) |
| 5783 | access = QRhiPassResourceTracker::TexStorageStore; |
| 5784 | else |
| 5785 | access = QRhiPassResourceTracker::TexStorageLoadStore; |
| 5786 | trackedRegisterTexture(passResTracker: &passResTracker, texD, |
| 5787 | access, |
| 5788 | stage: QRhiPassResourceTracker::toPassTrackerTextureStage(stages: b->stage)); |
| 5789 | |
| 5790 | if (texD->generation != bd.simage.generation || texD->m_id != bd.simage.id) { |
| 5791 | rewriteDescSet = true; |
| 5792 | bd.simage.id = texD->m_id; |
| 5793 | bd.simage.generation = texD->generation; |
| 5794 | } |
| 5795 | } |
| 5796 | break; |
| 5797 | case QRhiShaderResourceBinding::BufferLoad: |
| 5798 | case QRhiShaderResourceBinding::BufferStore: |
| 5799 | case QRhiShaderResourceBinding::BufferLoadStore: |
| 5800 | { |
| 5801 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, b->u.sbuf.buf); |
| 5802 | Q_ASSERT(bufD->m_usage.testFlag(QRhiBuffer::StorageBuffer)); |
| 5803 | |
| 5804 | if (bufD->m_type == QRhiBuffer::Dynamic) |
| 5805 | executeBufferHostWritesForSlot(bufD, slot: currentFrameSlot); |
| 5806 | |
| 5807 | bufD->lastActiveFrameSlot = currentFrameSlot; |
| 5808 | QRhiPassResourceTracker::BufferAccess access; |
| 5809 | if (b->type == QRhiShaderResourceBinding::BufferLoad) |
| 5810 | access = QRhiPassResourceTracker::BufStorageLoad; |
| 5811 | else if (b->type == QRhiShaderResourceBinding::BufferStore) |
| 5812 | access = QRhiPassResourceTracker::BufStorageStore; |
| 5813 | else |
| 5814 | access = QRhiPassResourceTracker::BufStorageLoadStore; |
| 5815 | trackedRegisterBuffer(passResTracker: &passResTracker, bufD, slot: bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0, |
| 5816 | access, |
| 5817 | stage: QRhiPassResourceTracker::toPassTrackerBufferStage(stages: b->stage)); |
| 5818 | |
| 5819 | if (bufD->generation != bd.sbuf.generation || bufD->m_id != bd.sbuf.id) { |
| 5820 | rewriteDescSet = true; |
| 5821 | bd.sbuf.id = bufD->m_id; |
| 5822 | bd.sbuf.generation = bufD->generation; |
| 5823 | } |
| 5824 | } |
| 5825 | break; |
| 5826 | default: |
| 5827 | Q_UNREACHABLE(); |
| 5828 | break; |
| 5829 | } |
| 5830 | } |
| 5831 | |
| 5832 | // write descriptor sets, if needed |
| 5833 | if (rewriteDescSet) |
| 5834 | updateShaderResourceBindings(srb); |
| 5835 | |
| 5836 | // make sure the descriptors for the correct slot will get bound. |
| 5837 | // also, dynamic offsets always need a bind. |
| 5838 | const bool forceRebind = cbD->currentDescSetSlot != currentFrameSlot || srbD->hasDynamicOffset; |
| 5839 | |
| 5840 | const bool srbChanged = gfxPsD ? (cbD->currentGraphicsSrb != srb) : (cbD->currentComputeSrb != srb); |
| 5841 | |
| 5842 | if (forceRebind || rewriteDescSet || srbChanged || cbD->currentSrbGeneration != srbD->generation) { |
| 5843 | QVarLengthArray<uint32_t, 4> dynOfs; |
| 5844 | if (srbD->hasDynamicOffset) { |
| 5845 | // Filling out dynOfs based on the sorted bindings is important |
| 5846 | // because dynOfs has to be ordered based on the binding numbers, |
| 5847 | // and neither srb nor dynamicOffsets has any such ordering |
| 5848 | // requirement. |
| 5849 | for (const QRhiShaderResourceBinding &binding : std::as_const(t&: srbD->sortedBindings)) { |
| 5850 | const QRhiShaderResourceBinding::Data *b = shaderResourceBindingData(binding); |
| 5851 | if (b->type == QRhiShaderResourceBinding::UniformBuffer && b->u.ubuf.hasDynamicOffset) { |
| 5852 | uint32_t offset = 0; |
| 5853 | for (int i = 0; i < dynamicOffsetCount; ++i) { |
| 5854 | const QRhiCommandBuffer::DynamicOffset &bindingOffsetPair(dynamicOffsets[i]); |
| 5855 | if (bindingOffsetPair.first == b->binding) { |
| 5856 | offset = bindingOffsetPair.second; |
| 5857 | break; |
| 5858 | } |
| 5859 | } |
| 5860 | dynOfs.append(t: offset); // use 0 if dynamicOffsets did not contain this binding |
| 5861 | } |
| 5862 | } |
| 5863 | } |
| 5864 | |
| 5865 | if (cbD->passUsesSecondaryCb) { |
| 5866 | df->vkCmdBindDescriptorSets(cbD->activeSecondaryCbStack.last(), |
| 5867 | gfxPsD ? VK_PIPELINE_BIND_POINT_GRAPHICS : VK_PIPELINE_BIND_POINT_COMPUTE, |
| 5868 | gfxPsD ? gfxPsD->layout : compPsD->layout, |
| 5869 | 0, 1, &srbD->descSets[currentFrameSlot], |
| 5870 | uint32_t(dynOfs.size()), |
| 5871 | dynOfs.size() ? dynOfs.constData() : nullptr); |
| 5872 | } else { |
| 5873 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 5874 | cmd.cmd = QVkCommandBuffer::Command::BindDescriptorSet; |
| 5875 | cmd.args.bindDescriptorSet.bindPoint = gfxPsD ? VK_PIPELINE_BIND_POINT_GRAPHICS |
| 5876 | : VK_PIPELINE_BIND_POINT_COMPUTE; |
| 5877 | cmd.args.bindDescriptorSet.pipelineLayout = gfxPsD ? gfxPsD->layout : compPsD->layout; |
| 5878 | cmd.args.bindDescriptorSet.descSet = srbD->descSets[currentFrameSlot]; |
| 5879 | cmd.args.bindDescriptorSet.dynamicOffsetCount = dynOfs.size(); |
| 5880 | cmd.args.bindDescriptorSet.dynamicOffsetIndex = cbD->pools.dynamicOffset.size(); |
| 5881 | cbD->pools.dynamicOffset.append(buf: dynOfs.constData(), sz: dynOfs.size()); |
| 5882 | } |
| 5883 | |
| 5884 | if (gfxPsD) { |
| 5885 | cbD->currentGraphicsSrb = srb; |
| 5886 | cbD->currentComputeSrb = nullptr; |
| 5887 | } else { |
| 5888 | cbD->currentGraphicsSrb = nullptr; |
| 5889 | cbD->currentComputeSrb = srb; |
| 5890 | } |
| 5891 | cbD->currentSrbGeneration = srbD->generation; |
| 5892 | cbD->currentDescSetSlot = currentFrameSlot; |
| 5893 | } |
| 5894 | |
| 5895 | srbD->lastActiveFrameSlot = currentFrameSlot; |
| 5896 | } |
| 5897 | |
| 5898 | void QRhiVulkan::setVertexInput(QRhiCommandBuffer *cb, |
| 5899 | int startBinding, int bindingCount, const QRhiCommandBuffer::VertexInput *bindings, |
| 5900 | QRhiBuffer *indexBuf, quint32 indexOffset, QRhiCommandBuffer::IndexFormat indexFormat) |
| 5901 | { |
| 5902 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 5903 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 5904 | QRhiPassResourceTracker &passResTracker(cbD->passResTrackers[cbD->currentPassResTrackerIndex]); |
| 5905 | |
| 5906 | bool needsBindVBuf = false; |
| 5907 | for (int i = 0; i < bindingCount; ++i) { |
| 5908 | const int inputSlot = startBinding + i; |
| 5909 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, bindings[i].first); |
| 5910 | Q_ASSERT(bufD->m_usage.testFlag(QRhiBuffer::VertexBuffer)); |
| 5911 | bufD->lastActiveFrameSlot = currentFrameSlot; |
| 5912 | if (bufD->m_type == QRhiBuffer::Dynamic) |
| 5913 | executeBufferHostWritesForSlot(bufD, slot: currentFrameSlot); |
| 5914 | |
| 5915 | const VkBuffer vkvertexbuf = bufD->buffers[bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0]; |
| 5916 | if (cbD->currentVertexBuffers[inputSlot] != vkvertexbuf |
| 5917 | || cbD->currentVertexOffsets[inputSlot] != bindings[i].second) |
| 5918 | { |
| 5919 | needsBindVBuf = true; |
| 5920 | cbD->currentVertexBuffers[inputSlot] = vkvertexbuf; |
| 5921 | cbD->currentVertexOffsets[inputSlot] = bindings[i].second; |
| 5922 | } |
| 5923 | } |
| 5924 | |
| 5925 | if (needsBindVBuf) { |
| 5926 | QVarLengthArray<VkBuffer, 4> bufs; |
| 5927 | QVarLengthArray<VkDeviceSize, 4> ofs; |
| 5928 | for (int i = 0; i < bindingCount; ++i) { |
| 5929 | QVkBuffer *bufD = QRHI_RES(QVkBuffer, bindings[i].first); |
| 5930 | const int slot = bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0; |
| 5931 | bufs.append(t: bufD->buffers[slot]); |
| 5932 | ofs.append(t: bindings[i].second); |
| 5933 | trackedRegisterBuffer(passResTracker: &passResTracker, bufD, slot, |
| 5934 | access: QRhiPassResourceTracker::BufVertexInput, |
| 5935 | stage: QRhiPassResourceTracker::BufVertexInputStage); |
| 5936 | } |
| 5937 | |
| 5938 | if (cbD->passUsesSecondaryCb) { |
| 5939 | df->vkCmdBindVertexBuffers(cbD->activeSecondaryCbStack.last(), uint32_t(startBinding), |
| 5940 | uint32_t(bufs.size()), bufs.constData(), ofs.constData()); |
| 5941 | } else { |
| 5942 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 5943 | cmd.cmd = QVkCommandBuffer::Command::BindVertexBuffer; |
| 5944 | cmd.args.bindVertexBuffer.startBinding = startBinding; |
| 5945 | cmd.args.bindVertexBuffer.count = bufs.size(); |
| 5946 | cmd.args.bindVertexBuffer.vertexBufferIndex = cbD->pools.vertexBuffer.size(); |
| 5947 | cbD->pools.vertexBuffer.append(buf: bufs.constData(), sz: bufs.size()); |
| 5948 | cmd.args.bindVertexBuffer.vertexBufferOffsetIndex = cbD->pools.vertexBufferOffset.size(); |
| 5949 | cbD->pools.vertexBufferOffset.append(buf: ofs.constData(), sz: ofs.size()); |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | if (indexBuf) { |
| 5954 | QVkBuffer *ibufD = QRHI_RES(QVkBuffer, indexBuf); |
| 5955 | Q_ASSERT(ibufD->m_usage.testFlag(QRhiBuffer::IndexBuffer)); |
| 5956 | ibufD->lastActiveFrameSlot = currentFrameSlot; |
| 5957 | if (ibufD->m_type == QRhiBuffer::Dynamic) |
| 5958 | executeBufferHostWritesForSlot(bufD: ibufD, slot: currentFrameSlot); |
| 5959 | |
| 5960 | const int slot = ibufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0; |
| 5961 | const VkBuffer vkindexbuf = ibufD->buffers[slot]; |
| 5962 | const VkIndexType type = indexFormat == QRhiCommandBuffer::IndexUInt16 ? VK_INDEX_TYPE_UINT16 |
| 5963 | : VK_INDEX_TYPE_UINT32; |
| 5964 | |
| 5965 | if (cbD->currentIndexBuffer != vkindexbuf |
| 5966 | || cbD->currentIndexOffset != indexOffset |
| 5967 | || cbD->currentIndexFormat != type) |
| 5968 | { |
| 5969 | cbD->currentIndexBuffer = vkindexbuf; |
| 5970 | cbD->currentIndexOffset = indexOffset; |
| 5971 | cbD->currentIndexFormat = type; |
| 5972 | |
| 5973 | if (cbD->passUsesSecondaryCb) { |
| 5974 | df->vkCmdBindIndexBuffer(cbD->activeSecondaryCbStack.last(), vkindexbuf, indexOffset, type); |
| 5975 | } else { |
| 5976 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 5977 | cmd.cmd = QVkCommandBuffer::Command::BindIndexBuffer; |
| 5978 | cmd.args.bindIndexBuffer.buf = vkindexbuf; |
| 5979 | cmd.args.bindIndexBuffer.ofs = indexOffset; |
| 5980 | cmd.args.bindIndexBuffer.type = type; |
| 5981 | } |
| 5982 | |
| 5983 | trackedRegisterBuffer(passResTracker: &passResTracker, bufD: ibufD, slot, |
| 5984 | access: QRhiPassResourceTracker::BufIndexRead, |
| 5985 | stage: QRhiPassResourceTracker::BufVertexInputStage); |
| 5986 | } |
| 5987 | } |
| 5988 | } |
| 5989 | |
| 5990 | void QRhiVulkan::setViewport(QRhiCommandBuffer *cb, const QRhiViewport &viewport) |
| 5991 | { |
| 5992 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 5993 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 5994 | const QSize outputSize = cbD->currentTarget->pixelSize(); |
| 5995 | |
| 5996 | // x,y is top-left in VkViewport but bottom-left in QRhiViewport |
| 5997 | float x, y, w, h; |
| 5998 | if (!qrhi_toTopLeftRenderTargetRect<UnBounded>(outputSize, r: viewport.viewport(), x: &x, y: &y, w: &w, h: &h)) |
| 5999 | return; |
| 6000 | |
| 6001 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6002 | VkViewport *vp = &cmd.args.setViewport.viewport; |
| 6003 | vp->x = x; |
| 6004 | vp->y = y; |
| 6005 | vp->width = w; |
| 6006 | vp->height = h; |
| 6007 | vp->minDepth = viewport.minDepth(); |
| 6008 | vp->maxDepth = viewport.maxDepth(); |
| 6009 | |
| 6010 | if (cbD->passUsesSecondaryCb) { |
| 6011 | df->vkCmdSetViewport(cbD->activeSecondaryCbStack.last(), 0, 1, vp); |
| 6012 | cbD->commands.unget(); |
| 6013 | } else { |
| 6014 | cmd.cmd = QVkCommandBuffer::Command::SetViewport; |
| 6015 | } |
| 6016 | |
| 6017 | if (cbD->currentGraphicsPipeline |
| 6018 | && !QRHI_RES(QVkGraphicsPipeline, cbD->currentGraphicsPipeline)->m_flags.testFlag(flag: QRhiGraphicsPipeline::UsesScissor)) |
| 6019 | { |
| 6020 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6021 | VkRect2D *s = &cmd.args.setScissor.scissor; |
| 6022 | qrhi_toTopLeftRenderTargetRect<Bounded>(outputSize, r: viewport.viewport(), x: &x, y: &y, w: &w, h: &h); |
| 6023 | s->offset.x = int32_t(x); |
| 6024 | s->offset.y = int32_t(y); |
| 6025 | s->extent.width = uint32_t(w); |
| 6026 | s->extent.height = uint32_t(h); |
| 6027 | if (cbD->passUsesSecondaryCb) { |
| 6028 | df->vkCmdSetScissor(cbD->activeSecondaryCbStack.last(), 0, 1, s); |
| 6029 | cbD->commands.unget(); |
| 6030 | } else { |
| 6031 | cmd.cmd = QVkCommandBuffer::Command::SetScissor; |
| 6032 | } |
| 6033 | } |
| 6034 | } |
| 6035 | |
| 6036 | void QRhiVulkan::setScissor(QRhiCommandBuffer *cb, const QRhiScissor &scissor) |
| 6037 | { |
| 6038 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6039 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 6040 | Q_ASSERT(!cbD->currentGraphicsPipeline |
| 6041 | || QRHI_RES(QVkGraphicsPipeline, cbD->currentGraphicsPipeline) |
| 6042 | ->m_flags.testFlag(QRhiGraphicsPipeline::UsesScissor)); |
| 6043 | const QSize outputSize = cbD->currentTarget->pixelSize(); |
| 6044 | |
| 6045 | // x,y is top-left in VkRect2D but bottom-left in QRhiScissor |
| 6046 | int x, y, w, h; |
| 6047 | if (!qrhi_toTopLeftRenderTargetRect<Bounded>(outputSize, r: scissor.scissor(), x: &x, y: &y, w: &w, h: &h)) |
| 6048 | return; |
| 6049 | |
| 6050 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6051 | VkRect2D *s = &cmd.args.setScissor.scissor; |
| 6052 | s->offset.x = x; |
| 6053 | s->offset.y = y; |
| 6054 | s->extent.width = uint32_t(w); |
| 6055 | s->extent.height = uint32_t(h); |
| 6056 | |
| 6057 | if (cbD->passUsesSecondaryCb) { |
| 6058 | df->vkCmdSetScissor(cbD->activeSecondaryCbStack.last(), 0, 1, s); |
| 6059 | cbD->commands.unget(); |
| 6060 | } else { |
| 6061 | cmd.cmd = QVkCommandBuffer::Command::SetScissor; |
| 6062 | } |
| 6063 | } |
| 6064 | |
| 6065 | void QRhiVulkan::setBlendConstants(QRhiCommandBuffer *cb, const QColor &c) |
| 6066 | { |
| 6067 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6068 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 6069 | |
| 6070 | if (cbD->passUsesSecondaryCb) { |
| 6071 | float constants[] = { float(c.redF()), float(c.greenF()), float(c.blueF()), float(c.alphaF()) }; |
| 6072 | df->vkCmdSetBlendConstants(cbD->activeSecondaryCbStack.last(), constants); |
| 6073 | } else { |
| 6074 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6075 | cmd.cmd = QVkCommandBuffer::Command::SetBlendConstants; |
| 6076 | cmd.args.setBlendConstants.c[0] = c.redF(); |
| 6077 | cmd.args.setBlendConstants.c[1] = c.greenF(); |
| 6078 | cmd.args.setBlendConstants.c[2] = c.blueF(); |
| 6079 | cmd.args.setBlendConstants.c[3] = c.alphaF(); |
| 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | void QRhiVulkan::setStencilRef(QRhiCommandBuffer *cb, quint32 refValue) |
| 6084 | { |
| 6085 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6086 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 6087 | |
| 6088 | if (cbD->passUsesSecondaryCb) { |
| 6089 | df->vkCmdSetStencilReference(cbD->activeSecondaryCbStack.last(), VK_STENCIL_FRONT_AND_BACK, refValue); |
| 6090 | } else { |
| 6091 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6092 | cmd.cmd = QVkCommandBuffer::Command::SetStencilRef; |
| 6093 | cmd.args.setStencilRef.ref = refValue; |
| 6094 | } |
| 6095 | } |
| 6096 | |
| 6097 | void QRhiVulkan::setShadingRate(QRhiCommandBuffer *cb, const QSize &coarsePixelSize) |
| 6098 | { |
| 6099 | #ifdef VK_KHR_fragment_shading_rate |
| 6100 | if (!vkCmdSetFragmentShadingRateKHR) |
| 6101 | return; |
| 6102 | |
| 6103 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6104 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 6105 | Q_ASSERT(!cbD->currentGraphicsPipeline || QRHI_RES(QVkGraphicsPipeline, cbD->currentGraphicsPipeline)->m_flags.testFlag(QRhiGraphicsPipeline::UsesShadingRate)); |
| 6106 | |
| 6107 | VkFragmentShadingRateCombinerOpKHR ops[2] = { |
| 6108 | VK_FRAGMENT_SHADING_RATE_COMBINER_OP_MAX_KHR, |
| 6109 | VK_FRAGMENT_SHADING_RATE_COMBINER_OP_MAX_KHR |
| 6110 | }; |
| 6111 | VkExtent2D size = { .width: uint32_t(coarsePixelSize.width()), .height: uint32_t(coarsePixelSize.height()) }; |
| 6112 | if (cbD->passUsesSecondaryCb) { |
| 6113 | vkCmdSetFragmentShadingRateKHR(cbD->activeSecondaryCbStack.last(), &size, ops); |
| 6114 | } else { |
| 6115 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6116 | cmd.cmd = QVkCommandBuffer::Command::SetShadingRate; |
| 6117 | cmd.args.setShadingRate.w = size.width; |
| 6118 | cmd.args.setShadingRate.h = size.height; |
| 6119 | } |
| 6120 | if (coarsePixelSize.width() != 1 || coarsePixelSize.height() != 1) |
| 6121 | cbD->hasShadingRateSet = true; |
| 6122 | #else |
| 6123 | Q_UNUSED(cb); |
| 6124 | Q_UNUSED(coarsePixelSize); |
| 6125 | #endif |
| 6126 | } |
| 6127 | |
| 6128 | void QRhiVulkan::draw(QRhiCommandBuffer *cb, quint32 vertexCount, |
| 6129 | quint32 instanceCount, quint32 firstVertex, quint32 firstInstance) |
| 6130 | { |
| 6131 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6132 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 6133 | |
| 6134 | if (cbD->passUsesSecondaryCb) { |
| 6135 | df->vkCmdDraw(cbD->activeSecondaryCbStack.last(), vertexCount, instanceCount, firstVertex, firstInstance); |
| 6136 | } else { |
| 6137 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6138 | cmd.cmd = QVkCommandBuffer::Command::Draw; |
| 6139 | cmd.args.draw.vertexCount = vertexCount; |
| 6140 | cmd.args.draw.instanceCount = instanceCount; |
| 6141 | cmd.args.draw.firstVertex = firstVertex; |
| 6142 | cmd.args.draw.firstInstance = firstInstance; |
| 6143 | } |
| 6144 | } |
| 6145 | |
| 6146 | void QRhiVulkan::drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount, |
| 6147 | quint32 instanceCount, quint32 firstIndex, qint32 vertexOffset, quint32 firstInstance) |
| 6148 | { |
| 6149 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6150 | Q_ASSERT(cbD->recordingPass == QVkCommandBuffer::RenderPass); |
| 6151 | |
| 6152 | if (cbD->passUsesSecondaryCb) { |
| 6153 | df->vkCmdDrawIndexed(cbD->activeSecondaryCbStack.last(), indexCount, instanceCount, |
| 6154 | firstIndex, vertexOffset, firstInstance); |
| 6155 | } else { |
| 6156 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6157 | cmd.cmd = QVkCommandBuffer::Command::DrawIndexed; |
| 6158 | cmd.args.drawIndexed.indexCount = indexCount; |
| 6159 | cmd.args.drawIndexed.instanceCount = instanceCount; |
| 6160 | cmd.args.drawIndexed.firstIndex = firstIndex; |
| 6161 | cmd.args.drawIndexed.vertexOffset = vertexOffset; |
| 6162 | cmd.args.drawIndexed.firstInstance = firstInstance; |
| 6163 | } |
| 6164 | } |
| 6165 | |
| 6166 | void QRhiVulkan::debugMarkBegin(QRhiCommandBuffer *cb, const QByteArray &name) |
| 6167 | { |
| 6168 | #ifdef VK_EXT_debug_utils |
| 6169 | if (!debugMarkers || !caps.debugUtils) |
| 6170 | return; |
| 6171 | |
| 6172 | VkDebugUtilsLabelEXT label = {}; |
| 6173 | label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT; |
| 6174 | |
| 6175 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6176 | if (cbD->recordingPass != QVkCommandBuffer::NoPass && cbD->passUsesSecondaryCb) { |
| 6177 | label.pLabelName = name.constData(); |
| 6178 | vkCmdBeginDebugUtilsLabelEXT(cbD->activeSecondaryCbStack.last(), &label); |
| 6179 | } else { |
| 6180 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6181 | cmd.cmd = QVkCommandBuffer::Command::DebugMarkerBegin; |
| 6182 | cmd.args.debugMarkerBegin.label = label; |
| 6183 | cmd.args.debugMarkerBegin.labelNameIndex = cbD->pools.debugMarkerData.size(); |
| 6184 | cbD->pools.debugMarkerData.append(t: name); |
| 6185 | } |
| 6186 | #else |
| 6187 | Q_UNUSED(cb); |
| 6188 | Q_UNUSED(name); |
| 6189 | #endif |
| 6190 | } |
| 6191 | |
| 6192 | void QRhiVulkan::debugMarkEnd(QRhiCommandBuffer *cb) |
| 6193 | { |
| 6194 | #ifdef VK_EXT_debug_utils |
| 6195 | if (!debugMarkers || !caps.debugUtils) |
| 6196 | return; |
| 6197 | |
| 6198 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6199 | if (cbD->recordingPass != QVkCommandBuffer::NoPass && cbD->passUsesSecondaryCb) { |
| 6200 | vkCmdEndDebugUtilsLabelEXT(cbD->activeSecondaryCbStack.last()); |
| 6201 | } else { |
| 6202 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6203 | cmd.cmd = QVkCommandBuffer::Command::DebugMarkerEnd; |
| 6204 | } |
| 6205 | #else |
| 6206 | Q_UNUSED(cb); |
| 6207 | #endif |
| 6208 | } |
| 6209 | |
| 6210 | void QRhiVulkan::debugMarkMsg(QRhiCommandBuffer *cb, const QByteArray &msg) |
| 6211 | { |
| 6212 | #ifdef VK_EXT_debug_utils |
| 6213 | if (!debugMarkers || !caps.debugUtils) |
| 6214 | return; |
| 6215 | |
| 6216 | VkDebugUtilsLabelEXT label = {}; |
| 6217 | label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT; |
| 6218 | |
| 6219 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6220 | if (cbD->recordingPass != QVkCommandBuffer::NoPass && cbD->passUsesSecondaryCb) { |
| 6221 | label.pLabelName = msg.constData(); |
| 6222 | vkCmdInsertDebugUtilsLabelEXT(cbD->activeSecondaryCbStack.last(), &label); |
| 6223 | } else { |
| 6224 | QVkCommandBuffer::Command &cmd(cbD->commands.get()); |
| 6225 | cmd.cmd = QVkCommandBuffer::Command::DebugMarkerInsert; |
| 6226 | cmd.args.debugMarkerInsert.label = label; |
| 6227 | cmd.args.debugMarkerInsert.labelNameIndex = cbD->pools.debugMarkerData.size(); |
| 6228 | cbD->pools.debugMarkerData.append(t: msg); |
| 6229 | } |
| 6230 | #else |
| 6231 | Q_UNUSED(cb); |
| 6232 | Q_UNUSED(msg); |
| 6233 | #endif |
| 6234 | } |
| 6235 | |
| 6236 | const QRhiNativeHandles *QRhiVulkan::nativeHandles(QRhiCommandBuffer *cb) |
| 6237 | { |
| 6238 | return QRHI_RES(QVkCommandBuffer, cb)->nativeHandles(); |
| 6239 | } |
| 6240 | |
| 6241 | static inline QVkRenderTargetData *maybeRenderTargetData(QVkCommandBuffer *cbD) |
| 6242 | { |
| 6243 | Q_ASSERT(cbD->currentTarget); |
| 6244 | QVkRenderTargetData *rtD = nullptr; |
| 6245 | if (cbD->recordingPass == QVkCommandBuffer::RenderPass) { |
| 6246 | switch (cbD->currentTarget->resourceType()) { |
| 6247 | case QRhiResource::SwapChainRenderTarget: |
| 6248 | rtD = &QRHI_RES(QVkSwapChainRenderTarget, cbD->currentTarget)->d; |
| 6249 | break; |
| 6250 | case QRhiResource::TextureRenderTarget: |
| 6251 | rtD = &QRHI_RES(QVkTextureRenderTarget, cbD->currentTarget)->d; |
| 6252 | break; |
| 6253 | default: |
| 6254 | Q_UNREACHABLE(); |
| 6255 | break; |
| 6256 | } |
| 6257 | } |
| 6258 | return rtD; |
| 6259 | } |
| 6260 | |
| 6261 | void QRhiVulkan::beginExternal(QRhiCommandBuffer *cb) |
| 6262 | { |
| 6263 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6264 | |
| 6265 | // When not in a pass, it is simple: record what we have (but do not |
| 6266 | // submit), the cb can then be used to record more external commands. |
| 6267 | if (cbD->recordingPass == QVkCommandBuffer::NoPass) { |
| 6268 | recordPrimaryCommandBuffer(cbD); |
| 6269 | cbD->resetCommands(); |
| 6270 | return; |
| 6271 | } |
| 6272 | |
| 6273 | // Otherwise, inside a pass, have a secondary command buffer (with |
| 6274 | // RENDER_PASS_CONTINUE). Using the main one is not acceptable since we |
| 6275 | // cannot just record at this stage, that would mess up the resource |
| 6276 | // tracking and commands like TransitionPassResources. |
| 6277 | |
| 6278 | if (cbD->inExternal) |
| 6279 | return; |
| 6280 | |
| 6281 | if (!cbD->passUsesSecondaryCb) { |
| 6282 | qWarning(msg: "beginExternal() within a pass is only supported with secondary command buffers. " |
| 6283 | "This can be enabled by passing QRhiCommandBuffer::ExternalContent to beginPass()." ); |
| 6284 | return; |
| 6285 | } |
| 6286 | |
| 6287 | VkCommandBuffer secondaryCb = cbD->activeSecondaryCbStack.last(); |
| 6288 | cbD->activeSecondaryCbStack.removeLast(); |
| 6289 | endAndEnqueueSecondaryCommandBuffer(cb: secondaryCb, cbD); |
| 6290 | |
| 6291 | VkCommandBuffer extCb = startSecondaryCommandBuffer(rtD: maybeRenderTargetData(cbD)); |
| 6292 | if (extCb) { |
| 6293 | cbD->activeSecondaryCbStack.append(t: extCb); |
| 6294 | cbD->inExternal = true; |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | void QRhiVulkan::endExternal(QRhiCommandBuffer *cb) |
| 6299 | { |
| 6300 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6301 | |
| 6302 | if (cbD->recordingPass == QVkCommandBuffer::NoPass) { |
| 6303 | Q_ASSERT(cbD->commands.isEmpty() && cbD->currentPassResTrackerIndex == -1); |
| 6304 | } else if (cbD->inExternal) { |
| 6305 | VkCommandBuffer extCb = cbD->activeSecondaryCbStack.last(); |
| 6306 | cbD->activeSecondaryCbStack.removeLast(); |
| 6307 | endAndEnqueueSecondaryCommandBuffer(cb: extCb, cbD); |
| 6308 | cbD->activeSecondaryCbStack.append(t: startSecondaryCommandBuffer(rtD: maybeRenderTargetData(cbD))); |
| 6309 | } |
| 6310 | |
| 6311 | cbD->resetPerPassState(); |
| 6312 | } |
| 6313 | |
| 6314 | double QRhiVulkan::lastCompletedGpuTime(QRhiCommandBuffer *cb) |
| 6315 | { |
| 6316 | QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb); |
| 6317 | return cbD->lastGpuTime; |
| 6318 | } |
| 6319 | |
| 6320 | void QRhiVulkan::setAllocationName(QVkAlloc allocation, const QByteArray &name, int slot) |
| 6321 | { |
| 6322 | if (!debugMarkers || name.isEmpty()) |
| 6323 | return; |
| 6324 | |
| 6325 | QByteArray decoratedName = name; |
| 6326 | if (slot >= 0) { |
| 6327 | decoratedName += '/'; |
| 6328 | decoratedName += QByteArray::number(slot); |
| 6329 | } |
| 6330 | vmaSetAllocationName(allocator: toVmaAllocator(a: allocator), allocation: toVmaAllocation(a: allocation), pName: decoratedName.constData()); |
| 6331 | } |
| 6332 | |
| 6333 | void QRhiVulkan::setObjectName(uint64_t object, VkObjectType type, const QByteArray &name, int slot) |
| 6334 | { |
| 6335 | #ifdef VK_EXT_debug_utils |
| 6336 | if (!debugMarkers || !caps.debugUtils || name.isEmpty()) |
| 6337 | return; |
| 6338 | |
| 6339 | VkDebugUtilsObjectNameInfoEXT nameInfo = {}; |
| 6340 | nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT; |
| 6341 | nameInfo.objectType = type; |
| 6342 | nameInfo.objectHandle = object; |
| 6343 | QByteArray decoratedName = name; |
| 6344 | if (slot >= 0) { |
| 6345 | decoratedName += '/'; |
| 6346 | decoratedName += QByteArray::number(slot); |
| 6347 | } |
| 6348 | nameInfo.pObjectName = decoratedName.constData(); |
| 6349 | vkSetDebugUtilsObjectNameEXT(dev, &nameInfo); |
| 6350 | #else |
| 6351 | Q_UNUSED(object); |
| 6352 | Q_UNUSED(type); |
| 6353 | Q_UNUSED(name); |
| 6354 | Q_UNUSED(slot); |
| 6355 | #endif |
| 6356 | } |
| 6357 | |
| 6358 | static inline VkBufferUsageFlagBits toVkBufferUsage(QRhiBuffer::UsageFlags usage) |
| 6359 | { |
| 6360 | int u = 0; |
| 6361 | if (usage.testFlag(flag: QRhiBuffer::VertexBuffer)) |
| 6362 | u |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; |
| 6363 | if (usage.testFlag(flag: QRhiBuffer::IndexBuffer)) |
| 6364 | u |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT; |
| 6365 | if (usage.testFlag(flag: QRhiBuffer::UniformBuffer)) |
| 6366 | u |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
| 6367 | if (usage.testFlag(flag: QRhiBuffer::StorageBuffer)) |
| 6368 | u |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 6369 | return VkBufferUsageFlagBits(u); |
| 6370 | } |
| 6371 | |
| 6372 | static inline VkFilter toVkFilter(QRhiSampler::Filter f) |
| 6373 | { |
| 6374 | switch (f) { |
| 6375 | case QRhiSampler::Nearest: |
| 6376 | return VK_FILTER_NEAREST; |
| 6377 | case QRhiSampler::Linear: |
| 6378 | return VK_FILTER_LINEAR; |
| 6379 | default: |
| 6380 | Q_UNREACHABLE_RETURN(VK_FILTER_NEAREST); |
| 6381 | } |
| 6382 | } |
| 6383 | |
| 6384 | static inline VkSamplerMipmapMode toVkMipmapMode(QRhiSampler::Filter f) |
| 6385 | { |
| 6386 | switch (f) { |
| 6387 | case QRhiSampler::None: |
| 6388 | return VK_SAMPLER_MIPMAP_MODE_NEAREST; |
| 6389 | case QRhiSampler::Nearest: |
| 6390 | return VK_SAMPLER_MIPMAP_MODE_NEAREST; |
| 6391 | case QRhiSampler::Linear: |
| 6392 | return VK_SAMPLER_MIPMAP_MODE_LINEAR; |
| 6393 | default: |
| 6394 | Q_UNREACHABLE_RETURN(VK_SAMPLER_MIPMAP_MODE_NEAREST); |
| 6395 | } |
| 6396 | } |
| 6397 | |
| 6398 | static inline VkSamplerAddressMode toVkAddressMode(QRhiSampler::AddressMode m) |
| 6399 | { |
| 6400 | switch (m) { |
| 6401 | case QRhiSampler::Repeat: |
| 6402 | return VK_SAMPLER_ADDRESS_MODE_REPEAT; |
| 6403 | case QRhiSampler::ClampToEdge: |
| 6404 | return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; |
| 6405 | case QRhiSampler::Mirror: |
| 6406 | return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT; |
| 6407 | default: |
| 6408 | Q_UNREACHABLE_RETURN(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE); |
| 6409 | } |
| 6410 | } |
| 6411 | |
| 6412 | static inline VkShaderStageFlagBits toVkShaderStage(QRhiShaderStage::Type type) |
| 6413 | { |
| 6414 | switch (type) { |
| 6415 | case QRhiShaderStage::Vertex: |
| 6416 | return VK_SHADER_STAGE_VERTEX_BIT; |
| 6417 | case QRhiShaderStage::TessellationControl: |
| 6418 | return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 6419 | case QRhiShaderStage::TessellationEvaluation: |
| 6420 | return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 6421 | case QRhiShaderStage::Fragment: |
| 6422 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 6423 | case QRhiShaderStage::Compute: |
| 6424 | return VK_SHADER_STAGE_COMPUTE_BIT; |
| 6425 | case QRhiShaderStage::Geometry: |
| 6426 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 6427 | default: |
| 6428 | Q_UNREACHABLE_RETURN(VK_SHADER_STAGE_VERTEX_BIT); |
| 6429 | } |
| 6430 | } |
| 6431 | |
| 6432 | static inline VkFormat toVkAttributeFormat(QRhiVertexInputAttribute::Format format) |
| 6433 | { |
| 6434 | switch (format) { |
| 6435 | case QRhiVertexInputAttribute::Float4: |
| 6436 | return VK_FORMAT_R32G32B32A32_SFLOAT; |
| 6437 | case QRhiVertexInputAttribute::Float3: |
| 6438 | return VK_FORMAT_R32G32B32_SFLOAT; |
| 6439 | case QRhiVertexInputAttribute::Float2: |
| 6440 | return VK_FORMAT_R32G32_SFLOAT; |
| 6441 | case QRhiVertexInputAttribute::Float: |
| 6442 | return VK_FORMAT_R32_SFLOAT; |
| 6443 | case QRhiVertexInputAttribute::UNormByte4: |
| 6444 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 6445 | case QRhiVertexInputAttribute::UNormByte2: |
| 6446 | return VK_FORMAT_R8G8_UNORM; |
| 6447 | case QRhiVertexInputAttribute::UNormByte: |
| 6448 | return VK_FORMAT_R8_UNORM; |
| 6449 | case QRhiVertexInputAttribute::UInt4: |
| 6450 | return VK_FORMAT_R32G32B32A32_UINT; |
| 6451 | case QRhiVertexInputAttribute::UInt3: |
| 6452 | return VK_FORMAT_R32G32B32_UINT; |
| 6453 | case QRhiVertexInputAttribute::UInt2: |
| 6454 | return VK_FORMAT_R32G32_UINT; |
| 6455 | case QRhiVertexInputAttribute::UInt: |
| 6456 | return VK_FORMAT_R32_UINT; |
| 6457 | case QRhiVertexInputAttribute::SInt4: |
| 6458 | return VK_FORMAT_R32G32B32A32_SINT; |
| 6459 | case QRhiVertexInputAttribute::SInt3: |
| 6460 | return VK_FORMAT_R32G32B32_SINT; |
| 6461 | case QRhiVertexInputAttribute::SInt2: |
| 6462 | return VK_FORMAT_R32G32_SINT; |
| 6463 | case QRhiVertexInputAttribute::SInt: |
| 6464 | return VK_FORMAT_R32_SINT; |
| 6465 | case QRhiVertexInputAttribute::Half4: |
| 6466 | return VK_FORMAT_R16G16B16A16_SFLOAT; |
| 6467 | case QRhiVertexInputAttribute::Half3: |
| 6468 | return VK_FORMAT_R16G16B16_SFLOAT; |
| 6469 | case QRhiVertexInputAttribute::Half2: |
| 6470 | return VK_FORMAT_R16G16_SFLOAT; |
| 6471 | case QRhiVertexInputAttribute::Half: |
| 6472 | return VK_FORMAT_R16_SFLOAT; |
| 6473 | case QRhiVertexInputAttribute::UShort4: |
| 6474 | return VK_FORMAT_R16G16B16A16_UINT; |
| 6475 | case QRhiVertexInputAttribute::UShort3: |
| 6476 | return VK_FORMAT_R16G16B16_UINT; |
| 6477 | case QRhiVertexInputAttribute::UShort2: |
| 6478 | return VK_FORMAT_R16G16_UINT; |
| 6479 | case QRhiVertexInputAttribute::UShort: |
| 6480 | return VK_FORMAT_R16_UINT; |
| 6481 | case QRhiVertexInputAttribute::SShort4: |
| 6482 | return VK_FORMAT_R16G16B16A16_SINT; |
| 6483 | case QRhiVertexInputAttribute::SShort3: |
| 6484 | return VK_FORMAT_R16G16B16_SINT; |
| 6485 | case QRhiVertexInputAttribute::SShort2: |
| 6486 | return VK_FORMAT_R16G16_SINT; |
| 6487 | case QRhiVertexInputAttribute::SShort: |
| 6488 | return VK_FORMAT_R16_SINT; |
| 6489 | default: |
| 6490 | Q_UNREACHABLE_RETURN(VK_FORMAT_R32G32B32A32_SFLOAT); |
| 6491 | } |
| 6492 | } |
| 6493 | |
| 6494 | static inline VkPrimitiveTopology toVkTopology(QRhiGraphicsPipeline::Topology t) |
| 6495 | { |
| 6496 | switch (t) { |
| 6497 | case QRhiGraphicsPipeline::Triangles: |
| 6498 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 6499 | case QRhiGraphicsPipeline::TriangleStrip: |
| 6500 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 6501 | case QRhiGraphicsPipeline::TriangleFan: |
| 6502 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN; |
| 6503 | case QRhiGraphicsPipeline::Lines: |
| 6504 | return VK_PRIMITIVE_TOPOLOGY_LINE_LIST; |
| 6505 | case QRhiGraphicsPipeline::LineStrip: |
| 6506 | return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 6507 | case QRhiGraphicsPipeline::Points: |
| 6508 | return VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 6509 | case QRhiGraphicsPipeline::Patches: |
| 6510 | return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST; |
| 6511 | default: |
| 6512 | Q_UNREACHABLE_RETURN(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); |
| 6513 | } |
| 6514 | } |
| 6515 | |
| 6516 | static inline VkCullModeFlags toVkCullMode(QRhiGraphicsPipeline::CullMode c) |
| 6517 | { |
| 6518 | switch (c) { |
| 6519 | case QRhiGraphicsPipeline::None: |
| 6520 | return VK_CULL_MODE_NONE; |
| 6521 | case QRhiGraphicsPipeline::Front: |
| 6522 | return VK_CULL_MODE_FRONT_BIT; |
| 6523 | case QRhiGraphicsPipeline::Back: |
| 6524 | return VK_CULL_MODE_BACK_BIT; |
| 6525 | default: |
| 6526 | Q_UNREACHABLE_RETURN(VK_CULL_MODE_NONE); |
| 6527 | } |
| 6528 | } |
| 6529 | |
| 6530 | static inline VkFrontFace toVkFrontFace(QRhiGraphicsPipeline::FrontFace f) |
| 6531 | { |
| 6532 | switch (f) { |
| 6533 | case QRhiGraphicsPipeline::CCW: |
| 6534 | return VK_FRONT_FACE_COUNTER_CLOCKWISE; |
| 6535 | case QRhiGraphicsPipeline::CW: |
| 6536 | return VK_FRONT_FACE_CLOCKWISE; |
| 6537 | default: |
| 6538 | Q_UNREACHABLE_RETURN(VK_FRONT_FACE_COUNTER_CLOCKWISE); |
| 6539 | } |
| 6540 | } |
| 6541 | |
| 6542 | static inline VkColorComponentFlags toVkColorComponents(QRhiGraphicsPipeline::ColorMask c) |
| 6543 | { |
| 6544 | int f = 0; |
| 6545 | if (c.testFlag(flag: QRhiGraphicsPipeline::R)) |
| 6546 | f |= VK_COLOR_COMPONENT_R_BIT; |
| 6547 | if (c.testFlag(flag: QRhiGraphicsPipeline::G)) |
| 6548 | f |= VK_COLOR_COMPONENT_G_BIT; |
| 6549 | if (c.testFlag(flag: QRhiGraphicsPipeline::B)) |
| 6550 | f |= VK_COLOR_COMPONENT_B_BIT; |
| 6551 | if (c.testFlag(flag: QRhiGraphicsPipeline::A)) |
| 6552 | f |= VK_COLOR_COMPONENT_A_BIT; |
| 6553 | return VkColorComponentFlags(f); |
| 6554 | } |
| 6555 | |
| 6556 | static inline VkBlendFactor toVkBlendFactor(QRhiGraphicsPipeline::BlendFactor f) |
| 6557 | { |
| 6558 | switch (f) { |
| 6559 | case QRhiGraphicsPipeline::Zero: |
| 6560 | return VK_BLEND_FACTOR_ZERO; |
| 6561 | case QRhiGraphicsPipeline::One: |
| 6562 | return VK_BLEND_FACTOR_ONE; |
| 6563 | case QRhiGraphicsPipeline::SrcColor: |
| 6564 | return VK_BLEND_FACTOR_SRC_COLOR; |
| 6565 | case QRhiGraphicsPipeline::OneMinusSrcColor: |
| 6566 | return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR; |
| 6567 | case QRhiGraphicsPipeline::DstColor: |
| 6568 | return VK_BLEND_FACTOR_DST_COLOR; |
| 6569 | case QRhiGraphicsPipeline::OneMinusDstColor: |
| 6570 | return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR; |
| 6571 | case QRhiGraphicsPipeline::SrcAlpha: |
| 6572 | return VK_BLEND_FACTOR_SRC_ALPHA; |
| 6573 | case QRhiGraphicsPipeline::OneMinusSrcAlpha: |
| 6574 | return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 6575 | case QRhiGraphicsPipeline::DstAlpha: |
| 6576 | return VK_BLEND_FACTOR_DST_ALPHA; |
| 6577 | case QRhiGraphicsPipeline::OneMinusDstAlpha: |
| 6578 | return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA; |
| 6579 | case QRhiGraphicsPipeline::ConstantColor: |
| 6580 | return VK_BLEND_FACTOR_CONSTANT_COLOR; |
| 6581 | case QRhiGraphicsPipeline::OneMinusConstantColor: |
| 6582 | return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR; |
| 6583 | case QRhiGraphicsPipeline::ConstantAlpha: |
| 6584 | return VK_BLEND_FACTOR_CONSTANT_ALPHA; |
| 6585 | case QRhiGraphicsPipeline::OneMinusConstantAlpha: |
| 6586 | return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA; |
| 6587 | case QRhiGraphicsPipeline::SrcAlphaSaturate: |
| 6588 | return VK_BLEND_FACTOR_SRC_ALPHA_SATURATE; |
| 6589 | case QRhiGraphicsPipeline::Src1Color: |
| 6590 | return VK_BLEND_FACTOR_SRC1_COLOR; |
| 6591 | case QRhiGraphicsPipeline::OneMinusSrc1Color: |
| 6592 | return VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR; |
| 6593 | case QRhiGraphicsPipeline::Src1Alpha: |
| 6594 | return VK_BLEND_FACTOR_SRC1_ALPHA; |
| 6595 | case QRhiGraphicsPipeline::OneMinusSrc1Alpha: |
| 6596 | return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA; |
| 6597 | default: |
| 6598 | Q_UNREACHABLE_RETURN(VK_BLEND_FACTOR_ZERO); |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | static inline VkBlendOp toVkBlendOp(QRhiGraphicsPipeline::BlendOp op) |
| 6603 | { |
| 6604 | switch (op) { |
| 6605 | case QRhiGraphicsPipeline::Add: |
| 6606 | return VK_BLEND_OP_ADD; |
| 6607 | case QRhiGraphicsPipeline::Subtract: |
| 6608 | return VK_BLEND_OP_SUBTRACT; |
| 6609 | case QRhiGraphicsPipeline::ReverseSubtract: |
| 6610 | return VK_BLEND_OP_REVERSE_SUBTRACT; |
| 6611 | case QRhiGraphicsPipeline::Min: |
| 6612 | return VK_BLEND_OP_MIN; |
| 6613 | case QRhiGraphicsPipeline::Max: |
| 6614 | return VK_BLEND_OP_MAX; |
| 6615 | default: |
| 6616 | Q_UNREACHABLE_RETURN(VK_BLEND_OP_ADD); |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | static inline VkCompareOp toVkCompareOp(QRhiGraphicsPipeline::CompareOp op) |
| 6621 | { |
| 6622 | switch (op) { |
| 6623 | case QRhiGraphicsPipeline::Never: |
| 6624 | return VK_COMPARE_OP_NEVER; |
| 6625 | case QRhiGraphicsPipeline::Less: |
| 6626 | return VK_COMPARE_OP_LESS; |
| 6627 | case QRhiGraphicsPipeline::Equal: |
| 6628 | return VK_COMPARE_OP_EQUAL; |
| 6629 | case QRhiGraphicsPipeline::LessOrEqual: |
| 6630 | return VK_COMPARE_OP_LESS_OR_EQUAL; |
| 6631 | case QRhiGraphicsPipeline::Greater: |
| 6632 | return VK_COMPARE_OP_GREATER; |
| 6633 | case QRhiGraphicsPipeline::NotEqual: |
| 6634 | return VK_COMPARE_OP_NOT_EQUAL; |
| 6635 | case QRhiGraphicsPipeline::GreaterOrEqual: |
| 6636 | return VK_COMPARE_OP_GREATER_OR_EQUAL; |
| 6637 | case QRhiGraphicsPipeline::Always: |
| 6638 | return VK_COMPARE_OP_ALWAYS; |
| 6639 | default: |
| 6640 | Q_UNREACHABLE_RETURN(VK_COMPARE_OP_ALWAYS); |
| 6641 | } |
| 6642 | } |
| 6643 | |
| 6644 | static inline VkStencilOp toVkStencilOp(QRhiGraphicsPipeline::StencilOp op) |
| 6645 | { |
| 6646 | switch (op) { |
| 6647 | case QRhiGraphicsPipeline::StencilZero: |
| 6648 | return VK_STENCIL_OP_ZERO; |
| 6649 | case QRhiGraphicsPipeline::Keep: |
| 6650 | return VK_STENCIL_OP_KEEP; |
| 6651 | case QRhiGraphicsPipeline::Replace: |
| 6652 | return VK_STENCIL_OP_REPLACE; |
| 6653 | case QRhiGraphicsPipeline::IncrementAndClamp: |
| 6654 | return VK_STENCIL_OP_INCREMENT_AND_CLAMP; |
| 6655 | case QRhiGraphicsPipeline::DecrementAndClamp: |
| 6656 | return VK_STENCIL_OP_DECREMENT_AND_CLAMP; |
| 6657 | case QRhiGraphicsPipeline::Invert: |
| 6658 | return VK_STENCIL_OP_INVERT; |
| 6659 | case QRhiGraphicsPipeline::IncrementAndWrap: |
| 6660 | return VK_STENCIL_OP_INCREMENT_AND_WRAP; |
| 6661 | case QRhiGraphicsPipeline::DecrementAndWrap: |
| 6662 | return VK_STENCIL_OP_DECREMENT_AND_WRAP; |
| 6663 | default: |
| 6664 | Q_UNREACHABLE_RETURN(VK_STENCIL_OP_KEEP); |
| 6665 | } |
| 6666 | } |
| 6667 | |
| 6668 | static inline VkPolygonMode toVkPolygonMode(QRhiGraphicsPipeline::PolygonMode mode) |
| 6669 | { |
| 6670 | switch (mode) { |
| 6671 | case QRhiGraphicsPipeline::Fill: |
| 6672 | return VK_POLYGON_MODE_FILL; |
| 6673 | case QRhiGraphicsPipeline::Line: |
| 6674 | return VK_POLYGON_MODE_LINE; |
| 6675 | default: |
| 6676 | Q_UNREACHABLE_RETURN(VK_POLYGON_MODE_FILL); |
| 6677 | } |
| 6678 | } |
| 6679 | |
| 6680 | static inline void fillVkStencilOpState(VkStencilOpState *dst, const QRhiGraphicsPipeline::StencilOpState &src) |
| 6681 | { |
| 6682 | dst->failOp = toVkStencilOp(op: src.failOp); |
| 6683 | dst->passOp = toVkStencilOp(op: src.passOp); |
| 6684 | dst->depthFailOp = toVkStencilOp(op: src.depthFailOp); |
| 6685 | dst->compareOp = toVkCompareOp(op: src.compareOp); |
| 6686 | } |
| 6687 | |
| 6688 | static inline VkDescriptorType toVkDescriptorType(const QRhiShaderResourceBinding::Data *b) |
| 6689 | { |
| 6690 | switch (b->type) { |
| 6691 | case QRhiShaderResourceBinding::UniformBuffer: |
| 6692 | return b->u.ubuf.hasDynamicOffset ? VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC |
| 6693 | : VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 6694 | |
| 6695 | case QRhiShaderResourceBinding::SampledTexture: |
| 6696 | return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 6697 | |
| 6698 | case QRhiShaderResourceBinding::Texture: |
| 6699 | return VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
| 6700 | |
| 6701 | case QRhiShaderResourceBinding::Sampler: |
| 6702 | return VK_DESCRIPTOR_TYPE_SAMPLER; |
| 6703 | |
| 6704 | case QRhiShaderResourceBinding::ImageLoad: |
| 6705 | case QRhiShaderResourceBinding::ImageStore: |
| 6706 | case QRhiShaderResourceBinding::ImageLoadStore: |
| 6707 | return VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 6708 | |
| 6709 | case QRhiShaderResourceBinding::BufferLoad: |
| 6710 | case QRhiShaderResourceBinding::BufferStore: |
| 6711 | case QRhiShaderResourceBinding::BufferLoadStore: |
| 6712 | return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 6713 | |
| 6714 | default: |
| 6715 | Q_UNREACHABLE_RETURN(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | static inline VkShaderStageFlags toVkShaderStageFlags(QRhiShaderResourceBinding::StageFlags stage) |
| 6720 | { |
| 6721 | int s = 0; |
| 6722 | if (stage.testFlag(flag: QRhiShaderResourceBinding::VertexStage)) |
| 6723 | s |= VK_SHADER_STAGE_VERTEX_BIT; |
| 6724 | if (stage.testFlag(flag: QRhiShaderResourceBinding::FragmentStage)) |
| 6725 | s |= VK_SHADER_STAGE_FRAGMENT_BIT; |
| 6726 | if (stage.testFlag(flag: QRhiShaderResourceBinding::ComputeStage)) |
| 6727 | s |= VK_SHADER_STAGE_COMPUTE_BIT; |
| 6728 | if (stage.testFlag(flag: QRhiShaderResourceBinding::TessellationControlStage)) |
| 6729 | s |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 6730 | if (stage.testFlag(flag: QRhiShaderResourceBinding::TessellationEvaluationStage)) |
| 6731 | s |= VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 6732 | if (stage.testFlag(flag: QRhiShaderResourceBinding::GeometryStage)) |
| 6733 | s |= VK_SHADER_STAGE_GEOMETRY_BIT; |
| 6734 | return VkShaderStageFlags(s); |
| 6735 | } |
| 6736 | |
| 6737 | static inline VkCompareOp toVkTextureCompareOp(QRhiSampler::CompareOp op) |
| 6738 | { |
| 6739 | switch (op) { |
| 6740 | case QRhiSampler::Never: |
| 6741 | return VK_COMPARE_OP_NEVER; |
| 6742 | case QRhiSampler::Less: |
| 6743 | return VK_COMPARE_OP_LESS; |
| 6744 | case QRhiSampler::Equal: |
| 6745 | return VK_COMPARE_OP_EQUAL; |
| 6746 | case QRhiSampler::LessOrEqual: |
| 6747 | return VK_COMPARE_OP_LESS_OR_EQUAL; |
| 6748 | case QRhiSampler::Greater: |
| 6749 | return VK_COMPARE_OP_GREATER; |
| 6750 | case QRhiSampler::NotEqual: |
| 6751 | return VK_COMPARE_OP_NOT_EQUAL; |
| 6752 | case QRhiSampler::GreaterOrEqual: |
| 6753 | return VK_COMPARE_OP_GREATER_OR_EQUAL; |
| 6754 | case QRhiSampler::Always: |
| 6755 | return VK_COMPARE_OP_ALWAYS; |
| 6756 | default: |
| 6757 | Q_UNREACHABLE_RETURN(VK_COMPARE_OP_NEVER); |
| 6758 | } |
| 6759 | } |
| 6760 | |
| 6761 | QVkBuffer::QVkBuffer(QRhiImplementation *rhi, Type type, UsageFlags usage, quint32 size) |
| 6762 | : QRhiBuffer(rhi, type, usage, size) |
| 6763 | { |
| 6764 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 6765 | buffers[i] = stagingBuffers[i] = VK_NULL_HANDLE; |
| 6766 | allocations[i] = stagingAllocations[i] = nullptr; |
| 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | QVkBuffer::~QVkBuffer() |
| 6771 | { |
| 6772 | destroy(); |
| 6773 | } |
| 6774 | |
| 6775 | void QVkBuffer::destroy() |
| 6776 | { |
| 6777 | if (!buffers[0]) |
| 6778 | return; |
| 6779 | |
| 6780 | QRhiVulkan::DeferredReleaseEntry e; |
| 6781 | e.type = QRhiVulkan::DeferredReleaseEntry::Buffer; |
| 6782 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 6783 | |
| 6784 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 6785 | e.buffer.buffers[i] = buffers[i]; |
| 6786 | e.buffer.allocations[i] = allocations[i]; |
| 6787 | e.buffer.stagingBuffers[i] = stagingBuffers[i]; |
| 6788 | e.buffer.stagingAllocations[i] = stagingAllocations[i]; |
| 6789 | |
| 6790 | buffers[i] = VK_NULL_HANDLE; |
| 6791 | allocations[i] = nullptr; |
| 6792 | stagingBuffers[i] = VK_NULL_HANDLE; |
| 6793 | stagingAllocations[i] = nullptr; |
| 6794 | pendingDynamicUpdates[i].clear(); |
| 6795 | } |
| 6796 | |
| 6797 | QRHI_RES_RHI(QRhiVulkan); |
| 6798 | // destroy() implementations, unlike other functions, are expected to test |
| 6799 | // for m_rhi being null, to allow surviving in case one attempts to destroy |
| 6800 | // a (leaked) resource after the QRhi. |
| 6801 | if (rhiD) { |
| 6802 | rhiD->releaseQueue.append(t: e); |
| 6803 | rhiD->unregisterResource(res: this); |
| 6804 | } |
| 6805 | } |
| 6806 | |
| 6807 | bool QVkBuffer::create() |
| 6808 | { |
| 6809 | if (buffers[0]) |
| 6810 | destroy(); |
| 6811 | |
| 6812 | if (m_usage.testFlag(flag: QRhiBuffer::StorageBuffer) && m_type == Dynamic) { |
| 6813 | qWarning(msg: "StorageBuffer cannot be combined with Dynamic" ); |
| 6814 | return false; |
| 6815 | } |
| 6816 | |
| 6817 | const quint32 nonZeroSize = m_size <= 0 ? 256 : m_size; |
| 6818 | |
| 6819 | VkBufferCreateInfo bufferInfo = {}; |
| 6820 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 6821 | bufferInfo.size = nonZeroSize; |
| 6822 | bufferInfo.usage = toVkBufferUsage(usage: m_usage); |
| 6823 | |
| 6824 | VmaAllocationCreateInfo allocInfo = {}; |
| 6825 | |
| 6826 | if (m_type == Dynamic) { |
| 6827 | #ifndef Q_OS_DARWIN // not for MoltenVK |
| 6828 | // Keep mapped all the time. Essential f.ex. with some mobile GPUs, |
| 6829 | // where mapping and unmapping an entire allocation every time updating |
| 6830 | // a suballocated buffer presents a significant perf. hit. |
| 6831 | allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; |
| 6832 | #endif |
| 6833 | // host visible, frequent changes |
| 6834 | allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 6835 | } else { |
| 6836 | allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; |
| 6837 | bufferInfo.usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; |
| 6838 | } |
| 6839 | |
| 6840 | QRHI_RES_RHI(QRhiVulkan); |
| 6841 | VkResult err = VK_SUCCESS; |
| 6842 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 6843 | buffers[i] = VK_NULL_HANDLE; |
| 6844 | allocations[i] = nullptr; |
| 6845 | usageState[i].access = usageState[i].stage = 0; |
| 6846 | if (i == 0 || m_type == Dynamic) { |
| 6847 | VmaAllocation allocation; |
| 6848 | err = vmaCreateBuffer(allocator: toVmaAllocator(a: rhiD->allocator), pBufferCreateInfo: &bufferInfo, pAllocationCreateInfo: &allocInfo, pBuffer: &buffers[i], pAllocation: &allocation, pAllocationInfo: nullptr); |
| 6849 | if (err != VK_SUCCESS) |
| 6850 | break; |
| 6851 | allocations[i] = allocation; |
| 6852 | rhiD->setAllocationName(allocation, name: m_objectName, slot: m_type == Dynamic ? i : -1); |
| 6853 | rhiD->setObjectName(object: uint64_t(buffers[i]), type: VK_OBJECT_TYPE_BUFFER, name: m_objectName, |
| 6854 | slot: m_type == Dynamic ? i : -1); |
| 6855 | } |
| 6856 | } |
| 6857 | |
| 6858 | if (err != VK_SUCCESS) { |
| 6859 | qWarning(msg: "Failed to create buffer of size %u: %d" , nonZeroSize, err); |
| 6860 | rhiD->printExtraErrorInfo(err); |
| 6861 | return false; |
| 6862 | } |
| 6863 | |
| 6864 | lastActiveFrameSlot = -1; |
| 6865 | generation += 1; |
| 6866 | rhiD->registerResource(res: this); |
| 6867 | return true; |
| 6868 | } |
| 6869 | |
| 6870 | QRhiBuffer::NativeBuffer QVkBuffer::nativeBuffer() |
| 6871 | { |
| 6872 | if (m_type == Dynamic) { |
| 6873 | QRHI_RES_RHI(QRhiVulkan); |
| 6874 | NativeBuffer b; |
| 6875 | Q_ASSERT(sizeof(b.objects) / sizeof(b.objects[0]) >= size_t(QVK_FRAMES_IN_FLIGHT)); |
| 6876 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 6877 | rhiD->executeBufferHostWritesForSlot(bufD: this, slot: i); |
| 6878 | b.objects[i] = &buffers[i]; |
| 6879 | } |
| 6880 | b.slotCount = QVK_FRAMES_IN_FLIGHT; |
| 6881 | return b; |
| 6882 | } |
| 6883 | return { .objects: { &buffers[0] }, .slotCount: 1 }; |
| 6884 | } |
| 6885 | |
| 6886 | char *QVkBuffer::beginFullDynamicBufferUpdateForCurrentFrame() |
| 6887 | { |
| 6888 | // Shortcut the entire buffer update mechanism and allow the client to do |
| 6889 | // the host writes directly to the buffer. This will lead to unexpected |
| 6890 | // results when combined with QRhiResourceUpdateBatch-based updates for the |
| 6891 | // buffer, but provides a fast path for dynamic buffers that have all their |
| 6892 | // content changed in every frame. |
| 6893 | Q_ASSERT(m_type == Dynamic); |
| 6894 | QRHI_RES_RHI(QRhiVulkan); |
| 6895 | Q_ASSERT(rhiD->inFrame); |
| 6896 | const int slot = rhiD->currentFrameSlot; |
| 6897 | void *p = nullptr; |
| 6898 | VmaAllocation a = toVmaAllocation(a: allocations[slot]); |
| 6899 | VkResult err = vmaMapMemory(allocator: toVmaAllocator(a: rhiD->allocator), allocation: a, ppData: &p); |
| 6900 | if (err != VK_SUCCESS) { |
| 6901 | qWarning(msg: "Failed to map buffer: %d" , err); |
| 6902 | return nullptr; |
| 6903 | } |
| 6904 | return static_cast<char *>(p); |
| 6905 | } |
| 6906 | |
| 6907 | void QVkBuffer::endFullDynamicBufferUpdateForCurrentFrame() |
| 6908 | { |
| 6909 | QRHI_RES_RHI(QRhiVulkan); |
| 6910 | const int slot = rhiD->currentFrameSlot; |
| 6911 | VmaAllocation a = toVmaAllocation(a: allocations[slot]); |
| 6912 | vmaFlushAllocation(allocator: toVmaAllocator(a: rhiD->allocator), allocation: a, offset: 0, size: m_size); |
| 6913 | vmaUnmapMemory(allocator: toVmaAllocator(a: rhiD->allocator), allocation: a); |
| 6914 | } |
| 6915 | |
| 6916 | QVkRenderBuffer::QVkRenderBuffer(QRhiImplementation *rhi, Type type, const QSize &pixelSize, |
| 6917 | int sampleCount, Flags flags, |
| 6918 | QRhiTexture::Format backingFormatHint) |
| 6919 | : QRhiRenderBuffer(rhi, type, pixelSize, sampleCount, flags, backingFormatHint) |
| 6920 | { |
| 6921 | } |
| 6922 | |
| 6923 | QVkRenderBuffer::~QVkRenderBuffer() |
| 6924 | { |
| 6925 | destroy(); |
| 6926 | delete backingTexture; |
| 6927 | } |
| 6928 | |
| 6929 | void QVkRenderBuffer::destroy() |
| 6930 | { |
| 6931 | if (!memory && !backingTexture) |
| 6932 | return; |
| 6933 | |
| 6934 | QRhiVulkan::DeferredReleaseEntry e; |
| 6935 | e.type = QRhiVulkan::DeferredReleaseEntry::RenderBuffer; |
| 6936 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 6937 | |
| 6938 | e.renderBuffer.memory = memory; |
| 6939 | e.renderBuffer.image = image; |
| 6940 | e.renderBuffer.imageView = imageView; |
| 6941 | |
| 6942 | memory = VK_NULL_HANDLE; |
| 6943 | image = VK_NULL_HANDLE; |
| 6944 | imageView = VK_NULL_HANDLE; |
| 6945 | |
| 6946 | if (backingTexture) { |
| 6947 | Q_ASSERT(backingTexture->lastActiveFrameSlot == -1); |
| 6948 | backingTexture->lastActiveFrameSlot = e.lastActiveFrameSlot; |
| 6949 | backingTexture->destroy(); |
| 6950 | } |
| 6951 | |
| 6952 | QRHI_RES_RHI(QRhiVulkan); |
| 6953 | if (rhiD) { |
| 6954 | rhiD->releaseQueue.append(t: e); |
| 6955 | rhiD->unregisterResource(res: this); |
| 6956 | } |
| 6957 | } |
| 6958 | |
| 6959 | bool QVkRenderBuffer::create() |
| 6960 | { |
| 6961 | if (memory || backingTexture) |
| 6962 | destroy(); |
| 6963 | |
| 6964 | if (m_pixelSize.isEmpty()) |
| 6965 | return false; |
| 6966 | |
| 6967 | QRHI_RES_RHI(QRhiVulkan); |
| 6968 | samples = rhiD->effectiveSampleCountBits(sampleCount: m_sampleCount); |
| 6969 | |
| 6970 | switch (m_type) { |
| 6971 | case QRhiRenderBuffer::Color: |
| 6972 | { |
| 6973 | if (!backingTexture) { |
| 6974 | backingTexture = QRHI_RES(QVkTexture, rhiD->createTexture(backingFormat(), |
| 6975 | m_pixelSize, |
| 6976 | 1, |
| 6977 | 0, |
| 6978 | m_sampleCount, |
| 6979 | QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource)); |
| 6980 | } else { |
| 6981 | backingTexture->setPixelSize(m_pixelSize); |
| 6982 | backingTexture->setSampleCount(m_sampleCount); |
| 6983 | } |
| 6984 | backingTexture->setName(m_objectName); |
| 6985 | if (!backingTexture->create()) |
| 6986 | return false; |
| 6987 | vkformat = backingTexture->vkformat; |
| 6988 | } |
| 6989 | break; |
| 6990 | case QRhiRenderBuffer::DepthStencil: |
| 6991 | vkformat = rhiD->optimalDepthStencilFormat(); |
| 6992 | if (!rhiD->createTransientImage(format: vkformat, |
| 6993 | pixelSize: m_pixelSize, |
| 6994 | usage: VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, |
| 6995 | aspectMask: VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, |
| 6996 | samples, |
| 6997 | mem: &memory, |
| 6998 | images: &image, |
| 6999 | views: &imageView, |
| 7000 | count: 1)) |
| 7001 | { |
| 7002 | return false; |
| 7003 | } |
| 7004 | rhiD->setObjectName(object: uint64_t(image), type: VK_OBJECT_TYPE_IMAGE, name: m_objectName); |
| 7005 | break; |
| 7006 | default: |
| 7007 | Q_UNREACHABLE(); |
| 7008 | break; |
| 7009 | } |
| 7010 | |
| 7011 | lastActiveFrameSlot = -1; |
| 7012 | generation += 1; |
| 7013 | rhiD->registerResource(res: this); |
| 7014 | return true; |
| 7015 | } |
| 7016 | |
| 7017 | QRhiTexture::Format QVkRenderBuffer::backingFormat() const |
| 7018 | { |
| 7019 | if (m_backingFormatHint != QRhiTexture::UnknownFormat) |
| 7020 | return m_backingFormatHint; |
| 7021 | else |
| 7022 | return m_type == Color ? QRhiTexture::RGBA8 : QRhiTexture::UnknownFormat; |
| 7023 | } |
| 7024 | |
| 7025 | QVkTexture::QVkTexture(QRhiImplementation *rhi, Format format, const QSize &pixelSize, int depth, |
| 7026 | int arraySize, int sampleCount, Flags flags) |
| 7027 | : QRhiTexture(rhi, format, pixelSize, depth, arraySize, sampleCount, flags) |
| 7028 | { |
| 7029 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 7030 | stagingBuffers[i] = VK_NULL_HANDLE; |
| 7031 | stagingAllocations[i] = nullptr; |
| 7032 | } |
| 7033 | for (int i = 0; i < QRhi::MAX_MIP_LEVELS; ++i) |
| 7034 | perLevelImageViews[i] = VK_NULL_HANDLE; |
| 7035 | } |
| 7036 | |
| 7037 | QVkTexture::~QVkTexture() |
| 7038 | { |
| 7039 | destroy(); |
| 7040 | } |
| 7041 | |
| 7042 | void QVkTexture::destroy() |
| 7043 | { |
| 7044 | if (!image) |
| 7045 | return; |
| 7046 | |
| 7047 | QRhiVulkan::DeferredReleaseEntry e; |
| 7048 | e.type = QRhiVulkan::DeferredReleaseEntry::Texture; |
| 7049 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 7050 | |
| 7051 | e.texture.image = owns ? image : VK_NULL_HANDLE; |
| 7052 | e.texture.imageView = imageView; |
| 7053 | e.texture.allocation = owns ? imageAlloc : nullptr; |
| 7054 | |
| 7055 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 7056 | e.texture.stagingBuffers[i] = stagingBuffers[i]; |
| 7057 | e.texture.stagingAllocations[i] = stagingAllocations[i]; |
| 7058 | |
| 7059 | stagingBuffers[i] = VK_NULL_HANDLE; |
| 7060 | stagingAllocations[i] = nullptr; |
| 7061 | } |
| 7062 | |
| 7063 | for (int i = 0; i < QRhi::MAX_MIP_LEVELS; ++i) { |
| 7064 | e.texture.extraImageViews[i] = perLevelImageViews[i]; |
| 7065 | perLevelImageViews[i] = VK_NULL_HANDLE; |
| 7066 | } |
| 7067 | |
| 7068 | image = VK_NULL_HANDLE; |
| 7069 | imageView = VK_NULL_HANDLE; |
| 7070 | imageAlloc = nullptr; |
| 7071 | |
| 7072 | QRHI_RES_RHI(QRhiVulkan); |
| 7073 | if (rhiD) { |
| 7074 | rhiD->releaseQueue.append(t: e); |
| 7075 | rhiD->unregisterResource(res: this); |
| 7076 | } |
| 7077 | } |
| 7078 | |
| 7079 | bool QVkTexture::prepareCreate(QSize *adjustedSize) |
| 7080 | { |
| 7081 | if (image) |
| 7082 | destroy(); |
| 7083 | |
| 7084 | QRHI_RES_RHI(QRhiVulkan); |
| 7085 | vkformat = toVkTextureFormat(format: m_format, flags: m_flags); |
| 7086 | if (m_writeViewFormat.format != UnknownFormat) |
| 7087 | viewFormat = toVkTextureFormat(format: m_writeViewFormat.format, flags: m_writeViewFormat.srgb ? sRGB : Flags()); |
| 7088 | else |
| 7089 | viewFormat = vkformat; |
| 7090 | if (m_readViewFormat.format != UnknownFormat) |
| 7091 | viewFormatForSampling = toVkTextureFormat(format: m_readViewFormat.format, flags: m_readViewFormat.srgb ? sRGB : Flags()); |
| 7092 | else |
| 7093 | viewFormatForSampling = vkformat; |
| 7094 | |
| 7095 | VkFormatProperties props; |
| 7096 | rhiD->f->vkGetPhysicalDeviceFormatProperties(rhiD->physDev, vkformat, &props); |
| 7097 | const bool canSampleOptimal = (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT); |
| 7098 | if (!canSampleOptimal) { |
| 7099 | qWarning(msg: "Texture sampling with optimal tiling for format %d not supported" , vkformat); |
| 7100 | return false; |
| 7101 | } |
| 7102 | |
| 7103 | const bool isCube = m_flags.testFlag(flag: CubeMap); |
| 7104 | const bool isArray = m_flags.testFlag(flag: TextureArray); |
| 7105 | const bool is3D = m_flags.testFlag(flag: ThreeDimensional); |
| 7106 | const bool is1D = m_flags.testFlag(flag: OneDimensional); |
| 7107 | const bool hasMipMaps = m_flags.testFlag(flag: MipMapped); |
| 7108 | |
| 7109 | const QSize size = is1D ? QSize(qMax(a: 1, b: m_pixelSize.width()), 1) |
| 7110 | : (m_pixelSize.isEmpty() ? QSize(1, 1) : m_pixelSize); |
| 7111 | |
| 7112 | mipLevelCount = uint(hasMipMaps ? rhiD->q->mipLevelsForSize(size) : 1); |
| 7113 | const int maxLevels = QRhi::MAX_MIP_LEVELS; |
| 7114 | if (mipLevelCount > maxLevels) { |
| 7115 | qWarning(msg: "Too many mip levels (%d, max is %d), truncating mip chain" , mipLevelCount, maxLevels); |
| 7116 | mipLevelCount = maxLevels; |
| 7117 | } |
| 7118 | samples = rhiD->effectiveSampleCountBits(sampleCount: m_sampleCount); |
| 7119 | if (samples > VK_SAMPLE_COUNT_1_BIT) { |
| 7120 | if (isCube) { |
| 7121 | qWarning(msg: "Cubemap texture cannot be multisample" ); |
| 7122 | return false; |
| 7123 | } |
| 7124 | if (is3D) { |
| 7125 | qWarning(msg: "3D texture cannot be multisample" ); |
| 7126 | return false; |
| 7127 | } |
| 7128 | if (hasMipMaps) { |
| 7129 | qWarning(msg: "Multisample texture cannot have mipmaps" ); |
| 7130 | return false; |
| 7131 | } |
| 7132 | } |
| 7133 | if (isCube && is3D) { |
| 7134 | qWarning(msg: "Texture cannot be both cube and 3D" ); |
| 7135 | return false; |
| 7136 | } |
| 7137 | if (isArray && is3D) { |
| 7138 | qWarning(msg: "Texture cannot be both array and 3D" ); |
| 7139 | return false; |
| 7140 | } |
| 7141 | if (isCube && is1D) { |
| 7142 | qWarning(msg: "Texture cannot be both cube and 1D" ); |
| 7143 | return false; |
| 7144 | } |
| 7145 | if (is1D && is3D) { |
| 7146 | qWarning(msg: "Texture cannot be both 1D and 3D" ); |
| 7147 | return false; |
| 7148 | } |
| 7149 | if (m_depth > 1 && !is3D) { |
| 7150 | qWarning(msg: "Texture cannot have a depth of %d when it is not 3D" , m_depth); |
| 7151 | return false; |
| 7152 | } |
| 7153 | if (m_arraySize > 0 && !isArray) { |
| 7154 | qWarning(msg: "Texture cannot have an array size of %d when it is not an array" , m_arraySize); |
| 7155 | return false; |
| 7156 | } |
| 7157 | if (m_arraySize < 1 && isArray) { |
| 7158 | qWarning(msg: "Texture is an array but array size is %d" , m_arraySize); |
| 7159 | return false; |
| 7160 | } |
| 7161 | |
| 7162 | usageState.layout = VK_IMAGE_LAYOUT_PREINITIALIZED; |
| 7163 | usageState.access = 0; |
| 7164 | usageState.stage = 0; |
| 7165 | |
| 7166 | if (adjustedSize) |
| 7167 | *adjustedSize = size; |
| 7168 | |
| 7169 | return true; |
| 7170 | } |
| 7171 | |
| 7172 | bool QVkTexture::finishCreate() |
| 7173 | { |
| 7174 | QRHI_RES_RHI(QRhiVulkan); |
| 7175 | |
| 7176 | const auto aspectMask = aspectMaskForTextureFormat(format: m_format); |
| 7177 | const bool isCube = m_flags.testFlag(flag: CubeMap); |
| 7178 | const bool isArray = m_flags.testFlag(flag: TextureArray); |
| 7179 | const bool is3D = m_flags.testFlag(flag: ThreeDimensional); |
| 7180 | const bool is1D = m_flags.testFlag(flag: OneDimensional); |
| 7181 | |
| 7182 | VkImageViewCreateInfo viewInfo = {}; |
| 7183 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7184 | viewInfo.image = image; |
| 7185 | viewInfo.viewType = isCube |
| 7186 | ? VK_IMAGE_VIEW_TYPE_CUBE |
| 7187 | : (is3D ? VK_IMAGE_VIEW_TYPE_3D |
| 7188 | : (is1D ? (isArray ? VK_IMAGE_VIEW_TYPE_1D_ARRAY : VK_IMAGE_VIEW_TYPE_1D) |
| 7189 | : (isArray ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D))); |
| 7190 | viewInfo.format = viewFormatForSampling; |
| 7191 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 7192 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 7193 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 7194 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 7195 | viewInfo.subresourceRange.aspectMask = aspectMask; |
| 7196 | // Force-remove the VK_IMAGE_ASPECT_STENCIL_BIT |
| 7197 | // Another view with this bit is probably needed for stencil |
| 7198 | viewInfo.subresourceRange.aspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT; |
| 7199 | viewInfo.subresourceRange.levelCount = mipLevelCount; |
| 7200 | if (isArray && m_arrayRangeStart >= 0 && m_arrayRangeLength >= 0) { |
| 7201 | viewInfo.subresourceRange.baseArrayLayer = uint32_t(m_arrayRangeStart); |
| 7202 | viewInfo.subresourceRange.layerCount = uint32_t(m_arrayRangeLength); |
| 7203 | } else { |
| 7204 | viewInfo.subresourceRange.layerCount = isCube ? 6 : (isArray ? qMax(a: 0, b: m_arraySize) : 1); |
| 7205 | } |
| 7206 | |
| 7207 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &imageView); |
| 7208 | if (err != VK_SUCCESS) { |
| 7209 | qWarning(msg: "Failed to create image view: %d" , err); |
| 7210 | return false; |
| 7211 | } |
| 7212 | |
| 7213 | lastActiveFrameSlot = -1; |
| 7214 | generation += 1; |
| 7215 | |
| 7216 | return true; |
| 7217 | } |
| 7218 | |
| 7219 | bool QVkTexture::create() |
| 7220 | { |
| 7221 | QSize size; |
| 7222 | if (!prepareCreate(adjustedSize: &size)) |
| 7223 | return false; |
| 7224 | |
| 7225 | QRHI_RES_RHI(QRhiVulkan); |
| 7226 | const bool isRenderTarget = m_flags.testFlag(flag: QRhiTexture::RenderTarget); |
| 7227 | const bool isDepth = isDepthTextureFormat(format: m_format); |
| 7228 | const bool isCube = m_flags.testFlag(flag: CubeMap); |
| 7229 | const bool isArray = m_flags.testFlag(flag: TextureArray); |
| 7230 | const bool is3D = m_flags.testFlag(flag: ThreeDimensional); |
| 7231 | const bool is1D = m_flags.testFlag(flag: OneDimensional); |
| 7232 | |
| 7233 | VkImageCreateInfo imageInfo = {}; |
| 7234 | imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 7235 | imageInfo.flags = 0; |
| 7236 | if (isCube) |
| 7237 | imageInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT; |
| 7238 | |
| 7239 | if (is3D && isRenderTarget) { |
| 7240 | // This relies on a Vulkan 1.1 constant. For guaranteed proper behavior |
| 7241 | // this also requires that at run time the VkInstance has at least API 1.1 |
| 7242 | // enabled. (though it works as expected with some Vulkan (1.2) |
| 7243 | // implementations regardless of the requested API version, but f.ex. the |
| 7244 | // validation layer complains when using this without enabling >=1.1) |
| 7245 | if (!rhiD->caps.texture3DSliceAs2D) |
| 7246 | qWarning(msg: "QRhiVulkan: Rendering to 3D texture slice may not be functional without API 1.1 on the VkInstance" ); |
| 7247 | #ifdef VK_VERSION_1_1 |
| 7248 | imageInfo.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT; |
| 7249 | #else |
| 7250 | imageInfo.flags |= 0x00000020; |
| 7251 | #endif |
| 7252 | } |
| 7253 | |
| 7254 | imageInfo.imageType = is1D ? VK_IMAGE_TYPE_1D : is3D ? VK_IMAGE_TYPE_3D : VK_IMAGE_TYPE_2D; |
| 7255 | imageInfo.format = vkformat; |
| 7256 | imageInfo.extent.width = uint32_t(size.width()); |
| 7257 | imageInfo.extent.height = uint32_t(size.height()); |
| 7258 | imageInfo.extent.depth = is3D ? qMax(a: 1, b: m_depth) : 1; |
| 7259 | imageInfo.mipLevels = mipLevelCount; |
| 7260 | imageInfo.arrayLayers = isCube ? 6 : (isArray ? qMax(a: 0, b: m_arraySize) : 1); |
| 7261 | imageInfo.samples = samples; |
| 7262 | imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; |
| 7263 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; |
| 7264 | |
| 7265 | imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 7266 | if (isRenderTarget) { |
| 7267 | if (isDepth) |
| 7268 | imageInfo.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 7269 | else |
| 7270 | imageInfo.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 7271 | } |
| 7272 | if (m_flags.testFlag(flag: QRhiTexture::UsedAsTransferSource)) |
| 7273 | imageInfo.usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 7274 | if (m_flags.testFlag(flag: QRhiTexture::UsedWithGenerateMips)) |
| 7275 | imageInfo.usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 7276 | if (m_flags.testFlag(flag: QRhiTexture::UsedWithLoadStore)) |
| 7277 | imageInfo.usage |= VK_IMAGE_USAGE_STORAGE_BIT; |
| 7278 | #ifdef VK_KHR_fragment_shading_rate |
| 7279 | if (m_flags.testFlag(flag: QRhiTexture::UsedAsShadingRateMap) && rhiD->caps.imageBasedShadingRate) |
| 7280 | imageInfo.usage |= VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR; |
| 7281 | #endif |
| 7282 | |
| 7283 | VmaAllocationCreateInfo allocInfo = {}; |
| 7284 | allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; |
| 7285 | |
| 7286 | VmaAllocation allocation; |
| 7287 | VkResult err = vmaCreateImage(allocator: toVmaAllocator(a: rhiD->allocator), pImageCreateInfo: &imageInfo, pAllocationCreateInfo: &allocInfo, pImage: &image, pAllocation: &allocation, pAllocationInfo: nullptr); |
| 7288 | if (err != VK_SUCCESS) { |
| 7289 | qWarning(msg: "Failed to create image (with VkImageCreateInfo %ux%u depth %u vkformat 0x%X mips %u layers %u vksamples 0x%X): %d" , |
| 7290 | imageInfo.extent.width, imageInfo.extent.height, imageInfo.extent.depth, |
| 7291 | int(imageInfo.format), |
| 7292 | imageInfo.mipLevels, |
| 7293 | imageInfo.arrayLayers, |
| 7294 | int(imageInfo.samples), |
| 7295 | err); |
| 7296 | rhiD->printExtraErrorInfo(err); |
| 7297 | return false; |
| 7298 | } |
| 7299 | imageAlloc = allocation; |
| 7300 | rhiD->setAllocationName(allocation, name: m_objectName); |
| 7301 | |
| 7302 | if (!finishCreate()) |
| 7303 | return false; |
| 7304 | |
| 7305 | rhiD->setObjectName(object: uint64_t(image), type: VK_OBJECT_TYPE_IMAGE, name: m_objectName); |
| 7306 | |
| 7307 | owns = true; |
| 7308 | rhiD->registerResource(res: this); |
| 7309 | return true; |
| 7310 | } |
| 7311 | |
| 7312 | bool QVkTexture::createFrom(QRhiTexture::NativeTexture src) |
| 7313 | { |
| 7314 | VkImage img = VkImage(src.object); |
| 7315 | if (img == 0) |
| 7316 | return false; |
| 7317 | |
| 7318 | if (!prepareCreate()) |
| 7319 | return false; |
| 7320 | |
| 7321 | image = img; |
| 7322 | |
| 7323 | if (!finishCreate()) |
| 7324 | return false; |
| 7325 | |
| 7326 | usageState.layout = VkImageLayout(src.layout); |
| 7327 | |
| 7328 | owns = false; |
| 7329 | QRHI_RES_RHI(QRhiVulkan); |
| 7330 | rhiD->registerResource(res: this); |
| 7331 | return true; |
| 7332 | } |
| 7333 | |
| 7334 | QRhiTexture::NativeTexture QVkTexture::nativeTexture() |
| 7335 | { |
| 7336 | return {.object: quint64(image), .layout: usageState.layout}; |
| 7337 | } |
| 7338 | |
| 7339 | void QVkTexture::setNativeLayout(int layout) |
| 7340 | { |
| 7341 | usageState.layout = VkImageLayout(layout); |
| 7342 | } |
| 7343 | |
| 7344 | VkImageView QVkTexture::perLevelImageViewForLoadStore(int level) |
| 7345 | { |
| 7346 | Q_ASSERT(level >= 0 && level < int(mipLevelCount)); |
| 7347 | if (perLevelImageViews[level] != VK_NULL_HANDLE) |
| 7348 | return perLevelImageViews[level]; |
| 7349 | |
| 7350 | const VkImageAspectFlags aspectMask = aspectMaskForTextureFormat(format: m_format); |
| 7351 | const bool isCube = m_flags.testFlag(flag: CubeMap); |
| 7352 | const bool isArray = m_flags.testFlag(flag: TextureArray); |
| 7353 | const bool is3D = m_flags.testFlag(flag: ThreeDimensional); |
| 7354 | const bool is1D = m_flags.testFlag(flag: OneDimensional); |
| 7355 | |
| 7356 | VkImageViewCreateInfo viewInfo = {}; |
| 7357 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7358 | viewInfo.image = image; |
| 7359 | viewInfo.viewType = isCube |
| 7360 | ? VK_IMAGE_VIEW_TYPE_CUBE |
| 7361 | : (is3D ? VK_IMAGE_VIEW_TYPE_3D |
| 7362 | : (is1D ? (isArray ? VK_IMAGE_VIEW_TYPE_1D_ARRAY : VK_IMAGE_VIEW_TYPE_1D) |
| 7363 | : (isArray ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D))); |
| 7364 | viewInfo.format = viewFormat; // this is writeViewFormat, regardless of Load, Store, or LoadStore; intentional |
| 7365 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 7366 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 7367 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 7368 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 7369 | viewInfo.subresourceRange.aspectMask = aspectMask; |
| 7370 | viewInfo.subresourceRange.baseMipLevel = uint32_t(level); |
| 7371 | viewInfo.subresourceRange.levelCount = 1; |
| 7372 | viewInfo.subresourceRange.baseArrayLayer = 0; |
| 7373 | viewInfo.subresourceRange.layerCount = isCube ? 6 : (isArray ? qMax(a: 0, b: m_arraySize) : 1); |
| 7374 | |
| 7375 | VkImageView v = VK_NULL_HANDLE; |
| 7376 | QRHI_RES_RHI(QRhiVulkan); |
| 7377 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &v); |
| 7378 | if (err != VK_SUCCESS) { |
| 7379 | qWarning(msg: "Failed to create image view: %d" , err); |
| 7380 | return VK_NULL_HANDLE; |
| 7381 | } |
| 7382 | |
| 7383 | perLevelImageViews[level] = v; |
| 7384 | return v; |
| 7385 | } |
| 7386 | |
| 7387 | QVkSampler::QVkSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode, |
| 7388 | AddressMode u, AddressMode v, AddressMode w) |
| 7389 | : QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w) |
| 7390 | { |
| 7391 | } |
| 7392 | |
| 7393 | QVkSampler::~QVkSampler() |
| 7394 | { |
| 7395 | destroy(); |
| 7396 | } |
| 7397 | |
| 7398 | void QVkSampler::destroy() |
| 7399 | { |
| 7400 | if (!sampler) |
| 7401 | return; |
| 7402 | |
| 7403 | QRhiVulkan::DeferredReleaseEntry e; |
| 7404 | e.type = QRhiVulkan::DeferredReleaseEntry::Sampler; |
| 7405 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 7406 | |
| 7407 | e.sampler.sampler = sampler; |
| 7408 | sampler = VK_NULL_HANDLE; |
| 7409 | |
| 7410 | QRHI_RES_RHI(QRhiVulkan); |
| 7411 | if (rhiD) { |
| 7412 | rhiD->releaseQueue.append(t: e); |
| 7413 | rhiD->unregisterResource(res: this); |
| 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | bool QVkSampler::create() |
| 7418 | { |
| 7419 | if (sampler) |
| 7420 | destroy(); |
| 7421 | |
| 7422 | VkSamplerCreateInfo samplerInfo = {}; |
| 7423 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 7424 | samplerInfo.magFilter = toVkFilter(f: m_magFilter); |
| 7425 | samplerInfo.minFilter = toVkFilter(f: m_minFilter); |
| 7426 | samplerInfo.mipmapMode = toVkMipmapMode(f: m_mipmapMode); |
| 7427 | samplerInfo.addressModeU = toVkAddressMode(m: m_addressU); |
| 7428 | samplerInfo.addressModeV = toVkAddressMode(m: m_addressV); |
| 7429 | samplerInfo.addressModeW = toVkAddressMode(m: m_addressW); |
| 7430 | samplerInfo.maxAnisotropy = 1.0f; |
| 7431 | samplerInfo.compareEnable = m_compareOp != Never; |
| 7432 | samplerInfo.compareOp = toVkTextureCompareOp(op: m_compareOp); |
| 7433 | samplerInfo.maxLod = m_mipmapMode == None ? 0.25f : 1000.0f; |
| 7434 | |
| 7435 | QRHI_RES_RHI(QRhiVulkan); |
| 7436 | VkResult err = rhiD->df->vkCreateSampler(rhiD->dev, &samplerInfo, nullptr, &sampler); |
| 7437 | if (err != VK_SUCCESS) { |
| 7438 | qWarning(msg: "Failed to create sampler: %d" , err); |
| 7439 | return false; |
| 7440 | } |
| 7441 | |
| 7442 | lastActiveFrameSlot = -1; |
| 7443 | generation += 1; |
| 7444 | rhiD->registerResource(res: this); |
| 7445 | return true; |
| 7446 | } |
| 7447 | |
| 7448 | QVkRenderPassDescriptor::QVkRenderPassDescriptor(QRhiImplementation *rhi) |
| 7449 | : QRhiRenderPassDescriptor(rhi) |
| 7450 | { |
| 7451 | serializedFormatData.reserve(asize: 64); |
| 7452 | } |
| 7453 | |
| 7454 | QVkRenderPassDescriptor::~QVkRenderPassDescriptor() |
| 7455 | { |
| 7456 | destroy(); |
| 7457 | } |
| 7458 | |
| 7459 | void QVkRenderPassDescriptor::destroy() |
| 7460 | { |
| 7461 | if (!rp) |
| 7462 | return; |
| 7463 | |
| 7464 | if (!ownsRp) { |
| 7465 | rp = VK_NULL_HANDLE; |
| 7466 | return; |
| 7467 | } |
| 7468 | |
| 7469 | QRhiVulkan::DeferredReleaseEntry e; |
| 7470 | e.type = QRhiVulkan::DeferredReleaseEntry::RenderPass; |
| 7471 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 7472 | |
| 7473 | e.renderPass.rp = rp; |
| 7474 | |
| 7475 | rp = VK_NULL_HANDLE; |
| 7476 | |
| 7477 | QRHI_RES_RHI(QRhiVulkan); |
| 7478 | if (rhiD) { |
| 7479 | rhiD->releaseQueue.append(t: e); |
| 7480 | rhiD->unregisterResource(res: this); |
| 7481 | } |
| 7482 | } |
| 7483 | |
| 7484 | static inline bool attachmentDescriptionEquals(const VkAttachmentDescription &a, const VkAttachmentDescription &b) |
| 7485 | { |
| 7486 | return a.format == b.format |
| 7487 | && a.samples == b.samples |
| 7488 | && a.loadOp == b.loadOp |
| 7489 | && a.storeOp == b.storeOp |
| 7490 | && a.stencilLoadOp == b.stencilLoadOp |
| 7491 | && a.stencilStoreOp == b.stencilStoreOp |
| 7492 | && a.initialLayout == b.initialLayout |
| 7493 | && a.finalLayout == b.finalLayout; |
| 7494 | } |
| 7495 | |
| 7496 | bool QVkRenderPassDescriptor::isCompatible(const QRhiRenderPassDescriptor *other) const |
| 7497 | { |
| 7498 | if (other == this) |
| 7499 | return true; |
| 7500 | |
| 7501 | if (!other) |
| 7502 | return false; |
| 7503 | |
| 7504 | const QVkRenderPassDescriptor *o = QRHI_RES(const QVkRenderPassDescriptor, other); |
| 7505 | |
| 7506 | if (attDescs.size() != o->attDescs.size()) |
| 7507 | return false; |
| 7508 | if (colorRefs.size() != o->colorRefs.size()) |
| 7509 | return false; |
| 7510 | if (resolveRefs.size() != o->resolveRefs.size()) |
| 7511 | return false; |
| 7512 | if (hasDepthStencil != o->hasDepthStencil) |
| 7513 | return false; |
| 7514 | if (hasDepthStencilResolve != o->hasDepthStencilResolve) |
| 7515 | return false; |
| 7516 | if (multiViewCount != o->multiViewCount) |
| 7517 | return false; |
| 7518 | if (hasShadingRateMap != o->hasShadingRateMap) |
| 7519 | return false; |
| 7520 | |
| 7521 | for (int i = 0, ie = colorRefs.size(); i != ie; ++i) { |
| 7522 | const uint32_t attIdx = colorRefs[i].attachment; |
| 7523 | if (attIdx != o->colorRefs[i].attachment) |
| 7524 | return false; |
| 7525 | if (attIdx != VK_ATTACHMENT_UNUSED && !attachmentDescriptionEquals(a: attDescs[attIdx], b: o->attDescs[attIdx])) |
| 7526 | return false; |
| 7527 | } |
| 7528 | |
| 7529 | if (hasDepthStencil) { |
| 7530 | const uint32_t attIdx = dsRef.attachment; |
| 7531 | if (attIdx != o->dsRef.attachment) |
| 7532 | return false; |
| 7533 | if (attIdx != VK_ATTACHMENT_UNUSED && !attachmentDescriptionEquals(a: attDescs[attIdx], b: o->attDescs[attIdx])) |
| 7534 | return false; |
| 7535 | } |
| 7536 | |
| 7537 | for (int i = 0, ie = resolveRefs.size(); i != ie; ++i) { |
| 7538 | const uint32_t attIdx = resolveRefs[i].attachment; |
| 7539 | if (attIdx != o->resolveRefs[i].attachment) |
| 7540 | return false; |
| 7541 | if (attIdx != VK_ATTACHMENT_UNUSED && !attachmentDescriptionEquals(a: attDescs[attIdx], b: o->attDescs[attIdx])) |
| 7542 | return false; |
| 7543 | } |
| 7544 | |
| 7545 | if (hasDepthStencilResolve) { |
| 7546 | const uint32_t attIdx = dsResolveRef.attachment; |
| 7547 | if (attIdx != o->dsResolveRef.attachment) |
| 7548 | return false; |
| 7549 | if (attIdx != VK_ATTACHMENT_UNUSED && !attachmentDescriptionEquals(a: attDescs[attIdx], b: o->attDescs[attIdx])) |
| 7550 | return false; |
| 7551 | } |
| 7552 | |
| 7553 | if (hasShadingRateMap) { |
| 7554 | const uint32_t attIdx = shadingRateRef.attachment; |
| 7555 | if (attIdx != o->shadingRateRef.attachment) |
| 7556 | return false; |
| 7557 | if (attIdx != VK_ATTACHMENT_UNUSED && !attachmentDescriptionEquals(a: attDescs[attIdx], b: o->attDescs[attIdx])) |
| 7558 | return false; |
| 7559 | } |
| 7560 | |
| 7561 | // subpassDeps is not included |
| 7562 | |
| 7563 | return true; |
| 7564 | } |
| 7565 | |
| 7566 | void QVkRenderPassDescriptor::updateSerializedFormat() |
| 7567 | { |
| 7568 | serializedFormatData.clear(); |
| 7569 | auto p = std::back_inserter(x&: serializedFormatData); |
| 7570 | |
| 7571 | *p++ = attDescs.size(); |
| 7572 | *p++ = colorRefs.size(); |
| 7573 | *p++ = resolveRefs.size(); |
| 7574 | *p++ = hasDepthStencil; |
| 7575 | *p++ = hasDepthStencilResolve; |
| 7576 | *p++ = hasShadingRateMap; |
| 7577 | *p++ = multiViewCount; |
| 7578 | |
| 7579 | auto serializeAttachmentData = [this, &p](uint32_t attIdx) { |
| 7580 | const bool used = attIdx != VK_ATTACHMENT_UNUSED; |
| 7581 | const VkAttachmentDescription *a = used ? &attDescs[attIdx] : nullptr; |
| 7582 | *p++ = used ? a->format : 0; |
| 7583 | *p++ = used ? a->samples : 0; |
| 7584 | *p++ = used ? a->loadOp : 0; |
| 7585 | *p++ = used ? a->storeOp : 0; |
| 7586 | *p++ = used ? a->stencilLoadOp : 0; |
| 7587 | *p++ = used ? a->stencilStoreOp : 0; |
| 7588 | *p++ = used ? a->initialLayout : 0; |
| 7589 | *p++ = used ? a->finalLayout : 0; |
| 7590 | }; |
| 7591 | |
| 7592 | for (int i = 0, ie = colorRefs.size(); i != ie; ++i) { |
| 7593 | const uint32_t attIdx = colorRefs[i].attachment; |
| 7594 | *p++ = attIdx; |
| 7595 | serializeAttachmentData(attIdx); |
| 7596 | } |
| 7597 | |
| 7598 | if (hasDepthStencil) { |
| 7599 | const uint32_t attIdx = dsRef.attachment; |
| 7600 | *p++ = attIdx; |
| 7601 | serializeAttachmentData(attIdx); |
| 7602 | } |
| 7603 | |
| 7604 | for (int i = 0, ie = resolveRefs.size(); i != ie; ++i) { |
| 7605 | const uint32_t attIdx = resolveRefs[i].attachment; |
| 7606 | *p++ = attIdx; |
| 7607 | serializeAttachmentData(attIdx); |
| 7608 | } |
| 7609 | |
| 7610 | if (hasDepthStencilResolve) { |
| 7611 | const uint32_t attIdx = dsResolveRef.attachment; |
| 7612 | *p++ = attIdx; |
| 7613 | serializeAttachmentData(attIdx); |
| 7614 | } |
| 7615 | |
| 7616 | if (hasShadingRateMap) { |
| 7617 | const uint32_t attIdx = shadingRateRef.attachment; |
| 7618 | *p++ = attIdx; |
| 7619 | serializeAttachmentData(attIdx); |
| 7620 | } |
| 7621 | } |
| 7622 | |
| 7623 | QRhiRenderPassDescriptor *QVkRenderPassDescriptor::newCompatibleRenderPassDescriptor() const |
| 7624 | { |
| 7625 | QVkRenderPassDescriptor *rpD = new QVkRenderPassDescriptor(m_rhi); |
| 7626 | |
| 7627 | rpD->ownsRp = true; |
| 7628 | rpD->attDescs = attDescs; |
| 7629 | rpD->colorRefs = colorRefs; |
| 7630 | rpD->resolveRefs = resolveRefs; |
| 7631 | rpD->subpassDeps = subpassDeps; |
| 7632 | rpD->hasDepthStencil = hasDepthStencil; |
| 7633 | rpD->hasDepthStencilResolve = hasDepthStencilResolve; |
| 7634 | rpD->hasShadingRateMap = hasShadingRateMap; |
| 7635 | rpD->multiViewCount = multiViewCount; |
| 7636 | rpD->dsRef = dsRef; |
| 7637 | rpD->dsResolveRef = dsResolveRef; |
| 7638 | rpD->shadingRateRef = shadingRateRef; |
| 7639 | |
| 7640 | VkRenderPassCreateInfo rpInfo; |
| 7641 | VkSubpassDescription subpassDesc; |
| 7642 | fillRenderPassCreateInfo(rpInfo: &rpInfo, subpassDesc: &subpassDesc, rpD); |
| 7643 | |
| 7644 | QRHI_RES_RHI(QRhiVulkan); |
| 7645 | MultiViewRenderPassSetupHelper multiViewHelper; |
| 7646 | if (!multiViewHelper.prepare(rpInfo: &rpInfo, multiViewCount, multiViewCap: rhiD->caps.multiView)) { |
| 7647 | delete rpD; |
| 7648 | return nullptr; |
| 7649 | } |
| 7650 | |
| 7651 | #ifdef VK_KHR_create_renderpass2 |
| 7652 | if (rhiD->caps.renderPass2KHR) { |
| 7653 | // Use the KHR extension, not the 1.2 core API, in order to support Vulkan 1.1. |
| 7654 | VkRenderPassCreateInfo2KHR rpInfo2; |
| 7655 | RenderPass2SetupHelper rp2Helper(rhiD); |
| 7656 | if (!rp2Helper.prepare(rpInfo2: &rpInfo2, rpInfo: &rpInfo, rpD, multiViewCount)) { |
| 7657 | delete rpD; |
| 7658 | return nullptr; |
| 7659 | } |
| 7660 | VkResult err = rhiD->vkCreateRenderPass2KHR(rhiD->dev, &rpInfo2, nullptr, &rpD->rp); |
| 7661 | if (err != VK_SUCCESS) { |
| 7662 | qWarning(msg: "Failed to create renderpass (using VkRenderPassCreateInfo2KHR): %d" , err); |
| 7663 | delete rpD; |
| 7664 | return nullptr; |
| 7665 | } |
| 7666 | } else |
| 7667 | #endif |
| 7668 | { |
| 7669 | VkResult err = rhiD->df->vkCreateRenderPass(rhiD->dev, &rpInfo, nullptr, &rpD->rp); |
| 7670 | if (err != VK_SUCCESS) { |
| 7671 | qWarning(msg: "Failed to create renderpass: %d" , err); |
| 7672 | delete rpD; |
| 7673 | return nullptr; |
| 7674 | } |
| 7675 | } |
| 7676 | |
| 7677 | rpD->updateSerializedFormat(); |
| 7678 | rhiD->registerResource(res: rpD); |
| 7679 | return rpD; |
| 7680 | } |
| 7681 | |
| 7682 | QVector<quint32> QVkRenderPassDescriptor::serializedFormat() const |
| 7683 | { |
| 7684 | return serializedFormatData; |
| 7685 | } |
| 7686 | |
| 7687 | const QRhiNativeHandles *QVkRenderPassDescriptor::nativeHandles() |
| 7688 | { |
| 7689 | nativeHandlesStruct.renderPass = rp; |
| 7690 | return &nativeHandlesStruct; |
| 7691 | } |
| 7692 | |
| 7693 | QVkShadingRateMap::QVkShadingRateMap(QRhiImplementation *rhi) |
| 7694 | : QRhiShadingRateMap(rhi) |
| 7695 | { |
| 7696 | } |
| 7697 | |
| 7698 | QVkShadingRateMap::~QVkShadingRateMap() |
| 7699 | { |
| 7700 | destroy(); |
| 7701 | } |
| 7702 | |
| 7703 | void QVkShadingRateMap::destroy() |
| 7704 | { |
| 7705 | if (!texture) |
| 7706 | return; |
| 7707 | |
| 7708 | texture = nullptr; |
| 7709 | } |
| 7710 | |
| 7711 | bool QVkShadingRateMap::createFrom(QRhiTexture *src) |
| 7712 | { |
| 7713 | if (texture) |
| 7714 | destroy(); |
| 7715 | |
| 7716 | texture = QRHI_RES(QVkTexture, src); |
| 7717 | |
| 7718 | return true; |
| 7719 | } |
| 7720 | |
| 7721 | QVkSwapChainRenderTarget::QVkSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain) |
| 7722 | : QRhiSwapChainRenderTarget(rhi, swapchain) |
| 7723 | { |
| 7724 | } |
| 7725 | |
| 7726 | QVkSwapChainRenderTarget::~QVkSwapChainRenderTarget() |
| 7727 | { |
| 7728 | destroy(); |
| 7729 | } |
| 7730 | |
| 7731 | void QVkSwapChainRenderTarget::destroy() |
| 7732 | { |
| 7733 | // nothing to do here |
| 7734 | } |
| 7735 | |
| 7736 | QSize QVkSwapChainRenderTarget::pixelSize() const |
| 7737 | { |
| 7738 | return d.pixelSize; |
| 7739 | } |
| 7740 | |
| 7741 | float QVkSwapChainRenderTarget::devicePixelRatio() const |
| 7742 | { |
| 7743 | return d.dpr; |
| 7744 | } |
| 7745 | |
| 7746 | int QVkSwapChainRenderTarget::sampleCount() const |
| 7747 | { |
| 7748 | return d.sampleCount; |
| 7749 | } |
| 7750 | |
| 7751 | QVkTextureRenderTarget::QVkTextureRenderTarget(QRhiImplementation *rhi, |
| 7752 | const QRhiTextureRenderTargetDescription &desc, |
| 7753 | Flags flags) |
| 7754 | : QRhiTextureRenderTarget(rhi, desc, flags) |
| 7755 | { |
| 7756 | for (int att = 0; att < QVkRenderTargetData::MAX_COLOR_ATTACHMENTS; ++att) { |
| 7757 | rtv[att] = VK_NULL_HANDLE; |
| 7758 | resrtv[att] = VK_NULL_HANDLE; |
| 7759 | } |
| 7760 | } |
| 7761 | |
| 7762 | QVkTextureRenderTarget::~QVkTextureRenderTarget() |
| 7763 | { |
| 7764 | destroy(); |
| 7765 | } |
| 7766 | |
| 7767 | void QVkTextureRenderTarget::destroy() |
| 7768 | { |
| 7769 | if (!d.fb) |
| 7770 | return; |
| 7771 | |
| 7772 | QRhiVulkan::DeferredReleaseEntry e; |
| 7773 | e.type = QRhiVulkan::DeferredReleaseEntry::TextureRenderTarget; |
| 7774 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 7775 | |
| 7776 | e.textureRenderTarget.fb = d.fb; |
| 7777 | d.fb = VK_NULL_HANDLE; |
| 7778 | |
| 7779 | for (int att = 0; att < QVkRenderTargetData::MAX_COLOR_ATTACHMENTS; ++att) { |
| 7780 | e.textureRenderTarget.rtv[att] = rtv[att]; |
| 7781 | e.textureRenderTarget.resrtv[att] = resrtv[att]; |
| 7782 | rtv[att] = VK_NULL_HANDLE; |
| 7783 | resrtv[att] = VK_NULL_HANDLE; |
| 7784 | } |
| 7785 | |
| 7786 | e.textureRenderTarget.dsv = dsv; |
| 7787 | dsv = VK_NULL_HANDLE; |
| 7788 | e.textureRenderTarget.resdsv = resdsv; |
| 7789 | resdsv = VK_NULL_HANDLE; |
| 7790 | |
| 7791 | e.textureRenderTarget.shadingRateMapView = shadingRateMapView; |
| 7792 | shadingRateMapView = VK_NULL_HANDLE; |
| 7793 | |
| 7794 | QRHI_RES_RHI(QRhiVulkan); |
| 7795 | if (rhiD) { |
| 7796 | rhiD->releaseQueue.append(t: e); |
| 7797 | rhiD->unregisterResource(res: this); |
| 7798 | } |
| 7799 | } |
| 7800 | |
| 7801 | QRhiRenderPassDescriptor *QVkTextureRenderTarget::newCompatibleRenderPassDescriptor() |
| 7802 | { |
| 7803 | // not yet built so cannot rely on data computed in create() |
| 7804 | |
| 7805 | QRHI_RES_RHI(QRhiVulkan); |
| 7806 | QVkRenderPassDescriptor *rp = new QVkRenderPassDescriptor(m_rhi); |
| 7807 | if (!rhiD->createOffscreenRenderPass(rpD: rp, |
| 7808 | colorAttachmentsBegin: m_desc.cbeginColorAttachments(), |
| 7809 | colorAttachmentsEnd: m_desc.cendColorAttachments(), |
| 7810 | preserveColor: m_flags.testFlag(flag: QRhiTextureRenderTarget::PreserveColorContents), |
| 7811 | preserveDs: m_flags.testFlag(flag: QRhiTextureRenderTarget::PreserveDepthStencilContents), |
| 7812 | storeDs: m_desc.depthTexture() && !m_flags.testFlag(flag: DoNotStoreDepthStencilContents) && !m_desc.depthResolveTexture(), |
| 7813 | depthStencilBuffer: m_desc.depthStencilBuffer(), |
| 7814 | depthTexture: m_desc.depthTexture(), |
| 7815 | depthResolveTexture: m_desc.depthResolveTexture(), |
| 7816 | shadingRateMap: m_desc.shadingRateMap())) |
| 7817 | { |
| 7818 | delete rp; |
| 7819 | return nullptr; |
| 7820 | } |
| 7821 | |
| 7822 | rp->ownsRp = true; |
| 7823 | rp->updateSerializedFormat(); |
| 7824 | rhiD->registerResource(res: rp); |
| 7825 | return rp; |
| 7826 | } |
| 7827 | |
| 7828 | bool QVkTextureRenderTarget::create() |
| 7829 | { |
| 7830 | if (d.fb) |
| 7831 | destroy(); |
| 7832 | |
| 7833 | Q_ASSERT(m_desc.colorAttachmentCount() > 0 || m_desc.depthTexture()); |
| 7834 | Q_ASSERT(!m_desc.depthStencilBuffer() || !m_desc.depthTexture()); |
| 7835 | const bool hasDepthStencil = m_desc.depthStencilBuffer() || m_desc.depthTexture(); |
| 7836 | |
| 7837 | QRHI_RES_RHI(QRhiVulkan); |
| 7838 | QVarLengthArray<VkImageView, 8> views; |
| 7839 | d.multiViewCount = 0; |
| 7840 | |
| 7841 | d.colorAttCount = 0; |
| 7842 | int attIndex = 0; |
| 7843 | for (auto it = m_desc.cbeginColorAttachments(), itEnd = m_desc.cendColorAttachments(); it != itEnd; ++it, ++attIndex) { |
| 7844 | d.colorAttCount += 1; |
| 7845 | QVkTexture *texD = QRHI_RES(QVkTexture, it->texture()); |
| 7846 | QVkRenderBuffer *rbD = QRHI_RES(QVkRenderBuffer, it->renderBuffer()); |
| 7847 | Q_ASSERT(texD || rbD); |
| 7848 | if (texD) { |
| 7849 | Q_ASSERT(texD->flags().testFlag(QRhiTexture::RenderTarget)); |
| 7850 | const bool is1D = texD->flags().testFlag(flag: QRhiTexture::OneDimensional); |
| 7851 | const bool isMultiView = it->multiViewCount() >= 2; |
| 7852 | if (isMultiView && d.multiViewCount == 0) |
| 7853 | d.multiViewCount = it->multiViewCount(); |
| 7854 | VkImageViewCreateInfo viewInfo = {}; |
| 7855 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7856 | viewInfo.image = texD->image; |
| 7857 | viewInfo.viewType = is1D ? VK_IMAGE_VIEW_TYPE_1D |
| 7858 | : (isMultiView ? VK_IMAGE_VIEW_TYPE_2D_ARRAY |
| 7859 | : VK_IMAGE_VIEW_TYPE_2D); |
| 7860 | viewInfo.format = texD->viewFormat; |
| 7861 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 7862 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 7863 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 7864 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 7865 | viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 7866 | viewInfo.subresourceRange.baseMipLevel = uint32_t(it->level()); |
| 7867 | viewInfo.subresourceRange.levelCount = 1; |
| 7868 | viewInfo.subresourceRange.baseArrayLayer = uint32_t(it->layer()); |
| 7869 | viewInfo.subresourceRange.layerCount = uint32_t(isMultiView ? it->multiViewCount() : 1); |
| 7870 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &rtv[attIndex]); |
| 7871 | if (err != VK_SUCCESS) { |
| 7872 | qWarning(msg: "Failed to create render target image view: %d" , err); |
| 7873 | return false; |
| 7874 | } |
| 7875 | views.append(t: rtv[attIndex]); |
| 7876 | if (attIndex == 0) { |
| 7877 | d.pixelSize = rhiD->q->sizeForMipLevel(mipLevel: it->level(), baseLevelSize: texD->pixelSize()); |
| 7878 | d.sampleCount = texD->samples; |
| 7879 | } |
| 7880 | } else if (rbD) { |
| 7881 | Q_ASSERT(rbD->backingTexture); |
| 7882 | views.append(t: rbD->backingTexture->imageView); |
| 7883 | if (attIndex == 0) { |
| 7884 | d.pixelSize = rbD->pixelSize(); |
| 7885 | d.sampleCount = rbD->samples; |
| 7886 | } |
| 7887 | } |
| 7888 | } |
| 7889 | d.dpr = 1; |
| 7890 | |
| 7891 | if (hasDepthStencil) { |
| 7892 | if (m_desc.depthTexture()) { |
| 7893 | QVkTexture *depthTexD = QRHI_RES(QVkTexture, m_desc.depthTexture()); |
| 7894 | // need a dedicated view just because viewFormat may differ from vkformat |
| 7895 | VkImageViewCreateInfo viewInfo = {}; |
| 7896 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7897 | viewInfo.image = depthTexD->image; |
| 7898 | viewInfo.viewType = d.multiViewCount > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; |
| 7899 | viewInfo.format = depthTexD->viewFormat; |
| 7900 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 7901 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 7902 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 7903 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 7904 | viewInfo.subresourceRange.aspectMask = aspectMaskForTextureFormat(format: depthTexD->format()); |
| 7905 | viewInfo.subresourceRange.levelCount = 1; |
| 7906 | viewInfo.subresourceRange.layerCount = qMax<uint32_t>(a: 1, b: d.multiViewCount); |
| 7907 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &dsv); |
| 7908 | if (err != VK_SUCCESS) { |
| 7909 | qWarning(msg: "Failed to create depth-stencil image view for rt: %d" , err); |
| 7910 | return false; |
| 7911 | } |
| 7912 | views.append(t: dsv); |
| 7913 | if (d.colorAttCount == 0) { |
| 7914 | d.pixelSize = depthTexD->pixelSize(); |
| 7915 | d.sampleCount = depthTexD->samples; |
| 7916 | } |
| 7917 | } else { |
| 7918 | QVkRenderBuffer *depthRbD = QRHI_RES(QVkRenderBuffer, m_desc.depthStencilBuffer()); |
| 7919 | views.append(t: depthRbD->imageView); |
| 7920 | if (d.colorAttCount == 0) { |
| 7921 | d.pixelSize = depthRbD->pixelSize(); |
| 7922 | d.sampleCount = depthRbD->samples; |
| 7923 | } |
| 7924 | } |
| 7925 | d.dsAttCount = 1; |
| 7926 | } else { |
| 7927 | d.dsAttCount = 0; |
| 7928 | } |
| 7929 | |
| 7930 | d.resolveAttCount = 0; |
| 7931 | attIndex = 0; |
| 7932 | Q_ASSERT(d.multiViewCount == 0 || d.multiViewCount >= 2); |
| 7933 | for (auto it = m_desc.cbeginColorAttachments(), itEnd = m_desc.cendColorAttachments(); it != itEnd; ++it, ++attIndex) { |
| 7934 | if (it->resolveTexture()) { |
| 7935 | QVkTexture *resTexD = QRHI_RES(QVkTexture, it->resolveTexture()); |
| 7936 | Q_ASSERT(resTexD->flags().testFlag(QRhiTexture::RenderTarget)); |
| 7937 | d.resolveAttCount += 1; |
| 7938 | |
| 7939 | VkImageViewCreateInfo viewInfo = {}; |
| 7940 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7941 | viewInfo.image = resTexD->image; |
| 7942 | viewInfo.viewType = d.multiViewCount ? VK_IMAGE_VIEW_TYPE_2D_ARRAY |
| 7943 | : VK_IMAGE_VIEW_TYPE_2D; |
| 7944 | viewInfo.format = resTexD->viewFormat; |
| 7945 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 7946 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 7947 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 7948 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 7949 | viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 7950 | viewInfo.subresourceRange.baseMipLevel = uint32_t(it->resolveLevel()); |
| 7951 | viewInfo.subresourceRange.levelCount = 1; |
| 7952 | viewInfo.subresourceRange.baseArrayLayer = uint32_t(it->resolveLayer()); |
| 7953 | viewInfo.subresourceRange.layerCount = qMax<uint32_t>(a: 1, b: d.multiViewCount); |
| 7954 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &resrtv[attIndex]); |
| 7955 | if (err != VK_SUCCESS) { |
| 7956 | qWarning(msg: "Failed to create render target resolve image view: %d" , err); |
| 7957 | return false; |
| 7958 | } |
| 7959 | views.append(t: resrtv[attIndex]); |
| 7960 | } |
| 7961 | } |
| 7962 | |
| 7963 | if (m_desc.depthResolveTexture()) { |
| 7964 | QVkTexture *resTexD = QRHI_RES(QVkTexture, m_desc.depthResolveTexture()); |
| 7965 | Q_ASSERT(resTexD->flags().testFlag(QRhiTexture::RenderTarget)); |
| 7966 | |
| 7967 | VkImageViewCreateInfo viewInfo = {}; |
| 7968 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7969 | viewInfo.image = resTexD->image; |
| 7970 | viewInfo.viewType = d.multiViewCount ? VK_IMAGE_VIEW_TYPE_2D_ARRAY |
| 7971 | : VK_IMAGE_VIEW_TYPE_2D; |
| 7972 | viewInfo.format = resTexD->viewFormat; |
| 7973 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 7974 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 7975 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 7976 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 7977 | viewInfo.subresourceRange.aspectMask = aspectMaskForTextureFormat(format: resTexD->format()); |
| 7978 | viewInfo.subresourceRange.baseMipLevel = 0; |
| 7979 | viewInfo.subresourceRange.levelCount = 1; |
| 7980 | viewInfo.subresourceRange.baseArrayLayer = 0; |
| 7981 | viewInfo.subresourceRange.layerCount = qMax<uint32_t>(a: 1, b: d.multiViewCount); |
| 7982 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &resdsv); |
| 7983 | if (err != VK_SUCCESS) { |
| 7984 | qWarning(msg: "Failed to create render target depth resolve image view: %d" , err); |
| 7985 | return false; |
| 7986 | } |
| 7987 | views.append(t: resdsv); |
| 7988 | d.dsResolveAttCount = 1; |
| 7989 | } else { |
| 7990 | d.dsResolveAttCount = 0; |
| 7991 | } |
| 7992 | |
| 7993 | if (m_desc.shadingRateMap() && rhiD->caps.renderPass2KHR && rhiD->caps.imageBasedShadingRate) { |
| 7994 | QVkTexture *texD = QRHI_RES(QVkShadingRateMap, m_desc.shadingRateMap())->texture; |
| 7995 | Q_ASSERT(texD->flags().testFlag(QRhiTexture::UsedAsShadingRateMap)); |
| 7996 | |
| 7997 | VkImageViewCreateInfo viewInfo = {}; |
| 7998 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 7999 | viewInfo.image = texD->image; |
| 8000 | viewInfo.viewType = d.multiViewCount ? VK_IMAGE_VIEW_TYPE_2D_ARRAY |
| 8001 | : VK_IMAGE_VIEW_TYPE_2D; |
| 8002 | viewInfo.format = texD->viewFormat; |
| 8003 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 8004 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 8005 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 8006 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 8007 | viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 8008 | viewInfo.subresourceRange.baseMipLevel = 0; |
| 8009 | viewInfo.subresourceRange.levelCount = 1; |
| 8010 | viewInfo.subresourceRange.baseArrayLayer = 0; |
| 8011 | viewInfo.subresourceRange.layerCount = qMax<uint32_t>(a: 1, b: d.multiViewCount); |
| 8012 | VkResult err = rhiD->df->vkCreateImageView(rhiD->dev, &viewInfo, nullptr, &shadingRateMapView); |
| 8013 | if (err != VK_SUCCESS) { |
| 8014 | qWarning(msg: "Failed to create render target shading rate map view: %d" , err); |
| 8015 | return false; |
| 8016 | } |
| 8017 | views.append(t: shadingRateMapView); |
| 8018 | d.shadingRateAttCount = 1; |
| 8019 | } else { |
| 8020 | d.shadingRateAttCount = 0; |
| 8021 | } |
| 8022 | |
| 8023 | if (!m_renderPassDesc) |
| 8024 | qWarning(msg: "QVkTextureRenderTarget: No renderpass descriptor set. See newCompatibleRenderPassDescriptor() and setRenderPassDescriptor()." ); |
| 8025 | |
| 8026 | d.rp = QRHI_RES(QVkRenderPassDescriptor, m_renderPassDesc); |
| 8027 | Q_ASSERT(d.rp && d.rp->rp); |
| 8028 | |
| 8029 | VkFramebufferCreateInfo fbInfo = {}; |
| 8030 | fbInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 8031 | fbInfo.renderPass = d.rp->rp; |
| 8032 | fbInfo.attachmentCount = uint32_t(views.count()); |
| 8033 | fbInfo.pAttachments = views.constData(); |
| 8034 | fbInfo.width = uint32_t(d.pixelSize.width()); |
| 8035 | fbInfo.height = uint32_t(d.pixelSize.height()); |
| 8036 | fbInfo.layers = 1; |
| 8037 | |
| 8038 | VkResult err = rhiD->df->vkCreateFramebuffer(rhiD->dev, &fbInfo, nullptr, &d.fb); |
| 8039 | if (err != VK_SUCCESS) { |
| 8040 | qWarning(msg: "Failed to create framebuffer: %d" , err); |
| 8041 | return false; |
| 8042 | } |
| 8043 | |
| 8044 | QRhiRenderTargetAttachmentTracker::updateResIdList<QVkTexture, QVkRenderBuffer>(desc: m_desc, dst: &d.currentResIdList); |
| 8045 | |
| 8046 | lastActiveFrameSlot = -1; |
| 8047 | rhiD->registerResource(res: this); |
| 8048 | return true; |
| 8049 | } |
| 8050 | |
| 8051 | QSize QVkTextureRenderTarget::pixelSize() const |
| 8052 | { |
| 8053 | if (!QRhiRenderTargetAttachmentTracker::isUpToDate<QVkTexture, QVkRenderBuffer>(desc: m_desc, currentResIdList: d.currentResIdList)) |
| 8054 | const_cast<QVkTextureRenderTarget *>(this)->create(); |
| 8055 | |
| 8056 | return d.pixelSize; |
| 8057 | } |
| 8058 | |
| 8059 | float QVkTextureRenderTarget::devicePixelRatio() const |
| 8060 | { |
| 8061 | return d.dpr; |
| 8062 | } |
| 8063 | |
| 8064 | int QVkTextureRenderTarget::sampleCount() const |
| 8065 | { |
| 8066 | return d.sampleCount; |
| 8067 | } |
| 8068 | |
| 8069 | QVkShaderResourceBindings::QVkShaderResourceBindings(QRhiImplementation *rhi) |
| 8070 | : QRhiShaderResourceBindings(rhi) |
| 8071 | { |
| 8072 | } |
| 8073 | |
| 8074 | QVkShaderResourceBindings::~QVkShaderResourceBindings() |
| 8075 | { |
| 8076 | destroy(); |
| 8077 | } |
| 8078 | |
| 8079 | void QVkShaderResourceBindings::destroy() |
| 8080 | { |
| 8081 | if (!layout) |
| 8082 | return; |
| 8083 | |
| 8084 | sortedBindings.clear(); |
| 8085 | |
| 8086 | QRhiVulkan::DeferredReleaseEntry e; |
| 8087 | e.type = QRhiVulkan::DeferredReleaseEntry::ShaderResourceBindings; |
| 8088 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 8089 | |
| 8090 | e.shaderResourceBindings.poolIndex = poolIndex; |
| 8091 | e.shaderResourceBindings.layout = layout; |
| 8092 | |
| 8093 | poolIndex = -1; |
| 8094 | layout = VK_NULL_HANDLE; |
| 8095 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) |
| 8096 | descSets[i] = VK_NULL_HANDLE; |
| 8097 | |
| 8098 | QRHI_RES_RHI(QRhiVulkan); |
| 8099 | if (rhiD) { |
| 8100 | rhiD->releaseQueue.append(t: e); |
| 8101 | rhiD->unregisterResource(res: this); |
| 8102 | } |
| 8103 | } |
| 8104 | |
| 8105 | bool QVkShaderResourceBindings::create() |
| 8106 | { |
| 8107 | if (layout) |
| 8108 | destroy(); |
| 8109 | |
| 8110 | QRHI_RES_RHI(QRhiVulkan); |
| 8111 | if (!rhiD->sanityCheckShaderResourceBindings(srb: this)) |
| 8112 | return false; |
| 8113 | |
| 8114 | rhiD->updateLayoutDesc(srb: this); |
| 8115 | |
| 8116 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) |
| 8117 | descSets[i] = VK_NULL_HANDLE; |
| 8118 | |
| 8119 | sortedBindings.clear(); |
| 8120 | std::copy(first: m_bindings.cbegin(), last: m_bindings.cend(), result: std::back_inserter(x&: sortedBindings)); |
| 8121 | std::sort(first: sortedBindings.begin(), last: sortedBindings.end(), comp: QRhiImplementation::sortedBindingLessThan); |
| 8122 | |
| 8123 | hasDynamicOffset = false; |
| 8124 | for (const QRhiShaderResourceBinding &binding : std::as_const(t&: sortedBindings)) { |
| 8125 | const QRhiShaderResourceBinding::Data *b = QRhiImplementation::shaderResourceBindingData(binding); |
| 8126 | if (b->type == QRhiShaderResourceBinding::UniformBuffer && b->u.ubuf.buf) { |
| 8127 | if (b->u.ubuf.hasDynamicOffset) |
| 8128 | hasDynamicOffset = true; |
| 8129 | } |
| 8130 | } |
| 8131 | |
| 8132 | QVarLengthArray<VkDescriptorSetLayoutBinding, 4> vkbindings; |
| 8133 | for (const QRhiShaderResourceBinding &binding : std::as_const(t&: sortedBindings)) { |
| 8134 | const QRhiShaderResourceBinding::Data *b = QRhiImplementation::shaderResourceBindingData(binding); |
| 8135 | VkDescriptorSetLayoutBinding vkbinding = {}; |
| 8136 | vkbinding.binding = uint32_t(b->binding); |
| 8137 | vkbinding.descriptorType = toVkDescriptorType(b); |
| 8138 | if (b->type == QRhiShaderResourceBinding::SampledTexture || b->type == QRhiShaderResourceBinding::Texture) |
| 8139 | vkbinding.descriptorCount = b->u.stex.count; |
| 8140 | else |
| 8141 | vkbinding.descriptorCount = 1; |
| 8142 | vkbinding.stageFlags = toVkShaderStageFlags(stage: b->stage); |
| 8143 | vkbindings.append(t: vkbinding); |
| 8144 | } |
| 8145 | |
| 8146 | VkDescriptorSetLayoutCreateInfo layoutInfo = {}; |
| 8147 | layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 8148 | layoutInfo.bindingCount = uint32_t(vkbindings.size()); |
| 8149 | layoutInfo.pBindings = vkbindings.constData(); |
| 8150 | |
| 8151 | VkResult err = rhiD->df->vkCreateDescriptorSetLayout(rhiD->dev, &layoutInfo, nullptr, &layout); |
| 8152 | if (err != VK_SUCCESS) { |
| 8153 | qWarning(msg: "Failed to create descriptor set layout: %d" , err); |
| 8154 | return false; |
| 8155 | } |
| 8156 | |
| 8157 | VkDescriptorSetAllocateInfo allocInfo = {}; |
| 8158 | allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 8159 | allocInfo.descriptorSetCount = QVK_FRAMES_IN_FLIGHT; |
| 8160 | VkDescriptorSetLayout layouts[QVK_FRAMES_IN_FLIGHT]; |
| 8161 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) |
| 8162 | layouts[i] = layout; |
| 8163 | allocInfo.pSetLayouts = layouts; |
| 8164 | if (!rhiD->allocateDescriptorSet(allocInfo: &allocInfo, result: descSets, resultPoolIndex: &poolIndex)) |
| 8165 | return false; |
| 8166 | |
| 8167 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 8168 | boundResourceData[i].resize(sz: sortedBindings.size()); |
| 8169 | for (BoundResourceData &bd : boundResourceData[i]) |
| 8170 | memset(s: &bd, c: 0, n: sizeof(BoundResourceData)); |
| 8171 | } |
| 8172 | |
| 8173 | lastActiveFrameSlot = -1; |
| 8174 | generation += 1; |
| 8175 | rhiD->registerResource(res: this); |
| 8176 | return true; |
| 8177 | } |
| 8178 | |
| 8179 | void QVkShaderResourceBindings::updateResources(UpdateFlags flags) |
| 8180 | { |
| 8181 | sortedBindings.clear(); |
| 8182 | std::copy(first: m_bindings.cbegin(), last: m_bindings.cend(), result: std::back_inserter(x&: sortedBindings)); |
| 8183 | if (!flags.testFlag(flag: BindingsAreSorted)) |
| 8184 | std::sort(first: sortedBindings.begin(), last: sortedBindings.end(), comp: QRhiImplementation::sortedBindingLessThan); |
| 8185 | |
| 8186 | // Reset the state tracking table too - it can deal with assigning a |
| 8187 | // different QRhiBuffer/Texture/Sampler for a binding point, but it cannot |
| 8188 | // detect changes in the associated data, such as the buffer offset. And |
| 8189 | // just like after a create(), a call to updateResources() may lead to now |
| 8190 | // specifying a different offset for the same QRhiBuffer for a given binding |
| 8191 | // point. The same applies to other type of associated data that is not part |
| 8192 | // of the layout, such as the mip level for a StorageImage. Instead of |
| 8193 | // complicating the checks in setShaderResources(), reset the table here |
| 8194 | // just like we do in create(). |
| 8195 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 8196 | Q_ASSERT(boundResourceData[i].size() == sortedBindings.size()); |
| 8197 | for (BoundResourceData &bd : boundResourceData[i]) |
| 8198 | memset(s: &bd, c: 0, n: sizeof(BoundResourceData)); |
| 8199 | } |
| 8200 | |
| 8201 | generation += 1; |
| 8202 | } |
| 8203 | |
| 8204 | QVkGraphicsPipeline::QVkGraphicsPipeline(QRhiImplementation *rhi) |
| 8205 | : QRhiGraphicsPipeline(rhi) |
| 8206 | { |
| 8207 | } |
| 8208 | |
| 8209 | QVkGraphicsPipeline::~QVkGraphicsPipeline() |
| 8210 | { |
| 8211 | destroy(); |
| 8212 | } |
| 8213 | |
| 8214 | void QVkGraphicsPipeline::destroy() |
| 8215 | { |
| 8216 | if (!pipeline && !layout) |
| 8217 | return; |
| 8218 | |
| 8219 | QRhiVulkan::DeferredReleaseEntry e; |
| 8220 | e.type = QRhiVulkan::DeferredReleaseEntry::Pipeline; |
| 8221 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 8222 | |
| 8223 | e.pipelineState.pipeline = pipeline; |
| 8224 | e.pipelineState.layout = layout; |
| 8225 | |
| 8226 | pipeline = VK_NULL_HANDLE; |
| 8227 | layout = VK_NULL_HANDLE; |
| 8228 | |
| 8229 | QRHI_RES_RHI(QRhiVulkan); |
| 8230 | if (rhiD) { |
| 8231 | rhiD->releaseQueue.append(t: e); |
| 8232 | rhiD->unregisterResource(res: this); |
| 8233 | } |
| 8234 | } |
| 8235 | |
| 8236 | bool QVkGraphicsPipeline::create() |
| 8237 | { |
| 8238 | if (pipeline) |
| 8239 | destroy(); |
| 8240 | |
| 8241 | QRHI_RES_RHI(QRhiVulkan); |
| 8242 | rhiD->pipelineCreationStart(); |
| 8243 | if (!rhiD->sanityCheckGraphicsPipeline(ps: this)) |
| 8244 | return false; |
| 8245 | |
| 8246 | if (!rhiD->ensurePipelineCache()) |
| 8247 | return false; |
| 8248 | |
| 8249 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; |
| 8250 | pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 8251 | pipelineLayoutInfo.setLayoutCount = 1; |
| 8252 | QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, m_shaderResourceBindings); |
| 8253 | Q_ASSERT(m_shaderResourceBindings && srbD->layout); |
| 8254 | pipelineLayoutInfo.pSetLayouts = &srbD->layout; |
| 8255 | VkResult err = rhiD->df->vkCreatePipelineLayout(rhiD->dev, &pipelineLayoutInfo, nullptr, &layout); |
| 8256 | if (err != VK_SUCCESS) { |
| 8257 | qWarning(msg: "Failed to create pipeline layout: %d" , err); |
| 8258 | return false; |
| 8259 | } |
| 8260 | |
| 8261 | VkGraphicsPipelineCreateInfo pipelineInfo = {}; |
| 8262 | pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 8263 | |
| 8264 | QVarLengthArray<VkShaderModule, 4> shaders; |
| 8265 | QVarLengthArray<VkPipelineShaderStageCreateInfo, 4> shaderStageCreateInfos; |
| 8266 | for (const QRhiShaderStage &shaderStage : m_shaderStages) { |
| 8267 | const QShader bakedShader = shaderStage.shader(); |
| 8268 | const QShaderCode spirv = bakedShader.shader(key: { QShader::SpirvShader, 100, shaderStage.shaderVariant() }); |
| 8269 | if (spirv.shader().isEmpty()) { |
| 8270 | qWarning() << "No SPIR-V 1.0 shader code found in baked shader" << bakedShader; |
| 8271 | return false; |
| 8272 | } |
| 8273 | VkShaderModule shader = rhiD->createShader(spirv: spirv.shader()); |
| 8274 | if (shader) { |
| 8275 | shaders.append(t: shader); |
| 8276 | VkPipelineShaderStageCreateInfo shaderInfo = {}; |
| 8277 | shaderInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 8278 | shaderInfo.stage = toVkShaderStage(type: shaderStage.type()); |
| 8279 | shaderInfo.module = shader; |
| 8280 | shaderInfo.pName = spirv.entryPoint().constData(); |
| 8281 | shaderStageCreateInfos.append(t: shaderInfo); |
| 8282 | } |
| 8283 | } |
| 8284 | pipelineInfo.stageCount = uint32_t(shaderStageCreateInfos.size()); |
| 8285 | pipelineInfo.pStages = shaderStageCreateInfos.constData(); |
| 8286 | |
| 8287 | QVarLengthArray<VkVertexInputBindingDescription, 4> vertexBindings; |
| 8288 | #ifdef VK_EXT_vertex_attribute_divisor |
| 8289 | QVarLengthArray<VkVertexInputBindingDivisorDescriptionEXT> nonOneStepRates; |
| 8290 | #endif |
| 8291 | int bindingIndex = 0; |
| 8292 | for (auto it = m_vertexInputLayout.cbeginBindings(), itEnd = m_vertexInputLayout.cendBindings(); |
| 8293 | it != itEnd; ++it, ++bindingIndex) |
| 8294 | { |
| 8295 | VkVertexInputBindingDescription bindingInfo = { |
| 8296 | .binding: uint32_t(bindingIndex), |
| 8297 | .stride: it->stride(), |
| 8298 | .inputRate: it->classification() == QRhiVertexInputBinding::PerVertex |
| 8299 | ? VK_VERTEX_INPUT_RATE_VERTEX : VK_VERTEX_INPUT_RATE_INSTANCE |
| 8300 | }; |
| 8301 | if (it->classification() == QRhiVertexInputBinding::PerInstance && it->instanceStepRate() != 1) { |
| 8302 | #ifdef VK_EXT_vertex_attribute_divisor |
| 8303 | if (rhiD->caps.vertexAttribDivisor) { |
| 8304 | nonOneStepRates.append(t: { .binding: uint32_t(bindingIndex), .divisor: it->instanceStepRate() }); |
| 8305 | } else |
| 8306 | #endif |
| 8307 | { |
| 8308 | qWarning(msg: "QRhiVulkan: Instance step rates other than 1 not supported without " |
| 8309 | "VK_EXT_vertex_attribute_divisor on the device and " |
| 8310 | "VK_KHR_get_physical_device_properties2 on the instance" ); |
| 8311 | } |
| 8312 | } |
| 8313 | vertexBindings.append(t: bindingInfo); |
| 8314 | } |
| 8315 | QVarLengthArray<VkVertexInputAttributeDescription, 4> vertexAttributes; |
| 8316 | for (auto it = m_vertexInputLayout.cbeginAttributes(), itEnd = m_vertexInputLayout.cendAttributes(); |
| 8317 | it != itEnd; ++it) |
| 8318 | { |
| 8319 | VkVertexInputAttributeDescription attributeInfo = { |
| 8320 | .location: uint32_t(it->location()), |
| 8321 | .binding: uint32_t(it->binding()), |
| 8322 | .format: toVkAttributeFormat(format: it->format()), |
| 8323 | .offset: it->offset() |
| 8324 | }; |
| 8325 | vertexAttributes.append(t: attributeInfo); |
| 8326 | } |
| 8327 | VkPipelineVertexInputStateCreateInfo vertexInputInfo = {}; |
| 8328 | vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 8329 | vertexInputInfo.vertexBindingDescriptionCount = uint32_t(vertexBindings.size()); |
| 8330 | vertexInputInfo.pVertexBindingDescriptions = vertexBindings.constData(); |
| 8331 | vertexInputInfo.vertexAttributeDescriptionCount = uint32_t(vertexAttributes.size()); |
| 8332 | vertexInputInfo.pVertexAttributeDescriptions = vertexAttributes.constData(); |
| 8333 | #ifdef VK_EXT_vertex_attribute_divisor |
| 8334 | VkPipelineVertexInputDivisorStateCreateInfoEXT divisorInfo = {}; |
| 8335 | if (!nonOneStepRates.isEmpty()) { |
| 8336 | divisorInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT; |
| 8337 | divisorInfo.vertexBindingDivisorCount = uint32_t(nonOneStepRates.size()); |
| 8338 | divisorInfo.pVertexBindingDivisors = nonOneStepRates.constData(); |
| 8339 | vertexInputInfo.pNext = &divisorInfo; |
| 8340 | } |
| 8341 | #endif |
| 8342 | pipelineInfo.pVertexInputState = &vertexInputInfo; |
| 8343 | |
| 8344 | QVarLengthArray<VkDynamicState, 8> dynEnable; |
| 8345 | dynEnable << VK_DYNAMIC_STATE_VIEWPORT; |
| 8346 | dynEnable << VK_DYNAMIC_STATE_SCISSOR; // ignore UsesScissor - Vulkan requires a scissor for the viewport always |
| 8347 | if (m_flags.testFlag(flag: QRhiGraphicsPipeline::UsesBlendConstants)) |
| 8348 | dynEnable << VK_DYNAMIC_STATE_BLEND_CONSTANTS; |
| 8349 | if (m_flags.testFlag(flag: QRhiGraphicsPipeline::UsesStencilRef)) |
| 8350 | dynEnable << VK_DYNAMIC_STATE_STENCIL_REFERENCE; |
| 8351 | #ifdef VK_KHR_fragment_shading_rate |
| 8352 | if (m_flags.testFlag(flag: QRhiGraphicsPipeline::UsesShadingRate) && rhiD->caps.perDrawShadingRate) |
| 8353 | dynEnable << VK_DYNAMIC_STATE_FRAGMENT_SHADING_RATE_KHR; |
| 8354 | #endif |
| 8355 | |
| 8356 | VkPipelineDynamicStateCreateInfo dynamicInfo = {}; |
| 8357 | dynamicInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; |
| 8358 | dynamicInfo.dynamicStateCount = uint32_t(dynEnable.size()); |
| 8359 | dynamicInfo.pDynamicStates = dynEnable.constData(); |
| 8360 | pipelineInfo.pDynamicState = &dynamicInfo; |
| 8361 | |
| 8362 | VkPipelineViewportStateCreateInfo viewportInfo = {}; |
| 8363 | viewportInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
| 8364 | viewportInfo.viewportCount = viewportInfo.scissorCount = 1; |
| 8365 | pipelineInfo.pViewportState = &viewportInfo; |
| 8366 | |
| 8367 | VkPipelineInputAssemblyStateCreateInfo inputAsmInfo = {}; |
| 8368 | inputAsmInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
| 8369 | inputAsmInfo.topology = toVkTopology(t: m_topology); |
| 8370 | inputAsmInfo.primitiveRestartEnable = (m_topology == TriangleStrip || m_topology == LineStrip); |
| 8371 | pipelineInfo.pInputAssemblyState = &inputAsmInfo; |
| 8372 | |
| 8373 | VkPipelineTessellationStateCreateInfo tessInfo = {}; |
| 8374 | #ifdef VK_VERSION_1_1 |
| 8375 | VkPipelineTessellationDomainOriginStateCreateInfo originInfo = {}; |
| 8376 | #endif |
| 8377 | if (m_topology == Patches) { |
| 8378 | tessInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO; |
| 8379 | tessInfo.patchControlPoints = uint32_t(qMax(a: 1, b: m_patchControlPointCount)); |
| 8380 | |
| 8381 | // To be able to use the same tess.evaluation shader with both OpenGL |
| 8382 | // and Vulkan, flip the tessellation domain origin to be lower left. |
| 8383 | // This allows declaring the winding order in the shader to be CCW and |
| 8384 | // still have it working with both APIs. This requires Vulkan 1.1 (or |
| 8385 | // VK_KHR_maintenance2 but don't bother with that). |
| 8386 | #ifdef VK_VERSION_1_1 |
| 8387 | if (rhiD->caps.apiVersion >= QVersionNumber(1, 1)) { |
| 8388 | originInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO; |
| 8389 | originInfo.domainOrigin = VK_TESSELLATION_DOMAIN_ORIGIN_LOWER_LEFT; |
| 8390 | tessInfo.pNext = &originInfo; |
| 8391 | } else { |
| 8392 | qWarning(msg: "Proper tessellation support requires Vulkan 1.1 or newer, leaving domain origin unset" ); |
| 8393 | } |
| 8394 | #else |
| 8395 | qWarning("QRhi was built without Vulkan 1.1 headers, this is not sufficient for proper tessellation support" ); |
| 8396 | #endif |
| 8397 | |
| 8398 | pipelineInfo.pTessellationState = &tessInfo; |
| 8399 | } |
| 8400 | |
| 8401 | VkPipelineRasterizationStateCreateInfo rastInfo = {}; |
| 8402 | rastInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; |
| 8403 | rastInfo.cullMode = toVkCullMode(c: m_cullMode); |
| 8404 | rastInfo.frontFace = toVkFrontFace(f: m_frontFace); |
| 8405 | if (m_depthBias != 0 || !qFuzzyIsNull(f: m_slopeScaledDepthBias)) { |
| 8406 | rastInfo.depthBiasEnable = true; |
| 8407 | rastInfo.depthBiasConstantFactor = float(m_depthBias); |
| 8408 | rastInfo.depthBiasSlopeFactor = m_slopeScaledDepthBias; |
| 8409 | } |
| 8410 | rastInfo.lineWidth = rhiD->caps.wideLines ? m_lineWidth : 1.0f; |
| 8411 | rastInfo.polygonMode = toVkPolygonMode(mode: m_polygonMode); |
| 8412 | pipelineInfo.pRasterizationState = &rastInfo; |
| 8413 | |
| 8414 | VkPipelineMultisampleStateCreateInfo msInfo = {}; |
| 8415 | msInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
| 8416 | msInfo.rasterizationSamples = rhiD->effectiveSampleCountBits(sampleCount: m_sampleCount); |
| 8417 | pipelineInfo.pMultisampleState = &msInfo; |
| 8418 | |
| 8419 | VkPipelineDepthStencilStateCreateInfo dsInfo = {}; |
| 8420 | dsInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 8421 | dsInfo.depthTestEnable = m_depthTest; |
| 8422 | dsInfo.depthWriteEnable = m_depthWrite; |
| 8423 | dsInfo.depthCompareOp = toVkCompareOp(op: m_depthOp); |
| 8424 | dsInfo.stencilTestEnable = m_stencilTest; |
| 8425 | if (m_stencilTest) { |
| 8426 | fillVkStencilOpState(dst: &dsInfo.front, src: m_stencilFront); |
| 8427 | dsInfo.front.compareMask = m_stencilReadMask; |
| 8428 | dsInfo.front.writeMask = m_stencilWriteMask; |
| 8429 | fillVkStencilOpState(dst: &dsInfo.back, src: m_stencilBack); |
| 8430 | dsInfo.back.compareMask = m_stencilReadMask; |
| 8431 | dsInfo.back.writeMask = m_stencilWriteMask; |
| 8432 | } |
| 8433 | pipelineInfo.pDepthStencilState = &dsInfo; |
| 8434 | |
| 8435 | VkPipelineColorBlendStateCreateInfo blendInfo = {}; |
| 8436 | blendInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 8437 | QVarLengthArray<VkPipelineColorBlendAttachmentState, 4> vktargetBlends; |
| 8438 | for (const QRhiGraphicsPipeline::TargetBlend &b : std::as_const(t&: m_targetBlends)) { |
| 8439 | VkPipelineColorBlendAttachmentState blend = {}; |
| 8440 | blend.blendEnable = b.enable; |
| 8441 | blend.srcColorBlendFactor = toVkBlendFactor(f: b.srcColor); |
| 8442 | blend.dstColorBlendFactor = toVkBlendFactor(f: b.dstColor); |
| 8443 | blend.colorBlendOp = toVkBlendOp(op: b.opColor); |
| 8444 | blend.srcAlphaBlendFactor = toVkBlendFactor(f: b.srcAlpha); |
| 8445 | blend.dstAlphaBlendFactor = toVkBlendFactor(f: b.dstAlpha); |
| 8446 | blend.alphaBlendOp = toVkBlendOp(op: b.opAlpha); |
| 8447 | blend.colorWriteMask = toVkColorComponents(c: b.colorWrite); |
| 8448 | vktargetBlends.append(t: blend); |
| 8449 | } |
| 8450 | if (vktargetBlends.isEmpty()) { |
| 8451 | VkPipelineColorBlendAttachmentState blend = {}; |
| 8452 | blend.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
| 8453 | | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; |
| 8454 | vktargetBlends.append(t: blend); |
| 8455 | } |
| 8456 | blendInfo.attachmentCount = uint32_t(vktargetBlends.size()); |
| 8457 | blendInfo.pAttachments = vktargetBlends.constData(); |
| 8458 | pipelineInfo.pColorBlendState = &blendInfo; |
| 8459 | |
| 8460 | pipelineInfo.layout = layout; |
| 8461 | |
| 8462 | Q_ASSERT(m_renderPassDesc && QRHI_RES(const QVkRenderPassDescriptor, m_renderPassDesc)->rp); |
| 8463 | pipelineInfo.renderPass = QRHI_RES(const QVkRenderPassDescriptor, m_renderPassDesc)->rp; |
| 8464 | |
| 8465 | err = rhiD->df->vkCreateGraphicsPipelines(rhiD->dev, rhiD->pipelineCache, 1, &pipelineInfo, nullptr, &pipeline); |
| 8466 | |
| 8467 | for (VkShaderModule shader : shaders) |
| 8468 | rhiD->df->vkDestroyShaderModule(rhiD->dev, shader, nullptr); |
| 8469 | |
| 8470 | if (err != VK_SUCCESS) { |
| 8471 | qWarning(msg: "Failed to create graphics pipeline: %d" , err); |
| 8472 | return false; |
| 8473 | } |
| 8474 | |
| 8475 | rhiD->setObjectName(object: uint64_t(pipeline), type: VK_OBJECT_TYPE_PIPELINE, name: m_objectName); |
| 8476 | |
| 8477 | rhiD->pipelineCreationEnd(); |
| 8478 | lastActiveFrameSlot = -1; |
| 8479 | generation += 1; |
| 8480 | rhiD->registerResource(res: this); |
| 8481 | return true; |
| 8482 | } |
| 8483 | |
| 8484 | QVkComputePipeline::QVkComputePipeline(QRhiImplementation *rhi) |
| 8485 | : QRhiComputePipeline(rhi) |
| 8486 | { |
| 8487 | } |
| 8488 | |
| 8489 | QVkComputePipeline::~QVkComputePipeline() |
| 8490 | { |
| 8491 | destroy(); |
| 8492 | } |
| 8493 | |
| 8494 | void QVkComputePipeline::destroy() |
| 8495 | { |
| 8496 | if (!pipeline && !layout) |
| 8497 | return; |
| 8498 | |
| 8499 | QRhiVulkan::DeferredReleaseEntry e; |
| 8500 | e.type = QRhiVulkan::DeferredReleaseEntry::Pipeline; |
| 8501 | e.lastActiveFrameSlot = lastActiveFrameSlot; |
| 8502 | |
| 8503 | e.pipelineState.pipeline = pipeline; |
| 8504 | e.pipelineState.layout = layout; |
| 8505 | |
| 8506 | pipeline = VK_NULL_HANDLE; |
| 8507 | layout = VK_NULL_HANDLE; |
| 8508 | |
| 8509 | QRHI_RES_RHI(QRhiVulkan); |
| 8510 | if (rhiD) { |
| 8511 | rhiD->releaseQueue.append(t: e); |
| 8512 | rhiD->unregisterResource(res: this); |
| 8513 | } |
| 8514 | } |
| 8515 | |
| 8516 | bool QVkComputePipeline::create() |
| 8517 | { |
| 8518 | if (pipeline) |
| 8519 | destroy(); |
| 8520 | |
| 8521 | QRHI_RES_RHI(QRhiVulkan); |
| 8522 | rhiD->pipelineCreationStart(); |
| 8523 | if (!rhiD->ensurePipelineCache()) |
| 8524 | return false; |
| 8525 | |
| 8526 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; |
| 8527 | pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 8528 | pipelineLayoutInfo.setLayoutCount = 1; |
| 8529 | QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, m_shaderResourceBindings); |
| 8530 | Q_ASSERT(m_shaderResourceBindings && srbD->layout); |
| 8531 | pipelineLayoutInfo.pSetLayouts = &srbD->layout; |
| 8532 | VkResult err = rhiD->df->vkCreatePipelineLayout(rhiD->dev, &pipelineLayoutInfo, nullptr, &layout); |
| 8533 | if (err != VK_SUCCESS) { |
| 8534 | qWarning(msg: "Failed to create pipeline layout: %d" , err); |
| 8535 | return false; |
| 8536 | } |
| 8537 | |
| 8538 | VkComputePipelineCreateInfo pipelineInfo = {}; |
| 8539 | pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; |
| 8540 | pipelineInfo.layout = layout; |
| 8541 | |
| 8542 | if (m_shaderStage.type() != QRhiShaderStage::Compute) { |
| 8543 | qWarning(msg: "Compute pipeline requires a compute shader stage" ); |
| 8544 | return false; |
| 8545 | } |
| 8546 | const QShader bakedShader = m_shaderStage.shader(); |
| 8547 | const QShaderCode spirv = bakedShader.shader(key: { QShader::SpirvShader, 100, m_shaderStage.shaderVariant() }); |
| 8548 | if (spirv.shader().isEmpty()) { |
| 8549 | qWarning() << "No SPIR-V 1.0 shader code found in baked shader" << bakedShader; |
| 8550 | return false; |
| 8551 | } |
| 8552 | if (bakedShader.stage() != QShader::ComputeStage) { |
| 8553 | qWarning() << bakedShader << "is not a compute shader" ; |
| 8554 | return false; |
| 8555 | } |
| 8556 | VkShaderModule shader = rhiD->createShader(spirv: spirv.shader()); |
| 8557 | VkPipelineShaderStageCreateInfo shaderInfo = {}; |
| 8558 | shaderInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 8559 | shaderInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT; |
| 8560 | shaderInfo.module = shader; |
| 8561 | shaderInfo.pName = spirv.entryPoint().constData(); |
| 8562 | pipelineInfo.stage = shaderInfo; |
| 8563 | |
| 8564 | err = rhiD->df->vkCreateComputePipelines(rhiD->dev, rhiD->pipelineCache, 1, &pipelineInfo, nullptr, &pipeline); |
| 8565 | rhiD->df->vkDestroyShaderModule(rhiD->dev, shader, nullptr); |
| 8566 | if (err != VK_SUCCESS) { |
| 8567 | qWarning(msg: "Failed to create graphics pipeline: %d" , err); |
| 8568 | return false; |
| 8569 | } |
| 8570 | |
| 8571 | rhiD->setObjectName(object: uint64_t(pipeline), type: VK_OBJECT_TYPE_PIPELINE, name: m_objectName); |
| 8572 | |
| 8573 | rhiD->pipelineCreationEnd(); |
| 8574 | lastActiveFrameSlot = -1; |
| 8575 | generation += 1; |
| 8576 | rhiD->registerResource(res: this); |
| 8577 | return true; |
| 8578 | } |
| 8579 | |
| 8580 | QVkCommandBuffer::QVkCommandBuffer(QRhiImplementation *rhi) |
| 8581 | : QRhiCommandBuffer(rhi) |
| 8582 | { |
| 8583 | resetState(); |
| 8584 | } |
| 8585 | |
| 8586 | QVkCommandBuffer::~QVkCommandBuffer() |
| 8587 | { |
| 8588 | destroy(); |
| 8589 | } |
| 8590 | |
| 8591 | void QVkCommandBuffer::destroy() |
| 8592 | { |
| 8593 | // nothing to do here, cb is not owned by us |
| 8594 | } |
| 8595 | |
| 8596 | const QRhiNativeHandles *QVkCommandBuffer::nativeHandles() |
| 8597 | { |
| 8598 | // Ok this is messy but no other way has been devised yet. Outside |
| 8599 | // begin(Compute)Pass - end(Compute)Pass it is simple - just return the |
| 8600 | // primary VkCommandBuffer. Inside, however, we need to provide the current |
| 8601 | // secondary command buffer (typically the one started by beginExternal(), |
| 8602 | // in case we are between beginExternal - endExternal inside a pass). |
| 8603 | |
| 8604 | if (recordingPass == QVkCommandBuffer::NoPass) { |
| 8605 | nativeHandlesStruct.commandBuffer = cb; |
| 8606 | } else { |
| 8607 | if (passUsesSecondaryCb && !activeSecondaryCbStack.isEmpty()) |
| 8608 | nativeHandlesStruct.commandBuffer = activeSecondaryCbStack.last(); |
| 8609 | else |
| 8610 | nativeHandlesStruct.commandBuffer = cb; |
| 8611 | } |
| 8612 | |
| 8613 | return &nativeHandlesStruct; |
| 8614 | } |
| 8615 | |
| 8616 | QVkSwapChain::QVkSwapChain(QRhiImplementation *rhi) |
| 8617 | : QRhiSwapChain(rhi), |
| 8618 | rtWrapper(rhi, this), |
| 8619 | rtWrapperRight(rhi, this), |
| 8620 | cbWrapper(rhi) |
| 8621 | { |
| 8622 | } |
| 8623 | |
| 8624 | QVkSwapChain::~QVkSwapChain() |
| 8625 | { |
| 8626 | destroy(); |
| 8627 | } |
| 8628 | |
| 8629 | void QVkSwapChain::destroy() |
| 8630 | { |
| 8631 | if (sc == VK_NULL_HANDLE) |
| 8632 | return; |
| 8633 | |
| 8634 | QRHI_RES_RHI(QRhiVulkan); |
| 8635 | if (rhiD) { |
| 8636 | rhiD->swapchains.remove(value: this); |
| 8637 | rhiD->releaseSwapChainResources(swapChain: this); |
| 8638 | } |
| 8639 | |
| 8640 | for (int i = 0; i < QVK_FRAMES_IN_FLIGHT; ++i) { |
| 8641 | QVkSwapChain::FrameResources &frame(frameRes[i]); |
| 8642 | frame.cmdBuf = VK_NULL_HANDLE; |
| 8643 | frame.timestampQueryIndex = -1; |
| 8644 | } |
| 8645 | |
| 8646 | surface = lastConnectedSurface = VK_NULL_HANDLE; |
| 8647 | |
| 8648 | if (rhiD) |
| 8649 | rhiD->unregisterResource(res: this); |
| 8650 | } |
| 8651 | |
| 8652 | QRhiCommandBuffer *QVkSwapChain::currentFrameCommandBuffer() |
| 8653 | { |
| 8654 | return &cbWrapper; |
| 8655 | } |
| 8656 | |
| 8657 | QRhiRenderTarget *QVkSwapChain::currentFrameRenderTarget() |
| 8658 | { |
| 8659 | return &rtWrapper; |
| 8660 | } |
| 8661 | |
| 8662 | QRhiRenderTarget *QVkSwapChain::currentFrameRenderTarget(StereoTargetBuffer targetBuffer) |
| 8663 | { |
| 8664 | return !stereo || targetBuffer == StereoTargetBuffer::LeftBuffer ? &rtWrapper : &rtWrapperRight; |
| 8665 | } |
| 8666 | |
| 8667 | QSize QVkSwapChain::surfacePixelSize() |
| 8668 | { |
| 8669 | if (!ensureSurface()) |
| 8670 | return QSize(); |
| 8671 | |
| 8672 | // The size from the QWindow may not exactly match the surface... so if a |
| 8673 | // size is reported from the surface, use that. |
| 8674 | VkSurfaceCapabilitiesKHR surfaceCaps = {}; |
| 8675 | QRHI_RES_RHI(QRhiVulkan); |
| 8676 | rhiD->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(rhiD->physDev, surface, &surfaceCaps); |
| 8677 | VkExtent2D bufferSize = surfaceCaps.currentExtent; |
| 8678 | if (bufferSize.width == uint32_t(-1)) { |
| 8679 | Q_ASSERT(bufferSize.height == uint32_t(-1)); |
| 8680 | return m_window->size() * m_window->devicePixelRatio(); |
| 8681 | } |
| 8682 | return QSize(int(bufferSize.width), int(bufferSize.height)); |
| 8683 | } |
| 8684 | |
| 8685 | static inline bool hdrFormatMatchesVkSurfaceFormat(QRhiSwapChain::Format f, const VkSurfaceFormatKHR &s) |
| 8686 | { |
| 8687 | switch (f) { |
| 8688 | case QRhiSwapChain::HDRExtendedSrgbLinear: |
| 8689 | return s.format == VK_FORMAT_R16G16B16A16_SFLOAT |
| 8690 | && s.colorSpace == VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT; |
| 8691 | case QRhiSwapChain::HDR10: |
| 8692 | return (s.format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || s.format == VK_FORMAT_A2R10G10B10_UNORM_PACK32) |
| 8693 | && s.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT; |
| 8694 | case QRhiSwapChain::HDRExtendedDisplayP3Linear: |
| 8695 | return s.format == VK_FORMAT_R16G16B16A16_SFLOAT |
| 8696 | && s.colorSpace == VK_COLOR_SPACE_DISPLAY_P3_LINEAR_EXT; |
| 8697 | default: |
| 8698 | break; |
| 8699 | } |
| 8700 | return false; |
| 8701 | } |
| 8702 | |
| 8703 | bool QVkSwapChain::isFormatSupported(Format f) |
| 8704 | { |
| 8705 | if (f == SDR) |
| 8706 | return true; |
| 8707 | |
| 8708 | if (!m_window) { |
| 8709 | qWarning(msg: "Attempted to call isFormatSupported() without a window set" ); |
| 8710 | return false; |
| 8711 | } |
| 8712 | |
| 8713 | // we may be called before create so query the surface |
| 8714 | VkSurfaceKHR surf = QVulkanInstance::surfaceForWindow(window: m_window); |
| 8715 | |
| 8716 | QRHI_RES_RHI(QRhiVulkan); |
| 8717 | uint32_t formatCount = 0; |
| 8718 | rhiD->vkGetPhysicalDeviceSurfaceFormatsKHR(rhiD->physDev, surf, &formatCount, nullptr); |
| 8719 | QVarLengthArray<VkSurfaceFormatKHR, 8> formats(formatCount); |
| 8720 | if (formatCount) { |
| 8721 | rhiD->vkGetPhysicalDeviceSurfaceFormatsKHR(rhiD->physDev, surf, &formatCount, formats.data()); |
| 8722 | for (uint32_t i = 0; i < formatCount; ++i) { |
| 8723 | if (hdrFormatMatchesVkSurfaceFormat(f, s: formats[i])) |
| 8724 | return true; |
| 8725 | } |
| 8726 | } |
| 8727 | |
| 8728 | return false; |
| 8729 | } |
| 8730 | |
| 8731 | QRhiSwapChainHdrInfo QVkSwapChain::hdrInfo() |
| 8732 | { |
| 8733 | QRhiSwapChainHdrInfo info = QRhiSwapChain::hdrInfo(); |
| 8734 | #ifdef Q_OS_WIN |
| 8735 | QRHI_RES_RHI(QRhiVulkan); |
| 8736 | // Must use m_window, not window, given this may be called before createOrResize(). |
| 8737 | if (m_window && rhiD->adapterLuidValid) |
| 8738 | info = rhiD->dxgiHdrInfo->queryHdrInfo(m_window); |
| 8739 | #endif |
| 8740 | return info; |
| 8741 | } |
| 8742 | |
| 8743 | QRhiRenderPassDescriptor *QVkSwapChain::newCompatibleRenderPassDescriptor() |
| 8744 | { |
| 8745 | // not yet built so cannot rely on data computed in createOrResize() |
| 8746 | |
| 8747 | if (!ensureSurface()) // make sure sampleCount and colorFormat reflect what was requested |
| 8748 | return nullptr; |
| 8749 | |
| 8750 | QRHI_RES_RHI(QRhiVulkan); |
| 8751 | QVkRenderPassDescriptor *rp = new QVkRenderPassDescriptor(m_rhi); |
| 8752 | if (!rhiD->createDefaultRenderPass(rpD: rp, |
| 8753 | hasDepthStencil: m_depthStencil != nullptr, |
| 8754 | samples, |
| 8755 | colorFormat, |
| 8756 | shadingRateMap: m_shadingRateMap)) |
| 8757 | { |
| 8758 | delete rp; |
| 8759 | return nullptr; |
| 8760 | } |
| 8761 | |
| 8762 | rp->ownsRp = true; |
| 8763 | rp->updateSerializedFormat(); |
| 8764 | rhiD->registerResource(res: rp); |
| 8765 | return rp; |
| 8766 | } |
| 8767 | |
| 8768 | static inline bool isSrgbFormat(VkFormat format) |
| 8769 | { |
| 8770 | switch (format) { |
| 8771 | case VK_FORMAT_R8_SRGB: |
| 8772 | case VK_FORMAT_R8G8_SRGB: |
| 8773 | case VK_FORMAT_R8G8B8_SRGB: |
| 8774 | case VK_FORMAT_B8G8R8_SRGB: |
| 8775 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 8776 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 8777 | case VK_FORMAT_A8B8G8R8_SRGB_PACK32: |
| 8778 | return true; |
| 8779 | default: |
| 8780 | return false; |
| 8781 | } |
| 8782 | } |
| 8783 | |
| 8784 | bool QVkSwapChain::ensureSurface() |
| 8785 | { |
| 8786 | // Do nothing when already done, however window may change so check the |
| 8787 | // surface is still the same. Some of the queries below are very expensive |
| 8788 | // with some implementations so it is important to do the rest only once |
| 8789 | // per surface. |
| 8790 | |
| 8791 | Q_ASSERT(m_window); |
| 8792 | VkSurfaceKHR surf = QVulkanInstance::surfaceForWindow(window: m_window); |
| 8793 | if (!surf) { |
| 8794 | qWarning(msg: "Failed to get surface for window" ); |
| 8795 | return false; |
| 8796 | } |
| 8797 | if (surface == surf) |
| 8798 | return true; |
| 8799 | |
| 8800 | surface = surf; |
| 8801 | |
| 8802 | QRHI_RES_RHI(QRhiVulkan); |
| 8803 | if (!rhiD->inst->supportsPresent(physicalDevice: rhiD->physDev, queueFamilyIndex: rhiD->gfxQueueFamilyIdx, window: m_window)) { |
| 8804 | qWarning(msg: "Presenting not supported on this window" ); |
| 8805 | return false; |
| 8806 | } |
| 8807 | |
| 8808 | quint32 formatCount = 0; |
| 8809 | rhiD->vkGetPhysicalDeviceSurfaceFormatsKHR(rhiD->physDev, surface, &formatCount, nullptr); |
| 8810 | QList<VkSurfaceFormatKHR> formats(formatCount); |
| 8811 | if (formatCount) |
| 8812 | rhiD->vkGetPhysicalDeviceSurfaceFormatsKHR(rhiD->physDev, surface, &formatCount, formats.data()); |
| 8813 | |
| 8814 | // See if there is a better match than the default BGRA8 format. (but if |
| 8815 | // not, we will stick to the default) |
| 8816 | const bool srgbRequested = m_flags.testFlag(flag: sRGB); |
| 8817 | for (int i = 0; i < int(formatCount); ++i) { |
| 8818 | if (formats[i].format != VK_FORMAT_UNDEFINED) { |
| 8819 | bool ok = srgbRequested == isSrgbFormat(format: formats[i].format); |
| 8820 | if (m_format != SDR) |
| 8821 | ok &= hdrFormatMatchesVkSurfaceFormat(f: m_format, s: formats[i]); |
| 8822 | if (ok) { |
| 8823 | colorFormat = formats[i].format; |
| 8824 | colorSpace = formats[i].colorSpace; |
| 8825 | break; |
| 8826 | } |
| 8827 | } |
| 8828 | } |
| 8829 | |
| 8830 | samples = rhiD->effectiveSampleCountBits(sampleCount: m_sampleCount); |
| 8831 | |
| 8832 | quint32 presModeCount = 0; |
| 8833 | rhiD->vkGetPhysicalDeviceSurfacePresentModesKHR(rhiD->physDev, surface, &presModeCount, nullptr); |
| 8834 | supportedPresentationModes.resize(sz: presModeCount); |
| 8835 | rhiD->vkGetPhysicalDeviceSurfacePresentModesKHR(rhiD->physDev, surface, &presModeCount, |
| 8836 | supportedPresentationModes.data()); |
| 8837 | |
| 8838 | return true; |
| 8839 | } |
| 8840 | |
| 8841 | bool QVkSwapChain::createOrResize() |
| 8842 | { |
| 8843 | QRHI_RES_RHI(QRhiVulkan); |
| 8844 | const bool needsRegistration = !window || window != m_window; |
| 8845 | |
| 8846 | // Can be called multiple times due to window resizes - that is not the |
| 8847 | // same as a simple destroy+create (as with other resources). Thus no |
| 8848 | // destroy() here. See recreateSwapChain(). |
| 8849 | |
| 8850 | // except if the window actually changes |
| 8851 | if (window && window != m_window) |
| 8852 | destroy(); |
| 8853 | |
| 8854 | window = m_window; |
| 8855 | m_currentPixelSize = surfacePixelSize(); |
| 8856 | pixelSize = m_currentPixelSize; |
| 8857 | |
| 8858 | if (!rhiD->recreateSwapChain(swapChain: this)) { |
| 8859 | qWarning(msg: "Failed to create new swapchain" ); |
| 8860 | return false; |
| 8861 | } |
| 8862 | |
| 8863 | if (needsRegistration || !rhiD->swapchains.contains(value: this)) |
| 8864 | rhiD->swapchains.insert(value: this); |
| 8865 | |
| 8866 | if (m_depthStencil && m_depthStencil->sampleCount() != m_sampleCount) { |
| 8867 | qWarning(msg: "Depth-stencil buffer's sampleCount (%d) does not match color buffers' sample count (%d). Expect problems." , |
| 8868 | m_depthStencil->sampleCount(), m_sampleCount); |
| 8869 | } |
| 8870 | if (m_depthStencil && m_depthStencil->pixelSize() != pixelSize) { |
| 8871 | if (m_depthStencil->flags().testFlag(flag: QRhiRenderBuffer::UsedWithSwapChainOnly)) { |
| 8872 | m_depthStencil->setPixelSize(pixelSize); |
| 8873 | if (!m_depthStencil->create()) |
| 8874 | qWarning(msg: "Failed to rebuild swapchain's associated depth-stencil buffer for size %dx%d" , |
| 8875 | pixelSize.width(), pixelSize.height()); |
| 8876 | } else { |
| 8877 | qWarning(msg: "Depth-stencil buffer's size (%dx%d) does not match the surface size (%dx%d). Expect problems." , |
| 8878 | m_depthStencil->pixelSize().width(), m_depthStencil->pixelSize().height(), |
| 8879 | pixelSize.width(), pixelSize.height()); |
| 8880 | } |
| 8881 | } |
| 8882 | |
| 8883 | if (!m_renderPassDesc) |
| 8884 | qWarning(msg: "QVkSwapChain: No renderpass descriptor set. See newCompatibleRenderPassDescriptor() and setRenderPassDescriptor()." ); |
| 8885 | |
| 8886 | rtWrapper.setRenderPassDescriptor(m_renderPassDesc); // for the public getter in QRhiRenderTarget |
| 8887 | rtWrapper.d.rp = QRHI_RES(QVkRenderPassDescriptor, m_renderPassDesc); |
| 8888 | Q_ASSERT(rtWrapper.d.rp && rtWrapper.d.rp->rp); |
| 8889 | |
| 8890 | rtWrapper.d.pixelSize = pixelSize; |
| 8891 | rtWrapper.d.dpr = float(window->devicePixelRatio()); |
| 8892 | rtWrapper.d.sampleCount = samples; |
| 8893 | rtWrapper.d.colorAttCount = 1; |
| 8894 | if (m_depthStencil) { |
| 8895 | rtWrapper.d.dsAttCount = 1; |
| 8896 | ds = QRHI_RES(QVkRenderBuffer, m_depthStencil); |
| 8897 | } else { |
| 8898 | rtWrapper.d.dsAttCount = 0; |
| 8899 | ds = nullptr; |
| 8900 | } |
| 8901 | rtWrapper.d.dsResolveAttCount = 0; |
| 8902 | if (samples > VK_SAMPLE_COUNT_1_BIT) |
| 8903 | rtWrapper.d.resolveAttCount = 1; |
| 8904 | else |
| 8905 | rtWrapper.d.resolveAttCount = 0; |
| 8906 | |
| 8907 | if (shadingRateMapView) |
| 8908 | rtWrapper.d.shadingRateAttCount = 1; |
| 8909 | else |
| 8910 | rtWrapper.d.shadingRateAttCount = 0; |
| 8911 | |
| 8912 | for (int i = 0; i < bufferCount; ++i) { |
| 8913 | QVkSwapChain::ImageResources &image(imageRes[i]); |
| 8914 | // color, ds, resolve, shading rate |
| 8915 | QVarLengthArray<VkImageView, 4> views; |
| 8916 | views.append(t: samples > VK_SAMPLE_COUNT_1_BIT ? image.msaaImageView : image.imageView); |
| 8917 | if (ds) |
| 8918 | views.append(t: ds->imageView); |
| 8919 | if (samples > VK_SAMPLE_COUNT_1_BIT) |
| 8920 | views.append(t: image.imageView); |
| 8921 | if (shadingRateMapView) |
| 8922 | views.append(t: shadingRateMapView); |
| 8923 | |
| 8924 | VkFramebufferCreateInfo fbInfo = {}; |
| 8925 | fbInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 8926 | fbInfo.renderPass = rtWrapper.d.rp->rp; |
| 8927 | fbInfo.attachmentCount = uint32_t(views.count()); |
| 8928 | fbInfo.pAttachments = views.constData(); |
| 8929 | fbInfo.width = uint32_t(pixelSize.width()); |
| 8930 | fbInfo.height = uint32_t(pixelSize.height()); |
| 8931 | fbInfo.layers = 1; |
| 8932 | |
| 8933 | VkResult err = rhiD->df->vkCreateFramebuffer(rhiD->dev, &fbInfo, nullptr, &image.fb); |
| 8934 | if (err != VK_SUCCESS) { |
| 8935 | qWarning(msg: "Failed to create framebuffer: %d" , err); |
| 8936 | return false; |
| 8937 | } |
| 8938 | } |
| 8939 | |
| 8940 | if (stereo) { |
| 8941 | rtWrapperRight.setRenderPassDescriptor( |
| 8942 | m_renderPassDesc); // for the public getter in QRhiRenderTarget |
| 8943 | rtWrapperRight.d.rp = QRHI_RES(QVkRenderPassDescriptor, m_renderPassDesc); |
| 8944 | Q_ASSERT(rtWrapperRight.d.rp && rtWrapperRight.d.rp->rp); |
| 8945 | |
| 8946 | rtWrapperRight.d.pixelSize = pixelSize; |
| 8947 | rtWrapperRight.d.dpr = float(window->devicePixelRatio()); |
| 8948 | rtWrapperRight.d.sampleCount = samples; |
| 8949 | rtWrapperRight.d.colorAttCount = 1; |
| 8950 | if (m_depthStencil) { |
| 8951 | rtWrapperRight.d.dsAttCount = 1; |
| 8952 | ds = QRHI_RES(QVkRenderBuffer, m_depthStencil); |
| 8953 | } else { |
| 8954 | rtWrapperRight.d.dsAttCount = 0; |
| 8955 | ds = nullptr; |
| 8956 | } |
| 8957 | rtWrapperRight.d.dsResolveAttCount = 0; |
| 8958 | if (samples > VK_SAMPLE_COUNT_1_BIT) |
| 8959 | rtWrapperRight.d.resolveAttCount = 1; |
| 8960 | else |
| 8961 | rtWrapperRight.d.resolveAttCount = 0; |
| 8962 | |
| 8963 | for (int i = 0; i < bufferCount; ++i) { |
| 8964 | QVkSwapChain::ImageResources &image(imageRes[i + bufferCount]); |
| 8965 | // color, ds, resolve, shading rate |
| 8966 | QVarLengthArray<VkImageView, 4> views; |
| 8967 | views.append(t: samples > VK_SAMPLE_COUNT_1_BIT ? image.msaaImageView : image.imageView); |
| 8968 | if (ds) |
| 8969 | views.append(t: ds->imageView); |
| 8970 | if (samples > VK_SAMPLE_COUNT_1_BIT) |
| 8971 | views.append(t: image.imageView); |
| 8972 | if (shadingRateMapView) |
| 8973 | views.append(t: shadingRateMapView); |
| 8974 | |
| 8975 | VkFramebufferCreateInfo fbInfo = {}; |
| 8976 | fbInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 8977 | fbInfo.renderPass = rtWrapperRight.d.rp->rp; |
| 8978 | fbInfo.attachmentCount = uint32_t(views.count()); |
| 8979 | fbInfo.pAttachments = views.constData(); |
| 8980 | fbInfo.width = uint32_t(pixelSize.width()); |
| 8981 | fbInfo.height = uint32_t(pixelSize.height()); |
| 8982 | fbInfo.layers = 1; |
| 8983 | |
| 8984 | VkResult err = rhiD->df->vkCreateFramebuffer(rhiD->dev, &fbInfo, nullptr, &image.fb); |
| 8985 | if (err != VK_SUCCESS) { |
| 8986 | qWarning(msg: "Failed to create framebuffer: %d" , err); |
| 8987 | return false; |
| 8988 | } |
| 8989 | } |
| 8990 | } |
| 8991 | |
| 8992 | frameCount = 0; |
| 8993 | |
| 8994 | if (needsRegistration) |
| 8995 | rhiD->registerResource(res: this); |
| 8996 | |
| 8997 | return true; |
| 8998 | } |
| 8999 | |
| 9000 | QT_END_NAMESPACE |
| 9001 | |