Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrhi.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#include "qrhi_p.h"
6#include <qmath.h>
7#include <QLoggingCategory>
8#include "private/qloggingregistry_p.h"
9
10#include "qrhinull_p.h"
11#ifndef QT_NO_OPENGL
12#include "qrhigles2_p.h"
13#endif
14#if QT_CONFIG(vulkan)
15#include "qrhivulkan_p.h"
16#endif
17#ifdef Q_OS_WIN
18#include "qrhid3d11_p.h"
19#include "qrhid3d12_p.h"
20#endif
21#if QT_CONFIG(metal)
22#include "qrhimetal_p.h"
23#endif
24
25#include <memory>
26
27QT_BEGIN_NAMESPACE
28
29// Play nice with QSG_INFO since that is still the most commonly used
30// way to get graphics info printed from Qt Quick apps, and the Quick
31// scenegraph is our primary user.
32Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(QRHI_LOG_INFO, "QSG_INFO", "qt.rhi.general")
33
34Q_LOGGING_CATEGORY(QRHI_LOG_RUB, "qt.rhi.rub")
35
36/*!
37 \class QRhi
38 \ingroup painting-3D
39 \inmodule QtGuiPrivate
40 \inheaderfile rhi/qrhi.h
41 \since 6.6
42
43 \brief Accelerated 2D/3D graphics API abstraction.
44
45 The Qt Rendering Hardware Interface is an abstraction for hardware accelerated
46 graphics APIs, such as, \l{https://www.khronos.org/opengl/}{OpenGL},
47 \l{https://www.khronos.org/opengles/}{OpenGL ES},
48 \l{https://docs.microsoft.com/en-us/windows/desktop/direct3d}{Direct3D},
49 \l{https://developer.apple.com/metal/}{Metal}, and
50 \l{https://www.khronos.org/vulkan/}{Vulkan}.
51
52 \warning The QRhi family of classes in the Qt Gui module, including QShader
53 and QShaderDescription, offer limited compatibility guarantees. There are
54 no source or binary compatibility guarantees for these classes, meaning the
55 API is only guaranteed to work with the Qt version the application was
56 developed against. Source incompatible changes are however aimed to be kept
57 at a minimum and will only be made in minor releases (6.7, 6.8, and so on).
58 To use these classes in an application, link to
59 \c{Qt::GuiPrivate} (if using CMake), and include the headers with the \c
60 rhi prefix, for example \c{#include <rhi/qrhi.h>}.
61
62 Each QRhi instance is backed by a backend for a specific graphics API. The
63 selection of the backend is a run time choice and is up to the application
64 or library that creates the QRhi instance. Some backends are available on
65 multiple platforms (OpenGL, Vulkan, Null), while APIs specific to a given
66 platform are only available when running on the platform in question (Metal
67 on macOS/iOS, Direct3D on Windows).
68
69 The available backends currently are:
70
71 \list
72
73 \li OpenGL 2.1 / OpenGL ES 2.0 or newer. Some extensions and newer core
74 specification features are utilized when present, for example to enable
75 multisample framebuffers or compute shaders. Operating in core profile
76 contexts is supported as well. If necessary, applications can query the
77 \l{QRhi::Feature}{feature flags} at runtime to check for features that are
78 not supported in the OpenGL context backing the QRhi. The OpenGL backend
79 builds on QOpenGLContext, QOpenGLFunctions, and the related cross-platform
80 infrastructure of the Qt GUI module.
81
82 \li Direct3D 11.2 and newer (with DXGI 1.3 and newer), using Shader Model
83 5.0 or newer. When the D3D runtime has no support for 11.2 features or
84 Shader Model 5.0, initialization using an accelerated graphics device will
85 fail, but using the
86 \l{https://learn.microsoft.com/en-us/windows/win32/direct3darticles/directx-warp}{software
87 adapter} is still an option.
88
89 \li Direct3D 12 on Windows 10 version 1703 and newer, with Shader Model 5.0
90 or newer. Qt requires ID3D12Device2 to be present, hence the requirement
91 for at least version 1703 of Windows 10. The D3D12 device is by default
92 created with specifying a minimum feature level of
93 \c{D3D_FEATURE_LEVEL_11_0}.
94
95 \li Metal 1.2 or newer.
96
97 \li Vulkan 1.0 or newer, optionally utilizing some Vulkan 1.1 level
98 features.
99
100 \li Null, a "dummy" backend that issues no graphics calls at all.
101
102 \endlist
103
104 In order to allow shader code to be written once in Qt applications and
105 libraries, all shaders are expected to be written in a single language
106 which is then compiled into SPIR-V. Versions for various shading language
107 are then generated from that, together with reflection information (inputs,
108 outputs, shader resources). This is then packed into easily and efficiently
109 serializable QShader instances. The compilers and tools to generate such
110 shaders are not part of QRhi and the Qt GUI module, but the core classes
111 for using such shaders, QShader and QShaderDescription, are. The APIs and
112 tools for performing compilation and translation are part of the Qt Shader
113 Tools module.
114
115 See the \l{RHI Window Example} for an introductory example of creating a
116 portable, cross-platform application that performs accelerated 3D rendering
117 onto a QWindow using QRhi.
118
119 \section1 An Impression of the API
120
121 To provide a quick look at the API with a short yet complete example that
122 does not involve window-related setup, the following is a complete,
123 runnable cross-platform application that renders 20 frames off-screen, and
124 then saves the generated images to files after reading back the texture
125 contents from the GPU. For an example that renders on-screen, which then
126 involves setting up a QWindow and a swapchain, refer to the
127 \l{RHI Window Example}.
128
129 For brevity, the initialization of the QRhi is done based on the platform:
130 the sample code here chooses Direct 3D 12 on Windows, Metal on macOS and
131 iOS, and Vulkan otherwise. OpenGL and Direct 3D 11 are never used by this
132 application, but support for those could be introduced with a few
133 additional lines.
134
135 \snippet rhioffscreen/main.cpp 0
136
137 The result of the application is 20 \c PNG images (frame0.png -
138 frame19.png). These contain a rotating triangle with varying opacity over a
139 green background.
140
141 The vertex and fragment shaders are expected to be processed and packaged
142 into \c{.qsb} files. The Vulkan-compatible GLSL source code is the
143 following:
144
145 \e color.vert
146 \snippet rhioffscreen/color.vert 0
147
148 \e color.frag
149 \snippet rhioffscreen/color.frag 0
150
151 To manually compile and transpile these shaders to a number of targets
152 (SPIR-V, HLSL, MSL, GLSL) and generate the \c{.qsb} files the application
153 loads at run time, run \c{qsb --qt6 color.vert -o color.vert.qsb} and
154 \c{qsb --qt6 color.frag -o color.frag.qsb}. Alternatively, the Qt Shader
155 Tools module offers build system integration for CMake, the
156 \c qt_add_shaders() CMake function, that can achieve the same at build time.
157
158 \section1 Security Considerations
159
160 All data consumed by QRhi and related classes such as QShader are considered
161 trusted content.
162
163 \warning Application developers are advised to carefully consider the
164 potential implications before allowing the feeding of user-provided content
165 that is not part of the application and is not under the developers'
166 control. (this includes all vertex/index data, shaders, pipeline and draw
167 call parameters, etc.)
168
169 \section1 Design Fundamentals
170
171 A QRhi cannot be instantiated directly. Instead, use the create()
172 function. Delete the QRhi instance normally to release the graphics device.
173
174 \section2 Resources
175
176 Instances of classes deriving from QRhiResource, such as, QRhiBuffer,
177 QRhiTexture, etc., encapsulate zero, one, or more native graphics
178 resources. Instances of such classes are always created via the \c new
179 functions of the QRhi, such as, newBuffer(), newTexture(),
180 newTextureRenderTarget(), newSwapChain().
181
182 \code
183 QRhiBuffer *vbuf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
184 if (!vbuf->create()) { error(); }
185 // ...
186 delete vbuf;
187 \endcode
188
189 \list
190
191 \li The returned value from functions like newBuffer() is always owned by
192 the caller.
193
194 \li Just creating an instance of a QRhiResource subclass never allocates or
195 initializes any native resources. That is only done when calling the
196 \c create() function of a subclass, for example, QRhiBuffer::create() or
197 QRhiTexture::create().
198
199 \li The exceptions are
200 QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor(),
201 QRhiSwapChain::newCompatibleRenderPassDescriptor(), and
202 QRhiRenderPassDescriptor::newCompatibleRenderPassDescriptor(). There is no
203 \c create() operation for these and the returned object is immediately
204 active.
205
206 \li The resource objects themselves are treated as immutable: once a
207 resource has create() called, changing any parameters via the setters, such as,
208 QRhiTexture::setPixelSize(), has no effect, unless the underlying native
209 resource is released and \c create() is called again. See more about resource
210 reuse in the sections below.
211
212 \li The underlying native resources are scheduled for releasing by the
213 QRhiResource destructor, or by calling QRhiResource::destroy(). Backends
214 often queue release requests and defer executing them to an unspecified
215 time, this is hidden from the applications. This way applications do not
216 have to worry about releasing native resources that may still be in use by
217 an in-flight frame.
218
219 \li Note that this does not mean that a QRhiResource can freely be
220 destroy()'ed or deleted within a frame (that is, in a
221 \l{QRhi::beginFrame()}{beginFrame()} - \l{QRhi::endFrame()}{endFrame()}
222 section). As a general rule, all referenced QRhiResource objects must stay
223 unchanged until the frame is submitted by calling
224 \l{QRhi::endFrame()}{endFrame()}. To ease this,
225 QRhiResource::deleteLater() is provided as a convenience.
226
227 \endlist
228
229 \section2 Command buffers and deferred command execution
230
231 Regardless of the design and capabilities of the underlying graphics API,
232 all QRhi backends implement some level of command buffers. No
233 QRhiCommandBuffer function issues any native bind or draw command (such as,
234 \c glDrawElements) directly. Commands are always recorded in a queue,
235 either native or provided by the QRhi backend. The command buffer is
236 submitted, and so execution starts only upon QRhi::endFrame() or
237 QRhi::finish().
238
239 The deferred nature has consequences for some types of objects. For example,
240 writing to a dynamic buffer multiple times within a frame, in case such
241 buffers are backed by host-visible memory, will result in making the
242 results of all writes are visible to all draw calls in the command buffer
243 of the frame, regardless of when the dynamic buffer update was recorded
244 relative to a draw call.
245
246 Furthermore, instances of QRhiResource subclasses must be treated immutable
247 within a frame in which they are referenced in any way. Create
248 all resources upfront, before starting to record commands for the next
249 frame. Reusing a QRhiResource instance within a frame (by calling \c create()
250 then referencing it again in the same \c{beginFrame - endFrame} section)
251 should be avoided as it may lead to unexpected results, depending on the
252 backend.
253
254 As a general rule, all referenced QRhiResource objects must stay valid and
255 unmodified until the frame is submitted by calling
256 \l{QRhi::endFrame()}{endFrame()}. On the other hand, calling
257 \l{QRhiResource::destroy()}{destroy()} or deleting the QRhiResource are
258 always safe once the frame is submitted, regardless of the status of the
259 underlying native resources (which may still be in use by the GPU - but
260 that is taken care of internally).
261
262 Unlike APIs like OpenGL, upload and copy type of commands cannot be mixed
263 with draw commands. The typical renderer will involve a sequence similar to
264 the following:
265
266 \list
267 \li (re)create resources
268 \li begin frame
269 \li record/issue uploads and copies
270 \li start recording a render pass
271 \li record draw calls
272 \li end render pass
273 \li end frame
274 \endlist
275
276 Recording copy type of operations happens via QRhiResourceUpdateBatch. Such
277 operations are committed typically on
278 \l{QRhiCommandBuffer::beginPass()}{beginPass()}.
279
280 When working with legacy rendering engines designed for OpenGL, the
281 migration to QRhi often involves redesigning from having a single \c render
282 step (that performs copies and uploads, clears buffers, and issues draw
283 calls, all mixed together) to a clearly separated, two phase \c prepare -
284 \c render setup where the \c render step only starts a renderpass and
285 records draw calls, while all resource creation and queuing of updates,
286 uploads and copies happens beforehand, in the \c prepare step.
287
288 QRhi does not at the moment allow freely creating and submitting command
289 buffers. This may be lifted in the future to some extent, in particular if
290 compute support is introduced, but the model of well defined
291 \c{frame-start} and \c{frame-end} points, combined with a dedicated,
292 "frame" command buffer, where \c{frame-end} implies presenting, is going to
293 remain the primary way of operating since this is what fits Qt's various UI
294 technologies best.
295
296 \section2 Threading
297
298 A QRhi instance and the associated resources can be created and used on any
299 thread but all usage must be limited to that one single thread. When
300 rendering to multiple QWindows in an application, having a dedicated thread
301 and QRhi instance for each window is often advisable, as this can eliminate
302 issues with unexpected throttling caused by presenting to multiple windows.
303 Conceptually that is then the same as how Qt Quick scene graph's threaded
304 render loop operates when working directly with OpenGL: one thread for each
305 window, one QOpenGLContext for each thread. When moving onto QRhi,
306 QOpenGLContext is replaced by QRhi, making the migration straightforward.
307
308 When it comes to externally created native objects, such as OpenGL contexts
309 passed in via QRhiGles2NativeHandles, it is up to the application to ensure
310 they are not misused by other threads.
311
312 Resources are not shareable between QRhi instances. This is an intentional
313 choice since QRhi hides most queue, command buffer, and resource
314 synchronization related tasks, and provides no API for them. Safe and
315 efficient concurrent use of graphics resources from multiple threads is
316 tied to those concepts, however, and is thus a topic that is currently out
317 of scope, but may be introduced in the future.
318
319 \note The Metal backend requires that an autorelease pool is available on
320 the rendering thread, ideally wrapping each iteration of the render loop.
321 This needs no action from the users of QRhi when rendering on the main
322 (gui) thread, but becomes important when a separate, dedicated render
323 thread is used.
324
325 \section2 Resource synchronization
326
327 QRhi does not expose APIs for resource barriers or image layout
328 transitions. Such synchronization is done implicitly by the backends, where
329 applicable (for example, Vulkan), by tracking resource usage as necessary.
330 Buffer and image barriers are inserted before render or compute passes
331 transparently to the application.
332
333 \note Resources within a render or compute pass are expected to be bound to
334 a single usage during that pass. For example, a buffer can be used as
335 vertex, index, uniform, or storage buffer, but not a combination of them
336 within a single pass. However, it is perfectly fine to use a buffer as a
337 storage buffer in a compute pass, and then as a vertex buffer in a render
338 pass, for example, assuming the buffer declared both usages upon creation.
339
340 \note Textures have this rule relaxed in certain cases, because using two
341 subresources (typically two different mip levels) of the same texture for
342 different access (one for load, one for store) is supported even within the
343 same pass.
344
345 \section2 Resource reuse
346
347 From the user's point of view a QRhiResource is reusable immediately after
348 calling QRhiResource::destroy(). With the exception of swapchains, calling
349 \c create() on an already created object does an implicit \c destroy(). This
350 provides a handy shortcut to reuse a QRhiResource instance with different
351 parameters, with a new native graphics object underneath.
352
353 The importance of reusing the same object lies in the fact that some
354 objects reference other objects: for example, a QRhiShaderResourceBindings
355 can reference QRhiBuffer, QRhiTexture, and QRhiSampler instances. If in a
356 later frame one of these buffers need to be resized or a sampler parameter
357 needs changing, destroying and creating a whole new QRhiBuffer or
358 QRhiSampler would invalidate all references to the old instance. By just
359 changing the appropriate parameters via QRhiBuffer::setSize() or similar
360 and then calling QRhiBuffer::create(), everything works as expected and
361 there is no need to touch the QRhiShaderResourceBindings at all, even
362 though there is a good chance that under the hood the QRhiBuffer is now
363 backed by a whole new native buffer.
364
365 \code
366 QRhiBuffer *ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 256);
367 ubuf->create();
368
369 QRhiShaderResourceBindings *srb = rhi->newShaderResourceBindings()
370 srb->setBindings({
371 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, ubuf)
372 });
373 srb->create();
374
375 // ...
376
377 // now in a later frame we need to grow the buffer to a larger size
378 ubuf->setSize(512);
379 ubuf->create(); // same as ubuf->destroy(); ubuf->create();
380
381 // srb needs no changes whatsoever, any references in it to ubuf
382 // stay valid. When it comes to internal details, such as that
383 // ubuf may now be backed by a completely different native buffer
384 // resource, that is is recognized and handled automatically by the
385 // next setShaderResources().
386 \endcode
387
388 QRhiTextureRenderTarget offers the same contract: calling
389 QRhiCommandBuffer::beginPass() is safe even when one of the render target's
390 associated textures or renderbuffers has been rebuilt (by calling \c
391 create() on it) since the creation of the render target object. This allows
392 the application to resize a texture by setting a new pixel size on the
393 QRhiTexture and calling create(), thus creating a whole new native texture
394 resource underneath, without having to update the QRhiTextureRenderTarget
395 as that will be done implicitly in beginPass().
396
397 \section2 Pooled objects
398
399 In addition to resources, there are pooled objects as well, such as,
400 QRhiResourceUpdateBatch. An instance is retrieved via a \c next function,
401 such as, nextResourceUpdateBatch(). The caller does not own the returned
402 instance in this case. The only valid way of operating here is calling
403 functions on the QRhiResourceUpdateBatch and then passing it to
404 QRhiCommandBuffer::beginPass() or QRhiCommandBuffer::endPass(). These
405 functions take care of returning the batch to the pool. Alternatively, a
406 batch can be "canceled" and returned to the pool without processing by
407 calling QRhiResourceUpdateBatch::release().
408
409 A typical pattern is thus:
410
411 \code
412 QRhiResourceUpdateBatch *resUpdates = rhi->nextResourceUpdateBatch();
413 // ...
414 resUpdates->updateDynamicBuffer(ubuf, 0, 64, mvp.constData());
415 if (!image.isNull()) {
416 resUpdates->uploadTexture(texture, image);
417 image = QImage();
418 }
419 // ...
420 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
421 // note the last argument
422 cb->beginPass(swapchain->currentFrameRenderTarget(), clearCol, clearDs, resUpdates);
423 \endcode
424
425 \section2 Swapchain specifics
426
427 QRhiSwapChain features some special semantics due to the peculiar nature of
428 swapchains.
429
430 \list
431
432 \li It has no \c create() but rather a QRhiSwapChain::createOrResize().
433 Repeatedly calling this function is \b not the same as calling
434 QRhiSwapChain::destroy() followed by QRhiSwapChain::createOrResize(). This
435 is because swapchains often have ways to handle the case where buffers need
436 to be resized in a manner that is more efficient than a brute force
437 destroying and recreating from scratch.
438
439 \li An active QRhiSwapChain must be released by calling
440 \l{QRhiSwapChain::destroy()}{destroy()}, or by destroying the object, before
441 the QWindow's underlying QPlatformWindow, and so the associated native
442 window object, is destroyed. It should not be postponed because releasing
443 the swapchain may become problematic (and with some APIs, like Vulkan, is
444 explicitly disallowed) when the native window is not around anymore, for
445 example because the QPlatformWindow got destroyed upon getting a
446 QWindow::close(). Therefore, releasing the swapchain must happen whenever
447 the targeted QWindow sends the
448 QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed event. If the event does
449 not arrive before the destruction of the QWindow - this can happen when
450 using QCoreApplication::quit() -, then check QWindow::handle() after the
451 event loop exits and invoke the swapchain release when non-null (meaning
452 the underlying native window is still around).
453
454 \endlist
455
456 \section2 Ownership
457
458 The general rule is no ownership transfer. Creating a QRhi with an already
459 existing graphics device does not mean the QRhi takes ownership of the
460 device object. Similarly, ownership is not given away when a device or
461 texture object is "exported" via QRhi::nativeHandles() or
462 QRhiTexture::nativeTexture(). Most importantly, passing pointers in structs
463 and via setters does not transfer ownership.
464
465 \section1 Troubleshooting and Profiling
466
467 \section2 Error reporting
468
469 Functions such as \l QRhi::create() and the resource classes' \c create()
470 member functions (e.g., \l QRhiBuffer::create()) indicate failure with the
471 return value (\nullptr or
472 \c false, respectively). When working with QShader, \l QShader::fromSerialized()
473 returns an invalid QShader (for which \l{QShader::isValid()}{isValid()} returns
474 \c false) when the data passed to the function cannot be successfully deserialized.
475 Some functions, beginFrame() in particular, may also sometimes report "soft failures",
476 such as \l FrameOpSwapChainOutOfDate, which do not indicate an unrecoverable error,
477 but rather should be seen as a "try again later" response.
478
479 Warnings and errors may get printed at any time to the debug output via
480 qWarning(). It is therefore always advisable to inspect the output of the
481 application.
482
483 Additional debug messages can be enabled via the following logging
484 categories. Messages from these categories are not printed by default
485 unless explicitly enabled via QLoggingCategory or the \c QT_LOGGING_RULES
486 environment variable. For better interoperation with Qt Quick, the
487 environment variable \c{QSG_INFO} also enables these debug prints.
488
489 \list
490 \li \c{qt.rhi.general}
491 \endlist
492
493 Additionally, applications can query the \l{QRhi::backendName()}{QRhi
494 backend name} and
495 \l{QRhi::driverInfo()}{graphics device information} from a successfully
496 initialized QRhi. This can then be printed to the user or stored in the
497 application logs even in production builds, if desired.
498
499 \section2 Investigating rendering problems
500
501 When the rendering results are not as expected, or the application is
502 experiencing problems, always consider checking with the the native 3D
503 APIs' debug and validation facilities. QRhi itself features limited error
504 checking since replicating the already existing, vast amount of
505 functionality in the underlying layers is not reasonable.
506
507 \list
508
509 \li For Vulkan, controlling the
510 \l{https://github.com/KhronosGroup/Vulkan-ValidationLayers}{Vulkan
511 Validation Layers} is not in the scope of the QRhi, but rather can be
512 achieved by configuring the \l QVulkanInstance with the appropriate layers.
513 For example, call \c{instance.setLayers({ "VK_LAYER_KHRONOS_validation" });}
514 before invoking \l{QVulkanInstance::create()}{create()} on the QVulkanInstance.
515 (note that this assumes that the validation layers are actually installed
516 and available, e.g. from the Vulkan SDK) By default, QVulkanInstance conveniently
517 redirects the Vulkan debug messages to qDebug, meaning the validation messages get
518 printed just like other Qt warnings.
519
520 \li With Direct 3D 11 and 12, a graphics device with the debug layer
521 enabled can be requested by toggling the \c enableDebugLayer flag in the
522 appropriate \l{QRhiD3D11InitParams}{init params struct}. The messages appear on the
523 debug output, which is visible in Qt Creator's messages panel or via a tool
524 such as \l{https://learn.microsoft.com/en-us/sysinternals/downloads/debugview}{DebugView}.
525
526 \li For Metal, controlling Metal Validation is outside of QRhi's scope.
527 Rather, to enable validation, run the application with the environment
528 variable \c{METAL_DEVICE_WRAPPER_TYPE=1} set, or run the application within
529 XCode. There may also be further settings and environment variable in modern
530 XCode and macOS versions. See for instance
531 \l{https://developer.apple.com/documentation/metal/diagnosing_metal_programming_issues_early}{this
532 page}.
533
534 \endlist
535
536 \section2 Frame captures and performance profiling
537
538 A Qt application rendering with QRhi to a window while relying on a 3D API
539 under the hood, is, from the windowing and graphics pipeline perspective at
540 least, no different from any other (non-Qt) applications using the same 3D
541 API. This means that tools and practices for debugging and profiling
542 applications involving 3D graphics, such as games, all apply to such a Qt
543 application as well.
544
545 A few examples of tools that can provide insights into the rendering
546 internals of Qt applications that use QRhi, which includes Qt Quick and Qt
547 Quick 3D based projects as well:
548
549 \list
550
551 \li \l{https://renderdoc.org/}{RenderDoc} allows taking frame captures and
552 introspecting the recorded commands and pipeline state on Windows and Linux
553 for applications using OpenGL, Vulkan, D3D11, or D3D12. When trying to
554 figure out why some parts of the 3D scene do not show up as expected,
555 RenderDoc is often a fast and efficient way to check the pipeline stages
556 and the related state and discover the missing or incorrect value. It is
557 also a tool that is actively used when developing Qt itself.
558
559 \li For NVIDIA-based systems,
560 \l{https://developer.nvidia.com/nsight-graphics}{Nsight Graphics} provides
561 a graphics debugger tool on Windows and Linux. In addition to investigating the commands
562 in the frame and the pipeline, the vendor-specific tools allow looking at timings and
563 hardware performance information, which is not something simple frame captures can provide.
564
565 \li For AMD-based systems, the \l{https://gpuopen.com/rgp/}{Radeon GPU
566 Profiler} can be used to gain deeper insights into the application's
567 rendering and its performance.
568
569 \li Overlays showing live performance information can be highly useful as well, and
570 are often preferable to implementing simple frames-per-second counters within the
571 application itself, since they are more reliable and show more information. An example
572 is \l{https://game.intel.com/us/intel-presentmon/}{PresentMon}, which supports
573 graphics hardware from multiple vendors.
574
575 \li As QRhi supports Direct 3D 12, using
576 \l{https://devblogs.microsoft.com/pix/download/}{PIX}, a performance tuning
577 and debugging tool for DirectX 12 games on Windows is an option as well.
578
579 \li On macOS,
580 \l{https://developer.apple.com/documentation/metal/debugging_tools/viewing_your_gpu_workload_with_the_metal_debugger}{the
581 XCode Metal debugger} can be used to take and introspect frame
582 captures, to investigate performance details, and debug shaders. In macOS 13 it is also possible
583 to enable an overlay that displays frame rate and other information for any Metal-based window by
584 setting the environment variable \c{MTL_HUD_ENABLED=1}.
585
586 \endlist
587
588 On mobile and embedded platforms, there may be vendor and platform-specific
589 tools, provided by the GPU or SoC vendor, available to perform performance
590 profiling of application using OpenGL ES or Vulkan.
591
592 When capturing frames, remember that objects and groups of commands can be
593 named via debug markers, as long as \l{QRhi::EnableDebugMarkers}{debug
594 markers were enabled} for the QRhi, and the graphics API in use supports
595 this. To annotate the command stream, call
596 \l{QRhiCommandBuffer::debugMarkBegin()}{debugMarkBegin()},
597 \l{QRhiCommandBuffer::debugMarkEnd()}{debugMarkEnd()} and/or
598 \l{QRhiCommandBuffer::debugMarkMsg()}{debugMarkMsg()}.
599 This can be particularly useful in larger frames with multiple render passes.
600 Resources are named by calling \l{QRhiResource::setName()}{setName()} before create().
601
602 To perform basic timing measurements on the CPU and GPU side within the
603 application, \l QElapsedTimer and
604 \l QRhiCommandBuffer::lastCompletedGpuTime() can be used. The latter is
605 only available with select graphics APIs at the moment and requires opting
606 in via the \l QRhi::EnableTimestamps flag.
607
608 \section2 Resource leak checking
609
610 When destroying a QRhi object without properly destroying all buffers,
611 textures, and other resources created from it, warnings about this are
612 printed to the debug output whenever the application is a debug build, or
613 when the \c QT_RHI_LEAK_CHECK environment variable is set to a non-zero
614 value. This is a simple way to discover design issues around resource
615 handling within the application rendering logic. Note however that some
616 platforms and underlying graphics APIs may perform their own allocation and
617 resource leak detection as well, over which Qt will have no direct control.
618 For example, when using Vulkan, the memory allocator may raise failing
619 assertions in debug builds when resources that own graphics memory
620 allocations are not destroyed before the QRhi. In addition, the Vulkan
621 validation layer, when enabled, will issue warnings about native graphics
622 resources that were not released. Similarly, with Direct 3D warnings may
623 get printed about unreleased COM objects when the application does not
624 destroy the QRhi and its resources in the correct order.
625
626 \sa {RHI Window Example}, QRhiCommandBuffer, QRhiResourceUpdateBatch,
627 QRhiShaderResourceBindings, QShader, QRhiBuffer, QRhiTexture,
628 QRhiRenderBuffer, QRhiSampler, QRhiTextureRenderTarget,
629 QRhiGraphicsPipeline, QRhiComputePipeline, QRhiSwapChain
630 */
631
632/*!
633 \enum QRhi::Implementation
634 Describes which graphics API-specific backend gets used by a QRhi instance.
635
636 \value Null
637 \value Vulkan
638 \value OpenGLES2
639 \value D3D11
640 \value D3D12
641 \value Metal
642 */
643
644/*!
645 \enum QRhi::Flag
646 Describes what special features to enable.
647
648 \value EnableDebugMarkers Enables debug marker groups. Without this frame
649 debugging features like making debug groups and custom resource name
650 visible in external GPU debugging tools will not be available and functions
651 like QRhiCommandBuffer::debugMarkBegin() will become no-ops. Avoid enabling
652 in production builds as it may involve a small performance impact. Has no
653 effect when the QRhi::DebugMarkers feature is not reported as supported.
654
655 \value EnableTimestamps Enables GPU timestamp collection. When not set,
656 QRhiCommandBuffer::lastCompletedGpuTime() always returns 0. Enable this
657 only when needed since there may be a small amount of extra work involved
658 (e.g. timestamp queries), depending on the underlying graphics API. Has no
659 effect when the QRhi::Timestamps feature is not reported as supported.
660
661 \value PreferSoftwareRenderer Indicates that backends should prefer
662 choosing an adapter or physical device that renders in software on the CPU.
663 For example, with Direct3D there is typically a "Basic Render Driver"
664 adapter available with \c{DXGI_ADAPTER_FLAG_SOFTWARE}. Setting this flag
665 requests the backend to choose that adapter over any other, as long as no
666 specific adapter was forced by other backend-specific means. With Vulkan
667 this maps to preferring physical devices with
668 \c{VK_PHYSICAL_DEVICE_TYPE_CPU}. When not available, or when it is not
669 possible to decide if an adapter/device is software-based, this flag is
670 ignored. It may also be ignored with graphics APIs that have no concept and
671 means of enumerating adapters/devices.
672
673 \value EnablePipelineCacheDataSave Enables retrieving the pipeline cache
674 contents, where applicable. When not set, pipelineCacheData() will return
675 an empty blob always. With backends where retrieving and restoring the
676 pipeline cache contents is not supported, the flag has no effect and the
677 serialized cache data is always empty. The flag provides an opt-in
678 mechanism because the cost of maintaining the related data structures is
679 not insignificant with some backends. With Vulkan this feature maps
680 directly to VkPipelineCache, vkGetPipelineCacheData and
681 VkPipelineCacheCreateInfo::pInitialData. With Direct3D 11 there is no real
682 pipline cache, but the results of HLSL->DXBC compilations are stored and
683 can be serialized/deserialized via this mechanism. This allows skipping the
684 time consuming D3DCompile() in future runs of the applications for shaders
685 that come with HLSL source instead of offline pre-compiled bytecode. This
686 can provide a huge boost in startup and load times, if there is a lot of
687 HLSL source compilation happening. With OpenGL the "pipeline cache" is
688 simulated by retrieving and loading shader program binaries (if supported
689 by the driver). With OpenGL there are additional, disk-based caching
690 mechanisms for shader/program binaries provided by Qt. Writing to those may
691 get disabled whenever this flag is set since storing program binaries to
692 multiple caches is not sensible.
693
694 \value SuppressSmokeTestWarnings Indicates that, with backends where this
695 is relevant, certain, non-fatal QRhi::create() failures should not
696 produce qWarning() calls. For example, with D3D11, passing this flag
697 makes a number of warning messages (that appear due to QRhi::create()
698 failing) to become categorized debug prints instead under the commonly used
699 \c{qt.rhi.general} logging category. This can be used by engines, such as
700 Qt Quick, that feature fallback logic, i.e. they retry calling create()
701 with a different set of flags (such as, \l PreferSoftwareRenderer), in order
702 to hide the unconditional warnings from the output that would be printed
703 when the first create() attempt had failed.
704 */
705
706/*!
707 \enum QRhi::FrameOpResult
708 Describes the result of operations that can have a soft failure.
709
710 \value FrameOpSuccess Success
711
712 \value FrameOpError Unspecified error
713
714 \value FrameOpSwapChainOutOfDate The swapchain is in an inconsistent state
715 internally. This can be recoverable by attempting to repeat the operation
716 (such as, beginFrame()) later.
717
718 \value FrameOpDeviceLost The graphics device was lost. This can be
719 recoverable by attempting to repeat the operation (such as, beginFrame())
720 after releasing and reinitializing all objects backed by native graphics
721 resources. See isDeviceLost().
722 */
723
724/*!
725 \enum QRhi::Feature
726 Flag values to indicate what features are supported by the backend currently in use.
727
728 \value MultisampleTexture Indicates that textures with a sample count larger
729 than 1 are supported. In practice this feature will be unsupported with
730 OpenGL ES versions older than 3.1, and OpenGL older than 3.0.
731
732 \value MultisampleRenderBuffer Indicates that renderbuffers with a sample
733 count larger than 1 are supported. In practice this feature will be
734 unsupported with OpenGL ES 2.0, and may also be unsupported with OpenGL 2.x
735 unless the relevant extensions are present.
736
737 \value DebugMarkers Indicates that debug marker groups (and so
738 QRhiCommandBuffer::debugMarkBegin()) are supported.
739
740 \value Timestamps Indicates that command buffer timestamps are supported.
741 Relevant for QRhiCommandBuffer::lastCompletedGpuTime(). This can be
742 expected to be supported on Metal, Vulkan, Direct 3D 11 and 12, and OpenGL
743 contexts of version 3.3 or newer. However, with some of these APIs support
744 for timestamp queries is technically optional, and therefore it cannot be
745 guaranteed that this feature is always supported with every implementation
746 of them.
747
748 \value Instancing Indicates that instanced drawing is supported. In
749 practice this feature will be unsupported with OpenGL ES 2.0 and OpenGL
750 3.2 or older.
751
752 \value CustomInstanceStepRate Indicates that instance step rates other
753 than 1 are supported. In practice this feature will always be unsupported
754 with OpenGL. In addition, running with Vulkan 1.0 without
755 VK_EXT_vertex_attribute_divisor will also lead to reporting false for this
756 feature.
757
758 \value PrimitiveRestart Indicates that restarting the assembly of
759 primitives when encountering an index value of 0xFFFF
760 (\l{QRhiCommandBuffer::IndexUInt16}{IndexUInt16}) or 0xFFFFFFFF
761 (\l{QRhiCommandBuffer::IndexUInt32}{IndexUInt32}) is enabled, for certain
762 primitive topologies at least. QRhi will try to enable this with all
763 backends, but in some cases it will not be supported. Dynamically
764 controlling primitive restart is not possible since with some APIs
765 primitive restart with a fixed index is always on. Applications must assume
766 that whenever this feature is reported as supported, the above mentioned
767 index values \c may be treated specially, depending on the topology. The
768 only two topologies where primitive restart is guaranteed to behave
769 identically across backends, as long as this feature is reported as
770 supported, are \l{QRhiGraphicsPipeline::LineStrip}{LineStrip} and
771 \l{QRhiGraphicsPipeline::TriangleStrip}{TriangleStrip}.
772
773 \value NonDynamicUniformBuffers Indicates that creating buffers with the
774 usage \l{QRhiBuffer::UniformBuffer}{UniformBuffer} and the types
775 \l{QRhiBuffer::Immutable}{Immutable} or \l{QRhiBuffer::Static}{Static} is
776 supported. When reported as unsupported, uniform (constant) buffers must be
777 created as \l{QRhiBuffer::Dynamic}{Dynamic}. (which is recommended
778 regardless)
779
780 \value NonFourAlignedEffectiveIndexBufferOffset Indicates that effective
781 index buffer offsets (\c{indexOffset + firstIndex * indexComponentSize})
782 that are not 4 byte aligned are supported. When not supported, attempting
783 to issue a \l{QRhiCommandBuffer::drawIndexed()}{drawIndexed()} with a
784 non-aligned effective offset may lead to unspecified behavior. Relevant in
785 particular for Metal, where this will be reported as unsupported.
786
787 \value NPOTTextureRepeat Indicates that the
788 \l{QRhiSampler::Repeat}{Repeat} wrap mode and mipmap filtering modes are
789 supported for textures with a non-power-of-two size. In practice this can
790 only be false with OpenGL ES 2.0 implementations without
791 \c{GL_OES_texture_npot}.
792
793 \value RedOrAlpha8IsRed Indicates that the
794 \l{QRhiTexture::RED_OR_ALPHA8}{RED_OR_ALPHA8} format maps to a one
795 component 8-bit \c red format. This is the case for all backends except
796 OpenGL when using either OpenGL ES or a non-core profile context. There
797 \c{GL_ALPHA}, a one component 8-bit \c alpha format, is used
798 instead. Using the special texture format allows having a single code
799 path for creating textures, leaving it up to the backend to decide the
800 actual format, while the feature flag can be used to pick the
801 appropriate shader variant for sampling the texture.
802
803 \value ElementIndexUint Indicates that 32-bit unsigned integer elements are
804 supported in the index buffer. In practice this is true everywhere except
805 when running on plain OpenGL ES 2.0 implementations without the necessary
806 extension. When false, only 16-bit unsigned elements are supported in the
807 index buffer.
808
809 \value Compute Indicates that compute shaders, image load/store, and
810 storage buffers are supported. OpenGL older than 4.3 and OpenGL ES older
811 than 3.1 have no compute support.
812
813 \value WideLines Indicates that lines with a width other than 1 are
814 supported. When reported as not supported, the line width set on the
815 graphics pipeline state is ignored. This can always be false with some
816 backends (D3D11, D3D12, Metal). With Vulkan, the value depends on the
817 implementation. With OpenGL, wide lines are not supported in core profile
818 contexts.
819
820 \value VertexShaderPointSize Indicates that the size of rasterized points
821 set via \c{gl_PointSize} in the vertex shader is taken into account. When
822 reported as not supported, drawing points with a size other than 1 is not
823 supported. Setting \c{gl_PointSize} in the shader is still valid then, but
824 is ignored. (for example, when generating HLSL, the assignment is silently
825 dropped from the generated code) Note that some APIs (Metal, Vulkan)
826 require the point size to be set in the shader explicitly whenever drawing
827 points, even when the size is 1, as they do not automatically default to 1.
828
829 \value BaseVertex Indicates that
830 \l{QRhiCommandBuffer::drawIndexed()}{drawIndexed()} supports the \c
831 vertexOffset argument. When reported as not supported, the vertexOffset
832 value in an indexed draw is ignored. In practice this feature will be
833 unsupported with OpenGL and OpenGL ES versions lower than 3.2, and with
834 Metal on older iOS devices, including the iOS Simulator.
835
836 \value BaseInstance Indicates that instanced draw commands support the \c
837 firstInstance argument. When reported as not supported, the firstInstance
838 value is ignored and the instance ID starts from 0. In practice this feature
839 will be unsupported with with Metal on older iOS devices, including the iOS
840 Simulator, and all versions of OpenGL. The latter is due to OpenGL ES not
841 supporting draw calls with a base instance at all. Currently QRhi's OpenGL
842 backend does not implement the functionality for OpenGL (non-ES) either,
843 because portable applications cannot rely on a non-zero base instance in
844 practice due to GLES. If the application still chooses to do so, it should
845 be aware of the InstanceIndexIncludesBaseInstance feature as well.
846
847 \value TriangleFanTopology Indicates that QRhiGraphicsPipeline::setTopology()
848 supports QRhiGraphicsPipeline::TriangleFan. In practice this feature will be
849 unsupported with Metal and Direct 3D 11/12.
850
851 \value ReadBackNonUniformBuffer Indicates that
852 \l{QRhiResourceUpdateBatch::readBackBuffer()}{reading buffer contents} is
853 supported for QRhiBuffer instances with a usage different than
854 UniformBuffer. In practice this feature will be unsupported with OpenGL ES
855 2.0.
856
857 \value ReadBackNonBaseMipLevel Indicates that specifying a mip level other
858 than 0 is supported when reading back texture contents. When not supported,
859 specifying a non-zero level in QRhiReadbackDescription leads to returning
860 an all-zero image. In practice this feature will be unsupported with OpenGL
861 ES 2.0.
862
863 \value TexelFetch Indicates that texelFetch() and textureLod() are available
864 in shaders. In practice this will be reported as unsupported with OpenGL ES
865 2.0 and OpenGL 2.x contexts, because GLSL 100 es and versions before 130 do
866 not support these functions.
867
868 \value RenderToNonBaseMipLevel Indicates that specifying a mip level other
869 than 0 is supported when creating a QRhiTextureRenderTarget with a
870 QRhiTexture as its color attachment. When not supported, create() will fail
871 whenever the target mip level is not zero. In practice this feature will be
872 unsupported with OpenGL ES 2.0.
873
874 \value IntAttributes Indicates that specifying input attributes with
875 signed and unsigned integer types for a shader pipeline is supported. When
876 not supported, build() will succeed but just show a warning message and the
877 values of the target attributes will be broken. In practice this feature
878 will be unsupported with OpenGL ES 2.0 and OpenGL 2.x.
879
880 \value ScreenSpaceDerivatives Indicates that functions such as dFdx(),
881 dFdy(), and fwidth() are supported in shaders. In practice this feature will
882 be unsupported with OpenGL ES 2.0 without the GL_OES_standard_derivatives
883 extension.
884
885 \value ReadBackAnyTextureFormat Indicates that reading back texture
886 contents can be expected to work for any QRhiTexture::Format. Backends
887 other than OpenGL can be expected to return true for this feature. When
888 reported as false, which will typically happen with OpenGL, only the
889 formats QRhiTexture::RGBA8 and QRhiTexture::BGRA8 are guaranteed to be
890 supported for readbacks. In addition, with OpenGL, but not OpenGL ES,
891 reading back the 1 byte per component formats QRhiTexture::R8 and
892 QRhiTexture::RED_OR_ALPHA8 are supported as well. Reading back floating
893 point formats QRhiTexture::RGBA16F and RGBA32F may work too with OpenGL, as
894 long as the implementation provides support for these, but QRhi can give no
895 guarantees, as indicated by this flag.
896
897 \value PipelineCacheDataLoadSave Indicates that the pipelineCacheData() and
898 setPipelineCacheData() functions are functional. When not supported, the
899 functions will not perform any action, the retrieved blob is always empty,
900 and thus no benefits can be expected from retrieving and, during a
901 subsequent run of the application, reloading the pipeline cache content.
902
903 \value ImageDataStride Indicates that specifying a custom stride (row
904 length) for raw image data in texture uploads is supported. When not
905 supported (which can happen when the underlying API is OpenGL ES 2.0 without
906 support for GL_UNPACK_ROW_LENGTH),
907 QRhiTextureSubresourceUploadDescription::setDataStride() must not be used.
908
909 \value RenderBufferImport Indicates that QRhiRenderBuffer::createFrom() is
910 supported. For most graphics APIs this is not sensible because
911 QRhiRenderBuffer encapsulates texture objects internally, just like
912 QRhiTexture. With OpenGL however, renderbuffer object exist as a separate
913 object type in the API, and in certain environments (for example, where one
914 may want to associated a renderbuffer object with an EGLImage object) it is
915 important to allow wrapping an existing OpenGL renderbuffer object with a
916 QRhiRenderBuffer.
917
918 \value ThreeDimensionalTextures Indicates that 3D textures are supported.
919 In practice this feature will be unsupported with OpenGL and OpenGL ES
920 versions lower than 3.0.
921
922 \value RenderTo3DTextureSlice Indicates that rendering to a slice in a 3D
923 texture is supported. This can be unsupported with Vulkan 1.0 due to
924 relying on VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT which is a Vulkan 1.1
925 feature.
926
927 \value TextureArrays Indicates that texture arrays are supported and
928 QRhi::newTextureArray() is functional. Note that even when texture arrays
929 are not supported, arrays of textures are still available as those are two
930 independent features.
931
932 \value Tessellation Indicates that the tessellation control and evaluation
933 stages are supported. When reported as supported, the topology of a
934 QRhiGraphicsPipeline can be set to
935 \l{QRhiGraphicsPipeline::Patches}{Patches}, the number of control points
936 can be set via
937 \l{QRhiGraphicsPipeline::setPatchControlPointCount()}{setPatchControlPointCount()},
938 and shaders for tessellation control and evaluation can be specified in the
939 QRhiShaderStage list. Tessellation shaders have portability issues between
940 APIs (for example, translating GLSL/SPIR-V to HLSL is problematic due to
941 the way hull shaders are structured, whereas Metal uses a somewhat
942 different tessellation pipeline than others), and therefore unexpected
943 issues may still arise, even though basic functionality is implemented
944 across all the underlying APIs. For Direct 3D in particular, handwritten
945 HLSL hull and domain shaders must be injected into each QShader for the
946 tessellation control and evaluation stages, respectively, since qsb cannot
947 generate these from SPIR-V. Note that isoline tessellation should be
948 avoided as it will not be supported by all backends. The maximum patch
949 control point count portable between backends is 32.
950
951 \value GeometryShader Indicates that the geometry shader stage is supported.
952 When supported, a geometry shader can be specified in the QRhiShaderStage
953 list. Geometry Shaders are considered an experimental feature in QRhi and
954 can only be expected to be supported with Vulkan, Direct 3D 11 and 12,
955 OpenGL (3.2+) and OpenGL ES (3.2+), assuming the implementation reports it
956 as supported at run time. Starting with Qt 6.11 geometry shaders are
957 automatically translated to HLSL, and therefore no injection of handwritten
958 HLSL geometry shaders is necessary anymore (but note that gl_in and
959 expressions such as gl_in[0].gl_Position are not supported; rather, pass the
960 position as an output variable from the vertex shader). Geometry shaders are
961 not supported with Metal.
962
963 \value TextureArrayRange Indicates that for
964 \l{QRhi::newTextureArray()}{texture arrays} it is possible to specify a
965 range that is exposed to the shaders. Normally all array layers are exposed
966 and it is up to the shader to select the layer (via the third coordinate
967 passed to texture() when sampling the \c sampler2DArray). When supported,
968 calling QRhiTexture::setArrayRangeStart() and
969 QRhiTexture::setArrayRangeLength() before
970 \l{QRhiTexture::create()}{building} or
971 \l{QRhiTexture::createFrom()}{importing} the native texture has an effect,
972 and leads to selecting only the specified range from the array. This will
973 be necessary in special cases, such as when working with accelerated video
974 decoding and Direct 3D 11, because a texture array with both
975 \c{D3D11_BIND_DECODER} and \c{D3D11_BIND_SHADER_RESOURCE} on it is only
976 usable as a shader resource if a single array layer is selected. Note that
977 all this is applicable only when the texture is used as a
978 QRhiShaderResourceBinding::SampledTexture or
979 QRhiShaderResourceBinding::Texture shader resource, and is not compatible
980 with image load/store. This feature is only available with some backends as
981 it does not map well to all graphics APIs, and it is only meant to provide
982 support for special cases anyhow. In practice the feature can be expected to
983 be supported with Direct3D 11/12 and Vulkan.
984
985 \value NonFillPolygonMode Indicates that setting a PolygonMode other than
986 the default Fill is supported for QRhiGraphicsPipeline. A common use case
987 for changing the mode to Line is to get wireframe rendering. This however
988 is not available as a core OpenGL ES feature, and is optional with Vulkan
989 as well as some mobile GPUs may not offer the feature.
990
991 \value OneDimensionalTextures Indicates that 1D textures are supported.
992 In practice this feature will be unsupported on OpenGL ES.
993
994 \value OneDimensionalTextureMipmaps Indicates that generating 1D texture
995 mipmaps are supported. In practice this feature will be unsupported on
996 backends that do not report support for
997 \l{OneDimensionalTextures}, Metal, and Direct 3D 12.
998
999 \value HalfAttributes Indicates that specifying input attributes with half
1000 precision (16bit) floating point types for a shader pipeline is supported.
1001 When not supported, build() will succeed but just show a warning message
1002 and the values of the target attributes will be broken. In practice this
1003 feature will be unsupported in some OpenGL ES 2.0 and OpenGL 2.x
1004 implementations. Note that while Direct3D 11/12 does support half precision
1005 input attributes, it does not support the half3 type. The D3D backends pass
1006 half3 attributes as half4. To ensure cross platform compatibility, half3
1007 inputs should be padded to 8 bytes.
1008
1009 \value RenderToOneDimensionalTexture Indicates that 1D texture render
1010 targets are supported. In practice this feature will be unsupported on
1011 backends that do not report support for
1012 \l{OneDimensionalTextures}, and Metal.
1013
1014 \value ThreeDimensionalTextureMipmaps Indicates that generating 3D texture
1015 mipmaps are supported. This is typically supported with all backends starting
1016 with Qt 6.10.
1017
1018 \value MultiView Indicates that multiview, see e.g.
1019 \l{https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_multiview.html}{VK_KHR_multiview}
1020 is supported. With OpenGL ES 2.0, Direct 3D 11, and OpenGL (ES)
1021 implementations without \c{GL_OVR_multiview2} this feature will not be
1022 supported. With Vulkan 1.1 and newer, and Direct 3D 12 multiview is
1023 typically supported. When reported as supported, creating a
1024 QRhiTextureRenderTarget with a QRhiColorAttachment that references a texture
1025 array and has \l{QRhiColorAttachment::setMultiViewCount()}{multiViewCount}
1026 set enables recording a render pass that uses multiview rendering. In addition,
1027 any QRhiGraphicsPipeline used in that render pass must have
1028 \l{QRhiGraphicsPipeline::setMultiViewCount()}{the same view count set}. Note that
1029 multiview is only available in combination with 2D texture arrays. It cannot
1030 be used to optimize the rendering into individual textures (e.g. two, for
1031 the left and right eyes). Rather, the target of a multiview render pass is
1032 always a texture array, automatically rendering to the layer (array element)
1033 corresponding to each view. Therefore this feature implies \l TextureArrays
1034 as well. Multiview rendering is not supported in combination with
1035 tessellation or geometry shaders. See QRhiColorAttachment::setMultiViewCount()
1036 for further details on multiview rendering. This enum value has been introduced in Qt 6.7.
1037
1038 \value TextureViewFormat Indicates that setting a
1039 \l{QRhiTexture::setWriteViewFormat()}{view format} on a QRhiTexture is
1040 effective. When reported as supported, setting the read (sampling) or write
1041 (render target / image load-store) view mode changes the texture's viewing
1042 format. When unsupported, setting a view format has no effect. Note that Qt
1043 has no knowledge or control over format compatibility or resource view rules
1044 in the underlying 3D API and its implementation. Passing in unsuitable,
1045 incompatible formats may lead to errors and unspecified behavior. This is
1046 provided mainly to allow "casting" rendering into a texture created with an
1047 sRGB format to non-sRGB to avoid the unwanted linear->sRGB conversion on
1048 shader writes. Other types of casting may or may not be functional,
1049 depending on the underlying API. Currently implemented for Vulkan and Direct
1050 3D 12. With D3D12 the feature is available only if
1051 \c CastingFullyTypedFormatSupported is supported, see
1052 \l{https://microsoft.github.io/DirectX-Specs/d3d/RelaxedCasting.html} (and
1053 note that QRhi always uses fully typed formats for textures.) This enum
1054 value has been introduced in Qt 6.8.
1055
1056 \value ResolveDepthStencil Indicates that resolving a multisample depth or
1057 depth-stencil texture is supported. Otherwise,
1058 \l{QRhiTextureRenderTargetDescription::setDepthResolveTexture()}{setting a
1059 depth resolve texture} is not functional and must be avoided. Direct 3D 11
1060 and 12 have no support for resolving depth/depth-stencil formats, and
1061 therefore this feature will never be supported with those. Vulkan 1.0 has no
1062 API to request resolving a depth-stencil attachment. Therefore, with Vulkan
1063 this feature will only be supported with Vulkan 1.2 and up, and on 1.1
1064 implementations with the appropriate extensions present. This feature is
1065 provided for the rare case when resolving into a non-multisample depth
1066 texture becomes necessary, for example when rendering into an
1067 OpenXR-provided depth texture (XR_KHR_composition_layer_depth). This enum
1068 value has been introduced in Qt 6.8.
1069
1070 \value VariableRateShading Indicates that per-draw (per-pipeline) variable
1071 rate shading is supported. When reported as supported, \l
1072 QRhiCommandBuffer::setShadingRate() is functional and has an effect for
1073 QRhiGraphicsPipeline objects that declared \l
1074 QRhiGraphicsPipeline::UsesShadingRate in their flags. Call \l
1075 QRhi::supportedShadingRates() to check which rates are supported. (1x1 is
1076 always supported, other typical values are 2x2, 1x2, 2x1, 2x4, 4x2, 4x4).
1077 This feature can be expected to be supported with Direct 3D 12 and Vulkan,
1078 assuming the implementation and GPU used at run time supports VRS. This enum
1079 value has been introduced in Qt 6.9.
1080
1081 \value VariableRateShadingMap Indicates that image-based specification of
1082 the shading rate is possible. The "image" is not necessarily a texture, it
1083 may be a native 3D API object, depending on the underlying backend and
1084 graphics API at run time. In practice this feature can be expected to be
1085 supported with Direct 3D 12, Vulkan, and Metal, assuming the GPU is modern
1086 enough to support VRS. To check if D3D12/Vulkan-style image-based VRS is
1087 suspported, use VariableRateShadingMapWithTexture instead. When this feature
1088 is reported as supported, there are two possibilities: when
1089 VariableRateShadingMapWithTexture is also true, then QRhiShadingRateMap
1090 consumes QRhiTexture objects via the createFrom() overload taking a
1091 QRhiTexture argument. When VariableRateShadingMapWithTexture is false, then
1092 QRhiShadingRateMap consumes some other type of native objects, for example
1093 an MTLRasterizationRateMap in case of Metal. Use the createFrom() overload
1094 taking a NativeShadingRateMap in this case. This enum value has been
1095 introduced in Qt 6.9.
1096
1097 \value VariableRateShadingMapWithTexture Indicates that image-based
1098 specification of the shading rate is supported via regular textures. In
1099 practice this may be supported with Direct 3D 12 and Vulkan. This enum value
1100 has been introduced in Qt 6.9.
1101
1102 \value PerRenderTargetBlending Indicates that per rendertarget blending is
1103 supported i.e. different render targets in MRT framebuffer can have different
1104 blending modes. In practice this can be expected to be supported everywhere
1105 except OpenGL ES, where it is only available with GLES 3.2 implementations.
1106 This enum value has been introduced in Qt 6.9.
1107
1108 \value SampleVariables Indicates that gl_SampleID, gl_SamplePosition,
1109 gl_SampleMaskIn and gl_SampleMask variables are available in fragment shaders.
1110 In practice this can be expected to be supported everywhere except OpenGL ES,
1111 where it is only available with GLES 3.2 implementations.
1112 This enum value has been introduced in Qt 6.9.
1113
1114 \value InstanceIndexIncludesBaseInstance Indicates that \c gl_InstanceIndex
1115 includes the base instance (the \c firstInstance argument in draw calls) in
1116 its value. When this feature is unsupported, but BaseInstance is, it
1117 indicates that \c gl_InstanceIndex always starts at 0, not the base value.
1118 In practice this will be the case for Direct 3D 11 and 12 at the moment.
1119 With Vulkan and Metal this feature is expected to be reported as supported
1120 always. This enum value has been introduced in Qt 6.11.
1121
1122 \value DepthClamp Indicates that enabling depth clamping is supported. When
1123 reported as unsupported, which will be the case with OpenGL ES, OpenGL
1124 versions before 3.2 without the relevant extension present, and Metal on the
1125 iOS Simulator, calling \l{QRhiCommandBuffer::}{setDepthClamp()} with an argument
1126 of \c true has no effect. This enum value has been introduced in Qt 6.11.
1127
1128 \value [since 6.12] DrawIndirect Indicates that the
1129 \l{QRhiCommandBuffer::drawIndirect()}{drawIndirect()}
1130 and \l{QRhiCommandBuffer::drawIndexedIndirect()}{drawIndexedIndirect()}
1131 functions are available.
1132 In practice this can be expected to be supported everywhere except on
1133 OpenGL ES < 3.1.
1134
1135 \value [since 6.12] DrawIndirectMulti Indicates that a drawCount > 1 is natively
1136 supported by the backend in \l{QRhiCommandBuffer::drawIndirect()}{drawIndirect()}
1137 and \l{QRhiCommandBuffer::drawIndexedIndirect()}{drawIndexedIndirect()}.
1138 Otherwise, multiple draw calls are issued on the CPU by the RHI.
1139 In practice this can be expected to be supported on Vulkan 1.1+, OpenGL 4.3+
1140 and D3D12.
1141 */
1142
1143/*!
1144 \enum QRhi::BeginFrameFlag
1145 Flag values for QRhi::beginFrame()
1146 */
1147
1148/*!
1149 \enum QRhi::EndFrameFlag
1150 Flag values for QRhi::endFrame()
1151
1152 \value SkipPresent Specifies that no present command is to be queued or no
1153 swapBuffers call is to be made. This way no image is presented. Generating
1154 multiple frames with all having this flag set is not recommended (except,
1155 for example, for benchmarking purposes - but keep in mind that backends may
1156 behave differently when it comes to waiting for command completion without
1157 presenting so the results are not comparable between them)
1158 */
1159
1160/*!
1161 \enum QRhi::ResourceLimit
1162 Describes the resource limit to query.
1163
1164 \value TextureSizeMin Minimum texture width and height. This is typically
1165 1. The minimum texture size is handled gracefully, meaning attempting to
1166 create a texture with an empty size will instead create a texture with the
1167 minimum size.
1168
1169 \value TextureSizeMax Maximum texture width and height. This depends on the
1170 graphics API and sometimes the platform or implementation as well.
1171 Typically the value is in the range 4096 - 16384. Attempting to create
1172 textures larger than this is expected to fail.
1173
1174 \value MaxColorAttachments The maximum number of color attachments for a
1175 QRhiTextureRenderTarget, in case multiple render targets are supported. When
1176 MRT is not supported, the value is 1. Otherwise this is typically 8, but
1177 watch out for the fact that OpenGL only mandates 4 as the minimum, and that
1178 is what some OpenGL ES implementations provide.
1179
1180 \value FramesInFlight The number of frames the backend may keep "in
1181 flight": with backends like Vulkan or Metal, it is the responsibility of
1182 QRhi to block whenever starting a new frame and finding the CPU is already
1183 \c{N - 1} frames ahead of the GPU (because the command buffer submitted in
1184 frame no. \c{current} - \c{N} has not yet completed). The value N is what
1185 is returned from here, and is typically 2. This can be relevant to
1186 applications that integrate rendering done directly with the graphics API,
1187 as such rendering code may want to perform double (if the value is 2)
1188 buffering for resources, such as, buffers, similarly to the QRhi backends
1189 themselves. The current frame slot index (a value running 0, 1, .., N-1,
1190 then wrapping around) is retrievable from QRhi::currentFrameSlot(). The
1191 value is 1 for backends where the graphics API offers no such low level
1192 control over the command submission process. Note that pipelining may still
1193 happen even when this value is 1 (some backends, such as D3D11, are
1194 designed to attempt to enable this, for instance, by using an update
1195 strategy for uniform buffers that does not stall the pipeline), but that is
1196 then not controlled by QRhi and so not reflected here in the API.
1197
1198 \value MaxAsyncReadbackFrames The number of \l{QRhi::endFrame()}{submitted}
1199 frames (including the one that contains the readback) after which an
1200 asynchronous texture or buffer readback is guaranteed to complete upon
1201 \l{QRhi::beginFrame()}{starting a new frame}.
1202
1203 \value MaxThreadGroupsPerDimension The maximum number of compute
1204 work/thread groups that can be dispatched. Effectively the maximum value
1205 for the arguments of QRhiCommandBuffer::dispatch(). Typically 65535.
1206
1207 \value MaxThreadsPerThreadGroup The maximum number of invocations in a
1208 single local work group, or in other terminology, the maximum number of
1209 threads in a thread group. Effectively the maximum value for the product of
1210 \c local_size_x, \c local_size_y, and \c local_size_z in the compute
1211 shader. Typical values are 128, 256, 512, 1024, or 1536. Watch out that
1212 both OpenGL ES and Vulkan specify only 128 as the minimum required limit
1213 for implementations. While uncommon for Vulkan, some OpenGL ES 3.1
1214 implementations for mobile/embedded devices only support the spec-mandated
1215 minimum value.
1216
1217 \value MaxThreadGroupX The maximum size of a work/thread group in the X
1218 dimension. Effectively the maximum value of \c local_size_x in the compute
1219 shader. Typically 256 or 1024.
1220
1221 \value MaxThreadGroupY The maximum size of a work/thread group in the Y
1222 dimension. Effectively the maximum value of \c local_size_y in the compute
1223 shader. Typically 256 or 1024.
1224
1225 \value MaxThreadGroupZ The maximum size of a work/thread group in the Z
1226 dimension. Effectively the maximum value of \c local_size_z in the compute
1227 shader. Typically 64 or 256.
1228
1229 \value TextureArraySizeMax Maximum texture array size. Typically in range
1230 256 - 2048. Attempting to \l{QRhi::newTextureArray()}{create a texture
1231 array} with more elements will likely fail.
1232
1233 \value MaxUniformBufferRange The number of bytes that can be exposed from a
1234 uniform buffer to the shaders at once. On OpenGL ES 2.0 and 3.0
1235 implementations this may be as low as 3584 bytes (224 four component, 32
1236 bits per component vectors). Elsewhere the value is typically 16384 (1024
1237 vec4s) or 65536 (4096 vec4s).
1238
1239 \value MaxVertexInputs The number of input attributes to the vertex shader.
1240 The location in a QRhiVertexInputAttribute must be in range \c{[0,
1241 MaxVertexInputs-1]}. The value may be as low as 8 with OpenGL ES 2.0.
1242 Elsewhere, typical values are 16, 31, or 32.
1243
1244 \value MaxVertexOutputs The maximum number of outputs (4 component vector
1245 \c out variables) from the vertex shader. The value may be as low as 8 with
1246 OpenGL ES 2.0, and 15 with OpenGL ES 3.0 and some Metal devices. Elsewhere,
1247 a typical value is 32.
1248
1249 \value ShadingRateImageTileSize The tile size for shading rate textures. 0
1250 if the QRhi::VariableRateShadingMapWithTexture feature is not supported.
1251 Otherwise a value such as 16, indicating, for example, a tile size of 16x16.
1252 Each byte in the (R8UI) shading rate texture defines then the shading rate
1253 for a tile of 16x16 pixels. See \l QRhiShadingRateMap for details.
1254 */
1255
1256/*!
1257 \class QRhiInitParams
1258 \inmodule QtGuiPrivate
1259 \inheaderfile rhi/qrhi.h
1260 \since 6.6
1261 \brief Base class for backend-specific initialization parameters.
1262
1263 Contains fields that are relevant to all backends.
1264
1265 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1266 for details.
1267 */
1268
1269/*!
1270 \class QRhiDepthStencilClearValue
1271 \inmodule QtGuiPrivate
1272 \inheaderfile rhi/qrhi.h
1273 \since 6.6
1274 \brief Specifies clear values for a depth or stencil buffer.
1275
1276 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1277 for details.
1278 */
1279
1280/*!
1281 \fn QRhiDepthStencilClearValue::QRhiDepthStencilClearValue() = default
1282
1283 Constructs a depth/stencil clear value with depth clear value 1.0f and
1284 stencil clear value 0.
1285 */
1286
1287/*!
1288 Constructs a depth/stencil clear value with depth clear value \a d and
1289 stencil clear value \a s.
1290 */
1291QRhiDepthStencilClearValue::QRhiDepthStencilClearValue(float d, quint32 s)
1292 : m_d(d),
1293 m_s(s)
1294{
1295}
1296
1297/*!
1298 \fn float QRhiDepthStencilClearValue::depthClearValue() const
1299 \return the depth clear value. In most cases this is 1.0f.
1300 */
1301
1302/*!
1303 \fn void QRhiDepthStencilClearValue::setDepthClearValue(float d)
1304 Sets the depth clear value to \a d.
1305 */
1306
1307/*!
1308 \fn quint32 QRhiDepthStencilClearValue::stencilClearValue() const
1309 \return the stencil clear value. In most cases this is 0.
1310 */
1311
1312/*!
1313 \fn void QRhiDepthStencilClearValue::setStencilClearValue(quint32 s)
1314 Sets the stencil clear value to \a s.
1315 */
1316
1317/*!
1318 \fn bool QRhiDepthStencilClearValue::operator==(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) noexcept
1319
1320 \return \c true if the values in the two QRhiDepthStencilClearValue objects
1321 \a a and \a b are equal.
1322 */
1323
1324/*!
1325 \fn bool QRhiDepthStencilClearValue::operator!=(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) noexcept
1326
1327 \return \c false if the values in the two QRhiDepthStencilClearValue
1328 objects \a a and \a b are equal; otherwise returns \c true.
1329
1330*/
1331
1332/*!
1333 \fn size_t QRhiDepthStencilClearValue::qHash(const QRhiDepthStencilClearValue &key, size_t seed)
1334 \qhash{QRhiDepthStencilClearValue}
1335 */
1336
1337#ifndef QT_NO_DEBUG_STREAM
1338QDebug operator<<(QDebug dbg, const QRhiDepthStencilClearValue &v)
1339{
1340 QDebugStateSaver saver(dbg);
1341 dbg.nospace() << "QRhiDepthStencilClearValue(depth-clear=" << v.depthClearValue()
1342 << " stencil-clear=" << v.stencilClearValue()
1343 << ')';
1344 return dbg;
1345}
1346#endif
1347
1348/*!
1349 \class QRhiViewport
1350 \inmodule QtGuiPrivate
1351 \inheaderfile rhi/qrhi.h
1352 \since 6.6
1353 \brief Specifies a viewport rectangle.
1354
1355 Used with QRhiCommandBuffer::setViewport().
1356
1357 QRhi assumes OpenGL-style viewport coordinates, meaning x and y are
1358 bottom-left. Negative width or height are not allowed.
1359
1360 Typical usage is like the following:
1361
1362 \code
1363 const QSize outputSizeInPixels = swapchain->currentPixelSize();
1364 const QRhiViewport viewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height());
1365 cb->beginPass(swapchain->currentFrameRenderTarget(), Qt::black, { 1.0f, 0 });
1366 cb->setGraphicsPipeline(ps);
1367 cb->setViewport(viewport);
1368 // ...
1369 \endcode
1370
1371 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1372 for details.
1373
1374 \sa QRhiCommandBuffer::setViewport(), QRhi::clipSpaceCorrMatrix(), QRhiScissor
1375 */
1376
1377/*!
1378 \fn QRhiViewport::QRhiViewport() = default
1379
1380 Constructs a viewport description with an empty rectangle and a depth range
1381 of 0.0f - 1.0f.
1382
1383 \sa QRhi::clipSpaceCorrMatrix()
1384 */
1385
1386/*!
1387 Constructs a viewport description with the rectangle specified by \a x, \a
1388 y, \a w, \a h and the depth range \a minDepth and \a maxDepth.
1389
1390 \note \a x and \a y are assumed to be the bottom-left position. \a w and \a
1391 h should not be negative, the viewport will be ignored by
1392 QRhiCommandBuffer::setViewport() otherwise.
1393
1394 \sa QRhi::clipSpaceCorrMatrix()
1395 */
1396QRhiViewport::QRhiViewport(float x, float y, float w, float h, float minDepth, float maxDepth)
1397 : m_rect { { x, y, w, h } },
1398 m_minDepth(minDepth),
1399 m_maxDepth(maxDepth)
1400{
1401}
1402
1403/*!
1404 \fn std::array<float, 4> QRhiViewport::viewport() const
1405 \return the viewport x, y, width, and height.
1406 */
1407
1408/*!
1409 \fn void QRhiViewport::setViewport(float x, float y, float w, float h)
1410 Sets the viewport's position and size to \a x, \a y, \a w, and \a h.
1411
1412 \note Viewports are specified in a coordinate system that has its origin in
1413 the bottom-left.
1414 */
1415
1416/*!
1417 \fn float QRhiViewport::minDepth() const
1418 \return the minDepth value of the depth range of the viewport.
1419 */
1420
1421/*!
1422 \fn void QRhiViewport::setMinDepth(float minDepth)
1423 Sets the \a minDepth of the depth range of the viewport.
1424 By default this is set to 0.0f.
1425 */
1426
1427/*!
1428 \fn float QRhiViewport::maxDepth() const
1429 \return the maxDepth value of the depth range of the viewport.
1430 */
1431
1432/*!
1433 \fn void QRhiViewport::setMaxDepth(float maxDepth)
1434 Sets the \a maxDepth of the depth range of the viewport.
1435 By default this is set to 1.0f.
1436 */
1437
1438/*!
1439 \fn bool QRhiViewport::operator==(const QRhiViewport &a, const QRhiViewport &b) noexcept
1440
1441 \return \c true if the values in the two QRhiViewport objects
1442 \a a and \a b are equal.
1443 */
1444
1445/*!
1446 \fn bool QRhiViewport::operator!=(const QRhiViewport &a, const QRhiViewport &b) noexcept
1447
1448 \return \c false if the values in the two QRhiViewport
1449 objects \a a and \a b are equal; otherwise returns \c true.
1450*/
1451
1452/*!
1453 \fn size_t QRhiViewport::qHash(const QRhiViewport &key, size_t seed)
1454 \qhash{QRhiViewport}
1455 */
1456
1457#ifndef QT_NO_DEBUG_STREAM
1458QDebug operator<<(QDebug dbg, const QRhiViewport &v)
1459{
1460 QDebugStateSaver saver(dbg);
1461 const std::array<float, 4> r = v.viewport();
1462 dbg.nospace() << "QRhiViewport(bottom-left-x=" << r[0]
1463 << " bottom-left-y=" << r[1]
1464 << " width=" << r[2]
1465 << " height=" << r[3]
1466 << " minDepth=" << v.minDepth()
1467 << " maxDepth=" << v.maxDepth()
1468 << ')';
1469 return dbg;
1470}
1471#endif
1472
1473/*!
1474 \class QRhiScissor
1475 \inmodule QtGuiPrivate
1476 \inheaderfile rhi/qrhi.h
1477 \since 6.6
1478 \brief Specifies a scissor rectangle.
1479
1480 Used with QRhiCommandBuffer::setScissor(). Setting a scissor rectangle is
1481 only possible with a QRhiGraphicsPipeline that has
1482 QRhiGraphicsPipeline::UsesScissor set.
1483
1484 QRhi assumes OpenGL-style scissor coordinates, meaning x and y are
1485 bottom-left. Negative width or height are not allowed. However, apart from
1486 that, the flexible OpenGL semantics apply: negative x and y, partially out
1487 of bounds rectangles, etc. will be handled gracefully, clamping as
1488 appropriate. Therefore, any rendering logic targeting OpenGL can feed
1489 scissor rectangles into QRhiScissor as-is, without any adaptation.
1490
1491 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1492 for details.
1493
1494 \sa QRhiCommandBuffer::setScissor(), QRhiViewport
1495 */
1496
1497/*!
1498 \fn QRhiScissor::QRhiScissor() = default
1499
1500 Constructs an empty scissor.
1501 */
1502
1503/*!
1504 Constructs a scissor with the rectangle specified by \a x, \a y, \a w, and
1505 \a h.
1506
1507 \note \a x and \a y are assumed to be the bottom-left position. Negative \a w
1508 or \a h are not allowed, such scissor rectangles will be ignored by
1509 QRhiCommandBuffer. Other than that, the flexible OpenGL semantics apply:
1510 negative x and y, partially out of bounds rectangles, etc. will be handled
1511 gracefully, clamping as appropriate.
1512 */
1513QRhiScissor::QRhiScissor(int x, int y, int w, int h)
1514 : m_rect { { x, y, w, h } }
1515{
1516}
1517
1518/*!
1519 \fn std::array<int, 4> QRhiScissor::scissor() const
1520 \return the scissor position and size.
1521 */
1522
1523/*!
1524 \fn void QRhiScissor::setScissor(int x, int y, int w, int h)
1525 Sets the scissor position and size to \a x, \a y, \a w, \a h.
1526
1527 \note The position is always expected to be specified in a coordinate
1528 system that has its origin in the bottom-left corner, like OpenGL.
1529 */
1530
1531/*!
1532 \fn bool QRhiScissor::operator==(const QRhiScissor &a, const QRhiScissor &b) noexcept
1533
1534 \return \c true if the values in the two QRhiScissor objects
1535 \a a and \a b are equal.
1536 */
1537
1538/*!
1539 \fn bool QRhiScissor::operator!=(const QRhiScissor &a, const QRhiScissor &b) noexcept
1540
1541 \return \c false if the values in the two QRhiScissor
1542 objects \a a and \a b are equal; otherwise returns \c true.
1543*/
1544
1545/*!
1546 \fn size_t QRhiScissor::qHash(const QRhiScissor &key, size_t seed)
1547 \qhash{QRhiScissor}
1548 */
1549
1550#ifndef QT_NO_DEBUG_STREAM
1551QDebug operator<<(QDebug dbg, const QRhiScissor &s)
1552{
1553 QDebugStateSaver saver(dbg);
1554 const std::array<int, 4> r = s.scissor();
1555 dbg.nospace() << "QRhiScissor(bottom-left-x=" << r[0]
1556 << " bottom-left-y=" << r[1]
1557 << " width=" << r[2]
1558 << " height=" << r[3]
1559 << ')';
1560 return dbg;
1561}
1562#endif
1563
1564/*!
1565 \class QRhiVertexInputBinding
1566 \inmodule QtGuiPrivate
1567 \inheaderfile rhi/qrhi.h
1568 \since 6.6
1569 \brief Describes a vertex input binding.
1570
1571 Specifies the stride (in bytes, must be a multiple of 4), the
1572 classification and optionally the instance step rate.
1573
1574 As an example, assume a vertex shader with the following inputs:
1575
1576 \badcode
1577 layout(location = 0) in vec4 position;
1578 layout(location = 1) in vec2 texcoord;
1579 \endcode
1580
1581 Now let's assume also that 3 component vertex positions \c{(x, y, z)} and 2
1582 component texture coordinates \c{(u, v)} are provided in a non-interleaved
1583 format in a buffer (or separate buffers even). Defining two bindings
1584 could then be done like this:
1585
1586 \code
1587 QRhiVertexInputLayout inputLayout;
1588 inputLayout.setBindings({
1589 { 3 * sizeof(float) },
1590 { 2 * sizeof(float) }
1591 });
1592 \endcode
1593
1594 Only the stride is interesting here since instancing is not used. The
1595 binding number is given by the index of the QRhiVertexInputBinding
1596 element in the bindings vector of the QRhiVertexInputLayout.
1597
1598 Once a graphics pipeline with this vertex input layout is bound, the vertex
1599 inputs could be set up like the following for drawing a cube with 36
1600 vertices, assuming we have a single buffer with first the positions and
1601 then the texture coordinates:
1602
1603 \code
1604 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
1605 { cubeBuf, 0 },
1606 { cubeBuf, 36 * 3 * sizeof(float) }
1607 };
1608 cb->setVertexInput(0, 2, vbufBindings);
1609 \endcode
1610
1611 Note how the index defined by \c {startBinding + i}, where \c i is the
1612 index in the second argument of
1613 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()}, matches the
1614 index of the corresponding entry in the \c bindings vector of the
1615 QRhiVertexInputLayout.
1616
1617 \note the stride must always be a multiple of 4.
1618
1619 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1620 for details.
1621
1622 \sa QRhiCommandBuffer::setVertexInput()
1623 */
1624
1625/*!
1626 \enum QRhiVertexInputBinding::Classification
1627 Describes the input data classification.
1628
1629 \value PerVertex Data is per-vertex
1630 \value PerInstance Data is per-instance
1631 */
1632
1633/*!
1634 \fn QRhiVertexInputBinding::QRhiVertexInputBinding() = default
1635
1636 Constructs a default vertex input binding description.
1637 */
1638
1639/*!
1640 Constructs a vertex input binding description with the specified \a stride,
1641 classification \a cls, and instance step rate \a stepRate.
1642
1643 \note \a stepRate other than 1 is only supported when
1644 QRhi::CustomInstanceStepRate is reported to be supported.
1645 */
1646QRhiVertexInputBinding::QRhiVertexInputBinding(quint32 stride, Classification cls, quint32 stepRate)
1647 : m_stride(stride),
1648 m_classification(cls),
1649 m_instanceStepRate(stepRate)
1650{
1651}
1652
1653/*!
1654 \fn quint32 QRhiVertexInputBinding::stride() const
1655 \return the stride in bytes.
1656 */
1657
1658/*!
1659 \fn void QRhiVertexInputBinding::setStride(quint32 s)
1660 Sets the stride to \a s.
1661 */
1662
1663/*!
1664 \fn QRhiVertexInputBinding::Classification QRhiVertexInputBinding::classification() const
1665 \return the input data classification.
1666 */
1667
1668/*!
1669 \fn void QRhiVertexInputBinding::setClassification(Classification c)
1670 Sets the input data classification \a c. By default this is set to PerVertex.
1671 */
1672
1673/*!
1674 \fn quint32 QRhiVertexInputBinding::instanceStepRate() const
1675 \return the instance step rate.
1676 */
1677
1678/*!
1679 \fn void QRhiVertexInputBinding::setInstanceStepRate(quint32 rate)
1680 Sets the instance step \a rate. By default this is set to 1.
1681 */
1682
1683/*!
1684 \fn bool QRhiVertexInputBinding::operator==(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) noexcept
1685
1686 \return \c true if the values in the two QRhiVertexInputBinding objects
1687 \a a and \a b are equal.
1688 */
1689
1690/*!
1691 \fn bool QRhiVertexInputBinding::operator!=(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) noexcept
1692
1693 \return \c false if the values in the two QRhiVertexInputBinding
1694 objects \a a and \a b are equal; otherwise returns \c true.
1695*/
1696
1697/*!
1698 \fn size_t QRhiVertexInputBinding::qHash(const QRhiVertexInputBinding &key, size_t seed)
1699 \qhash{QRhiVertexInputBinding}
1700 */
1701
1702#ifndef QT_NO_DEBUG_STREAM
1703QDebug operator<<(QDebug dbg, const QRhiVertexInputBinding &b)
1704{
1705 QDebugStateSaver saver(dbg);
1706 dbg.nospace() << "QRhiVertexInputBinding(stride=" << b.stride()
1707 << " cls=" << b.classification()
1708 << " step-rate=" << b.instanceStepRate()
1709 << ')';
1710 return dbg;
1711}
1712#endif
1713
1714/*!
1715 \class QRhiVertexInputAttribute
1716 \inmodule QtGuiPrivate
1717 \inheaderfile rhi/qrhi.h
1718 \since 6.6
1719 \brief Describes a single vertex input element.
1720
1721 The members specify the binding number, location, format, and offset for a
1722 single vertex input element.
1723
1724 \note For HLSL it is assumed that the vertex shader translated from SPIR-V
1725 uses
1726 \c{TEXCOORD<location>} as the semantic for each input. Hence no separate
1727 semantic name and index.
1728
1729 As an example, assume a vertex shader with the following inputs:
1730
1731 \badcode
1732 layout(location = 0) in vec4 position;
1733 layout(location = 1) in vec2 texcoord;
1734 \endcode
1735
1736 Now let's assume that we have 3 component vertex positions \c{(x, y, z)}
1737 and 2 component texture coordinates \c{(u, v)} are provided in a
1738 non-interleaved format in a buffer (or separate buffers even). Once two
1739 bindings are defined, the attributes could be specified as:
1740
1741 \code
1742 QRhiVertexInputLayout inputLayout;
1743 inputLayout.setBindings({
1744 { 3 * sizeof(float) },
1745 { 2 * sizeof(float) }
1746 });
1747 inputLayout.setAttributes({
1748 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
1749 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
1750 });
1751 \endcode
1752
1753 Once a graphics pipeline with this vertex input layout is bound, the vertex
1754 inputs could be set up like the following for drawing a cube with 36
1755 vertices, assuming we have a single buffer with first the positions and
1756 then the texture coordinates:
1757
1758 \code
1759 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
1760 { cubeBuf, 0 },
1761 { cubeBuf, 36 * 3 * sizeof(float) }
1762 };
1763 cb->setVertexInput(0, 2, vbufBindings);
1764 \endcode
1765
1766 When working with interleaved data, there will typically be just one
1767 binding, with multiple attributes referring to that same buffer binding
1768 point:
1769
1770 \code
1771 QRhiVertexInputLayout inputLayout;
1772 inputLayout.setBindings({
1773 { 5 * sizeof(float) }
1774 });
1775 inputLayout.setAttributes({
1776 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
1777 { 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) }
1778 });
1779 \endcode
1780
1781 and then:
1782
1783 \code
1784 const QRhiCommandBuffer::VertexInput vbufBinding(interleavedCubeBuf, 0);
1785 cb->setVertexInput(0, 1, &vbufBinding);
1786 \endcode
1787
1788 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1789 for details.
1790
1791 \sa QRhiCommandBuffer::setVertexInput()
1792 */
1793
1794/*!
1795 \enum QRhiVertexInputAttribute::Format
1796 Specifies the type of the element data.
1797
1798 \value Float4 Four component float vector
1799 \value Float3 Three component float vector
1800 \value Float2 Two component float vector
1801 \value Float Float
1802 \value UNormByte4 Four component normalized unsigned byte vector
1803 \value UNormByte2 Two component normalized unsigned byte vector
1804 \value UNormByte Normalized unsigned byte
1805 \value UInt4 Four component unsigned integer vector
1806 \value UInt3 Three component unsigned integer vector
1807 \value UInt2 Two component unsigned integer vector
1808 \value UInt Unsigned integer
1809 \value SInt4 Four component signed integer vector
1810 \value SInt3 Three component signed integer vector
1811 \value SInt2 Two component signed integer vector
1812 \value SInt Signed integer
1813 \value Half4 Four component half precision (16 bit) float vector
1814 \value Half3 Three component half precision (16 bit) float vector
1815 \value Half2 Two component half precision (16 bit) float vector
1816 \value Half Half precision (16 bit) float
1817 \value UShort4 Four component unsigned short (16 bit) integer vector
1818 \value UShort3 Three component unsigned short (16 bit) integer vector
1819 \value UShort2 Two component unsigned short (16 bit) integer vector
1820 \value UShort Unsigned short (16 bit) integer
1821 \value SShort4 Four component signed short (16 bit) integer vector
1822 \value SShort3 Three component signed short (16 bit) integer vector
1823 \value SShort2 Two component signed short (16 bit) integer vector
1824 \value SShort Signed short (16 bit) integer
1825
1826 \note Support for half precision floating point attributes is indicated at
1827 run time by the QRhi::Feature::HalfAttributes feature flag.
1828
1829 \note Direct3D 11/12 supports 16 bit input attributes, but does not support
1830 the Half3, UShort3 or SShort3 types. The D3D backends pass through Half3 as
1831 Half4, UShort3 as UShort4, and SShort3 as SShort4. To ensure cross platform
1832 compatibility, 16 bit inputs should be padded to 8 bytes.
1833 */
1834
1835/*!
1836 \fn QRhiVertexInputAttribute::QRhiVertexInputAttribute() = default
1837
1838 Constructs a default vertex input attribute description.
1839 */
1840
1841/*!
1842 Constructs a vertex input attribute description with the specified \a
1843 binding number, \a location, \a format, and \a offset.
1844
1845 \a matrixSlice should be -1 except when this attribute corresponds to a row
1846 or column of a matrix (for example, a 4x4 matrix becomes 4 vec4s, consuming
1847 4 consecutive vertex input locations), in which case it is the index of the
1848 row or column. \c{location - matrixSlice} must always be equal to the \c
1849 location for the first row or column of the unrolled matrix.
1850 */
1851QRhiVertexInputAttribute::QRhiVertexInputAttribute(int binding, int location, Format format, quint32 offset, int matrixSlice)
1852 : m_binding(binding),
1853 m_location(location),
1854 m_format(format),
1855 m_offset(offset),
1856 m_matrixSlice(matrixSlice)
1857{
1858}
1859
1860/*!
1861 \fn int QRhiVertexInputAttribute::binding() const
1862 \return the binding point index.
1863 */
1864
1865/*!
1866 \fn void QRhiVertexInputAttribute::setBinding(int b)
1867 Sets the binding point index to \a b.
1868 By default this is set to 0.
1869 */
1870
1871/*!
1872 \fn int QRhiVertexInputAttribute::location() const
1873 \return the location of the vertex input element.
1874 */
1875
1876/*!
1877 \fn void QRhiVertexInputAttribute::setLocation(int loc)
1878 Sets the location of the vertex input element to \a loc.
1879 By default this is set to 0.
1880 */
1881
1882/*!
1883 \fn QRhiVertexInputAttribute::Format QRhiVertexInputAttribute::format() const
1884 \return the format of the vertex input element.
1885 */
1886
1887/*!
1888 \fn void QRhiVertexInputAttribute::setFormat(Format f)
1889 Sets the format of the vertex input element to \a f.
1890 By default this is set to Float4.
1891 */
1892
1893/*!
1894 \fn quint32 QRhiVertexInputAttribute::offset() const
1895 \return the byte offset for the input element.
1896 */
1897
1898/*!
1899 \fn void QRhiVertexInputAttribute::setOffset(quint32 ofs)
1900 Sets the byte offset for the input element to \a ofs.
1901 */
1902
1903/*!
1904 \fn int QRhiVertexInputAttribute::matrixSlice() const
1905
1906 \return the matrix slice if the input element corresponds to a row or
1907 column of a matrix, or -1 if not relevant.
1908 */
1909
1910/*!
1911 \fn void QRhiVertexInputAttribute::setMatrixSlice(int slice)
1912
1913 Sets the matrix \a slice. By default this is set to -1, and should be set
1914 to a >= 0 value only when this attribute corresponds to a row or column of
1915 a matrix (for example, a 4x4 matrix becomes 4 vec4s, consuming 4
1916 consecutive vertex input locations), in which case it is the index of the
1917 row or column. \c{location - matrixSlice} must always be equal to the \c
1918 location for the first row or column of the unrolled matrix.
1919 */
1920
1921/*!
1922 \fn bool QRhiVertexInputAttribute::operator==(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) noexcept
1923
1924 \return \c true if the values in the two QRhiVertexInputAttribute objects
1925 \a a and \a b are equal.
1926 */
1927
1928/*!
1929 \fn bool QRhiVertexInputAttribute::operator!=(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) noexcept
1930
1931 \return \c false if the values in the two QRhiVertexInputAttribute
1932 objects \a a and \a b are equal; otherwise returns \c true.
1933*/
1934
1935/*!
1936 \fn size_t QRhiVertexInputAttribute::qHash(const QRhiVertexInputAttribute &key, size_t seed)
1937 \qhash{QRhiVertexInputAttribute}
1938 */
1939
1940#ifndef QT_NO_DEBUG_STREAM
1941QDebug operator<<(QDebug dbg, const QRhiVertexInputAttribute &a)
1942{
1943 QDebugStateSaver saver(dbg);
1944 dbg.nospace() << "QRhiVertexInputAttribute(binding=" << a.binding()
1945 << " location=" << a.location()
1946 << " format=" << a.format()
1947 << " offset=" << a.offset()
1948 << ')';
1949 return dbg;
1950}
1951#endif
1952
1953QRhiVertexInputAttribute::Format QRhiImplementation::shaderDescVariableFormatToVertexInputFormat(QShaderDescription::VariableType type) const
1954{
1955 switch (type) {
1956 case QShaderDescription::Vec4:
1957 return QRhiVertexInputAttribute::Float4;
1958 case QShaderDescription::Vec3:
1959 return QRhiVertexInputAttribute::Float3;
1960 case QShaderDescription::Vec2:
1961 return QRhiVertexInputAttribute::Float2;
1962 case QShaderDescription::Float:
1963 return QRhiVertexInputAttribute::Float;
1964
1965 case QShaderDescription::Int4:
1966 return QRhiVertexInputAttribute::SInt4;
1967 case QShaderDescription::Int3:
1968 return QRhiVertexInputAttribute::SInt3;
1969 case QShaderDescription::Int2:
1970 return QRhiVertexInputAttribute::SInt2;
1971 case QShaderDescription::Int:
1972 return QRhiVertexInputAttribute::SInt;
1973
1974 case QShaderDescription::Uint4:
1975 return QRhiVertexInputAttribute::UInt4;
1976 case QShaderDescription::Uint3:
1977 return QRhiVertexInputAttribute::UInt3;
1978 case QShaderDescription::Uint2:
1979 return QRhiVertexInputAttribute::UInt2;
1980 case QShaderDescription::Uint:
1981 return QRhiVertexInputAttribute::UInt;
1982
1983 case QShaderDescription::Half4:
1984 return QRhiVertexInputAttribute::Half4;
1985 case QShaderDescription::Half3:
1986 return QRhiVertexInputAttribute::Half3;
1987 case QShaderDescription::Half2:
1988 return QRhiVertexInputAttribute::Half2;
1989 case QShaderDescription::Half:
1990 return QRhiVertexInputAttribute::Half;
1991
1992 default:
1993 Q_UNREACHABLE_RETURN(QRhiVertexInputAttribute::Float);
1994 }
1995}
1996
1997quint32 QRhiImplementation::byteSizePerVertexForVertexInputFormat(QRhiVertexInputAttribute::Format format) const
1998{
1999 switch (format) {
2000 case QRhiVertexInputAttribute::Float4:
2001 return 4 * sizeof(float);
2002 case QRhiVertexInputAttribute::Float3:
2003 return 4 * sizeof(float); // vec3 still takes 16 bytes
2004 case QRhiVertexInputAttribute::Float2:
2005 return 2 * sizeof(float);
2006 case QRhiVertexInputAttribute::Float:
2007 return sizeof(float);
2008
2009 case QRhiVertexInputAttribute::UNormByte4:
2010 return 4 * sizeof(quint8);
2011 case QRhiVertexInputAttribute::UNormByte2:
2012 return 2 * sizeof(quint8);
2013 case QRhiVertexInputAttribute::UNormByte:
2014 return sizeof(quint8);
2015
2016 case QRhiVertexInputAttribute::UInt4:
2017 return 4 * sizeof(quint32);
2018 case QRhiVertexInputAttribute::UInt3:
2019 return 4 * sizeof(quint32); // ivec3 still takes 16 bytes
2020 case QRhiVertexInputAttribute::UInt2:
2021 return 2 * sizeof(quint32);
2022 case QRhiVertexInputAttribute::UInt:
2023 return sizeof(quint32);
2024
2025 case QRhiVertexInputAttribute::SInt4:
2026 return 4 * sizeof(qint32);
2027 case QRhiVertexInputAttribute::SInt3:
2028 return 4 * sizeof(qint32); // uvec3 still takes 16 bytes
2029 case QRhiVertexInputAttribute::SInt2:
2030 return 2 * sizeof(qint32);
2031 case QRhiVertexInputAttribute::SInt:
2032 return sizeof(qint32);
2033
2034 case QRhiVertexInputAttribute::Half4:
2035 return 4 * sizeof(qfloat16);
2036 case QRhiVertexInputAttribute::Half3:
2037 return 4 * sizeof(qfloat16); // half3 still takes 8 bytes
2038 case QRhiVertexInputAttribute::Half2:
2039 return 2 * sizeof(qfloat16);
2040 case QRhiVertexInputAttribute::Half:
2041 return sizeof(qfloat16);
2042
2043 case QRhiVertexInputAttribute::UShort4:
2044 return 4 * sizeof(quint16);
2045 case QRhiVertexInputAttribute::UShort3:
2046 return 4 * sizeof(quint16); // ivec3 still takes 8 bytes
2047 case QRhiVertexInputAttribute::UShort2:
2048 return 2 * sizeof(quint16);
2049 case QRhiVertexInputAttribute::UShort:
2050 return sizeof(quint16);
2051
2052 case QRhiVertexInputAttribute::SShort4:
2053 return 4 * sizeof(qint16);
2054 case QRhiVertexInputAttribute::SShort3:
2055 return 4 * sizeof(qint16); // uvec3 still takes 8 bytes
2056 case QRhiVertexInputAttribute::SShort2:
2057 return 2 * sizeof(qint16);
2058 case QRhiVertexInputAttribute::SShort:
2059 return sizeof(qint16);
2060
2061 default:
2062 Q_UNREACHABLE_RETURN(1);
2063 }
2064}
2065
2066/*!
2067 \class QRhiVertexInputLayout
2068 \inmodule QtGuiPrivate
2069 \inheaderfile rhi/qrhi.h
2070 \since 6.6
2071 \brief Describes the layout of vertex inputs consumed by a vertex shader.
2072
2073 The vertex input layout is defined by the collections of
2074 QRhiVertexInputBinding and QRhiVertexInputAttribute.
2075
2076 As an example, let's assume that we have a single buffer with 3 component
2077 vertex positions and 2 component UV coordinates interleaved (\c x, \c y, \c
2078 z, \c u, \c v), that the position and UV are expected at input locations 0
2079 and 1 by the vertex shader, and that the vertex buffer will be bound at
2080 binding point 0 using
2081 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()} later on:
2082
2083 \code
2084 QRhiVertexInputLayout inputLayout;
2085 inputLayout.setBindings({
2086 { 5 * sizeof(float) }
2087 });
2088 inputLayout.setAttributes({
2089 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
2090 { 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) }
2091 });
2092 \endcode
2093
2094 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2095 for details.
2096 */
2097
2098/*!
2099 \fn QRhiVertexInputLayout::QRhiVertexInputLayout() = default
2100
2101 Constructs an empty vertex input layout description.
2102 */
2103
2104/*!
2105 \fn void QRhiVertexInputLayout::setBindings(std::initializer_list<QRhiVertexInputBinding> list)
2106 Sets the bindings from the specified \a list.
2107 */
2108
2109/*!
2110 \fn template<typename InputIterator> void QRhiVertexInputLayout::setBindings(InputIterator first, InputIterator last)
2111 Sets the bindings using the iterators \a first and \a last.
2112 */
2113
2114/*!
2115 \fn const QRhiVertexInputBinding *QRhiVertexInputLayout::cbeginBindings() const
2116 \return a const iterator pointing to the first item in the binding list.
2117 */
2118
2119/*!
2120 \fn const QRhiVertexInputBinding *QRhiVertexInputLayout::cendBindings() const
2121 \return a const iterator pointing just after the last item in the binding list.
2122 */
2123
2124/*!
2125 \fn const QRhiVertexInputBinding *QRhiVertexInputLayout::bindingAt(qsizetype index) const
2126 \return the binding at the given \a index.
2127 */
2128
2129/*!
2130 \fn qsizetype QRhiVertexInputLayout::bindingCount() const
2131 \return the number of bindings.
2132 */
2133
2134/*!
2135 \fn void QRhiVertexInputLayout::setAttributes(std::initializer_list<QRhiVertexInputAttribute> list)
2136 Sets the attributes from the specified \a list.
2137 */
2138
2139/*!
2140 \fn template<typename InputIterator> void QRhiVertexInputLayout::setAttributes(InputIterator first, InputIterator last)
2141 Sets the attributes using the iterators \a first and \a last.
2142 */
2143
2144/*!
2145 \fn const QRhiVertexInputAttribute *QRhiVertexInputLayout::cbeginAttributes() const
2146 \return a const iterator pointing to the first item in the attribute list.
2147 */
2148
2149/*!
2150 \fn const QRhiVertexInputAttribute *QRhiVertexInputLayout::cendAttributes() const
2151 \return a const iterator pointing just after the last item in the attribute list.
2152 */
2153
2154/*!
2155 \fn const QRhiVertexInputAttribute *QRhiVertexInputLayout::attributeAt(qsizetype index) const
2156 \return the attribute at the given \a index.
2157 */
2158
2159/*!
2160 \fn qsizetype QRhiVertexInputLayout::attributeCount() const
2161 \return the number of attributes.
2162 */
2163
2164/*!
2165 \fn bool QRhiVertexInputLayout::operator==(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) noexcept
2166
2167 \return \c true if the values in the two QRhiVertexInputLayout objects
2168 \a a and \a b are equal.
2169 */
2170
2171/*!
2172 \fn bool QRhiVertexInputLayout::operator!=(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) noexcept
2173
2174 \return \c false if the values in the two QRhiVertexInputLayout
2175 objects \a a and \a b are equal; otherwise returns \c true.
2176*/
2177
2178/*!
2179 \fn size_t QRhiVertexInputLayout::qHash(const QRhiVertexInputLayout &key, size_t seed)
2180 \qhash{QRhiVertexInputLayout}
2181 */
2182
2183#ifndef QT_NO_DEBUG_STREAM
2184QDebug operator<<(QDebug dbg, const QRhiVertexInputLayout &v)
2185{
2186 QDebugStateSaver saver(dbg);
2187 dbg.nospace() << "QRhiVertexInputLayout(bindings=" << v.m_bindings
2188 << " attributes=" << v.m_attributes
2189 << ')';
2190 return dbg;
2191}
2192#endif
2193
2194/*!
2195 \class QRhiShaderStage
2196 \inmodule QtGuiPrivate
2197 \inheaderfile rhi/qrhi.h
2198 \since 6.6
2199 \brief Specifies the type and the shader code for a shader stage in the pipeline.
2200
2201 When setting up a QRhiGraphicsPipeline, a collection of shader stages are
2202 specified. The QRhiShaderStage contains a QShader and some associated
2203 metadata, such as the graphics pipeline stage, and the
2204 \l{QShader::Variant}{shader variant} to select. There is no need to specify
2205 the shader language or version because the QRhi backend in use at runtime
2206 will take care of choosing the appropriate shader version from the
2207 collection within the QShader.
2208
2209 The typical usage is in combination with
2210 QRhiGraphicsPipeline::setShaderStages(), shown here with a simple approach
2211 to load the QShader from \c{.qsb} files generated offline or at build time:
2212
2213 \code
2214 QShader getShader(const QString &name)
2215 {
2216 QFile f(name);
2217 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
2218 }
2219
2220 QShader vs = getShader("material.vert.qsb");
2221 QShader fs = getShader("material.frag.qsb");
2222 pipeline->setShaderStages({
2223 { QRhiShaderStage::Vertex, vs },
2224 { QRhiShaderStage::Fragment, fs }
2225 });
2226 \endcode
2227
2228 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2229 for details.
2230 */
2231
2232/*!
2233 \enum QRhiShaderStage::Type
2234 Specifies the type of the shader stage.
2235
2236 \value Vertex Vertex stage
2237
2238 \value TessellationControl Tessellation control (hull shader) stage. Must
2239 be used only when the QRhi::Tessellation feature is supported.
2240
2241 \value TessellationEvaluation Tessellation evaluation (domain shader)
2242 stage. Must be used only when the QRhi::Tessellation feature is supported.
2243
2244 \value Fragment Fragment (pixel shader) stage
2245
2246 \value Compute Compute stage. Must be used only when the QRhi::Compute
2247 feature is supported.
2248
2249 \value Geometry Geometry stage. Must be used only when the
2250 QRhi::GeometryShader feature is supported.
2251 */
2252
2253/*!
2254 \fn QRhiShaderStage::QRhiShaderStage() = default
2255
2256 Constructs a shader stage description for the vertex stage with an empty
2257 QShader.
2258 */
2259
2260/*!
2261 \fn QRhiShaderStage::Type QRhiShaderStage::type() const
2262 \return the type of the stage.
2263 */
2264
2265/*!
2266 \fn void QRhiShaderStage::setType(Type t)
2267
2268 Sets the type of the stage to \a t. Setters should rarely be needed in
2269 pratice. Most applications will likely use the QRhiShaderStage constructor
2270 in most cases.
2271 */
2272
2273/*!
2274 \fn QShader QRhiShaderStage::shader() const
2275 \return the QShader to be used for this stage in the graphics pipeline.
2276 */
2277
2278/*!
2279 \fn void QRhiShaderStage::setShader(const QShader &s)
2280 Sets the shader collection \a s.
2281 */
2282
2283/*!
2284 \fn QShader::Variant QRhiShaderStage::shaderVariant() const
2285 \return the requested shader variant.
2286 */
2287
2288/*!
2289 \fn void QRhiShaderStage::setShaderVariant(QShader::Variant v)
2290 Sets the requested shader variant \a v.
2291 */
2292
2293/*!
2294 Constructs a shader stage description with the \a type of the stage and the
2295 \a shader.
2296
2297 The shader variant \a v defaults to QShader::StandardShader. A
2298 QShader contains multiple source and binary versions of a shader.
2299 In addition, it can also contain variants of the shader with slightly
2300 modified code. \a v can then be used to select the desired variant.
2301 */
2302QRhiShaderStage::QRhiShaderStage(Type type, const QShader &shader, QShader::Variant v)
2303 : m_type(type),
2304 m_shader(shader),
2305 m_shaderVariant(v)
2306{
2307}
2308
2309/*!
2310 \fn bool QRhiShaderStage::operator==(const QRhiShaderStage &a, const QRhiShaderStage &b) noexcept
2311
2312 \return \c true if the values in the two QRhiShaderStage objects
2313 \a a and \a b are equal.
2314 */
2315
2316/*!
2317 \fn bool QRhiShaderStage::operator!=(const QRhiShaderStage &a, const QRhiShaderStage &b) noexcept
2318
2319 \return \c false if the values in the two QRhiShaderStage
2320 objects \a a and \a b are equal; otherwise returns \c true.
2321*/
2322
2323/*!
2324 \fn size_t QRhiShaderStage::qHash(const QRhiShaderStage &key, size_t seed)
2325 \qhash{QRhiShaderStage}
2326 */
2327
2328#ifndef QT_NO_DEBUG_STREAM
2329QDebug operator<<(QDebug dbg, const QRhiShaderStage &s)
2330{
2331 QDebugStateSaver saver(dbg);
2332 dbg.nospace() << "QRhiShaderStage(type=" << s.type()
2333 << " shader=" << s.shader()
2334 << " variant=" << s.shaderVariant()
2335 << ')';
2336 return dbg;
2337}
2338#endif
2339
2340/*!
2341 \class QRhiColorAttachment
2342 \inmodule QtGuiPrivate
2343 \inheaderfile rhi/qrhi.h
2344 \since 6.6
2345 \brief Describes the a single color attachment of a render target.
2346
2347 A color attachment is either a QRhiTexture or a QRhiRenderBuffer. The
2348 former, i.e. when texture() is set, is used in most cases.
2349 QRhiColorAttachment is commonly used in combination with
2350 QRhiTextureRenderTargetDescription.
2351
2352 \note texture() and renderBuffer() cannot be both set (be non-null at the
2353 same time).
2354
2355 Setting renderBuffer instead is recommended only when multisampling is
2356 needed. Relying on QRhi::MultisampleRenderBuffer is a better choice than
2357 QRhi::MultisampleTexture in practice since the former is available in more
2358 run time configurations (e.g. when running on OpenGL ES 3.0 which has no
2359 support for multisample textures, but does support multisample
2360 renderbuffers).
2361
2362 When targeting a non-multisample texture, the layer() and level() indicate
2363 the targeted layer (face index \c{0-5} for cubemaps) and mip level. For 3D
2364 textures layer() specifies the slice (one 2D image within the 3D texture)
2365 to render to. For texture arrays layer() is the array index.
2366
2367 When texture() or renderBuffer() is multisample, resolveTexture() can be
2368 set optionally. When set, samples are resolved automatically into that
2369 (non-multisample) texture at the end of the render pass. When rendering
2370 into a multisample renderbuffers, this is the only way to get resolved,
2371 non-multisample content out of them. Multisample textures allow sampling in
2372 shaders so for them this is just one option.
2373
2374 \note when resolving is enabled, the multisample data may not be written
2375 out at all. This means that the multisample texture() must not be used
2376 afterwards with shaders for sampling when resolveTexture() is set.
2377
2378 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2379 for details.
2380
2381 \sa QRhiTextureRenderTargetDescription
2382 */
2383
2384/*!
2385 \fn QRhiColorAttachment::QRhiColorAttachment() = default
2386
2387 Constructs an empty color attachment description.
2388 */
2389
2390/*!
2391 Constructs a color attachment description that specifies \a texture as the
2392 associated color buffer.
2393 */
2394QRhiColorAttachment::QRhiColorAttachment(QRhiTexture *texture)
2395 : m_texture(texture)
2396{
2397}
2398
2399/*!
2400 Constructs a color attachment description that specifies \a renderBuffer as
2401 the associated color buffer.
2402 */
2403QRhiColorAttachment::QRhiColorAttachment(QRhiRenderBuffer *renderBuffer)
2404 : m_renderBuffer(renderBuffer)
2405{
2406}
2407
2408/*!
2409 \fn QRhiTexture *QRhiColorAttachment::texture() const
2410
2411 \return the texture this attachment description references, or \nullptr if
2412 there is none.
2413 */
2414
2415/*!
2416 \fn void QRhiColorAttachment::setTexture(QRhiTexture *tex)
2417
2418 Sets the texture \a tex.
2419
2420 \note texture() and renderBuffer() cannot be both set (be non-null at the
2421 same time).
2422 */
2423
2424/*!
2425 \fn QRhiRenderBuffer *QRhiColorAttachment::renderBuffer() const
2426
2427 \return the renderbuffer this attachment description references, or
2428 \nullptr if there is none.
2429
2430 In practice associating a QRhiRenderBuffer with a QRhiColorAttachment makes
2431 the most sense when setting up multisample rendering via a multisample
2432 \l{QRhiRenderBuffer::Type}{color} renderbuffer that is then resolved into a
2433 non-multisample texture at the end of the render pass.
2434 */
2435
2436/*!
2437 \fn void QRhiColorAttachment::setRenderBuffer(QRhiRenderBuffer *rb)
2438
2439 Sets the renderbuffer \a rb.
2440
2441 \note texture() and renderBuffer() cannot be both set (be non-null at the
2442 same time).
2443 */
2444
2445/*!
2446 \fn int QRhiColorAttachment::layer() const
2447 \return the layer index (cubemap face or array layer). 0 by default.
2448 */
2449
2450/*!
2451 \fn void QRhiColorAttachment::setLayer(int layer)
2452 Sets the \a layer index.
2453 */
2454
2455/*!
2456 \fn int QRhiColorAttachment::level() const
2457 \return the mip level. 0 by default.
2458 */
2459
2460/*!
2461 \fn void QRhiColorAttachment::setLevel(int level)
2462 Sets the mip \a level.
2463 */
2464
2465/*!
2466 \fn QRhiTexture *QRhiColorAttachment::resolveTexture() const
2467
2468 \return the resolve texture this attachment description references, or
2469 \nullptr if there is none.
2470
2471 Setting a non-null resolve texture is applicable when the attachment
2472 references a multisample texture or renderbuffer. The QRhiTexture in the
2473 resolveTexture() is then a non-multisample 2D texture (or texture array)
2474 with the same size (but a sample count of 1). The multisample content is
2475 automatically resolved into this texture at the end of each render pass.
2476 */
2477
2478/*!
2479 \fn void QRhiColorAttachment::setResolveTexture(QRhiTexture *tex)
2480
2481 Sets the resolve texture \a tex.
2482
2483 \a tex is expected to be a 2D texture or a 2D texture array. In either
2484 case, resolving targets a single mip level of a single layer (array
2485 element) of \a tex. The mip level and array layer are specified by
2486 resolveLevel() and resolveLayer().
2487
2488 An exception is \l{setMultiViewCount()}{multiview}: when the color
2489 attachment is associated with a texture array and multiview is enabled, the
2490 resolve texture must also be a texture array with sufficient elements for
2491 all views. In this case all elements that correspond to views are resolved
2492 automatically; the behavior is similar to the following pseudo-code:
2493 \badcode
2494 for (i = 0; i < multiViewCount(); ++i)
2495 resolve texture's layer() + i into resolveTexture's resolveLayer() + i
2496 \endcode
2497
2498 Setting a non-multisample texture to resolve a multisample texture or
2499 renderbuffer automatically at the end of the render pass is often
2500 preferable to working with multisample textures (and not setting a resolve
2501 texture), because it avoids the need for writing dedicated fragment shaders
2502 that work exclusively with multisample textures (\c sampler2DMS, \c
2503 texelFetch, etc.), and rather allows using the same shader as one would if
2504 the attachment's texture was not multisampled to begin with. This comes at
2505 the expense of an additional resource (the non-multisample \a tex).
2506 */
2507
2508/*!
2509 \fn int QRhiColorAttachment::resolveLayer() const
2510 \return the currently set resolve texture layer. Defaults to 0.
2511 */
2512
2513/*!
2514 \fn void QRhiColorAttachment::setResolveLayer(int layer)
2515 Sets the resolve texture \a layer to use.
2516 */
2517
2518/*!
2519 \fn int QRhiColorAttachment::resolveLevel() const
2520 \return the currently set resolve texture mip level. Defaults to 0.
2521 */
2522
2523/*!
2524 \fn void QRhiColorAttachment::setResolveLevel(int level)
2525 Sets the resolve texture mip \a level to use.
2526 */
2527
2528/*!
2529 \fn int QRhiColorAttachment::multiViewCount() const
2530
2531 \return the currently set number of views. Defaults to 0 which indicates
2532 the render target with this color attachment is not going to be used with
2533 multiview rendering.
2534
2535 \since 6.7
2536 */
2537
2538/*!
2539 \fn void QRhiColorAttachment::setMultiViewCount(int count)
2540
2541 Sets the view \a count. Setting a value larger than 1 indicates that the
2542 render target with this color attachment is going to be used with multiview
2543 rendering. The default value is 0. Values smaller than 2 indicate no
2544 multiview rendering.
2545
2546 When \a count is set to \c 2 or greater, the color attachment must be
2547 associated with a 2D texture array. layer() and multiViewCount() together
2548 define the range of texture array elements that are targeted during
2549 multiview rendering.
2550
2551 For example, if \c layer is \c 0 and \c multiViewCount is \c 2, the texture
2552 array must have 2 (or more) elements, and the multiview rendering will
2553 target elements 0 and 1. The \c{gl_ViewIndex} variable in the shaders has a
2554 value of \c 0 or \c 1 then, where view \c 0 corresponds to the texture array
2555 element \c 0, and view \c 1 to the array element \c 1.
2556
2557 \note Setting a \a count larger than 1, using a texture array as texture(),
2558 and calling \l{QRhiCommandBuffer::beginPass()}{beginPass()} on a
2559 QRhiTextureRenderTarget with this color attachment implies multiview
2560 rendering for the entire render pass. multiViewCount() should not be set
2561 unless multiview rendering is wanted. Multiview cannot be used with texture
2562 types other than 2D texture arrays. (although 3D textures may work,
2563 depending on the graphics API and backend; applications are nonetheless
2564 advised not to rely on that and only use 2D texture arrays as the render
2565 targets of multiview rendering)
2566
2567 See
2568 \l{https://registry.khronos.org/OpenGL/extensions/OVR/OVR_multiview.txt}{GL_OVR_multiview}
2569 for more details regarding multiview rendering. Do note that Qt requires
2570 \l{https://registry.khronos.org/OpenGL/extensions/OVR/OVR_multiview2.txt}{GL_OVR_multiview2}
2571 as well, when running on OpenGL (ES).
2572
2573 Multiview rendering is available only when the
2574 \l{QRhi::MultiView}{MultiView} feature is reported as supported from
2575 \l{QRhi::isFeatureSupported()}{isFeatureSupported()}.
2576
2577 \note For portability, be aware of limitations that exist for multiview
2578 rendering with some of the graphics APIs. It is recommended that multiview
2579 render passes do not rely on any of the features that
2580 \l{https://registry.khronos.org/OpenGL/extensions/OVR/OVR_multiview.txt}{GL_OVR_multiview}
2581 declares as unsupported. The one exception is shader stage outputs other
2582 than \c{gl_Position} depending on \c{gl_ViewIndex}: that can be relied on
2583 (even with OpenGL) because QRhi never reports multiview as supported without
2584 \c{GL_OVR_multiview2} also being present.
2585
2586 \note Multiview rendering is not supported in combination with tessellation
2587 or geometry shaders, even though some implementations of some graphics APIs
2588 may allow this.
2589
2590 \since 6.7
2591 */
2592
2593/*!
2594 \class QRhiTextureRenderTargetDescription
2595 \inmodule QtGuiPrivate
2596 \inheaderfile rhi/qrhi.h
2597 \since 6.6
2598 \brief Describes the color and depth or depth/stencil attachments of a render target.
2599
2600 A texture render target has zero or more textures as color attachments,
2601 zero or one renderbuffer as combined depth/stencil buffer or zero or one
2602 texture as depth buffer.
2603
2604 \note depthStencilBuffer() and depthTexture() cannot be both set (cannot be
2605 non-null at the same time).
2606
2607 Let's look at some example usages in combination with
2608 QRhiTextureRenderTarget.
2609
2610 Due to the constructors, the targeting a texture (and no depth/stencil
2611 buffer) is simple:
2612
2613 \code
2614 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(256, 256), 1, QRhiTexture::RenderTarget);
2615 texture->create();
2616 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ texture }));
2617 \endcode
2618
2619 The following creates a texture render target that is set up to target mip
2620 level #2 of a texture:
2621
2622 \code
2623 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget | QRhiTexture::MipMapped);
2624 texture->create();
2625 QRhiColorAttachment colorAtt(texture);
2626 colorAtt.setLevel(2);
2627 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ colorAtt });
2628 \endcode
2629
2630 Another example, this time to render into a depth texture:
2631
2632 \code
2633 QRhiTexture *shadowMap = rhi->newTexture(QRhiTexture::D32F, QSize(1024, 1024), 1, QRhiTexture::RenderTarget);
2634 shadowMap->create();
2635 QRhiTextureRenderTargetDescription rtDesc;
2636 rtDesc.setDepthTexture(shadowMap);
2637 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget(rtDesc);
2638 \endcode
2639
2640 A very common case, having a texture as the color attachment and a
2641 renderbuffer as depth/stencil to enable depth testing:
2642
2643 \code
2644 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
2645 texture->create();
2646 QRhiRenderBuffer *depthStencil = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, QSize(512, 512));
2647 depthStencil->create();
2648 QRhiTextureRenderTargetDescription rtDesc({ texture }, depthStencil);
2649 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget(rtDesc);
2650 \endcode
2651
2652 Finally, to enable multisample rendering in a portable manner (so also
2653 supporting OpenGL ES 3.0), using a QRhiRenderBuffer as the (multisample)
2654 color buffer and then resolving into a regular (non-multisample) 2D
2655 texture. To enable depth testing, a depth-stencil buffer, which also must
2656 use the same sample count, is used as well:
2657
2658 \code
2659 QRhiRenderBuffer *colorBuffer = rhi->newRenderBuffer(QRhiRenderBuffer::Color, QSize(512, 512), 4); // 4x MSAA
2660 colorBuffer->create();
2661 QRhiRenderBuffer *depthStencil = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, QSize(512, 512), 4);
2662 depthStencil->create();
2663 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
2664 texture->create();
2665 QRhiColorAttachment colorAtt(colorBuffer);
2666 colorAtt.setResolveTexture(texture);
2667 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ colorAtt, depthStencil });
2668 \endcode
2669
2670 \note when multisample resolving is enabled, the multisample data may not be
2671 written out at all. This means that the multisample texture in a color
2672 attachment must not be used afterwards with shaders for sampling (or other
2673 purposes) whenever a resolve texture is set, since the multisample color
2674 buffer is merely an intermediate storage then that gets no data written back
2675 on some GPU architectures at all. See
2676 \l{QRhiTextureRenderTarget::Flag}{PreserveColorContents} for more details.
2677
2678 \note When using setDepthTexture(), not setDepthStencilBuffer(), and the
2679 depth (stencil) data is not of interest afterwards, set the
2680 DoNotStoreDepthStencilContents flag on the QRhiTextureRenderTarget. This
2681 allows indicating to the underlying 3D API that the depth/stencil data can
2682 be discarded, leading potentially to better performance with tiled GPU
2683 architectures. When the depth-stencil buffer is a QRhiRenderBuffer (and also
2684 for the multisample color texture, see previous note) this is implicit, but
2685 with a depth (stencil) QRhiTexture the intention needs to be declared
2686 explicitly. By default QRhi assumes that the data is of interest (e.g., the
2687 depth texture is sampled in a shader afterwards).
2688
2689 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2690 for details.
2691
2692 \sa QRhiColorAttachment, QRhiTextureRenderTarget
2693 */
2694
2695/*!
2696 \fn QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription() = default
2697
2698 Constructs an empty texture render target description.
2699 */
2700
2701/*!
2702 Constructs a texture render target description with one attachment
2703 described by \a colorAttachment.
2704 */
2705QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment)
2706{
2707 m_colorAttachments.append(colorAttachment);
2708}
2709
2710/*!
2711 Constructs a texture render target description with two attachments, a
2712 color attachment described by \a colorAttachment, and a depth/stencil
2713 attachment with \a depthStencilBuffer.
2714 */
2715QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment,
2716 QRhiRenderBuffer *depthStencilBuffer)
2717 : m_depthStencilBuffer(depthStencilBuffer)
2718{
2719 m_colorAttachments.append(colorAttachment);
2720}
2721
2722/*!
2723 Constructs a texture render target description with two attachments, a
2724 color attachment described by \a colorAttachment, and a depth attachment
2725 with \a depthTexture.
2726
2727 \note \a depthTexture must have a suitable format, such as QRhiTexture::D16
2728 or QRhiTexture::D32F.
2729 */
2730QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment,
2731 QRhiTexture *depthTexture)
2732 : m_depthTexture(depthTexture)
2733{
2734 m_colorAttachments.append(colorAttachment);
2735}
2736
2737/*!
2738 \fn void QRhiTextureRenderTargetDescription::setColorAttachments(std::initializer_list<QRhiColorAttachment> list)
2739 Sets the \a list of color attachments.
2740 */
2741
2742/*!
2743 \fn template<typename InputIterator> void QRhiTextureRenderTargetDescription::setColorAttachments(InputIterator first, InputIterator last)
2744 Sets the list of color attachments via the iterators \a first and \a last.
2745 */
2746
2747/*!
2748 \fn const QRhiColorAttachment *QRhiTextureRenderTargetDescription::cbeginColorAttachments() const
2749 \return a const iterator pointing to the first item in the attachment list.
2750 */
2751
2752/*!
2753 \fn const QRhiColorAttachment *QRhiTextureRenderTargetDescription::cendColorAttachments() const
2754 \return a const iterator pointing just after the last item in the attachment list.
2755 */
2756
2757/*!
2758 \fn const QRhiColorAttachment *QRhiTextureRenderTargetDescription::colorAttachmentAt(qsizetype index) const
2759 \return the color attachment at the specified \a index.
2760 */
2761
2762/*!
2763 \fn qsizetype QRhiTextureRenderTargetDescription::colorAttachmentCount() const
2764 \return the number of currently set color attachments.
2765 */
2766
2767/*!
2768 \fn QRhiRenderBuffer *QRhiTextureRenderTargetDescription::depthStencilBuffer() const
2769 \return the renderbuffer used as depth-stencil buffer, or \nullptr if none was set.
2770 */
2771
2772/*!
2773 \fn void QRhiTextureRenderTargetDescription::setDepthStencilBuffer(QRhiRenderBuffer *renderBuffer)
2774
2775 Sets the \a renderBuffer for depth-stencil. Not mandatory, e.g. when no
2776 depth test/write or stencil-related features are used within any graphics
2777 pipelines in any of the render passes for this render target, it can be
2778 left set to \nullptr.
2779
2780 \note depthStencilBuffer() and depthTexture() cannot be both set (cannot be
2781 non-null at the same time).
2782
2783 Using a QRhiRenderBuffer over a 2D QRhiTexture as the depth or
2784 depth/stencil buffer is very common, and is the recommended approach for
2785 applications. Using a QRhiTexture, and so setDepthTexture() becomes
2786 relevant if the depth data is meant to be accessed (e.g. sampled in a
2787 shader) afterwards, or when
2788 \l{QRhiColorAttachment::setMultiViewCount()}{multiview rendering} is
2789 involved (because then the depth texture must be a texture array).
2790
2791 \sa setDepthTexture()
2792 */
2793
2794/*!
2795 \fn QRhiTexture *QRhiTextureRenderTargetDescription::depthTexture() const
2796 \return the currently referenced depth texture, or \nullptr if none was set.
2797 */
2798
2799/*!
2800 \fn void QRhiTextureRenderTargetDescription::setDepthTexture(QRhiTexture *texture)
2801
2802 Sets the \a texture for depth-stencil. This is an alternative to
2803 setDepthStencilBuffer(), where instead of a QRhiRenderBuffer a QRhiTexture
2804 with a suitable type (e.g., QRhiTexture::D32F) is provided.
2805
2806 \note depthStencilBuffer() and depthTexture() cannot be both set (cannot be
2807 non-null at the same time).
2808
2809 \a texture can either be a 2D texture or a 2D texture array (when texture
2810 arrays are supported). Specifying a texture array is relevant in particular
2811 with
2812 \l{QRhiColorAttachment::setMultiViewCount()}{multiview rendering}.
2813
2814 \note If \a texture is a format with a stencil component, such as
2815 \l QRhiTexture::D24S8, it will serve as the stencil buffer as well.
2816
2817 \sa setDepthStencilBuffer()
2818 */
2819
2820/*!
2821 \fn QRhiTexture *QRhiTextureRenderTargetDescription::depthResolveTexture() const
2822
2823 \return the texture to which a multisample depth (or depth-stencil) texture
2824 (or texture array) is resolved to. \nullptr if there is none, which is the
2825 most common case.
2826
2827 \since 6.8
2828 \sa QRhiColorAttachment::resolveTexture(), depthTexture()
2829 */
2830
2831/*!
2832 \fn void QRhiTextureRenderTargetDescription::setDepthResolveTexture(QRhiTexture *tex)
2833
2834 Sets the depth (or depth-stencil) resolve texture \a tex.
2835
2836 \a tex is expected to be a 2D texture or a 2D texture array with a format
2837 matching the texture set via setDepthTexture().
2838
2839 \note Resolving depth (or depth-stencil) data is only functional when the
2840 \l QRhi::ResolveDepthStencil feature is reported as supported at run time.
2841 Support for depth-stencil resolve is not universally available among the
2842 graphics APIs. Designs assuming unconditional availability of depth-stencil
2843 resolve are therefore non-portable, and should be avoided.
2844
2845 \note As an additional limitation for OpenGL ES in particular, setting a
2846 depth resolve texture may only be functional in combination with
2847 setDepthTexture(), not with setDepthStencilBuffer().
2848
2849 \since 6.8
2850 \sa QRhiColorAttachment::setResolveTexture(), setDepthTexture()
2851 */
2852
2853/*!
2854 \fn QRhiShadingRateMap *QRhiTextureRenderTargetDescription::shadingRateMap() const
2855 \return the currently set QRhiShadingRateMap. By default this is \nullptr.
2856 \since 6.9
2857 */
2858
2859/*!
2860 \fn void QRhiTextureRenderTargetDescription::setShadingRateMap(QRhiShadingRateMap *map)
2861
2862 Associates with the specified QRhiShadingRateMap \a map. This is functional
2863 only when the \l QRhi::VariableRateShadingMap feature is reported as
2864 supported.
2865
2866 When QRhiCommandBuffer::setShadingRate() is also called, the higher of two
2867 the shading rates are used for each tile. There is currently no control
2868 offered over the combiner behavior.
2869
2870 \note When the render target had already been built (create() was called
2871 successfully), setting a shading rate map implies that a different, new
2872 QRhiRenderPassDescriptor is needed and thus a rebuild is needed. Call
2873 setRenderPassDescriptor() again (outside of a render pass) and then rebuild
2874 by calling create(). This has other rolling consequences as well, for
2875 example for graphics pipelines: those also need to be associated with the
2876 new QRhiRenderPassDescriptor and then rebuilt. See \l
2877 QRhiRenderPassDescriptor::serializedFormat() for some suggestions on how to
2878 deal with this. Remember to set the QRhiGraphicsPipeline::UsesShadingRate
2879 flag as well.
2880
2881 \since 6.9
2882 */
2883
2884/*!
2885 \class QRhiTextureSubresourceUploadDescription
2886 \inmodule QtGuiPrivate
2887 \inheaderfile rhi/qrhi.h
2888 \since 6.6
2889 \brief Describes the source for one mip level in a layer in a texture upload operation.
2890
2891 The source content is specified either as a QImage or as a raw blob. The
2892 former is only allowed for uncompressed textures with a format that can be
2893 mapped to QImage, while the latter is supported for all formats, including
2894 floating point and compressed.
2895
2896 \note image() and data() cannot be both set at the same time.
2897
2898 destinationTopLeft() specifies the top-left corner of the target
2899 rectangle. Defaults to (0, 0).
2900
2901 An empty sourceSize() (the default) indicates that size is assumed to be
2902 the size of the subresource. With QImage-based uploads this implies that
2903 the size of the source image() must match the subresource. When providing
2904 raw data instead, sufficient number of bytes must be provided in data().
2905
2906 sourceTopLeft() is supported only for QImage-based uploads, and specifies
2907 the top-left corner of the source rectangle.
2908
2909 \note Setting sourceSize() or sourceTopLeft() may trigger a QImage copy
2910 internally, depending on the format and the backend.
2911
2912 When providing raw data, and the stride is not specified via
2913 setDataStride(), the stride (row pitch, row length in bytes) of the
2914 provided data must be equal to \c{width * pixelSize} where \c pixelSize is
2915 the number of bytes used for one pixel, and there must be no additional
2916 padding between rows. There is no row start alignment requirement.
2917
2918 When there is unused data at the end of each row in the input raw data,
2919 call setDataStride() with the total number of bytes per row. The stride
2920 must always be a multiple of the number of bytes for one pixel. The row
2921 stride is only applicable to image data for textures with an uncompressed
2922 format.
2923
2924 \note The format of the source data must be compatible with the texture
2925 format. With many graphics APIs the data is copied as-is into a staging
2926 buffer, there is no intermediate format conversion provided by QRhi. This
2927 applies to floating point formats as well, with, for example, RGBA16F
2928 requiring half floats in the source data.
2929
2930 \note Setting the stride via setDataStride() is only functional when
2931 QRhi::ImageDataStride is reported as
2932 \l{QRhi::isFeatureSupported()}{supported}. In practice this can be expected
2933 to be supported everywhere except for OpenGL ES 2.0.
2934
2935 \note When a QImage is given, the stride returned from
2936 QImage::bytesPerLine() is taken into account automatically.
2937
2938 \warning When a QImage is given and the QImage does not own the underlying
2939 pixel data, it is up to the caller to ensure that the associated data stays
2940 valid until the end of the frame. (just submitting the resource update batch
2941 is not sufficient, the data must stay valid until QRhi::endFrame() is called
2942 in order to be portable across all backends) If this cannot be ensured, the
2943 caller is strongly encouraged to call QImage::detach() on the image before
2944 passing it to uploadTexture().
2945
2946 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2947 for details.
2948
2949 \sa QRhiTextureUploadDescription
2950 */
2951
2952/*!
2953 \fn QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription() = default
2954
2955 Constructs an empty subresource description.
2956
2957 \note an empty QRhiTextureSubresourceUploadDescription is not useful on its
2958 own and should not be submitted to a QRhiTextureUploadEntry. At minimum
2959 image or data must be set first.
2960 */
2961
2962/*!
2963 Constructs a mip level description with a \a image.
2964
2965 The \l{QImage::size()}{size} of \a image must match the size of the mip
2966 level. For level 0 that is the \l{QRhiTexture::pixelSize()}{texture size}.
2967
2968 The bit depth of \a image must be compatible with the
2969 \l{QRhiTexture::Format}{texture format}.
2970
2971 To describe a partial upload, call setSourceSize(), setSourceTopLeft(), or
2972 setDestinationTopLeft() afterwards.
2973 */
2974QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription(const QImage &image)
2975 : m_image(image)
2976{
2977}
2978
2979/*!
2980 Constructs a mip level description with the image data is specified by \a
2981 data and \a size. This is suitable for floating point and compressed
2982 formats as well.
2983
2984 \a data can safely be destroyed or changed once this function returns.
2985 */
2986QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription(const void *data, quint32 size)
2987 : m_data(reinterpret_cast<const char *>(data), size)
2988{
2989}
2990
2991/*!
2992 Constructs a mip level description with the image data specified by \a
2993 data. This is suitable for floating point and compressed formats as well.
2994 */
2995QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription(const QByteArray &data)
2996 : m_data(data)
2997{
2998}
2999
3000/*!
3001 \fn QImage QRhiTextureSubresourceUploadDescription::image() const
3002 \return the currently set QImage.
3003 */
3004
3005/*!
3006 \fn void QRhiTextureSubresourceUploadDescription::setImage(const QImage &image)
3007
3008 Sets \a image.
3009 Upon textures loading, the image data will be read as is, with no formats conversions.
3010
3011 \note image() and data() cannot be both set at the same time.
3012 */
3013
3014/*!
3015 \fn QByteArray QRhiTextureSubresourceUploadDescription::data() const
3016 \return the currently set raw pixel data.
3017 */
3018
3019/*!
3020 \fn void QRhiTextureSubresourceUploadDescription::setData(const QByteArray &data)
3021
3022 Sets \a data.
3023
3024 \note image() and data() cannot be both set at the same time.
3025 */
3026
3027/*!
3028 \fn quint32 QRhiTextureSubresourceUploadDescription::dataStride() const
3029 \return the currently set data stride.
3030 */
3031
3032/*!
3033 \fn void QRhiTextureSubresourceUploadDescription::setDataStride(quint32 stride)
3034
3035 Sets the data \a stride in bytes. By default this is 0 and not always
3036 relevant. When providing raw data(), and the stride is not specified via
3037 setDataStride(), the stride (row pitch, row length in bytes) of the
3038 provided data must be equal to \c{width * pixelSize} where \c pixelSize is
3039 the number of bytes used for one pixel, and there must be no additional
3040 padding between rows. Otherwise, if there is additional space between the
3041 lines, set a non-zero \a stride. All this is applicable only when raw image
3042 data is provided, and is not necessary when working QImage since that has
3043 its own \l{QImage::bytesPerLine()}{stride} value.
3044
3045 \note Setting the stride via setDataStride() is only functional when
3046 QRhi::ImageDataStride is reported as
3047 \l{QRhi::isFeatureSupported()}{supported}.
3048
3049 \note When a QImage is given, the stride returned from
3050 QImage::bytesPerLine() is taken into account automatically and therefore
3051 there is no need to set the data stride manually.
3052 */
3053
3054/*!
3055 \fn QPoint QRhiTextureSubresourceUploadDescription::destinationTopLeft() const
3056 \return the currently set destination top-left position. Defaults to (0, 0).
3057 */
3058
3059/*!
3060 \fn void QRhiTextureSubresourceUploadDescription::setDestinationTopLeft(const QPoint &p)
3061 Sets the destination top-left position \a p.
3062
3063 \note In the most common case of sourcing the image data from a QImage, Qt
3064 performs clamping of invalid texture upload sizes when the destination
3065 position + the source size exceeds the size of the targeted texture
3066 subresource (i.e, the size at the given mip level). There is also a
3067 qWarning() message printed on the debug output in this case. This is done in
3068 order to avoid confusion when the underlying 3D APIs crash and lead to GPU
3069 device removals at a later point when submitting the commands. Regardless,
3070 developers are encouraged to always validate applications by running with the
3071 Vulkan, D3D12, or Metal validation/debug layers enabled, since those offer a
3072 much wider range of checks on API usage.
3073 */
3074
3075/*!
3076 \fn QSize QRhiTextureSubresourceUploadDescription::sourceSize() const
3077
3078 \return the source size in pixels. Defaults to a default-constructed QSize,
3079 which indicates the entire subresource.
3080 */
3081
3082/*!
3083 \fn void QRhiTextureSubresourceUploadDescription::setSourceSize(const QSize &size)
3084
3085 Sets the source \a size in pixels.
3086
3087 \note Setting sourceSize() or sourceTopLeft() may trigger a QImage copy
3088 internally, depending on the format and the backend.
3089 */
3090
3091/*!
3092 \fn QPoint QRhiTextureSubresourceUploadDescription::sourceTopLeft() const
3093 \return the currently set source top-left position. Defaults to (0, 0).
3094 */
3095
3096/*!
3097 \fn void QRhiTextureSubresourceUploadDescription::setSourceTopLeft(const QPoint &p)
3098
3099 Sets the source top-left position \a p.
3100
3101 \note Setting sourceSize() or sourceTopLeft() may trigger a QImage copy
3102 internally, depending on the format and the backend.
3103 */
3104
3105/*!
3106 \class QRhiTextureUploadEntry
3107 \inmodule QtGuiPrivate
3108 \inheaderfile rhi/qrhi.h
3109 \since 6.6
3110
3111 \brief Describes one layer (face for cubemaps, slice for 3D textures,
3112 element for texture arrays) in a texture upload operation.
3113
3114 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3115 for details.
3116 */
3117
3118/*!
3119 \fn QRhiTextureUploadEntry::QRhiTextureUploadEntry()
3120
3121 Constructs an empty QRhiTextureUploadEntry targeting layer 0 and level 0.
3122
3123 \note an empty QRhiTextureUploadEntry should not be submitted without
3124 setting a QRhiTextureSubresourceUploadDescription via setDescription()
3125 first.
3126 */
3127
3128/*!
3129 Constructs a QRhiTextureUploadEntry targeting the given \a layer and mip
3130 \a level, with the subresource contents described by \a desc.
3131 */
3132QRhiTextureUploadEntry::QRhiTextureUploadEntry(int layer, int level,
3133 const QRhiTextureSubresourceUploadDescription &desc)
3134 : m_layer(layer),
3135 m_level(level),
3136 m_desc(desc)
3137{
3138}
3139
3140/*!
3141 \fn int QRhiTextureUploadEntry::layer() const
3142 \return the currently set layer index (cubemap face, array layer). Defaults to 0.
3143 */
3144
3145/*!
3146 \fn void QRhiTextureUploadEntry::setLayer(int layer)
3147 Sets the \a layer.
3148 */
3149
3150/*!
3151 \fn int QRhiTextureUploadEntry::level() const
3152 \return the currently set mip level. Defaults to 0.
3153 */
3154
3155/*!
3156 \fn void QRhiTextureUploadEntry::setLevel(int level)
3157 Sets the mip \a level.
3158 */
3159
3160/*!
3161 \fn QRhiTextureSubresourceUploadDescription QRhiTextureUploadEntry::description() const
3162 \return the currently set subresource description.
3163 */
3164
3165/*!
3166 \fn void QRhiTextureUploadEntry::setDescription(const QRhiTextureSubresourceUploadDescription &desc)
3167 Sets the subresource description \a desc.
3168 */
3169
3170/*!
3171 \class QRhiTextureUploadDescription
3172 \inmodule QtGuiPrivate
3173 \inheaderfile rhi/qrhi.h
3174 \since 6.6
3175 \brief Describes a texture upload operation.
3176
3177 Used with QRhiResourceUpdateBatch::uploadTexture(). That function has two
3178 variants: one taking a QImage and one taking a
3179 QRhiTextureUploadDescription. The former is a convenience version,
3180 internally creating a QRhiTextureUploadDescription with a single image
3181 targeting level 0 for layer 0.
3182
3183 An example of the the common, simple case of wanting to upload the contents
3184 of a QImage to a QRhiTexture with a matching pixel size:
3185
3186 \code
3187 QImage image(256, 256, QImage::Format_RGBA8888);
3188 image.fill(Qt::green); // or could use a QPainter targeting image
3189 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(256, 256));
3190 texture->create();
3191 QRhiResourceUpdateBatch *u = rhi->nextResourceUpdateBatch();
3192 u->uploadTexture(texture, image);
3193 \endcode
3194
3195 When cubemaps, pre-generated mip images, compressed textures, or partial
3196 uploads are involved, applications will have to use this class instead.
3197
3198 QRhiTextureUploadDescription also enables specifying batched uploads, which
3199 are useful for example when generating an atlas or glyph cache texture:
3200 multiple, partial uploads for the same subresource (meaning the same layer
3201 and level) are supported, and can be, depending on the backend and the
3202 underlying graphics API, more efficient when batched into the same
3203 QRhiTextureUploadDescription as opposed to issuing individual
3204 \l{QRhiResourceUpdateBatch::uploadTexture()}{uploadTexture()} commands for
3205 each of them.
3206
3207 \note Cubemaps have one layer for each of the six faces in the order +X,
3208 -X, +Y, -Y, +Z, -Z.
3209
3210 For example, specifying the faces of a cubemap could look like the following:
3211
3212 \code
3213 QImage faces[6];
3214 // ...
3215 QVarLengthArray<QRhiTextureUploadEntry, 6> entries;
3216 for (int i = 0; i < 6; ++i)
3217 entries.append(QRhiTextureUploadEntry(i, 0, faces[i]));
3218 QRhiTextureUploadDescription desc;
3219 desc.setEntries(entries.cbegin(), entries.cend());
3220 resourceUpdates->uploadTexture(texture, desc);
3221 \endcode
3222
3223 Another example that specifies mip images for a compressed texture:
3224
3225 \code
3226 QList<QRhiTextureUploadEntry> entries;
3227 const int mipCount = rhi->mipLevelsForSize(compressedTexture->pixelSize());
3228 for (int level = 0; level < mipCount; ++level) {
3229 const QByteArray compressedDataForLevel = ..
3230 entries.append(QRhiTextureUploadEntry(0, level, compressedDataForLevel));
3231 }
3232 QRhiTextureUploadDescription desc;
3233 desc.setEntries(entries.cbegin(), entries.cend());
3234 resourceUpdates->uploadTexture(compressedTexture, desc);
3235 \endcode
3236
3237 With partial uploads targeting the same subresource, it is recommended to
3238 batch them into a single upload request, whenever possible:
3239
3240 \code
3241 QRhiTextureSubresourceUploadDescription subresDesc(image);
3242 subresDesc.setSourceSize(QSize(10, 10));
3243 subResDesc.setDestinationTopLeft(QPoint(50, 40));
3244 QRhiTextureUploadEntry entry(0, 0, subresDesc); // layer 0, level 0
3245
3246 QRhiTextureSubresourceUploadDescription subresDesc2(image);
3247 subresDesc2.setSourceSize(QSize(30, 40));
3248 subResDesc2.setDestinationTopLeft(QPoint(100, 200));
3249 QRhiTextureUploadEntry entry2(0, 0, subresDesc2); // layer 0, level 0, i.e. same subresource
3250
3251 QRhiTextureUploadDescription desc({ entry, entry2});
3252 resourceUpdates->uploadTexture(texture, desc);
3253 \endcode
3254
3255 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3256 for details.
3257
3258 \sa QRhiResourceUpdateBatch
3259 */
3260
3261/*!
3262 \fn QRhiTextureUploadDescription::QRhiTextureUploadDescription()
3263
3264 Constructs an empty texture upload description.
3265 */
3266
3267/*!
3268 Constructs a texture upload description with a single subresource upload
3269 described by \a entry.
3270 */
3271QRhiTextureUploadDescription::QRhiTextureUploadDescription(const QRhiTextureUploadEntry &entry)
3272{
3273 m_entries.append(entry);
3274}
3275
3276/*!
3277 Constructs a texture upload description with the specified \a list of entries.
3278
3279 \note \a list can also contain multiple QRhiTextureUploadEntry elements
3280 with the same layer and level. This makes sense when those uploads are
3281 partial, meaning their subresource description has a source size or image
3282 smaller than the subresource dimensions, and can be more efficient than
3283 issuing separate uploadTexture()'s.
3284 */
3285QRhiTextureUploadDescription::QRhiTextureUploadDescription(std::initializer_list<QRhiTextureUploadEntry> list)
3286 : m_entries(list)
3287{
3288}
3289
3290/*!
3291 \fn void QRhiTextureUploadDescription::setEntries(std::initializer_list<QRhiTextureUploadEntry> list)
3292 Sets the \a list of entries.
3293 */
3294
3295/*!
3296 \fn template<typename InputIterator> void QRhiTextureUploadDescription::setEntries(InputIterator first, InputIterator last)
3297 Sets the list of entries using the iterators \a first and \a last.
3298 */
3299
3300/*!
3301 \fn const QRhiTextureUploadEntry *QRhiTextureUploadDescription::cbeginEntries() const
3302 \return a const iterator pointing to the first item in the entry list.
3303 */
3304
3305/*!
3306 \fn const QRhiTextureUploadEntry *QRhiTextureUploadDescription::cendEntries() const
3307 \return a const iterator pointing just after the last item in the entry list.
3308 */
3309
3310/*!
3311 \fn const QRhiTextureUploadEntry *QRhiTextureUploadDescription::entryAt(qsizetype index) const
3312 \return the entry at \a index.
3313 */
3314
3315/*!
3316 \fn qsizetype QRhiTextureUploadDescription::entryCount() const
3317 \return the number of entries.
3318 */
3319
3320/*!
3321 \class QRhiTextureCopyDescription
3322 \inmodule QtGuiPrivate
3323 \inheaderfile rhi/qrhi.h
3324 \since 6.6
3325 \brief Describes a texture-to-texture copy operation.
3326
3327 An empty pixelSize() indicates that the entire subresource is to be copied.
3328 A default constructed copy description therefore leads to copying the
3329 entire subresource at level 0 of layer 0.
3330
3331 \note The source texture must be created with
3332 QRhiTexture::UsedAsTransferSource.
3333
3334 \note The source and destination rectangles defined by pixelSize(),
3335 sourceTopLeft(), and destinationTopLeft() must fit the source and
3336 destination textures, respectively. The behavior is undefined otherwise.
3337
3338 With cubemaps, 3D textures, and texture arrays one face or slice can be
3339 copied at a time. The face or slice is specified by the source and
3340 destination layer indices. With mipmapped textures one mip level can be
3341 copied at a time. The source and destination layer and mip level indices can
3342 differ, but the size and position must be carefully controlled to avoid out
3343 of bounds copies, in which case the behavior is undefined.
3344
3345 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3346 for details.
3347 */
3348
3349/*!
3350 \fn QRhiTextureCopyDescription::QRhiTextureCopyDescription()
3351
3352 Constructs an empty texture copy description.
3353 */
3354
3355/*!
3356 \fn QSize QRhiTextureCopyDescription::pixelSize() const
3357 \return the size of the region to copy.
3358
3359 \note An empty pixelSize() indicates that the entire subresource is to be
3360 copied. A default constructed copy description therefore leads to copying
3361 the entire subresource at level 0 of layer 0.
3362 */
3363
3364/*!
3365 \fn void QRhiTextureCopyDescription::setPixelSize(const QSize &sz)
3366 Sets the size of the region to copy to \a sz.
3367 */
3368
3369/*!
3370 \fn int QRhiTextureCopyDescription::sourceLayer() const
3371 \return the source array layer (cubemap face or array layer index). Defaults to 0.
3372 */
3373
3374/*!
3375 \fn void QRhiTextureCopyDescription::setSourceLayer(int layer)
3376 Sets the source array \a layer.
3377 */
3378
3379/*!
3380 \fn int QRhiTextureCopyDescription::sourceLevel() const
3381 \return the source mip level. Defaults to 0.
3382 */
3383
3384/*!
3385 \fn void QRhiTextureCopyDescription::setSourceLevel(int level)
3386 Sets the source mip \a level.
3387 */
3388
3389/*!
3390 \fn QPoint QRhiTextureCopyDescription::sourceTopLeft() const
3391 \return the source top-left position (in pixels). Defaults to (0, 0).
3392 */
3393
3394/*!
3395 \fn void QRhiTextureCopyDescription::setSourceTopLeft(const QPoint &p)
3396 Sets the source top-left position to \a p.
3397 */
3398
3399/*!
3400 \fn int QRhiTextureCopyDescription::destinationLayer() const
3401 \return the destination array layer (cubemap face or array layer index). Default to 0.
3402 */
3403
3404/*!
3405 \fn void QRhiTextureCopyDescription::setDestinationLayer(int layer)
3406 Sets the destination array \a layer.
3407 */
3408
3409/*!
3410 \fn int QRhiTextureCopyDescription::destinationLevel() const
3411 \return the destionation mip level. Defaults to 0.
3412 */
3413
3414/*!
3415 \fn void QRhiTextureCopyDescription::setDestinationLevel(int level)
3416 Sets the destination mip \a level.
3417 */
3418
3419/*!
3420 \fn QPoint QRhiTextureCopyDescription::destinationTopLeft() const
3421 \return the destionation top-left position in pixels. Defaults to (0, 0).
3422 */
3423
3424/*!
3425 \fn void QRhiTextureCopyDescription::setDestinationTopLeft(const QPoint &p)
3426 Sets the destination top-left position \a p.
3427 */
3428
3429/*!
3430 \class QRhiReadbackDescription
3431 \inmodule QtGuiPrivate
3432 \inheaderfile rhi/qrhi.h
3433 \since 6.6
3434 \brief Describes a readback (reading back texture contents from possibly GPU-only memory) operation.
3435
3436 The source of the readback operation is either a QRhiTexture or the
3437 current backbuffer of the currently targeted QRhiSwapChain. When
3438 texture() is not set, the swapchain is used. Otherwise the specified
3439 QRhiTexture is treated as the source.
3440
3441 \note Textures used in readbacks must be created with
3442 QRhiTexture::UsedAsTransferSource.
3443
3444 \note Swapchains used in readbacks must be created with
3445 QRhiSwapChain::UsedAsTransferSource.
3446
3447 layer() and level() are only applicable when the source is a QRhiTexture.
3448
3449 \note Multisample textures cannot be read back. Readbacks are supported for
3450 multisample swapchain buffers however.
3451
3452 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3453 for details.
3454 */
3455
3456/*!
3457 \fn QRhiReadbackDescription::QRhiReadbackDescription() = default
3458
3459 Constructs an empty texture readback description.
3460
3461 \note The source texture is set to null by default, which is still a valid
3462 readback: it specifies that the backbuffer of the current swapchain is to
3463 be read back. (current meaning the frame's target swapchain at the time of
3464 committing the QRhiResourceUpdateBatch with the
3465 \l{QRhiResourceUpdateBatch::readBackTexture()}{texture readback} on it)
3466 */
3467
3468/*!
3469 Constructs an texture readback description that specifies that level 0 of
3470 layer 0 of \a texture is to be read back.
3471
3472 \note \a texture can also be null in which case this constructor is
3473 identical to the argumentless variant.
3474 */
3475QRhiReadbackDescription::QRhiReadbackDescription(QRhiTexture *texture)
3476 : m_texture(texture)
3477{
3478}
3479
3480/*!
3481 \fn QRhiTexture *QRhiReadbackDescription::texture() const
3482
3483 \return the QRhiTexture that is read back. Can be left set to \nullptr
3484 which indicates that the backbuffer of the current swapchain is to be used
3485 instead.
3486 */
3487
3488/*!
3489 \fn void QRhiReadbackDescription::setTexture(QRhiTexture *tex)
3490
3491 Sets the texture \a tex as the source of the readback operation.
3492
3493 Setting \nullptr is valid too, in which case the current swapchain's
3494 current backbuffer is used. (but then the readback cannot be issued in a
3495 non-swapchain-based frame)
3496
3497 \note Multisample textures cannot be read back. Readbacks are supported for
3498 multisample swapchain buffers however.
3499
3500 \note Textures used in readbacks must be created with
3501 QRhiTexture::UsedAsTransferSource.
3502
3503 \note Swapchains used in readbacks must be created with
3504 QRhiSwapChain::UsedAsTransferSource.
3505 */
3506
3507/*!
3508 \fn int QRhiReadbackDescription::layer() const
3509
3510 \return the currently set array layer (cubemap face, array index). Defaults to 0.
3511
3512 Applicable only when the source of the readback is a QRhiTexture.
3513 */
3514
3515/*!
3516 \fn void QRhiReadbackDescription::setLayer(int layer)
3517 Sets the array \a layer to read back.
3518 */
3519
3520/*!
3521 \fn int QRhiReadbackDescription::level() const
3522
3523 \return the currently set mip level. Defaults to 0.
3524
3525 Applicable only when the source of the readback is a QRhiTexture.
3526 */
3527
3528/*!
3529 \fn void QRhiReadbackDescription::setLevel(int level)
3530 Sets the mip \a level to read back.
3531 */
3532
3533/*!
3534 \fn const QRect &QRhiReadbackDescription::rect() const
3535 \since 6.10
3536
3537 \return the rectangle to read back. Defaults to an invalid rectangle.
3538
3539 If invalid, the entire texture or swapchain backbuffer is read back.
3540 */
3541
3542/*!
3543 \fn void QRhiReadbackDescription::setRect(const QRect &rectangle)
3544 \since 6.10
3545
3546 Sets the \a rectangle to read back.
3547 */
3548
3549/*!
3550 \class QRhiReadbackResult
3551 \inmodule QtGuiPrivate
3552 \inheaderfile rhi/qrhi.h
3553 \since 6.6
3554 \brief Describes the results of a potentially asynchronous buffer or texture readback operation.
3555
3556 When \l completed is set, the function is invoked when the \l data is
3557 available. \l format and \l pixelSize are set upon completion together with
3558 \l data.
3559
3560 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3561 for details.
3562 */
3563
3564/*!
3565 \variable QRhiReadbackResult::completed
3566
3567 Callback that is invoked upon completion, on the thread the QRhi operates
3568 on. Can be left set to \nullptr, in which case no callback is invoked.
3569 */
3570
3571/*!
3572 \variable QRhiReadbackResult::format
3573
3574 Valid only for textures, the texture format.
3575 */
3576
3577/*!
3578 \variable QRhiReadbackResult::pixelSize
3579
3580 Valid only for textures, the size in pixels.
3581 */
3582
3583/*!
3584 \variable QRhiReadbackResult::data
3585
3586 The buffer or image data.
3587
3588 \sa QRhiResourceUpdateBatch::readBackTexture(), QRhiResourceUpdateBatch::readBackBuffer()
3589 */
3590
3591
3592/*!
3593 \class QRhiNativeHandles
3594 \inmodule QtGuiPrivate
3595 \inheaderfile rhi/qrhi.h
3596 \since 6.6
3597 \brief Base class for classes exposing backend-specific collections of native resource objects.
3598
3599 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3600 for details.
3601 */
3602
3603/*!
3604 \class QRhiResource
3605 \inmodule QtGuiPrivate
3606 \inheaderfile rhi/qrhi.h
3607 \since 6.6
3608 \brief Base class for classes encapsulating native resource objects.
3609
3610 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3611 for details.
3612 */
3613
3614/*!
3615 \enum QRhiResource::Type
3616 Specifies type of the resource.
3617
3618 \value Buffer
3619 \value Texture
3620 \value Sampler
3621 \value RenderBuffer
3622 \value RenderPassDescriptor
3623 \value SwapChainRenderTarget
3624 \value TextureRenderTarget
3625 \value ShaderResourceBindings
3626 \value GraphicsPipeline
3627 \value SwapChain
3628 \value ComputePipeline
3629 \value CommandBuffer
3630 \value ShadingRateMap
3631 */
3632
3633/*!
3634 \fn virtual QRhiResource::Type QRhiResource::resourceType() const = 0
3635
3636 \return the type of the resource.
3637 */
3638
3639/*!
3640 \internal
3641 */
3642QRhiResource::QRhiResource(QRhiImplementation *rhi)
3643 : m_rhi(rhi)
3644{
3645 m_id = QRhiGlobalObjectIdGenerator::newId();
3646}
3647
3648/*!
3649 Destructor.
3650
3651 Releases (or requests deferred releasing of) the underlying native graphics
3652 resources, if there are any.
3653
3654 \note Resources referenced by commands for the current frame should not be
3655 released until the frame is submitted by QRhi::endFrame().
3656
3657 \sa destroy()
3658 */
3659QRhiResource::~QRhiResource()
3660{
3661 // destroy() cannot be called here, due to virtuals; it is up to the
3662 // subclasses to do that.
3663}
3664
3665/*!
3666 \fn virtual void QRhiResource::destroy() = 0
3667
3668 Releases (or requests deferred releasing of) the underlying native graphics
3669 resources. Safe to call multiple times, subsequent invocations will be a
3670 no-op then.
3671
3672 Once destroy() is called, the QRhiResource instance can be reused, by
3673 calling \c create() again. That will then result in creating new native
3674 graphics resources underneath.
3675
3676 \note Resources referenced by commands for the current frame should not be
3677 released until the frame is submitted by QRhi::endFrame().
3678
3679 The QRhiResource destructor also performs the same task, so calling this
3680 function is not necessary before deleting a QRhiResource.
3681
3682 \sa deleteLater()
3683 */
3684
3685/*!
3686 When called without a frame being recorded, this function is equivalent to
3687 deleting the object. Between a QRhi::beginFrame() and QRhi::endFrame()
3688 however the behavior is different: the QRhiResource will not be destroyed
3689 until the frame is submitted via QRhi::endFrame(), thus satisfying the QRhi
3690 requirement of not altering QRhiResource objects that are referenced by the
3691 frame being recorded.
3692
3693 If the QRhi that created this object is already destroyed, the object is
3694 deleted immediately.
3695
3696 Using deleteLater() can be a useful convenience in many cases, and it
3697 complements the low-level guarantee (that the underlying native graphics
3698 objects are never destroyed until it is safe to do so and it is known for
3699 sure that they are not used by the GPU in an still in-flight frame), by
3700 offering a way to make sure the C++ object instances (of QRhiBuffer,
3701 QRhiTexture, etc.) themselves also stay valid until the end of the current
3702 frame.
3703
3704 The following example shows a convenient way of creating a throwaway buffer
3705 that is only used in one frame and gets automatically released in
3706 endFrame(). (when it comes to the underlying native buffer(s), the usual
3707 guarantee applies: the QRhi backend defers the releasing of those until it
3708 is guaranteed that the frame in which the buffer is accessed by the GPU has
3709 completed)
3710
3711 \code
3712 rhi->beginFrame(swapchain);
3713 QRhiBuffer *buf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, 256);
3714 buf->deleteLater(); // !
3715 u = rhi->nextResourceUpdateBatch();
3716 u->uploadStaticBuffer(buf, data);
3717 // ... draw with buf
3718 rhi->endFrame();
3719 \endcode
3720
3721 \sa destroy()
3722 */
3723void QRhiResource::deleteLater()
3724{
3725 if (m_rhi)
3726 m_rhi->addDeleteLater(this);
3727 else
3728 delete this;
3729}
3730
3731/*!
3732 \return the currently set object name. By default the name is empty.
3733 */
3734QByteArray QRhiResource::name() const
3735{
3736 return m_objectName;
3737}
3738
3739/*!
3740 Sets a \a name for the object.
3741
3742 This allows getting descriptive names for the native graphics
3743 resources visible in graphics debugging tools, such as
3744 \l{https://renderdoc.org/}{RenderDoc} and
3745 \l{https://developer.apple.com/xcode/}{XCode}.
3746
3747 When it comes to naming native objects by relaying the name via the
3748 appropriate graphics API, note that the name is ignored when
3749 QRhi::DebugMarkers are not supported, and may, depending on the backend,
3750 also be ignored when QRhi::EnableDebugMarkers is not set.
3751
3752 \note The name may be ignored for objects other than buffers,
3753 renderbuffers, and textures, depending on the backend.
3754
3755 \note The name may be modified. For slotted resources, such as a QRhiBuffer
3756 backed by multiple native buffers, QRhi will append a suffix to make the
3757 underlying native buffers easily distinguishable from each other.
3758 */
3759void QRhiResource::setName(const QByteArray &name)
3760{
3761 m_objectName = name;
3762}
3763
3764/*!
3765 \return the global, unique identifier of this QRhiResource.
3766
3767 User code rarely needs to deal with the value directly. It is used
3768 internally for tracking and bookkeeping purposes.
3769 */
3770quint64 QRhiResource::globalResourceId() const
3771{
3772 return m_id;
3773}
3774
3775/*!
3776 \return the QRhi that created this resource.
3777
3778 If the QRhi that created this object is already destroyed, the result is
3779 \nullptr.
3780 */
3781QRhi *QRhiResource::rhi() const
3782{
3783 return m_rhi ? m_rhi->q : nullptr;
3784}
3785
3786/*!
3787 \class QRhiBuffer
3788 \inmodule QtGuiPrivate
3789 \inheaderfile rhi/qrhi.h
3790 \since 6.6
3791 \brief Vertex, index, or uniform (constant) buffer resource.
3792
3793 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3794 for details.
3795
3796 A QRhiBuffer encapsulates zero, one, or more native buffer objects (such as
3797 a \c VkBuffer or \c MTLBuffer). With some graphics APIs and backends
3798 certain types of buffers may not use a native buffer object at all (e.g.
3799 OpenGL if uniform buffer objects are not used), but this is transparent to
3800 the user of the QRhiBuffer API. Similarly, the fact that some types of
3801 buffers may use two or three native buffers underneath, in order to allow
3802 efficient per-frame content update without stalling the GPU pipeline, is
3803 mostly invisible to the applications and libraries.
3804
3805 A QRhiBuffer instance is always created by calling
3806 \l{QRhi::newBuffer()}{the QRhi's newBuffer() function}. This creates no
3807 native graphics resources. To do that, call create() after setting the
3808 appropriate options, such as the type, usage flags, size, although in most cases these
3809 are already set based on the arguments passed to
3810 \l{QRhi::newBuffer()}{newBuffer()}.
3811
3812 \section2 Example usage
3813
3814 To create a uniform buffer for a shader where the GLSL uniform block
3815 contains a single \c mat4 member, and update the contents:
3816
3817 \code
3818 QRhiBuffer *ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64);
3819 if (!ubuf->create()) { error(); }
3820 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3821 QMatrix4x4 mvp;
3822 // ... set up the modelview-projection matrix
3823 batch->updateDynamicBuffer(ubuf, 0, 64, mvp.constData());
3824 // ...
3825 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
3826 \endcode
3827
3828 An example of creating a buffer with vertex data:
3829
3830 \code
3831 const float vertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f };
3832 QRhiBuffer *vbuf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertices));
3833 if (!vbuf->create()) { error(); }
3834 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3835 batch->uploadStaticBuffer(vbuf, vertices);
3836 // ...
3837 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
3838 \endcode
3839
3840 An index buffer:
3841
3842 \code
3843 static const quint16 indices[] = { 0, 1, 2 };
3844 QRhiBuffer *ibuf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indices));
3845 if (!ibuf->create()) { error(); }
3846 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3847 batch->uploadStaticBuffer(ibuf, indices);
3848 // ...
3849 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
3850 \endcode
3851
3852 \section2 Common patterns
3853
3854 A call to create() destroys any existing native resources if create() was
3855 successfully called before. If those native resources are still in use by
3856 an in-flight frame (i.e., there's a chance they are still read by the GPU),
3857 the destroying of those resources is deferred automatically. Thus a very
3858 common and convenient pattern to safely increase the size of an already
3859 initialized buffer is the following. In practice this drops and creates a
3860 whole new set of native resources underneath, so it is not necessarily a
3861 cheap operation, but is more convenient and still faster than the
3862 alternatives, because by not destroying the \c buf object itself, all
3863 references to it stay valid in other data structures (e.g., in any
3864 QRhiShaderResourceBinding the QRhiBuffer is referenced from).
3865
3866 \code
3867 if (buf->size() < newSize) {
3868 buf->setSize(newSize);
3869 if (!buf->create()) { error(); }
3870 }
3871 // continue using buf, fill it with new data
3872 \endcode
3873
3874 When working with uniform buffers, it will sometimes be necessary to
3875 combine data for multiple draw calls into a single buffer for efficiency
3876 reasons. Be aware of the aligment requirements: with some graphics APIs
3877 offsets for a uniform buffer must be aligned to 256 bytes. This applies
3878 both to QRhiShaderResourceBinding and to the dynamic offsets passed to
3879 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()}. Use the
3880 \l{QRhi::ubufAlignment()}{ubufAlignment()} and
3881 \l{QRhi::ubufAligned()}{ubufAligned()} functions to create portable code.
3882 As an example, the following is an outline for issuing multiple (\c N) draw
3883 calls with the same pipeline and geometry, but with a different data in the
3884 uniform buffers exposed at binding point 0. This assumes the buffer is
3885 exposed via
3886 \l{QRhiShaderResourceBinding::uniformBufferWithDynamicOffset()}{uniformBufferWithDynamicOffset()}
3887 which allows passing a QRhiCommandBuffer::DynamicOffset list to
3888 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()}.
3889
3890 \code
3891 const int N = 2;
3892 const int UB_SIZE = 64 + 4; // assuming a uniform block with { mat4 matrix; float opacity; }
3893 const int ONE_UBUF_SIZE = rhi->ubufAligned(UB_SIZE);
3894 const int TOTAL_UBUF_SIZE = N * ONE_UBUF_SIZE;
3895 QRhiBuffer *ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, TOTAL_UBUF_SIZE);
3896 if (!ubuf->create()) { error(); }
3897 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3898 for (int i = 0; i < N; ++i) {
3899 batch->updateDynamicBuffer(ubuf, i * ONE_UBUF_SIZE, 64, matrix.constData());
3900 batch->updateDynamicBuffer(ubuf, i * ONE_UBUF_SIZE + 64, 4, &opacity);
3901 }
3902 // ...
3903 // beginPass(), set pipeline, etc., and then:
3904 for (int i = 0; i < N; ++i) {
3905 QRhiCommandBuffer::DynamicOffset dynOfs[] = { { 0, i * ONE_UBUF_SIZE } };
3906 cb->setShaderResources(srb, 1, dynOfs);
3907 cb->draw(36);
3908 }
3909 \endcode
3910
3911 \sa QRhiResourceUpdateBatch, QRhi, QRhiCommandBuffer
3912 */
3913
3914/*!
3915 \enum QRhiBuffer::Type
3916 Specifies storage type of buffer resource.
3917
3918 \value Immutable Indicates that the data is not expected to change ever
3919 after the initial upload. Under the hood such buffer resources are
3920 typically placed in device local (GPU) memory (on systems where
3921 applicable). Uploading new data is possible, but may be expensive. The
3922 upload typically happens by copying to a separate, host visible staging
3923 buffer from which a GPU buffer-to-buffer copy is issued into the actual
3924 GPU-only buffer.
3925
3926 \value Static Indicates that the data is expected to change only
3927 infrequently. Typically placed in device local (GPU) memory, where
3928 applicable. On backends where host visible staging buffers are used for
3929 uploading, the staging buffers are kept around for this type, unlike with
3930 Immutable, so subsequent uploads do not suffer in performance. Frequent
3931 updates, especially updates in consecutive frames, should be avoided.
3932
3933 \value Dynamic Indicates that the data is expected to change frequently.
3934 Not recommended for large buffers. Typically backed by host visible memory
3935 in 2 copies in order to allow for changing without stalling the graphics
3936 pipeline. The double buffering is managed transparently to the applications
3937 and is not exposed in the API here in any form. This is the recommended,
3938 and, with some backends, the only possible, type for buffers with
3939 UniformBuffer usage.
3940 */
3941
3942/*!
3943 \enum QRhiBuffer::UsageFlag
3944 Flag values to specify how the buffer is going to be used.
3945
3946 \value VertexBuffer Vertex buffer. This allows the QRhiBuffer to be used in
3947 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()}.
3948
3949 \value IndexBuffer Index buffer. This allows the QRhiBuffer to be used in
3950 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()}.
3951
3952 \value UniformBuffer Uniform buffer (also called constant buffer). This
3953 allows the QRhiBuffer to be used in combination with
3954 \l{QRhiShaderResourceBinding::UniformBuffer}{UniformBuffer}. When
3955 \l{QRhi::NonDynamicUniformBuffers}{NonDynamicUniformBuffers} is reported as
3956 not supported, this usage can only be combined with the type Dynamic.
3957
3958 \value StorageBuffer Storage buffer. This allows the QRhiBuffer to be used
3959 in combination with \l{QRhiShaderResourceBinding::BufferLoad}{BufferLoad},
3960 \l{QRhiShaderResourceBinding::BufferStore}{BufferStore}, or
3961 \l{QRhiShaderResourceBinding::BufferLoadStore}{BufferLoadStore}. This usage
3962 can only be combined with the types Immutable or Static, and is only
3963 available when the \l{QRhi::Compute}{Compute feature} is reported as
3964 supported.
3965
3966 \value [since 6.12] IndirectBuffer Indirect draw buffer. This allows the
3967 QRhiBuffer to be used in \l{QRhiCommandBuffer::drawIndirect()}{drawIndirect()}
3968 and \l{QRhiCommandBuffer::drawIndexedIndirect()}{drawIndexedIndirect()}.
3969 This usage can be combined with types Immutable or Static. Combining it with
3970 Dynamic is unsupported with D3D11, where create() will fail. This usage may
3971 also be combined with StorageBuffer on backends that support
3972 \l{QRhi::Compute}{compute shaders}, allowing indirect draw commands to be
3973 generated by compute shaders and consumed by indirect draw calls.
3974 */
3975
3976/*!
3977 \class QRhiBuffer::NativeBuffer
3978 \inmodule QtGuiPrivate
3979 \inheaderfile rhi/qrhi.h
3980 \brief Contains information about the underlying native resources of a buffer.
3981 */
3982
3983/*!
3984 \variable QRhiBuffer::NativeBuffer::objects
3985 \brief an array with pointers to the native object handles.
3986
3987 With OpenGL, the native handle is a GLuint value, so the elements in the \c
3988 objects array are pointers to a GLuint. With Vulkan, the native handle is a
3989 VkBuffer, so the elements of the array are pointers to a VkBuffer. With
3990 Direct3D 11 and Metal the elements are pointers to a ID3D11Buffer or
3991 MTLBuffer pointer, respectively. With Direct3D 12, the elements are
3992 pointers to a ID3D12Resource.
3993
3994 \note Pay attention to the fact that the elements are always pointers to
3995 the native buffer handle type, even if the native type itself is a pointer.
3996 (so the elements are \c{VkBuffer *} on Vulkan, even though VkBuffer itself
3997 is a pointer on 64-bit architectures).
3998 */
3999
4000/*!
4001 \variable QRhiBuffer::NativeBuffer::slotCount
4002 \brief Specifies the number of valid elements in the objects array.
4003
4004 The value can be 0, 1, 2, or 3 in practice. 0 indicates that the QRhiBuffer
4005 is not backed by any native buffer objects. This can happen with
4006 QRhiBuffers with the usage UniformBuffer when the underlying API does not
4007 support (or the backend chooses not to use) native uniform buffers. 1 is
4008 commonly used for Immutable and Static types (but some backends may
4009 differ). 2 or 3 is typical when the type is Dynamic (but some backends may
4010 differ).
4011
4012 \sa QRhi::currentFrameSlot(), QRhi::FramesInFlight
4013 */
4014
4015/*!
4016 \internal
4017 */
4018QRhiBuffer::QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, quint32 size_)
4019 : QRhiResource(rhi),
4020 m_type(type_), m_usage(usage_), m_size(size_)
4021{
4022}
4023
4024/*!
4025 \return the resource type.
4026 */
4027QRhiResource::Type QRhiBuffer::resourceType() const
4028{
4029 return Buffer;
4030}
4031
4032/*!
4033 \fn virtual bool QRhiBuffer::create() = 0
4034
4035 Creates the corresponding native graphics resources. If there are already
4036 resources present due to an earlier create() with no corresponding
4037 destroy(), then destroy() is called implicitly first.
4038
4039 \return \c true when successful, \c false when a graphics operation failed.
4040 Regardless of the return value, calling destroy() is always safe.
4041 */
4042
4043/*!
4044 \fn QRhiBuffer::Type QRhiBuffer::type() const
4045 \return the buffer type.
4046 */
4047
4048/*!
4049 \fn void QRhiBuffer::setType(Type t)
4050 Sets the buffer's type to \a t.
4051 */
4052
4053/*!
4054 \fn QRhiBuffer::UsageFlags QRhiBuffer::usage() const
4055 \return the buffer's usage flags.
4056 */
4057
4058/*!
4059 \fn void QRhiBuffer::setUsage(UsageFlags u)
4060 Sets the buffer's usage flags to \a u.
4061 */
4062
4063/*!
4064 \fn quint32 QRhiBuffer::size() const
4065
4066 \return the buffer's size in bytes.
4067
4068 This is always the value that was passed to setSize() or QRhi::newBuffer().
4069 Internally, the native buffers may be bigger if that is required by the
4070 underlying graphics API.
4071 */
4072
4073/*!
4074 \fn void QRhiBuffer::setSize(quint32 sz)
4075
4076 Sets the size of the buffer in bytes. The size is normally specified in
4077 QRhi::newBuffer() so this function is only used when the size has to be
4078 changed. As with other setters, the size only takes effect when calling
4079 create(), and for already created buffers this involves releasing the previous
4080 native resource and creating new ones under the hood.
4081
4082 Backends may choose to allocate buffers bigger than \a sz in order to
4083 fulfill alignment requirements. This is hidden from the applications and
4084 size() will always report the size requested in \a sz.
4085 */
4086
4087/*!
4088 \return the underlying native resources for this buffer. The returned value
4089 will be empty if exposing the underlying native resources is not supported by
4090 the backend.
4091
4092 A QRhiBuffer may be backed by multiple native buffer objects, depending on
4093 the type() and the QRhi backend in use. When this is the case, all of them
4094 are returned in the objects array in the returned struct, with slotCount
4095 specifying the number of native buffer objects. While
4096 \l{QRhi::beginFrame()}{recording a frame}, QRhi::currentFrameSlot() can be
4097 used to determine which of the native buffers QRhi is using for operations
4098 that read or write from this QRhiBuffer within the frame being recorded.
4099
4100 In some cases a QRhiBuffer will not be backed by a native buffer object at
4101 all. In this case slotCount will be set to 0 and no valid native objects
4102 are returned. This is not an error, and is perfectly valid when a given
4103 backend does not use native buffers for QRhiBuffers with certain types or
4104 usages.
4105
4106 \note Be aware that QRhi backends may employ various buffer update
4107 strategies. Unlike textures, where uploading image data always means
4108 recording a buffer-to-image (or similar) copy command on the command
4109 buffer, buffers, in particular Dynamic and UniformBuffer ones, can operate
4110 in many different ways. For example, a QRhiBuffer with usage type
4111 UniformBuffer may not even be backed by a native buffer object at all if
4112 uniform buffers are not used or supported by a given backend and graphics
4113 API. There are also differences to how data is written to the buffer and
4114 the type of backing memory used. For buffers backed by host visible memory,
4115 calling this function guarantees that pending host writes are executed for
4116 all the returned native buffers.
4117
4118 \sa QRhi::currentFrameSlot(), QRhi::FramesInFlight
4119 */
4120QRhiBuffer::NativeBuffer QRhiBuffer::nativeBuffer()
4121{
4122 return { {}, 0 };
4123}
4124
4125/*!
4126 \return a pointer to a memory block with the host visible buffer data.
4127
4128 This is a shortcut for medium-to-large dynamic uniform buffers that have
4129 their \b entire contents (or at least all regions that are read by the
4130 shaders in the current frame) changed \b{in every frame} and the
4131 QRhiResourceUpdateBatch-based update mechanism is seen too heavy due to the
4132 amount of data copying involved.
4133
4134 The call to this function must be eventually followed by a call to
4135 endFullDynamicUniformBufferUpdateForCurrentFrame(), before recording any
4136 render or compute pass that relies on this buffer.
4137
4138 \warning Updating data via this method is not compatible with
4139 QRhiResourceUpdateBatch-based updates and readbacks. Unexpected behavior
4140 may occur when attempting to combine the two update models for the same
4141 buffer. Similarly, the data updated this direct way may not be visible to
4142 \l{QRhiResourceUpdateBatch::readBackBuffer()}{readBackBuffer operations},
4143 depending on the backend.
4144
4145 \warning When updating buffer data via this method, the update must be done
4146 in every frame, otherwise backends that perform double or triple buffering
4147 of resources may end up in unexpected behavior.
4148
4149 \warning Partial updates are not possible with this approach since some
4150 backends may choose a strategy where the previous contents of the buffer is
4151 lost upon calling this function. Data must be written to all regions that
4152 are read by shaders in the frame currently being prepared.
4153
4154 \warning This function can only be called when recording a frame, so
4155 between QRhi::beginFrame() and QRhi::endFrame().
4156
4157 \warning This function can only be called on Dynamic buffers.
4158 */
4159char *QRhiBuffer::beginFullDynamicBufferUpdateForCurrentFrame()
4160{
4161 return nullptr;
4162}
4163
4164/*!
4165 To be called when the entire contents of the buffer data has been updated
4166 in the memory block returned from
4167 beginFullDynamicBufferUpdateForCurrentFrame().
4168 */
4169void QRhiBuffer::endFullDynamicBufferUpdateForCurrentFrame()
4170{
4171}
4172
4173/*!
4174 \internal
4175 */
4176void QRhiBuffer::fullDynamicBufferUpdateForCurrentFrame(const void *data, quint32 size)
4177{
4178 char *p = beginFullDynamicBufferUpdateForCurrentFrame();
4179 if (p) {
4180 memcpy(p, data, size > 0 ? size : m_size);
4181 endFullDynamicBufferUpdateForCurrentFrame();
4182 }
4183}
4184
4185/*!
4186 \class QRhiRenderBuffer
4187 \inmodule QtGuiPrivate
4188 \inheaderfile rhi/qrhi.h
4189 \since 6.6
4190 \brief Renderbuffer resource.
4191
4192 Renderbuffers cannot be sampled or read but have some benefits over
4193 textures in some cases:
4194
4195 A \l DepthStencil renderbuffer may be lazily allocated and be backed by
4196 transient memory with some APIs. On some platforms this may mean the
4197 depth/stencil buffer uses no physical backing at all.
4198
4199 \l Color renderbuffers are useful since QRhi::MultisampleRenderBuffer may be
4200 supported even when QRhi::MultisampleTexture is not.
4201
4202 How the renderbuffer is implemented by a backend is not exposed to the
4203 applications. In some cases it may be backed by ordinary textures, while in
4204 others there may be a different kind of native resource used.
4205
4206 Renderbuffers that are used as (and are only used as) depth-stencil buffers
4207 in combination with a QRhiSwapChain's color buffers should have the
4208 UsedWithSwapChainOnly flag set. This serves a double purpose: such buffers,
4209 depending on the backend and the underlying APIs, be more efficient, and
4210 QRhi provides automatic sizing behavior to match the color buffers, which
4211 means calling setPixelSize() and create() are not necessary for such
4212 renderbuffers.
4213
4214 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4215 for details.
4216 */
4217
4218/*!
4219 \enum QRhiRenderBuffer::Type
4220 Specifies the type of the renderbuffer
4221
4222 \value DepthStencil Combined depth/stencil
4223 \value Color Color
4224 */
4225
4226/*!
4227 \struct QRhiRenderBuffer::NativeRenderBuffer
4228 \inmodule QtGuiPrivate
4229 \inheaderfile rhi/qrhi.h
4230 \brief Wraps a native renderbuffer object.
4231 */
4232
4233/*!
4234 \variable QRhiRenderBuffer::NativeRenderBuffer::object
4235 \brief 64-bit integer containing the native object handle.
4236
4237 Used with QRhiRenderBuffer::createFrom().
4238
4239 With OpenGL the native handle is a GLuint value. \c object is expected to
4240 be a valid OpenGL renderbuffer object ID.
4241 */
4242
4243/*!
4244 \enum QRhiRenderBuffer::Flag
4245 Flag values for flags() and setFlags()
4246
4247 \value UsedWithSwapChainOnly For DepthStencil renderbuffers this indicates
4248 that the renderbuffer is only used in combination with a QRhiSwapChain, and
4249 never in any other way. This provides automatic sizing and resource
4250 rebuilding, so calling setPixelSize() or create() is not needed whenever
4251 this flag is set. This flag value may also trigger backend-specific
4252 behavior, for example with OpenGL, where a separate windowing system
4253 interface API is in use (EGL, GLX, etc.), the flag is especially important
4254 as it avoids creating any actual renderbuffer resource as there is already
4255 a windowing system provided depth/stencil buffer as requested by
4256 QSurfaceFormat.
4257 */
4258
4259/*!
4260 \internal
4261 */
4262QRhiRenderBuffer::QRhiRenderBuffer(QRhiImplementation *rhi, Type type_, const QSize &pixelSize_,
4263 int sampleCount_, Flags flags_,
4264 QRhiTexture::Format backingFormatHint_)
4265 : QRhiResource(rhi),
4266 m_type(type_), m_pixelSize(pixelSize_), m_sampleCount(sampleCount_), m_flags(flags_),
4267 m_backingFormatHint(backingFormatHint_)
4268{
4269}
4270
4271/*!
4272 \return the resource type.
4273 */
4274QRhiResource::Type QRhiRenderBuffer::resourceType() const
4275{
4276 return RenderBuffer;
4277}
4278
4279/*!
4280 \fn virtual bool QRhiRenderBuffer::create() = 0
4281
4282 Creates the corresponding native graphics resources. If there are already
4283 resources present due to an earlier create() with no corresponding
4284 destroy(), then destroy() is called implicitly first.
4285
4286 \return \c true when successful, \c false when a graphics operation failed.
4287 Regardless of the return value, calling destroy() is always safe.
4288 */
4289
4290/*!
4291 Similar to create() except that no new native renderbuffer objects are
4292 created. Instead, the native renderbuffer object specified by \a src is
4293 used.
4294
4295 This allows importing an existing renderbuffer object (which must belong to
4296 the same device or sharing context, depending on the graphics API) from an
4297 external graphics engine.
4298
4299 \note This is currently applicable to OpenGL only. This function exists
4300 solely to allow importing a renderbuffer object that is bound to some
4301 special, external object, such as an EGLImageKHR. Once the application
4302 performed the glEGLImageTargetRenderbufferStorageOES call, the renderbuffer
4303 object can be passed to this function to create a wrapping
4304 QRhiRenderBuffer, which in turn can be passed in as a color attachment to
4305 a QRhiTextureRenderTarget to enable rendering to the EGLImage.
4306
4307 \note pixelSize(), sampleCount(), and flags() must still be set correctly.
4308 Passing incorrect sizes and other values to QRhi::newRenderBuffer() and
4309 then following it with a createFrom() expecting that the native
4310 renderbuffer object alone is sufficient to deduce such values is \b wrong
4311 and will lead to problems.
4312
4313 \note QRhiRenderBuffer does not take ownership of the native object, and
4314 destroy() will not release that object.
4315
4316 \note This function is only implemented when the QRhi::RenderBufferImport
4317 feature is reported as \l{QRhi::isFeatureSupported()}{supported}. Otherwise,
4318 the function does nothing and the return value is \c false.
4319
4320 \return \c true when successful, \c false when not supported.
4321 */
4322bool QRhiRenderBuffer::createFrom(NativeRenderBuffer src)
4323{
4324 Q_UNUSED(src);
4325 return false;
4326}
4327
4328/*!
4329 \fn QRhiRenderBuffer::Type QRhiRenderBuffer::type() const
4330 \return the renderbuffer type.
4331 */
4332
4333/*!
4334 \fn void QRhiRenderBuffer::setType(Type t)
4335 Sets the type to \a t.
4336 */
4337
4338/*!
4339 \fn QSize QRhiRenderBuffer::pixelSize() const
4340 \return the pixel size.
4341 */
4342
4343/*!
4344 \fn void QRhiRenderBuffer::setPixelSize(const QSize &sz)
4345 Sets the size (in pixels) to \a sz.
4346 */
4347
4348/*!
4349 \fn int QRhiRenderBuffer::sampleCount() const
4350 \return the sample count. 1 means no multisample antialiasing.
4351 */
4352
4353/*!
4354 \fn void QRhiRenderBuffer::setSampleCount(int s)
4355 Sets the sample count to \a s.
4356 */
4357
4358/*!
4359 \fn QRhiRenderBuffer::Flags QRhiRenderBuffer::flags() const
4360 \return the flags.
4361 */
4362
4363/*!
4364 \fn void QRhiRenderBuffer::setFlags(Flags f)
4365 Sets the flags to \a f.
4366 */
4367
4368/*!
4369 \fn virtual QRhiTexture::Format QRhiRenderBuffer::backingFormat() const = 0
4370
4371 \internal
4372 */
4373
4374/*!
4375 \class QRhiTexture
4376 \inmodule QtGuiPrivate
4377 \inheaderfile rhi/qrhi.h
4378 \since 6.6
4379 \brief Texture resource.
4380
4381 A QRhiTexture encapsulates a native texture object, such as a \c VkImage or
4382 \c MTLTexture.
4383
4384 A QRhiTexture instance is always created by calling
4385 \l{QRhi::newTexture()}{the QRhi's newTexture() function}. This creates no
4386 native graphics resources. To do that, call create() after setting the
4387 appropriate options, such as the format and size, although in most cases
4388 these are already set based on the arguments passed to
4389 \l{QRhi::newTexture()}{newTexture()}.
4390
4391 Setting the \l{QRhiTexture::Flags}{flags} correctly is essential, otherwise
4392 various errors can occur depending on the underlying QRhi backend and
4393 graphics API. For example, when a texture will be rendered into from a
4394 render pass via QRhiTextureRenderTarget, the texture must be created with
4395 the \l RenderTarget flag set. Similarly, when the texture is going to be
4396 \l{QRhiResourceUpdateBatch::readBackTexture()}{read back}, the \l
4397 UsedAsTransferSource flag must be set upfront. Mipmapped textures must have
4398 the MipMapped flag set. And so on. It is not possible to change the flags
4399 once create() has succeeded. To release the existing and create a new
4400 native texture object with the changed settings, call the setters and call
4401 create() again. This then might be a potentially expensive operation.
4402
4403 \section2 Example usage
4404
4405 To create a 2D texture with a size of 512x512 pixels and set its contents to all green:
4406
4407 \code
4408 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512));
4409 if (!texture->create()) { error(); }
4410 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
4411 QImage image(512, 512, QImage::Format_RGBA8888);
4412 image.fill(Qt::green);
4413 batch->uploadTexture(texture, image);
4414 // ...
4415 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
4416 \endcode
4417
4418 \section2 Common patterns
4419
4420 A call to create() destroys any existing native resources if create() was
4421 successfully called before. If those native resources are still in use by
4422 an in-flight frame (i.e., there's a chance they are still read by the GPU),
4423 the destroying of those resources is deferred automatically. Thus a very
4424 common and convenient pattern to safely change the size of an already
4425 existing texture is the following. In practice this drops and creates a
4426 whole new native texture resource underneath, so it is not necessarily a
4427 cheap operation, but is more convenient and still faster than the
4428 alternatives, because by not destroying the \c texture object itself, all
4429 references to it stay valid in other data structures (e.g., in any
4430 QShaderResourceBinding the QRhiTexture is referenced from).
4431
4432 \code
4433 // determine newSize, e.g. based on the swapchain's output size or other factors
4434 if (texture->pixelSize() != newSize) {
4435 texture->setPixelSize(newSize);
4436 if (!texture->create()) { error(); }
4437 }
4438 // continue using texture, fill it with new data
4439 \endcode
4440
4441 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4442 for details.
4443
4444 \sa QRhiResourceUpdateBatch, QRhi, QRhiTextureRenderTarget
4445 */
4446
4447/*!
4448 \enum QRhiTexture::Flag
4449
4450 Flag values to specify how the texture is going to be used. Not honoring
4451 the flags set before create() and attempting to use the texture in ways that
4452 was not declared upfront can lead to unspecified behavior or decreased
4453 performance depending on the backend and the underlying graphics API.
4454
4455 \value RenderTarget The texture going to be used in combination with
4456 QRhiTextureRenderTarget.
4457
4458 \value CubeMap The texture is a cubemap. Such textures have 6 layers, one
4459 for each face in the order of +X, -X, +Y, -Y, +Z, -Z. Cubemap textures
4460 cannot be multisample.
4461
4462 \value MipMapped The texture has mipmaps. The appropriate mip count is
4463 calculated automatically and can also be retrieved via
4464 QRhi::mipLevelsForSize(). The images for the mip levels have to be
4465 provided in the texture uploaded or generated via
4466 QRhiResourceUpdateBatch::generateMips(). Multisample textures cannot have
4467 mipmaps.
4468
4469 \value sRGB Use an sRGB format.
4470
4471 \value UsedAsTransferSource The texture is used as the source of a texture
4472 copy or readback, meaning the texture is given as the source in
4473 QRhiResourceUpdateBatch::copyTexture() or
4474 QRhiResourceUpdateBatch::readBackTexture().
4475
4476 \value UsedWithGenerateMips The texture is going to be used with
4477 QRhiResourceUpdateBatch::generateMips().
4478
4479 \value UsedWithLoadStore The texture is going to be used with image
4480 load/store operations, for example, in a compute shader.
4481
4482 \value UsedAsCompressedAtlas The texture has a compressed format and the
4483 dimensions of subresource uploads may not match the texture size.
4484
4485 \value ExternalOES The texture should use the GL_TEXTURE_EXTERNAL_OES
4486 target with OpenGL. This flag is ignored with other graphics APIs.
4487
4488 \value ThreeDimensional The texture is a 3D texture. Such textures should
4489 be created with the QRhi::newTexture() overload taking a depth in addition
4490 to width and height. A 3D texture can have mipmaps but cannot be
4491 multisample. When rendering into, or uploading data to a 3D texture, the \c
4492 layer specified in the render target's color attachment or the upload
4493 description refers to a single slice in range [0..depth-1]. The underlying
4494 graphics API may not support 3D textures at run time. Support is indicated
4495 by the QRhi::ThreeDimensionalTextures feature.
4496
4497 \value TextureRectangleGL The texture should use the GL_TEXTURE_RECTANGLE
4498 target with OpenGL. This flag is ignored with other graphics APIs. Just
4499 like ExternalOES, this flag is useful when working with platform APIs where
4500 native OpenGL texture objects received from the platform are wrapped in a
4501 QRhiTexture, and the platform can only provide textures for a non-2D
4502 texture target.
4503
4504 \value TextureArray The texture is a texture array, i.e. a single texture
4505 object that is a homogeneous array of 2D textures. Texture arrays are
4506 created with QRhi::newTextureArray(). The underlying graphics API may not
4507 support texture array objects at run time. Support is indicated by the
4508 QRhi::TextureArrays feature. When rendering into, or uploading data to a
4509 texture array, the \c layer specified in the render target's color
4510 attachment or the upload description selects a single element in the array.
4511
4512 \value OneDimensional The texture is a 1D texture. Such textures can be
4513 created by passing a 0 height and depth to QRhi::newTexture(). Note that
4514 there can be limitations on one dimensional textures depending on the
4515 underlying graphics API. For example, rendering to them or using them with
4516 mipmap-based filtering may be unsupported. This is indicated by the
4517 QRhi::OneDimensionalTextures and QRhi::OneDimensionalTextureMipmaps
4518 feature flags.
4519
4520 \value UsedAsShadingRateMap
4521 */
4522
4523/*!
4524 \enum QRhiTexture::Format
4525
4526 Specifies the texture format. See also QRhi::isTextureFormatSupported() and
4527 note that flags() can modify the format when QRhiTexture::sRGB is set.
4528
4529 \value UnknownFormat Not a valid format. This cannot be passed to setFormat().
4530
4531 \value RGBA8 Four components, unsigned normalized 8-bit per component. Always supported. (32 bits total)
4532
4533 \value BGRA8 Four components, unsigned normalized 8-bit per component. (32 bits total)
4534
4535 \value R8 One component, unsigned normalized 8-bit. (8 bits total)
4536
4537 \value RG8 Two components, unsigned normalized 8-bit. (16 bits total)
4538
4539 \value R16 One component, unsigned normalized 16-bit. (16 bits total)
4540
4541 \value RG16 Two components, unsigned normalized 16-bit. (32 bits total)
4542
4543 \value RED_OR_ALPHA8 Either same as R8, or is a similar format with the component swizzled to alpha,
4544 depending on \l{QRhi::RedOrAlpha8IsRed}{RedOrAlpha8IsRed}. (8 bits total)
4545
4546 \value RGBA16F Four components, 16-bit float. (64 bits total)
4547
4548 \value RGBA32F Four components, 32-bit float. (128 bits total)
4549
4550 \value R16F One component, 16-bit float. (16 bits total)
4551
4552 \value R32F One component, 32-bit float. (32 bits total)
4553
4554 \value RGB10A2 Four components, unsigned normalized 10 bit R, G, and B,
4555 2-bit alpha. This is a packed format so native endianness applies. Note
4556 that there is no BGR10A2. This is because RGB10A2 maps to
4557 DXGI_FORMAT_R10G10B10A2_UNORM with D3D, MTLPixelFormatRGB10A2Unorm with
4558 Metal, VK_FORMAT_A2B10G10R10_UNORM_PACK32 with Vulkan, and
4559 GL_RGB10_A2/GL_RGB/GL_UNSIGNED_INT_2_10_10_10_REV on OpenGL (ES). This is
4560 the only universally supported RGB30 option. The corresponding QImage
4561 formats are QImage::Format_BGR30 and QImage::Format_A2BGR30_Premultiplied.
4562 (32 bits total)
4563
4564 \value D16 16-bit depth (normalized unsigned integer)
4565
4566 \value D24 24-bit depth (normalized unsigned integer)
4567
4568 \value D24S8 24-bit depth (normalized unsigned integer), 8 bit stencil
4569
4570 \value D32F 32-bit depth (32-bit float)
4571
4572 \value [since 6.9] D32FS8 32-bit depth (32-bit float), 8 bits of stencil, 24 bits unused
4573 (64 bits total)
4574
4575 \value BC1
4576 \value BC2
4577 \value BC3
4578 \value BC4
4579 \value BC5
4580 \value BC6H
4581 \value BC7
4582
4583 \value ETC2_RGB8
4584 \value ETC2_RGB8A1
4585 \value ETC2_RGBA8
4586
4587 \value ASTC_4x4
4588 \value ASTC_5x4
4589 \value ASTC_5x5
4590 \value ASTC_6x5
4591 \value ASTC_6x6
4592 \value ASTC_8x5
4593 \value ASTC_8x6
4594 \value ASTC_8x8
4595 \value ASTC_10x5
4596 \value ASTC_10x6
4597 \value ASTC_10x8
4598 \value ASTC_10x10
4599 \value ASTC_12x10
4600 \value ASTC_12x12
4601
4602 \value [since 6.9] R8UI One component, unsigned 8-bit. (8 bits total)
4603 \value [since 6.9] R32UI One component, unsigned 32-bit. (32 bits total)
4604 \value [since 6.9] RG32UI Two components, unsigned 32-bit. (64 bits total)
4605 \value [since 6.9] RGBA32UI Four components, unsigned 32-bit. (128 bits total)
4606
4607 \value [since 6.10] R8SI One component, signed 8-bit. (8 bits total)
4608 \value [since 6.10] R32SI One component, signed 32-bit. (32 bits total)
4609 \value [since 6.10] RG32SI Two components, signed 32-bit. (64 bits total)
4610 \value [since 6.10] RGBA32SI Four components, signed 32-bit. (128 bits total)
4611 */
4612
4613// When adding new texture formats, update void tst_QRhi::textureFormats_data().
4614
4615/*!
4616 \struct QRhiTexture::NativeTexture
4617 \inmodule QtGuiPrivate
4618 \inheaderfile rhi/qrhi.h
4619 \brief Contains information about the underlying native resources of a texture.
4620 */
4621
4622/*!
4623 \variable QRhiTexture::NativeTexture::object
4624 \brief 64-bit integer containing the native object handle.
4625
4626 With OpenGL, the native handle is a GLuint value, so \c object can then be
4627 cast to a GLuint. With Vulkan, the native handle is a VkImage, so \c object
4628 can be cast to a VkImage. With Direct3D 11 and Metal \c object contains a
4629 ID3D11Texture2D or MTLTexture pointer, respectively. With Direct3D 12
4630 \c object contains a ID3D12Resource pointer.
4631 */
4632
4633/*!
4634 \variable QRhiTexture::NativeTexture::layout
4635 \brief Specifies the current image layout for APIs like Vulkan.
4636
4637 For Vulkan, \c layout contains a \c VkImageLayout value.
4638 */
4639
4640/*!
4641 \internal
4642 */
4643QRhiTexture::QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_, int depth_,
4644 int arraySize_, int sampleCount_, Flags flags_)
4645 : QRhiResource(rhi),
4646 m_format(format_), m_pixelSize(pixelSize_), m_depth(depth_),
4647 m_arraySize(arraySize_), m_sampleCount(sampleCount_), m_flags(flags_)
4648{
4649}
4650
4651/*!
4652 \return the resource type.
4653 */
4654QRhiResource::Type QRhiTexture::resourceType() const
4655{
4656 return Texture;
4657}
4658
4659/*!
4660 \fn virtual bool QRhiTexture::create() = 0
4661
4662 Creates the corresponding native graphics resources. If there are already
4663 resources present due to an earlier create() with no corresponding
4664 destroy(), then destroy() is called implicitly first.
4665
4666 \return \c true when successful, \c false when a graphics operation failed.
4667 Regardless of the return value, calling destroy() is always safe.
4668 */
4669
4670/*!
4671 \return the underlying native resources for this texture. The returned value
4672 will be empty if exposing the underlying native resources is not supported by
4673 the backend.
4674
4675 \sa createFrom()
4676 */
4677QRhiTexture::NativeTexture QRhiTexture::nativeTexture()
4678{
4679 return {};
4680}
4681
4682/*!
4683 Similar to create(), except that no new native textures are created.
4684 Instead, the native texture resources specified by \a src is used.
4685
4686 This allows importing an existing native texture object (which must belong
4687 to the same device or sharing context, depending on the graphics API) from
4688 an external graphics engine.
4689
4690 \return true if the specified existing native texture object has been
4691 successfully wrapped as a non-owning QRhiTexture.
4692
4693 \note format(), pixelSize(), sampleCount(), and flags() must still be set
4694 correctly. Passing incorrect sizes and other values to QRhi::newTexture()
4695 and then following it with a createFrom() expecting that the native texture
4696 object alone is sufficient to deduce such values is \b wrong and will lead
4697 to problems.
4698
4699 \note QRhiTexture does not take ownership of the texture object. destroy()
4700 does not free the object or any associated memory.
4701
4702 The opposite of this operation, exposing a QRhiTexture-created native
4703 texture object to a foreign engine, is possible via nativeTexture().
4704
4705 \note When importing a 3D texture, or a texture array object, or, with
4706 OpenGL ES, an external texture, it is then especially important to set the
4707 corresponding flags (ThreeDimensional, TextureArray, ExternalOES) via
4708 setFlags() before calling this function.
4709*/
4710bool QRhiTexture::createFrom(QRhiTexture::NativeTexture src)
4711{
4712 Q_UNUSED(src);
4713 return false;
4714}
4715
4716/*!
4717 With some graphics APIs, such as Vulkan, integrating custom rendering code
4718 that uses the graphics API directly needs special care when it comes to
4719 image layouts. This function allows communicating the expected \a layout the
4720 image backing the QRhiTexture is in after the native rendering commands.
4721
4722 For example, consider rendering into a QRhiTexture's VkImage directly with
4723 Vulkan in a code block enclosed by QRhiCommandBuffer::beginExternal() and
4724 QRhiCommandBuffer::endExternal(), followed by using the image for texture
4725 sampling in a QRhi-based render pass. To avoid potentially incorrect image
4726 layout transitions, this function can be used to indicate what the image
4727 layout will be once the commands recorded in said code block complete.
4728
4729 Calling this function makes sense only after
4730 QRhiCommandBuffer::endExternal() and before a subsequent
4731 QRhiCommandBuffer::beginPass().
4732
4733 This function has no effect with QRhi backends where the underlying
4734 graphics API does not expose a concept of image layouts.
4735
4736 \note With Vulkan \a layout is a \c VkImageLayout. With Direct 3D 12 \a
4737 layout is a value composed of the bits from \c D3D12_RESOURCE_STATES.
4738 */
4739void QRhiTexture::setNativeLayout(int layout)
4740{
4741 Q_UNUSED(layout);
4742}
4743
4744/*!
4745 \fn QRhiTexture::Format QRhiTexture::format() const
4746 \return the texture format.
4747 */
4748
4749/*!
4750 \fn void QRhiTexture::setFormat(QRhiTexture::Format fmt)
4751
4752 Sets the requested texture format to \a fmt.
4753
4754 \note The value set is only taken into account upon the next call to
4755 create(), i.e. when the underlying graphics resource are (re)created.
4756 Setting a new value is futile otherwise and must be avoided since it can
4757 lead to inconsistent state.
4758 */
4759
4760/*!
4761 \fn QSize QRhiTexture::pixelSize() const
4762 \return the size in pixels.
4763 */
4764
4765/*!
4766 \fn void QRhiTexture::setPixelSize(const QSize &sz)
4767
4768 Sets the texture size, specified in pixels, to \a sz.
4769
4770 \note The value set is only taken into account upon the next call to
4771 create(), i.e. when the underlying graphics resource are (re)created.
4772 Setting a new value is futile otherwise and must be avoided since it can
4773 lead to inconsistent state. The same applies to all other setters as well.
4774 */
4775
4776/*!
4777 \fn int QRhiTexture::depth() const
4778 \return the depth for 3D textures.
4779 */
4780
4781/*!
4782 \fn void QRhiTexture::setDepth(int depth)
4783 Sets the \a depth for a 3D texture.
4784 */
4785
4786/*!
4787 \fn int QRhiTexture::arraySize() const
4788 \return the texture array size.
4789 */
4790
4791/*!
4792 \fn void QRhiTexture::setArraySize(int arraySize)
4793 Sets the texture \a arraySize.
4794 */
4795
4796/*!
4797 \fn int QRhiTexture::arrayRangeStart() const
4798
4799 \return the first array layer when setArrayRange() was called.
4800
4801 \sa setArrayRange()
4802 */
4803
4804/*!
4805 \fn int QRhiTexture::arrayRangeLength() const
4806
4807 \return the exposed array range size when setArrayRange() was called.
4808
4809 \sa setArrayRange()
4810*/
4811
4812/*!
4813 \fn void QRhiTexture::setArrayRange(int startIndex, int count)
4814
4815 Normally all array layers are exposed and it is up to the shader to select
4816 the layer via the third coordinate passed to the \c{texture()} GLSL
4817 function when sampling the \c sampler2DArray. When QRhi::TextureArrayRange
4818 is reported as supported, calling setArrayRange() before create() or
4819 createFrom() requests selecting only the specified range, \a count elements
4820 starting from \a startIndex. The shader logic can then be written with this
4821 in mind.
4822
4823 \sa QRhi::TextureArrayRange
4824 */
4825
4826/*!
4827 \fn Flags QRhiTexture::flags() const
4828 \return the texture flags.
4829 */
4830
4831/*!
4832 \fn void QRhiTexture::setFlags(Flags f)
4833 Sets the texture flags to \a f.
4834 */
4835
4836/*!
4837 \fn int QRhiTexture::sampleCount() const
4838 \return the sample count. 1 means no multisample antialiasing.
4839 */
4840
4841/*!
4842 \fn void QRhiTexture::setSampleCount(int s)
4843 Sets the sample count to \a s.
4844 */
4845
4846/*!
4847 \struct QRhiTexture::ViewFormat
4848 \inmodule QtGuiPrivate
4849 \inheaderfile rhi/qrhi.h
4850 \since 6.8
4851 \brief Specifies the view format for reading or writing from or to the texture.
4852
4853 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4854 for details.
4855 */
4856
4857/*!
4858 \variable QRhiTexture::ViewFormat::format
4859 */
4860
4861/*!
4862 \variable QRhiTexture::ViewFormat::srgb
4863 */
4864
4865/*!
4866 \fn QRhiTexture::ViewFormat QRhiTexture::readViewFormat() const
4867 \since 6.8
4868 \return the view format used when sampling the texture. When not called, the view
4869 format is assumed to be the same as format().
4870 */
4871
4872/*!
4873 \fn void QRhiTexture::setReadViewFormat(const ViewFormat &fmt)
4874 \since 6.8
4875
4876 Sets the shader resource view format (or the format of the view used for
4877 sampling the texture) to \a fmt. By default the same format (and sRGB-ness)
4878 is used as the texture itself, and in most cases this function does not need
4879 to be called.
4880
4881 This setting is only taken into account when the \l QRhi::TextureViewFormat
4882 feature is reported as supported.
4883
4884 \note This functionality is provided to allow "casting" between
4885 non-sRGB and sRGB in order to get the shader reads perform, or not perform,
4886 the implicit sRGB conversions. Other types of casting may or may not be
4887 functional.
4888 */
4889
4890/*!
4891 \fn QRhiTexture::ViewFormat QRhiTexture::writeViewFormat() const
4892 \since 6.8
4893 \return the view format used when writing to the texture and when using it
4894 with image load/store. When not called, the view format is assumed to be the
4895 same as format().
4896 */
4897
4898/*!
4899 \fn void QRhiTexture::setWriteViewFormat(const ViewFormat &fmt)
4900 \since 6.8
4901
4902 Sets the render target view format to \a fmt. By default the same format
4903 (and sRGB-ness) is used as the texture itself, and in most cases this
4904 function does not need to be called.
4905
4906 One common use case for providing a write view format is working with
4907 externally provided textures that, outside of our control, use an sRGB
4908 format with 3D APIs such as Vulkan or Direct 3D, but the rendering engine is
4909 already prepared to handle linearization and conversion to sRGB at the end
4910 of its shading pipeline. In this case what is wanted when rendering into
4911 such a texture is a render target view (e.g. VkImageView) that has the same,
4912 but non-sRGB format. (if e.g. from an OpenXR implementation one gets a
4913 VK_FORMAT_R8G8B8A8_SRGB texture, it is likely that rendering into it should
4914 be done using a VK_FORMAT_R8G8B8A8_UNORM view, if that is what the rendering
4915 engine's pipeline requires; in this example one would call this function
4916 with a ViewFormat that has a format of QRhiTexture::RGBA8 and \c srgb set to
4917 \c false).
4918
4919 This setting is only taken into account when the \l QRhi::TextureViewFormat
4920 feature is reported as supported.
4921
4922 \note This functionality is provided to allow "casting" between
4923 non-sRGB and sRGB in order to get the shader write not perform, or perform,
4924 the implicit sRGB conversions. Other types of casting may or may not be
4925 functional.
4926 */
4927
4928/*!
4929 \class QRhiSampler
4930 \inmodule QtGuiPrivate
4931 \inheaderfile rhi/qrhi.h
4932 \since 6.6
4933 \brief Sampler resource.
4934
4935 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4936 for details.
4937 */
4938
4939/*!
4940 \enum QRhiSampler::Filter
4941 Specifies the minification, magnification, or mipmap filtering
4942
4943 \value None Applicable only for mipmapMode(), indicates no mipmaps to be used
4944 \value Nearest
4945 \value Linear
4946 */
4947
4948/*!
4949 \enum QRhiSampler::AddressMode
4950 Specifies the addressing mode
4951
4952 \value Repeat
4953 \value ClampToEdge
4954 \value Mirror
4955 */
4956
4957/*!
4958 \enum QRhiSampler::CompareOp
4959 Specifies the texture comparison function.
4960
4961 \value Never (default)
4962 \value Less
4963 \value Equal
4964 \value LessOrEqual
4965 \value Greater
4966 \value NotEqual
4967 \value GreaterOrEqual
4968 \value Always
4969 */
4970
4971/*!
4972 \internal
4973 */
4974QRhiSampler::QRhiSampler(QRhiImplementation *rhi,
4975 Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
4976 AddressMode u_, AddressMode v_, AddressMode w_)
4977 : QRhiResource(rhi),
4978 m_magFilter(magFilter_), m_minFilter(minFilter_), m_mipmapMode(mipmapMode_),
4979 m_addressU(u_), m_addressV(v_), m_addressW(w_),
4980 m_compareOp(QRhiSampler::Never)
4981{
4982}
4983
4984/*!
4985 \return the resource type.
4986 */
4987QRhiResource::Type QRhiSampler::resourceType() const
4988{
4989 return Sampler;
4990}
4991
4992/*!
4993 \fn QRhiSampler::Filter QRhiSampler::magFilter() const
4994 \return the magnification filter mode.
4995 */
4996
4997/*!
4998 \fn void QRhiSampler::setMagFilter(Filter f)
4999 Sets the magnification filter mode to \a f.
5000 */
5001
5002/*!
5003 \fn QRhiSampler::Filter QRhiSampler::minFilter() const
5004 \return the minification filter mode.
5005 */
5006
5007/*!
5008 \fn void QRhiSampler::setMinFilter(Filter f)
5009 Sets the minification filter mode to \a f.
5010 */
5011
5012/*!
5013 \fn QRhiSampler::Filter QRhiSampler::mipmapMode() const
5014 \return the mipmap filter mode.
5015 */
5016
5017/*!
5018 \fn void QRhiSampler::setMipmapMode(Filter f)
5019
5020 Sets the mipmap filter mode to \a f.
5021
5022 Leave this set to None when the texture has no mip levels, or when the mip
5023 levels are not to be taken into account.
5024 */
5025
5026/*!
5027 \fn QRhiSampler::AddressMode QRhiSampler::addressU() const
5028 \return the horizontal wrap mode.
5029 */
5030
5031/*!
5032 \fn void QRhiSampler::setAddressU(AddressMode mode)
5033 Sets the horizontal wrap \a mode.
5034 */
5035
5036/*!
5037 \fn QRhiSampler::AddressMode QRhiSampler::addressV() const
5038 \return the vertical wrap mode.
5039 */
5040
5041/*!
5042 \fn void QRhiSampler::setAddressV(AddressMode mode)
5043 Sets the vertical wrap \a mode.
5044 */
5045
5046/*!
5047 \fn QRhiSampler::AddressMode QRhiSampler::addressW() const
5048 \return the depth wrap mode.
5049 */
5050
5051/*!
5052 \fn void QRhiSampler::setAddressW(AddressMode mode)
5053 Sets the depth wrap \a mode.
5054 */
5055
5056/*!
5057 \fn QRhiSampler::CompareOp QRhiSampler::textureCompareOp() const
5058 \return the texture comparison function.
5059 */
5060
5061/*!
5062 \fn void QRhiSampler::setTextureCompareOp(CompareOp op)
5063 Sets the texture comparison function \a op.
5064 */
5065
5066/*!
5067 \class QRhiShadingRateMap
5068 \inmodule QtGuiPrivate
5069 \inheaderfile rhi/qrhi.h
5070 \since 6.9
5071 \brief An object that wraps a texture or another kind of native 3D API object.
5072
5073 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5074 for details.
5075
5076 For an introduction to Variable Rate Shading (VRS), see
5077 \l{https://learn.microsoft.com/en-us/windows/win32/direct3d12/vrs}. Qt
5078 supports a subset of the VRS features offered by Direct 3D 12 and Vulkan. In
5079 addition, Metal's somewhat different mechanism is supported by making it
5080 possible to set up a QRhiShadingRateMap with an existing
5081 MTLRasterizationRateMap object.
5082 */
5083
5084/*!
5085 \struct QRhiShadingRateMap::NativeShadingRateMap
5086 \inmodule QtGuiPrivate
5087 \inheaderfile rhi/qrhi.h
5088 \since 6.9
5089 \brief Wraps a native shading rate map.
5090
5091 An example is MTLRasterizationRateMap with Metal. Other 3D APIs that use
5092 textures for image-based VRS do not use this struct since those can function
5093 via the QRhiTexture-based overload of QRhiShadingRate::createFrom().
5094 */
5095
5096/*!
5097 \variable QRhiShadingRateMap::NativeShadingRateMap::object
5098 \brief 64-bit integer containing the native object handle.
5099
5100 Used with QRhiShadingRateMap::createFrom(). For example, with Metal,
5101 \c object is expected to be an id<MTLRasterizationRateMap>.
5102 */
5103
5104/*!
5105 \internal
5106 */
5107QRhiShadingRateMap::QRhiShadingRateMap(QRhiImplementation *rhi)
5108 : QRhiResource(rhi)
5109{
5110}
5111
5112/*!
5113 \return the resource type.
5114 */
5115QRhiResource::Type QRhiShadingRateMap::resourceType() const
5116{
5117 return ShadingRateMap;
5118}
5119
5120/*!
5121 Sets up the shading rate map to use a native 3D API shading rate object
5122 \a src.
5123
5124 \return \c true when successful, \c false when not supported.
5125
5126 \note This is functional only when the QRhi::VariableRateShadingMap feature
5127 is reported as supported, while QRhi::VariableShadingRateMapWithTexture
5128 feature is not. Currently this is true for Metal, assuming variable rate
5129 shading is supported by the GPU.
5130
5131 \note With Metal, the \c object field of \a src is expected to contain an
5132 id<MTLRasterizationRateMap>. Note that Qt does not perform anything else
5133 apart from passing the MTLRasterizationRateMap on to the
5134 MTLRenderPassDescriptor. If any special scaling is required, it is up to the
5135 application (or the XR compositor) to perform that.
5136 */
5137bool QRhiShadingRateMap::createFrom(NativeShadingRateMap src)
5138{
5139 Q_UNUSED(src);
5140 return false;
5141}
5142
5143/*!
5144 Sets up the shading rate map to use the texture \a src as the
5145 image containing the per-tile shading rates.
5146
5147 \return \c true when successful, \c false when not supported.
5148
5149 The QRhiShadingRateMap does not take ownership of \a src.
5150
5151 \note This is functional only when the
5152 QRhi::VariableRateShadingMapWithTexture feature is reported as supported. In
5153 practice may be supported on Vulkan and Direct 3D 12 when using modern
5154 graphics cards. It will never be supported on OpenGL or Metal, for example.
5155
5156 \note \a src must have a format of QRhiTexture::R8UI.
5157
5158 \note \a src must have a width of \c{ceil(render_target_pixel_width /
5159 (float)tile_width)} and a height of \c{ceil(render_target_pixel_height /
5160 (float)tile_height)}. It is up to the application to ensure the size of the
5161 texture is as expected, using the above formula, at all times. The tile size
5162 can be queried via \l QRhi::resourceLimit() and
5163 QRhi::ShadingRateImageTileSize.
5164
5165 Each byte (texel) in the texture corresponds to the shading rate value for
5166 one tile. 0 indicates 1x1, while a value of 10 indicates 4x4. See
5167 \l{https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_shading_rate}{D3D12_SHADING_RATE}
5168 for other possible values.
5169 */
5170bool QRhiShadingRateMap::createFrom(QRhiTexture *src)
5171{
5172 Q_UNUSED(src);
5173 return false;
5174}
5175
5176/*!
5177 \class QRhiRenderPassDescriptor
5178 \inmodule QtGuiPrivate
5179 \inheaderfile rhi/qrhi.h
5180 \since 6.6
5181 \brief Render pass resource.
5182
5183 A render pass, if such a concept exists in the underlying graphics API, is
5184 a collection of attachments (color, depth, stencil) and describes how those
5185 attachments are used.
5186
5187 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5188 for details.
5189 */
5190
5191/*!
5192 \internal
5193 */
5194QRhiRenderPassDescriptor::QRhiRenderPassDescriptor(QRhiImplementation *rhi)
5195 : QRhiResource(rhi)
5196{
5197}
5198
5199/*!
5200 \return the resource type.
5201 */
5202QRhiResource::Type QRhiRenderPassDescriptor::resourceType() const
5203{
5204 return RenderPassDescriptor;
5205}
5206
5207/*!
5208 \fn virtual bool QRhiRenderPassDescriptor::isCompatible(const QRhiRenderPassDescriptor *other) const = 0
5209
5210 \return true if the \a other QRhiRenderPassDescriptor is compatible with
5211 this one, meaning \c this and \a other can be used interchangebly in
5212 QRhiGraphicsPipeline::setRenderPassDescriptor().
5213
5214 The concept of the compatibility of renderpass descriptors is similar to
5215 the \l{QRhiShaderResourceBindings::isLayoutCompatible}{layout
5216 compatibility} of QRhiShaderResourceBindings instances. They allow better
5217 reuse of QRhiGraphicsPipeline instances: for example, a
5218 QRhiGraphicsPipeline instance cache is expected to use these functions to
5219 look for a matching pipeline, instead of just comparing pointers, thus
5220 allowing a different QRhiRenderPassDescriptor and
5221 QRhiShaderResourceBindings to be used in combination with the pipeline, as
5222 long as they are compatible.
5223
5224 The exact details of compatibility depend on the underlying graphics API.
5225 Two renderpass descriptors
5226 \l{QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor()}{created}
5227 from the same QRhiTextureRenderTarget are always compatible.
5228
5229 Similarly to QRhiShaderResourceBindings, compatibility can also be tested
5230 without having two existing objects available. Extracting the opaque blob by
5231 calling serializedFormat() allows testing for compatibility by comparing the
5232 returned vector to another QRhiRenderPassDescriptor's
5233 serializedFormat(). This has benefits in certain situations, because it
5234 allows testing the compatibility of a QRhiRenderPassDescriptor with a
5235 QRhiGraphicsPipeline even when the QRhiRenderPassDescriptor the pipeline was
5236 originally built was is no longer available (but the data returned from its
5237 serializedFormat() still is).
5238
5239 \sa newCompatibleRenderPassDescriptor(), serializedFormat()
5240 */
5241
5242/*!
5243 \fn virtual QRhiRenderPassDescriptor *QRhiRenderPassDescriptor::newCompatibleRenderPassDescriptor() const = 0
5244
5245 \return a new QRhiRenderPassDescriptor that is
5246 \l{isCompatible()}{compatible} with this one.
5247
5248 This function allows cloning a QRhiRenderPassDescriptor. The returned
5249 object is ready to be used, and the ownership is transferred to the caller.
5250 Cloning a QRhiRenderPassDescriptor object can become useful in situations
5251 where the object is stored in data structures related to graphics pipelines
5252 (in order to allow creating new pipelines which in turn requires a
5253 renderpass descriptor object), and the lifetime of the renderpass
5254 descriptor created from a render target may be shorter than the pipelines.
5255 (for example, because the engine manages and destroys renderpasses together
5256 with the textures and render targets it was created from) In such a
5257 situation, it can be beneficial to store a cloned version in the data
5258 structures, and thus transferring ownership as well.
5259
5260 \sa isCompatible()
5261 */
5262
5263/*!
5264 \fn virtual QVector<quint32> QRhiRenderPassDescriptor::serializedFormat() const = 0
5265
5266 \return a vector of integers containing an opaque blob describing the data
5267 relevant for \l{isCompatible()}{compatibility}.
5268
5269 Given two QRhiRenderPassDescriptor objects \c rp1 and \c rp2, if the data
5270 returned from this function is identical, then \c{rp1->isCompatible(rp2)},
5271 and vice versa hold true as well.
5272
5273 \note The returned data is meant to be used for storing in memory and
5274 comparisons during the lifetime of the QRhi the object belongs to. It is not
5275 meant for storing on disk, reusing between processes, or using with multiple
5276 QRhi instances with potentially different backends.
5277
5278 \note Calling this function is expected to be a cheap operation since the
5279 backends are not supposed to calculate the data in this function, but rather
5280 return an already calculated series of data.
5281
5282 When creating reusable components as part of a library, where graphics
5283 pipelines are created and maintained while targeting a QRhiRenderTarget (be
5284 it a swapchain or a texture) managed by the client of the library, the
5285 components must be able to deal with a changing QRhiRenderPassDescriptor.
5286 For example, because the render target changes and so invalidates the
5287 previously QRhiRenderPassDescriptor (with regards to the new render target
5288 at least) due to having a potentially different color format and attachments
5289 now. Or because \l{QRhiShadingRateMap}{variable rate shading} is taken into
5290 use dynamically. A simple pattern that helps dealing with this is performing
5291 the following check on every frame, to recognize the case when the pipeline
5292 needs to be associated with a new QRhiRenderPassDescriptor, because
5293 something is different about the render target now, compared to earlier
5294 frames:
5295
5296 \code
5297 QRhiRenderPassDescriptor *rp = m_renderTarget->renderPassDescriptor();
5298 if (m_pipeline && rp->serializedFormat() != m_renderPassFormat) {
5299 m_pipeline->setRenderPassDescriptor(rp);
5300 m_renderPassFormat = rp->serializedFormat();
5301 m_pipeline->create();
5302 }
5303 // remember to store m_renderPassFormat also when creating m_pipeline the first time
5304 \endcode
5305
5306 \sa isCompatible()
5307 */
5308
5309/*!
5310 \return a pointer to a backend-specific QRhiNativeHandles subclass, such as
5311 QRhiVulkanRenderPassNativeHandles. The returned value is \nullptr when exposing
5312 the underlying native resources is not supported by the backend.
5313
5314 \sa QRhiVulkanRenderPassNativeHandles
5315 */
5316const QRhiNativeHandles *QRhiRenderPassDescriptor::nativeHandles()
5317{
5318 return nullptr;
5319}
5320
5321/*!
5322 \class QRhiRenderTarget
5323 \inmodule QtGuiPrivate
5324 \inheaderfile rhi/qrhi.h
5325 \since 6.6
5326 \brief Represents an onscreen (swapchain) or offscreen (texture) render target.
5327
5328 Applications do not create an instance of this class directly. Rather, it
5329 is the subclass QRhiTextureRenderTarget that is instantiable by clients of
5330 the API via \l{QRhi::newTextureRenderTarget()}{newTextureRenderTarget()}.
5331 The other subclass is QRhiSwapChainRenderTarget, which is the type
5332 QRhiSwapChain returns when calling
5333 \l{QRhiSwapChain::currentFrameRenderTarget()}{currentFrameRenderTarget()}.
5334
5335 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5336 for details.
5337
5338 \sa QRhiSwapChainRenderTarget, QRhiTextureRenderTarget
5339 */
5340
5341/*!
5342 \internal
5343 */
5344QRhiRenderTarget::QRhiRenderTarget(QRhiImplementation *rhi)
5345 : QRhiResource(rhi)
5346{
5347}
5348
5349/*!
5350 \fn virtual QSize QRhiRenderTarget::pixelSize() const = 0
5351
5352 \return the size in pixels.
5353
5354 Valid only after create() has been called successfully. Until then the
5355 result is a default-constructed QSize.
5356
5357 With QRhiTextureRenderTarget the returned size is the size of the
5358 associated attachments at the time of create(), in practice the size of the
5359 first color attachment, or the depth/stencil buffer if there are no color
5360 attachments. If the associated textures or renderbuffers are resized and
5361 rebuilt afterwards, then pixelSize() performs an implicit call to create()
5362 in order to rebuild the underlying data structures. This implicit check is
5363 similar to what QRhiCommandBuffer::beginPass() does, and ensures that the
5364 returned size is always up-to-date.
5365 */
5366
5367/*!
5368 \fn virtual float QRhiRenderTarget::devicePixelRatio() const = 0
5369
5370 \return the device pixel ratio. For QRhiTextureRenderTarget this is always
5371 1. For targets retrieved from a QRhiSwapChain the value reflects the
5372 \l{QWindow::devicePixelRatio()}{device pixel ratio} of the targeted
5373 QWindow.
5374 */
5375
5376/*!
5377 \fn virtual int QRhiRenderTarget::sampleCount() const = 0
5378
5379 \return the sample count or 1 if multisample antialiasing is not relevant for
5380 this render target.
5381 */
5382
5383/*!
5384 \fn QRhiRenderPassDescriptor *QRhiRenderTarget::renderPassDescriptor() const
5385
5386 \return the associated QRhiRenderPassDescriptor.
5387 */
5388
5389/*!
5390 \fn void QRhiRenderTarget::setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
5391
5392 Sets the QRhiRenderPassDescriptor \a desc for use with this render target.
5393 */
5394
5395/*!
5396 \internal
5397 */
5398QRhiSwapChainRenderTarget::QRhiSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain_)
5399 : QRhiRenderTarget(rhi),
5400 m_swapchain(swapchain_)
5401{
5402}
5403
5404/*!
5405 \class QRhiSwapChainRenderTarget
5406 \inmodule QtGuiPrivate
5407 \inheaderfile rhi/qrhi.h
5408 \since 6.6
5409 \brief Swapchain render target resource.
5410
5411 When targeting the color buffers of a swapchain, active render target is a
5412 QRhiSwapChainRenderTarget. This is what
5413 QRhiSwapChain::currentFrameRenderTarget() returns.
5414
5415 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5416 for details.
5417
5418 \sa QRhiSwapChain
5419 */
5420
5421/*!
5422 \return the resource type.
5423 */
5424QRhiResource::Type QRhiSwapChainRenderTarget::resourceType() const
5425{
5426 return SwapChainRenderTarget;
5427}
5428
5429/*!
5430 \fn QRhiSwapChain *QRhiSwapChainRenderTarget::swapChain() const
5431
5432 \return the swapchain object.
5433 */
5434
5435/*!
5436 \class QRhiTextureRenderTarget
5437 \inmodule QtGuiPrivate
5438 \inheaderfile rhi/qrhi.h
5439 \since 6.6
5440 \brief Texture render target resource.
5441
5442 A texture render target allows rendering into one or more textures,
5443 optionally with a depth texture or depth/stencil renderbuffer.
5444
5445 For multisample rendering the common approach is to use a renderbuffer as
5446 the color attachment and set the non-multisample destination texture as the
5447 \c{resolve texture}. For more information, read the detailed description of
5448 the \l QRhiColorAttachment class.
5449
5450 \note Textures used in combination with QRhiTextureRenderTarget must be
5451 created with the QRhiTexture::RenderTarget flag.
5452
5453 The simplest example of creating a render target with a texture as its
5454 single color attachment:
5455
5456 \code
5457 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, size, 1, QRhiTexture::RenderTarget);
5458 texture->create();
5459 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ texture });
5460 rp = rt->newCompatibleRenderPassDescriptor();
5461 rt->setRenderPassDescriptor(rp);
5462 rt->create();
5463 // rt can now be used with beginPass()
5464 \endcode
5465
5466 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5467 for details.
5468 */
5469
5470/*!
5471 \enum QRhiTextureRenderTarget::Flag
5472
5473 Flag values describing the load/store behavior for the render target. The
5474 load/store behavior may be baked into native resources under the hood,
5475 depending on the backend, and therefore it needs to be known upfront and
5476 cannot be changed without rebuilding (and so releasing and creating new
5477 native resources).
5478
5479 \value PreserveColorContents Indicates that the contents of the color
5480 attachments is to be loaded when starting a render pass, instead of
5481 clearing. This is potentially more expensive, especially on mobile (tiled)
5482 GPUs, but allows preserving the existing contents between passes. When doing
5483 multisample rendering with a resolve texture set, setting this flag also
5484 requests the multisample color data to be stored (written out) to the
5485 multisample texture or render buffer. (for non-multisample rendering the
5486 color data is always stored, but for MSAA storing the multisample data
5487 decreases efficiency for certain GPU architectures, hence defaulting to not
5488 writing it out) Note however that this is non-portable: in some cases there
5489 is no intermediate multisample texture on the graphics API level, e.g. when
5490 using OpenGL ES's \c{GL_EXT_multisampled_render_to_texture} as it is all
5491 implicit, handled by the OpenGL ES implementation. In that case,
5492 PreserveColorContents will likely have no effect. Therefore, avoid relying
5493 on this flag when using multisample rendering and the color attachment is
5494 using a multisample QRhiTexture (not QRhiRenderBuffer).
5495
5496 \value PreserveDepthStencilContents Indicates that the contents of the
5497 depth texture is to be loaded when starting a render pass, instead
5498 clearing. Only applicable when a texture is used as the depth buffer
5499 (QRhiTextureRenderTargetDescription::depthTexture() is set) because
5500 depth/stencil renderbuffers may not have any physical backing and data may
5501 not be written out in the first place.
5502
5503 \value DoNotStoreDepthStencilContents Indicates that the contents of the
5504 depth texture does not need to be written out. Relevant only when a
5505 QRhiTexture, not QRhiRenderBuffer, is used as the depth-stencil buffer,
5506 because for QRhiRenderBuffer this is implicit. When a depthResolveTexture is
5507 set, the flag is not relevant, because the behavior is then as if the flag
5508 was set. This enum value is introduced in Qt 6.8.
5509 */
5510
5511/*!
5512 \internal
5513 */
5514QRhiTextureRenderTarget::QRhiTextureRenderTarget(QRhiImplementation *rhi,
5515 const QRhiTextureRenderTargetDescription &desc_,
5516 Flags flags_)
5517 : QRhiRenderTarget(rhi),
5518 m_desc(desc_),
5519 m_flags(flags_)
5520{
5521}
5522
5523/*!
5524 \return the resource type.
5525 */
5526QRhiResource::Type QRhiTextureRenderTarget::resourceType() const
5527{
5528 return TextureRenderTarget;
5529}
5530
5531/*!
5532 \fn virtual QRhiRenderPassDescriptor *QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor() = 0
5533
5534 \return a new QRhiRenderPassDescriptor that is compatible with this render
5535 target.
5536
5537 The returned value is used in two ways: it can be passed to
5538 setRenderPassDescriptor() and
5539 QRhiGraphicsPipeline::setRenderPassDescriptor(). A render pass descriptor
5540 describes the attachments (color, depth/stencil) and the load/store
5541 behavior that can be affected by flags(). A QRhiGraphicsPipeline can only
5542 be used in combination with a render target that has a
5543 \l{QRhiRenderPassDescriptor::isCompatible()}{compatible}
5544 QRhiRenderPassDescriptor set.
5545
5546 Two QRhiTextureRenderTarget instances can share the same render pass
5547 descriptor as long as they have the same number and type of attachments.
5548 The associated QRhiTexture or QRhiRenderBuffer instances are not part of
5549 the render pass descriptor so those can differ in the two
5550 QRhiTextureRenderTarget instances.
5551
5552 \note resources, such as QRhiTexture instances, referenced in description()
5553 must already have create() called on them.
5554
5555 \sa create()
5556 */
5557
5558/*!
5559 \fn virtual bool QRhiTextureRenderTarget::create() = 0
5560
5561 Creates the corresponding native graphics resources. If there are already
5562 resources present due to an earlier create() with no corresponding
5563 destroy(), then destroy() is called implicitly first.
5564
5565 \note renderPassDescriptor() must be set before calling create(). To obtain
5566 a QRhiRenderPassDescriptor compatible with the render target, call
5567 newCompatibleRenderPassDescriptor() before create() but after setting all
5568 other parameters, such as description() and flags(). To save resources,
5569 reuse the same QRhiRenderPassDescriptor with multiple
5570 QRhiTextureRenderTarget instances, whenever possible. Sharing the same
5571 render pass descriptor is only possible when the render targets have the
5572 same number and type of attachments (the actual textures can differ) and
5573 the same flags.
5574
5575 \note resources, such as QRhiTexture instances, referenced in description()
5576 must already have create() called on them.
5577
5578 \return \c true when successful, \c false when a graphics operation failed.
5579 Regardless of the return value, calling destroy() is always safe.
5580 */
5581
5582/*!
5583 \fn QRhiTextureRenderTargetDescription QRhiTextureRenderTarget::description() const
5584 \return the render target description.
5585 */
5586
5587/*!
5588 \fn void QRhiTextureRenderTarget::setDescription(const QRhiTextureRenderTargetDescription &desc)
5589 Sets the render target description \a desc.
5590 */
5591
5592/*!
5593 \fn QRhiTextureRenderTarget::Flags QRhiTextureRenderTarget::flags() const
5594 \return the currently set flags.
5595 */
5596
5597/*!
5598 \fn void QRhiTextureRenderTarget::setFlags(Flags f)
5599 Sets the flags to \a f.
5600 */
5601
5602/*!
5603 \class QRhiShaderResourceBindings
5604 \inmodule QtGuiPrivate
5605 \inheaderfile rhi/qrhi.h
5606 \since 6.6
5607 \brief Encapsulates resources for making buffer, texture, sampler resources visible to shaders.
5608
5609 A QRhiShaderResourceBindings is a collection of QRhiShaderResourceBinding
5610 objects, each of which describe a single binding.
5611
5612 Take a fragment shader with the following interface:
5613
5614 \badcode
5615 layout(std140, binding = 0) uniform buf {
5616 mat4 mvp;
5617 int flip;
5618 } ubuf;
5619
5620 layout(binding = 1) uniform sampler2D tex;
5621 \endcode
5622
5623 To make resources visible to the shader, the following
5624 QRhiShaderResourceBindings could be created and then passed to
5625 QRhiGraphicsPipeline::setShaderResourceBindings():
5626
5627 \code
5628 QRhiShaderResourceBindings *srb = rhi->newShaderResourceBindings();
5629 srb->setBindings({
5630 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, ubuf),
5631 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, texture, sampler)
5632 });
5633 srb->create();
5634 // ...
5635 QRhiGraphicsPipeline *ps = rhi->newGraphicsPipeline();
5636 // ...
5637 ps->setShaderResourceBindings(srb);
5638 ps->create();
5639 // ...
5640 cb->setGraphicsPipeline(ps);
5641 cb->setShaderResources(); // binds srb
5642 \endcode
5643
5644 This assumes that \c ubuf is a QRhiBuffer, \c texture is a QRhiTexture,
5645 while \a sampler is a QRhiSampler. The example also assumes that the
5646 uniform block is present in the vertex shader as well so the same buffer is
5647 made visible to the vertex stage too.
5648
5649 \section3 Advanced usage
5650
5651 Building on the above example, let's assume that a pass now needs to use
5652 the exact same pipeline and shaders with a different texture. Creating a
5653 whole separate QRhiGraphicsPipeline just for this would be an overkill.
5654 This is why QRhiCommandBuffer::setShaderResources() allows specifying a \a
5655 srb argument. As long as the layouts (so the number of bindings and the
5656 binding points) match between two QRhiShaderResourceBindings, they can both
5657 be used with the same pipeline, assuming the pipeline was created with one of
5658 them in the first place. See isLayoutCompatible() for more details.
5659
5660 \code
5661 QRhiShaderResourceBindings *srb2 = rhi->newShaderResourceBindings();
5662 // ...
5663 cb->setGraphicsPipeline(ps);
5664 cb->setShaderResources(srb2); // binds srb2
5665 \endcode
5666
5667 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5668 for details.
5669 */
5670
5671/*!
5672 \typedef QRhiShaderResourceBindingSet
5673 \relates QRhi
5674 \since 6.7
5675
5676 Synonym for QRhiShaderResourceBindings.
5677*/
5678
5679/*!
5680 \internal
5681 */
5682QRhiShaderResourceBindings::QRhiShaderResourceBindings(QRhiImplementation *rhi)
5683 : QRhiResource(rhi)
5684{
5685 m_layoutDesc.reserve(BINDING_PREALLOC * QRhiShaderResourceBinding::LAYOUT_DESC_ENTRIES_PER_BINDING);
5686}
5687
5688/*!
5689 \return the resource type.
5690 */
5691QRhiResource::Type QRhiShaderResourceBindings::resourceType() const
5692{
5693 return ShaderResourceBindings;
5694}
5695
5696/*!
5697 \return \c true if the layout is compatible with \a other. The layout does
5698 not include the actual resource (such as, buffer or texture) and related
5699 parameters (such as, offset or size). It does include the binding point,
5700 pipeline stage, and resource type, however. The number and order of the
5701 bindings must also match in order to be compatible.
5702
5703 When there is a QRhiGraphicsPipeline created with this
5704 QRhiShaderResourceBindings, and the function returns \c true, \a other can
5705 then safely be passed to QRhiCommandBuffer::setShaderResources(), and so
5706 be used with the pipeline in place of this QRhiShaderResourceBindings.
5707
5708 \note This function must only be called after a successful create(), because
5709 it relies on data generated during the baking of the underlying data
5710 structures. This way the function can implement a comparison approach that
5711 is more efficient than iterating through two binding lists and calling
5712 QRhiShaderResourceBinding::isLayoutCompatible() on each pair. This becomes
5713 relevant especially when this function is called at a high frequency.
5714
5715 \sa serializedLayoutDescription()
5716 */
5717bool QRhiShaderResourceBindings::isLayoutCompatible(const QRhiShaderResourceBindings *other) const
5718{
5719 if (other == this)
5720 return true;
5721
5722 if (!other)
5723 return false;
5724
5725 // This can become a hot code path. Therefore we do not iterate and call
5726 // isLayoutCompatible() on m_bindings, but rather check a pre-calculated
5727 // hash code and then, if the hash matched, do a uint array comparison
5728 // (that's still more cache friendly).
5729
5730 return m_layoutDescHash == other->m_layoutDescHash
5731 && m_layoutDesc == other->m_layoutDesc;
5732}
5733
5734/*!
5735 \fn QVector<quint32> QRhiShaderResourceBindings::serializedLayoutDescription() const
5736
5737 \return a vector of integers containing an opaque blob describing the layout
5738 of the binding list, i.e. the data relevant for
5739 \l{isLayoutCompatible()}{layout compatibility tests}.
5740
5741 Given two objects \c srb1 and \c srb2, if the data returned from this
5742 function is identical, then \c{srb1->isLayoutCompatible(srb2)}, and vice
5743 versa hold true as well.
5744
5745 \note The returned data is meant to be used for storing in memory and
5746 comparisons during the lifetime of the QRhi the object belongs to. It is not
5747 meant for storing on disk, reusing between processes, or using with multiple
5748 QRhi instances with potentially different backends.
5749
5750 \sa isLayoutCompatible()
5751 */
5752
5753void QRhiImplementation::updateLayoutDesc(QRhiShaderResourceBindings *srb)
5754{
5755 srb->m_layoutDescHash = 0;
5756 srb->m_layoutDesc.clear();
5757 auto layoutDescAppender = std::back_inserter(srb->m_layoutDesc);
5758 for (const QRhiShaderResourceBinding &b : std::as_const(srb->m_bindings)) {
5759 const QRhiShaderResourceBinding::Data *d = &b.d;
5760 srb->m_layoutDescHash ^= uint(d->binding) ^ uint(d->stage) ^ uint(d->type)
5761 ^ uint(d->arraySize());
5762 layoutDescAppender = d->serialize(layoutDescAppender);
5763 }
5764}
5765
5766/*!
5767 \fn virtual bool QRhiShaderResourceBindings::create() = 0
5768
5769 Creates the corresponding resource binding set. Depending on the underlying
5770 graphics API, this may involve creating native graphics resources, and
5771 therefore it should not be assumed that this is a cheap operation.
5772
5773 If create() has been called before with no corresponding destroy(), then
5774 destroy() is called implicitly first.
5775
5776 \return \c true when successful, \c false when failed.
5777 Regardless of the return value, calling destroy() is always safe.
5778 */
5779
5780/*!
5781 \fn void QRhiShaderResourceBindings::setBindings(std::initializer_list<QRhiShaderResourceBinding> list)
5782 Sets the \a list of bindings.
5783 */
5784
5785/*!
5786 \fn template<typename InputIterator> void QRhiShaderResourceBindings::setBindings(InputIterator first, InputIterator last)
5787 Sets the list of bindings from the iterators \a first and \a last.
5788 */
5789
5790/*!
5791 \fn const QRhiShaderResourceBinding *QRhiShaderResourceBindings::cbeginBindings() const
5792 \return a const iterator pointing to the first item in the binding list.
5793 */
5794
5795/*!
5796 \fn const QRhiShaderResourceBinding *QRhiShaderResourceBindings::cendBindings() const
5797 \return a const iterator pointing just after the last item in the binding list.
5798 */
5799
5800/*!
5801 \fn const QRhiShaderResourceBinding *QRhiShaderResourceBindings::bindingAt(qsizetype index) const
5802 \return the binding at the specified \a index.
5803 */
5804
5805/*!
5806 \fn qsizetype QRhiShaderResourceBindings::bindingCount() const
5807 \return the number of bindings.
5808 */
5809
5810/*!
5811 \class QRhiShaderResourceBinding
5812 \inmodule QtGuiPrivate
5813 \inheaderfile rhi/qrhi.h
5814 \since 6.6
5815 \brief Describes the shader resource for a single binding point.
5816
5817 A QRhiShaderResourceBinding cannot be constructed directly. Instead, use the
5818 static functions such as uniformBuffer() or sampledTexture() to get an
5819 instance.
5820
5821 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5822 for details.
5823 */
5824
5825/*!
5826 \enum QRhiShaderResourceBinding::Type
5827 Specifies type of the shader resource bound to a binding point
5828
5829 \value UniformBuffer Uniform buffer
5830
5831 \value SampledTexture Combined image sampler (a texture and sampler pair).
5832 Even when the shading language associated with the underlying 3D API has no
5833 support for this concept (e.g. D3D and HLSL), this is still supported
5834 because the shader translation layer takes care of the appropriate
5835 translation and remapping of binding points or shader registers.
5836
5837 \value Texture Texture (separate)
5838
5839 \value Sampler Sampler (separate)
5840
5841 \value ImageLoad Image load (with GLSL this maps to doing imageLoad() on a
5842 single level - and either one or all layers - of a texture exposed to the
5843 shader as an image object)
5844
5845 \value ImageStore Image store (with GLSL this maps to doing imageStore() or
5846 imageAtomic*() on a single level - and either one or all layers - of a
5847 texture exposed to the shader as an image object)
5848
5849 \value ImageLoadStore Image load and store
5850
5851 \value BufferLoad Storage buffer load (with GLSL this maps to reading from
5852 a shader storage buffer)
5853
5854 \value BufferStore Storage buffer store (with GLSL this maps to writing to
5855 a shader storage buffer)
5856
5857 \value BufferLoadStore Storage buffer load and store
5858 */
5859
5860/*!
5861 \enum QRhiShaderResourceBinding::StageFlag
5862 Flag values to indicate which stages the shader resource is visible in
5863
5864 \value VertexStage Vertex stage
5865 \value TessellationControlStage Tessellation control (hull shader) stage
5866 \value TessellationEvaluationStage Tessellation evaluation (domain shader) stage
5867 \value FragmentStage Fragment (pixel shader) stage
5868 \value ComputeStage Compute stage
5869 \value GeometryStage Geometry stage
5870 */
5871
5872/*!
5873 \return \c true if the layout is compatible with \a other. The layout does not
5874 include the actual resource (such as, buffer or texture) and related
5875 parameters (such as, offset or size).
5876
5877 For example, \c a and \c b below are not equal, but are compatible layout-wise:
5878
5879 \code
5880 auto a = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, buffer);
5881 auto b = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, someOtherBuffer, 256);
5882 \endcode
5883 */
5884bool QRhiShaderResourceBinding::isLayoutCompatible(const QRhiShaderResourceBinding &other) const
5885{
5886 // everything that goes into a VkDescriptorSetLayoutBinding must match
5887 return d.binding == other.d.binding
5888 && d.stage == other.d.stage
5889 && d.type == other.d.type
5890 && d.arraySize() == other.d.arraySize();
5891}
5892
5893/*!
5894 \return a shader resource binding for the given binding number, pipeline
5895 stages, and buffer specified by \a binding, \a stage, and \a buf.
5896
5897 \note When \a buf is not null, it must have been created with
5898 QRhiBuffer::UniformBuffer.
5899
5900 \note \a buf can be null. It is valid to create a
5901 QRhiShaderResourceBindings with unspecified resources, but such an object
5902 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5903 suitable for creating pipelines. Such a pipeline must then always be used
5904 together with another, layout compatible QRhiShaderResourceBindings with
5905 resources present passed to QRhiCommandBuffer::setShaderResources().
5906
5907 \note If the size of \a buf exceeds the limit reported for
5908 QRhi::MaxUniformBufferRange, unexpected errors may occur.
5909 */
5910QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBuffer(
5911 int binding, StageFlags stage, QRhiBuffer *buf)
5912{
5913 QRhiShaderResourceBinding b;
5914 b.d.binding = binding;
5915 b.d.stage = stage;
5916 b.d.type = UniformBuffer;
5917 b.d.u.ubuf.buf = buf;
5918 b.d.u.ubuf.offset = 0;
5919 b.d.u.ubuf.maybeSize = 0; // entire buffer
5920 b.d.u.ubuf.hasDynamicOffset = false;
5921 return b;
5922}
5923
5924/*!
5925 \return a shader resource binding for the given binding number, pipeline
5926 stages, and buffer specified by \a binding, \a stage, and \a buf. This
5927 overload binds a region only, as specified by \a offset and \a size.
5928
5929 \note It is up to the user to ensure the offset is aligned to
5930 QRhi::ubufAlignment().
5931
5932 \note \a size must be greater than 0.
5933
5934 \note When \a buf is not null, it must have been created with
5935 QRhiBuffer::UniformBuffer.
5936
5937 \note \a buf can be null. It is valid to create a
5938 QRhiShaderResourceBindings with unspecified resources, but such an object
5939 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5940 suitable for creating pipelines. Such a pipeline must then always be used
5941 together with another, layout compatible QRhiShaderResourceBindings with
5942 resources present passed to QRhiCommandBuffer::setShaderResources().
5943
5944 \note If \a size exceeds the limit reported for QRhi::MaxUniformBufferRange,
5945 unexpected errors may occur.
5946 */
5947QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBuffer(
5948 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
5949{
5950 Q_ASSERT(size > 0);
5951 QRhiShaderResourceBinding b;
5952 b.d.binding = binding;
5953 b.d.stage = stage;
5954 b.d.type = UniformBuffer;
5955 b.d.u.ubuf.buf = buf;
5956 b.d.u.ubuf.offset = offset;
5957 b.d.u.ubuf.maybeSize = size;
5958 b.d.u.ubuf.hasDynamicOffset = false;
5959 return b;
5960}
5961
5962/*!
5963 \return a shader resource binding for the given binding number, pipeline
5964 stages, and buffer specified by \a binding, \a stage, and \a buf. The
5965 uniform buffer is assumed to have dynamic offset. The dynamic offset can be
5966 specified in QRhiCommandBuffer::setShaderResources(), thus allowing using
5967 varying offset values without creating new bindings for the buffer. The
5968 size of the bound region is specified by \a size. Like with non-dynamic
5969 offsets, \c{offset + size} cannot exceed the size of \a buf.
5970
5971 \note When \a buf is not null, it must have been created with
5972 QRhiBuffer::UniformBuffer.
5973
5974 \note \a buf can be null. It is valid to create a
5975 QRhiShaderResourceBindings with unspecified resources, but such an object
5976 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5977 suitable for creating pipelines. Such a pipeline must then always be used
5978 together with another, layout compatible QRhiShaderResourceBindings with
5979 resources present passed to QRhiCommandBuffer::setShaderResources().
5980
5981 \note If \a size exceeds the limit reported for QRhi::MaxUniformBufferRange,
5982 unexpected errors may occur.
5983 */
5984QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(
5985 int binding, StageFlags stage, QRhiBuffer *buf, quint32 size)
5986{
5987 Q_ASSERT(size > 0);
5988 QRhiShaderResourceBinding b;
5989 b.d.binding = binding;
5990 b.d.stage = stage;
5991 b.d.type = UniformBuffer;
5992 b.d.u.ubuf.buf = buf;
5993 b.d.u.ubuf.offset = 0;
5994 b.d.u.ubuf.maybeSize = size;
5995 b.d.u.ubuf.hasDynamicOffset = true;
5996 return b;
5997}
5998
5999/*!
6000 \return a shader resource binding for the given binding number, pipeline
6001 stages, texture, and sampler specified by \a binding, \a stage, \a tex,
6002 \a sampler.
6003
6004 \note This function is equivalent to calling sampledTextures() with a
6005 \c count of 1.
6006
6007 \note \a tex and \a sampler can be null. It is valid to create a
6008 QRhiShaderResourceBindings with unspecified resources, but such an object
6009 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6010 suitable for creating pipelines. Such a pipeline must then always be used
6011 together with another, layout compatible QRhiShaderResourceBindings with
6012 resources present passed to QRhiCommandBuffer::setShaderResources().
6013
6014 \note A shader may not be able to consume more than 16 textures/samplers,
6015 depending on the underlying graphics API. This hard limit must be kept in
6016 mind in renderer design. This does not apply to texture arrays which
6017 consume a single binding point (shader register) and can contain 256-2048
6018 textures, depending on the underlying graphics API. Arrays of textures (see
6019 sampledTextures()) are however no different in this regard than using the
6020 same number of individual textures.
6021
6022 \sa sampledTextures()
6023 */
6024QRhiShaderResourceBinding QRhiShaderResourceBinding::sampledTexture(
6025 int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler)
6026{
6027 QRhiShaderResourceBinding b;
6028 b.d.binding = binding;
6029 b.d.stage = stage;
6030 b.d.type = SampledTexture;
6031 b.d.u.stex.count = 1;
6032 b.d.u.stex.texSamplers[0] = { tex, sampler };
6033 return b;
6034}
6035
6036/*!
6037 \return a shader resource binding for the given binding number, pipeline
6038 stages, and the array of texture-sampler pairs specified by \a binding, \a
6039 stage, \a count, and \a texSamplers.
6040
6041 \note \a count must be at least 1, and not larger than 16.
6042
6043 \note When \a count is 1, this function is equivalent to sampledTexture().
6044
6045 This function is relevant when arrays of combined image samplers are
6046 involved. For example, in GLSL \c{layout(binding = 5) uniform sampler2D
6047 shadowMaps[8];} declares an array of combined image samplers. The
6048 application is then expected provide a QRhiShaderResourceBinding for
6049 binding point 5, set up by calling this function with \a count set to 8 and
6050 a valid texture and sampler for each element of the array.
6051
6052 \warning All elements of the array must be specified. With the above
6053 example, the only valid, portable approach is calling this function with a
6054 \a count of 8. Additionally, all QRhiTexture and QRhiSampler instances must
6055 be valid, meaning nullptr is not an accepted value. This is due to some of
6056 the underlying APIs, such as, Vulkan, that require a valid image and
6057 sampler object for each element in descriptor arrays. Applications are
6058 advised to provide "dummy" samplers and textures if some array elements are
6059 not relevant (due to not being accessed in the shader).
6060
6061 \note \a texSamplers can be null. It is valid to create a
6062 QRhiShaderResourceBindings with unspecified resources, but such an object
6063 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6064 suitable for creating pipelines. Such a pipeline must then always be used
6065 together with another, layout compatible QRhiShaderResourceBindings with
6066 resources present passed to QRhiCommandBuffer::setShaderResources().
6067
6068 \sa sampledTexture()
6069 */
6070QRhiShaderResourceBinding QRhiShaderResourceBinding::sampledTextures(
6071 int binding, StageFlags stage, int count, const TextureAndSampler *texSamplers)
6072{
6073 Q_ASSERT(count >= 1 && count <= Data::MAX_TEX_SAMPLER_ARRAY_SIZE);
6074 QRhiShaderResourceBinding b;
6075 b.d.binding = binding;
6076 b.d.stage = stage;
6077 b.d.type = SampledTexture;
6078 b.d.u.stex.count = count;
6079 for (int i = 0; i < count; ++i) {
6080 if (texSamplers)
6081 b.d.u.stex.texSamplers[i] = texSamplers[i];
6082 else
6083 b.d.u.stex.texSamplers[i] = { nullptr, nullptr };
6084 }
6085 return b;
6086}
6087
6088/*!
6089 \return a shader resource binding for the given binding number, pipeline
6090 stages, and texture specified by \a binding, \a stage, \a tex.
6091
6092 \note This function is equivalent to calling textures() with a
6093 \c count of 1.
6094
6095 \note \a tex can be null. It is valid to create a
6096 QRhiShaderResourceBindings with unspecified resources, but such an object
6097 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6098 suitable for creating pipelines. Such a pipeline must then always be used
6099 together with another, layout compatible QRhiShaderResourceBindings with
6100 resources present passed to QRhiCommandBuffer::setShaderResources().
6101
6102 This creates a binding for a separate texture (image) object, whereas
6103 sampledTexture() is suitable for combined image samplers. In
6104 Vulkan-compatible GLSL code separate textures are declared as \c texture2D
6105 as opposed to \c sampler2D: \c{layout(binding = 1) uniform texture2D tex;}
6106
6107 \note A shader may not be able to consume more than 16 textures, depending
6108 on the underlying graphics API. This hard limit must be kept in mind in
6109 renderer design. This does not apply to texture arrays which consume a
6110 single binding point (shader register) and can contain 256-2048 textures,
6111 depending on the underlying graphics API. Arrays of textures (see
6112 sampledTextures()) are however no different in this regard than using the
6113 same number of individual textures.
6114
6115 \sa textures(), sampler()
6116 */
6117QRhiShaderResourceBinding QRhiShaderResourceBinding::texture(int binding, StageFlags stage, QRhiTexture *tex)
6118{
6119 QRhiShaderResourceBinding b;
6120 b.d.binding = binding;
6121 b.d.stage = stage;
6122 b.d.type = Texture;
6123 b.d.u.stex.count = 1;
6124 b.d.u.stex.texSamplers[0] = { tex, nullptr };
6125 return b;
6126}
6127
6128/*!
6129 \return a shader resource binding for the given binding number, pipeline
6130 stages, and the array of (separate) textures specified by \a binding, \a
6131 stage, \a count, and \a tex.
6132
6133 \note \a count must be at least 1, and not larger than 16.
6134
6135 \note When \a count is 1, this function is equivalent to texture().
6136
6137 \warning All elements of the array must be specified.
6138
6139 \note \a tex can be null. It is valid to create a
6140 QRhiShaderResourceBindings with unspecified resources, but such an object
6141 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6142 suitable for creating pipelines. Such a pipeline must then always be used
6143 together with another, layout compatible QRhiShaderResourceBindings with
6144 resources present passed to QRhiCommandBuffer::setShaderResources().
6145
6146 \sa texture(), sampler()
6147 */
6148QRhiShaderResourceBinding QRhiShaderResourceBinding::textures(int binding, StageFlags stage, int count, QRhiTexture **tex)
6149{
6150 Q_ASSERT(count >= 1 && count <= Data::MAX_TEX_SAMPLER_ARRAY_SIZE);
6151 QRhiShaderResourceBinding b;
6152 b.d.binding = binding;
6153 b.d.stage = stage;
6154 b.d.type = Texture;
6155 b.d.u.stex.count = count;
6156 for (int i = 0; i < count; ++i) {
6157 if (tex)
6158 b.d.u.stex.texSamplers[i] = { tex[i], nullptr };
6159 else
6160 b.d.u.stex.texSamplers[i] = { nullptr, nullptr };
6161 }
6162 return b;
6163}
6164
6165/*!
6166 \return a shader resource binding for the given binding number, pipeline
6167 stages, and sampler specified by \a binding, \a stage, \a sampler.
6168
6169 \note \a sampler can be null. It is valid to create a
6170 QRhiShaderResourceBindings with unspecified resources, but such an object
6171 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6172 suitable for creating pipelines. Such a pipeline must then always be used
6173 together with another, layout compatible QRhiShaderResourceBindings with
6174 resources present passed to QRhiCommandBuffer::setShaderResources().
6175
6176 Arrays of separate samplers are not supported.
6177
6178 This creates a binding for a separate sampler object, whereas
6179 sampledTexture() is suitable for combined image samplers. In
6180 Vulkan-compatible GLSL code separate samplers are declared as \c sampler
6181 as opposed to \c sampler2D: \c{layout(binding = 2) uniform sampler samp;}
6182
6183 With both a \c texture2D and \c sampler present, they can be used together
6184 to sample the texture: \c{fragColor = texture(sampler2D(tex, samp),
6185 texcoord);}.
6186
6187 \note A shader may not be able to consume more than 16 samplers, depending
6188 on the underlying graphics API. This hard limit must be kept in mind in
6189 renderer design.
6190
6191 \sa texture()
6192 */
6193QRhiShaderResourceBinding QRhiShaderResourceBinding::sampler(int binding, StageFlags stage, QRhiSampler *sampler)
6194{
6195 QRhiShaderResourceBinding b;
6196 b.d.binding = binding;
6197 b.d.stage = stage;
6198 b.d.type = Sampler;
6199 b.d.u.stex.count = 1;
6200 b.d.u.stex.texSamplers[0] = { nullptr, sampler };
6201 return b;
6202}
6203
6204/*!
6205 \return a shader resource binding for a read-only storage image with the
6206 given \a binding number and pipeline \a stage. The image load operations
6207 will have access to all layers of the specified \a level. (so if the texture
6208 is a cubemap, the shader must use imageCube instead of image2D)
6209
6210 \note When \a tex is not null, it must have been created with
6211 QRhiTexture::UsedWithLoadStore.
6212
6213 \note \a tex can be null. It is valid to create a QRhiShaderResourceBindings
6214 with unspecified resources, but such an object cannot be used with
6215 QRhiCommandBuffer::setShaderResources(). It is however suitable for creating
6216 pipelines. Such a pipeline must then always be used together with another,
6217 layout compatible QRhiShaderResourceBindings with resources present passed
6218 to QRhiCommandBuffer::setShaderResources().
6219
6220 \note Image load/store is only available within the compute and fragment stages.
6221 */
6222QRhiShaderResourceBinding QRhiShaderResourceBinding::imageLoad(
6223 int binding, StageFlags stage, QRhiTexture *tex, int level)
6224{
6225 QRhiShaderResourceBinding b;
6226 b.d.binding = binding;
6227 b.d.stage = stage;
6228 b.d.type = ImageLoad;
6229 b.d.u.simage.tex = tex;
6230 b.d.u.simage.level = level;
6231 return b;
6232}
6233
6234/*!
6235 \return a shader resource binding for a write-only storage image with the
6236 given \a binding number and pipeline \a stage. The image store operations
6237 will have access to all layers of the specified \a level. (so if the texture
6238 is a cubemap, the shader must use imageCube instead of image2D)
6239
6240 \note When \a tex is not null, it must have been created with
6241 QRhiTexture::UsedWithLoadStore.
6242
6243 \note \a tex can be null. It is valid to create a QRhiShaderResourceBindings
6244 with unspecified resources, but such an object cannot be used with
6245 QRhiCommandBuffer::setShaderResources(). It is however suitable for creating
6246 pipelines. Such a pipeline must then always be used together with another,
6247 layout compatible QRhiShaderResourceBindings with resources present passed
6248 to QRhiCommandBuffer::setShaderResources().
6249
6250 \note Image load/store is only available within the compute and fragment stages.
6251 */
6252QRhiShaderResourceBinding QRhiShaderResourceBinding::imageStore(
6253 int binding, StageFlags stage, QRhiTexture *tex, int level)
6254{
6255 QRhiShaderResourceBinding b;
6256 b.d.binding = binding;
6257 b.d.stage = stage;
6258 b.d.type = ImageStore;
6259 b.d.u.simage.tex = tex;
6260 b.d.u.simage.level = level;
6261 return b;
6262}
6263
6264/*!
6265 \return a shader resource binding for a read/write storage image with the
6266 given \a binding number and pipeline \a stage. The image load/store operations
6267 will have access to all layers of the specified \a level. (so if the texture
6268 is a cubemap, the shader must use imageCube instead of image2D)
6269
6270 \note When \a tex is not null, it must have been created with
6271 QRhiTexture::UsedWithLoadStore.
6272
6273 \note \a tex can be null. It is valid to create a QRhiShaderResourceBindings
6274 with unspecified resources, but such an object cannot be used with
6275 QRhiCommandBuffer::setShaderResources(). It is however suitable for creating
6276 pipelines. Such a pipeline must then always be used together with another,
6277 layout compatible QRhiShaderResourceBindings with resources present passed
6278 to QRhiCommandBuffer::setShaderResources().
6279
6280 \note Image load/store is only available within the compute and fragment stages.
6281 */
6282QRhiShaderResourceBinding QRhiShaderResourceBinding::imageLoadStore(
6283 int binding, StageFlags stage, QRhiTexture *tex, int level)
6284{
6285 QRhiShaderResourceBinding b;
6286 b.d.binding = binding;
6287 b.d.stage = stage;
6288 b.d.type = ImageLoadStore;
6289 b.d.u.simage.tex = tex;
6290 b.d.u.simage.level = level;
6291 return b;
6292}
6293
6294/*!
6295 \return a shader resource binding for a read-only storage buffer with the
6296 given \a binding number and pipeline \a stage.
6297
6298 \note When \a buf is not null, must have been created with
6299 QRhiBuffer::StorageBuffer.
6300
6301 \note \a buf can be null. It is valid to create a
6302 QRhiShaderResourceBindings with unspecified resources, but such an object
6303 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6304 suitable for creating pipelines. Such a pipeline must then always be used
6305 together with another, layout compatible QRhiShaderResourceBindings with
6306 resources present passed to QRhiCommandBuffer::setShaderResources().
6307
6308 \note Buffer load/store is only guaranteed to be available within a compute
6309 pipeline. While some backends may support using these resources in a
6310 graphics pipeline as well, this is not universally supported, and even when
6311 it is, unexpected problems may arise when it comes to barriers and
6312 synchronization. Therefore, avoid using such resources with shaders other
6313 than compute.
6314 */
6315QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoad(
6316 int binding, StageFlags stage, QRhiBuffer *buf)
6317{
6318 QRhiShaderResourceBinding b;
6319 b.d.binding = binding;
6320 b.d.stage = stage;
6321 b.d.type = BufferLoad;
6322 b.d.u.sbuf.buf = buf;
6323 b.d.u.sbuf.offset = 0;
6324 b.d.u.sbuf.maybeSize = 0; // entire buffer
6325 return b;
6326}
6327
6328/*!
6329 \return a shader resource binding for a read-only storage buffer with the
6330 given \a binding number and pipeline \a stage. This overload binds a region
6331 only, as specified by \a offset and \a size.
6332
6333 \note When \a buf is not null, must have been created with
6334 QRhiBuffer::StorageBuffer.
6335
6336 \note \a buf can be null. It is valid to create a
6337 QRhiShaderResourceBindings with unspecified resources, but such an object
6338 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6339 suitable for creating pipelines. Such a pipeline must then always be used
6340 together with another, layout compatible QRhiShaderResourceBindings with
6341 resources present passed to QRhiCommandBuffer::setShaderResources().
6342
6343 \note Buffer load/store is only guaranteed to be available within a compute
6344 pipeline. While some backends may support using these resources in a
6345 graphics pipeline as well, this is not universally supported, and even when
6346 it is, unexpected problems may arise when it comes to barriers and
6347 synchronization. Therefore, avoid using such resources with shaders other
6348 than compute.
6349 */
6350QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoad(
6351 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
6352{
6353 Q_ASSERT(size > 0);
6354 QRhiShaderResourceBinding b;
6355 b.d.binding = binding;
6356 b.d.stage = stage;
6357 b.d.type = BufferLoad;
6358 b.d.u.sbuf.buf = buf;
6359 b.d.u.sbuf.offset = offset;
6360 b.d.u.sbuf.maybeSize = size;
6361 return b;
6362}
6363
6364/*!
6365 \return a shader resource binding for a write-only storage buffer with the
6366 given \a binding number and pipeline \a stage.
6367
6368 \note When \a buf is not null, must have been created with
6369 QRhiBuffer::StorageBuffer.
6370
6371 \note \a buf can be null. It is valid to create a
6372 QRhiShaderResourceBindings with unspecified resources, but such an object
6373 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6374 suitable for creating pipelines. Such a pipeline must then always be used
6375 together with another, layout compatible QRhiShaderResourceBindings with
6376 resources present passed to QRhiCommandBuffer::setShaderResources().
6377
6378 \note Buffer load/store is only guaranteed to be available within a compute
6379 pipeline. While some backends may support using these resources in a
6380 graphics pipeline as well, this is not universally supported, and even when
6381 it is, unexpected problems may arise when it comes to barriers and
6382 synchronization. Therefore, avoid using such resources with shaders other
6383 than compute.
6384 */
6385QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferStore(
6386 int binding, StageFlags stage, QRhiBuffer *buf)
6387{
6388 QRhiShaderResourceBinding b;
6389 b.d.binding = binding;
6390 b.d.stage = stage;
6391 b.d.type = BufferStore;
6392 b.d.u.sbuf.buf = buf;
6393 b.d.u.sbuf.offset = 0;
6394 b.d.u.sbuf.maybeSize = 0; // entire buffer
6395 return b;
6396}
6397
6398/*!
6399 \return a shader resource binding for a write-only storage buffer with the
6400 given \a binding number and pipeline \a stage. This overload binds a region
6401 only, as specified by \a offset and \a size.
6402
6403 \note When \a buf is not null, must have been created with
6404 QRhiBuffer::StorageBuffer.
6405
6406 \note \a buf can be null. It is valid to create a
6407 QRhiShaderResourceBindings with unspecified resources, but such an object
6408 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6409 suitable for creating pipelines. Such a pipeline must then always be used
6410 together with another, layout compatible QRhiShaderResourceBindings with
6411 resources present passed to QRhiCommandBuffer::setShaderResources().
6412
6413 \note Buffer load/store is only guaranteed to be available within a compute
6414 pipeline. While some backends may support using these resources in a
6415 graphics pipeline as well, this is not universally supported, and even when
6416 it is, unexpected problems may arise when it comes to barriers and
6417 synchronization. Therefore, avoid using such resources with shaders other
6418 than compute.
6419 */
6420QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferStore(
6421 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
6422{
6423 Q_ASSERT(size > 0);
6424 QRhiShaderResourceBinding b;
6425 b.d.binding = binding;
6426 b.d.stage = stage;
6427 b.d.type = BufferStore;
6428 b.d.u.sbuf.buf = buf;
6429 b.d.u.sbuf.offset = offset;
6430 b.d.u.sbuf.maybeSize = size;
6431 return b;
6432}
6433
6434/*!
6435 \return a shader resource binding for a read-write storage buffer with the
6436 given \a binding number and pipeline \a stage.
6437
6438 \note When \a buf is not null, must have been created with
6439 QRhiBuffer::StorageBuffer.
6440
6441 \note \a buf can be null. It is valid to create a
6442 QRhiShaderResourceBindings with unspecified resources, but such an object
6443 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6444 suitable for creating pipelines. Such a pipeline must then always be used
6445 together with another, layout compatible QRhiShaderResourceBindings with
6446 resources present passed to QRhiCommandBuffer::setShaderResources().
6447
6448 \note Buffer load/store is only guaranteed to be available within a compute
6449 pipeline. While some backends may support using these resources in a
6450 graphics pipeline as well, this is not universally supported, and even when
6451 it is, unexpected problems may arise when it comes to barriers and
6452 synchronization. Therefore, avoid using such resources with shaders other
6453 than compute.
6454 */
6455QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoadStore(
6456 int binding, StageFlags stage, QRhiBuffer *buf)
6457{
6458 QRhiShaderResourceBinding b;
6459 b.d.binding = binding;
6460 b.d.stage = stage;
6461 b.d.type = BufferLoadStore;
6462 b.d.u.sbuf.buf = buf;
6463 b.d.u.sbuf.offset = 0;
6464 b.d.u.sbuf.maybeSize = 0; // entire buffer
6465 return b;
6466}
6467
6468/*!
6469 \return a shader resource binding for a read-write storage buffer with the
6470 given \a binding number and pipeline \a stage. This overload binds a region
6471 only, as specified by \a offset and \a size.
6472
6473 \note When \a buf is not null, must have been created with
6474 QRhiBuffer::StorageBuffer.
6475
6476 \note \a buf can be null. It is valid to create a
6477 QRhiShaderResourceBindings with unspecified resources, but such an object
6478 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6479 suitable for creating pipelines. Such a pipeline must then always be used
6480 together with another, layout compatible QRhiShaderResourceBindings with
6481 resources present passed to QRhiCommandBuffer::setShaderResources().
6482
6483 \note Buffer load/store is only guaranteed to be available within a compute
6484 pipeline. While some backends may support using these resources in a
6485 graphics pipeline as well, this is not universally supported, and even when
6486 it is, unexpected problems may arise when it comes to barriers and
6487 synchronization. Therefore, avoid using such resources with shaders other
6488 than compute.
6489 */
6490QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoadStore(
6491 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
6492{
6493 Q_ASSERT(size > 0);
6494 QRhiShaderResourceBinding b;
6495 b.d.binding = binding;
6496 b.d.stage = stage;
6497 b.d.type = BufferLoadStore;
6498 b.d.u.sbuf.buf = buf;
6499 b.d.u.sbuf.offset = offset;
6500 b.d.u.sbuf.maybeSize = size;
6501 return b;
6502}
6503
6504/*!
6505 \return \c true if the contents of the two QRhiShaderResourceBinding
6506 objects \a a and \a b are equal. This includes the resources (buffer,
6507 texture) and related parameters (offset, size) as well. To only compare
6508 layouts (binding point, pipeline stage, resource type), use
6509 \l{QRhiShaderResourceBinding::isLayoutCompatible()}{isLayoutCompatible()}
6510 instead.
6511
6512 \relates QRhiShaderResourceBinding
6513 */
6514bool operator==(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) noexcept
6515{
6516 const QRhiShaderResourceBinding::Data *da = QRhiImplementation::shaderResourceBindingData(a);
6517 const QRhiShaderResourceBinding::Data *db = QRhiImplementation::shaderResourceBindingData(b);
6518
6519 if (da == db)
6520 return true;
6521
6522
6523 if (da->binding != db->binding
6524 || da->stage != db->stage
6525 || da->type != db->type)
6526 {
6527 return false;
6528 }
6529
6530 switch (da->type) {
6531 case QRhiShaderResourceBinding::UniformBuffer:
6532 if (da->u.ubuf.buf != db->u.ubuf.buf
6533 || da->u.ubuf.offset != db->u.ubuf.offset
6534 || da->u.ubuf.maybeSize != db->u.ubuf.maybeSize)
6535 {
6536 return false;
6537 }
6538 break;
6539 case QRhiShaderResourceBinding::SampledTexture:
6540 if (da->u.stex.count != db->u.stex.count)
6541 return false;
6542 for (int i = 0; i < da->u.stex.count; ++i) {
6543 if (da->u.stex.texSamplers[i].tex != db->u.stex.texSamplers[i].tex
6544 || da->u.stex.texSamplers[i].sampler != db->u.stex.texSamplers[i].sampler)
6545 {
6546 return false;
6547 }
6548 }
6549 break;
6550 case QRhiShaderResourceBinding::Texture:
6551 if (da->u.stex.count != db->u.stex.count)
6552 return false;
6553 for (int i = 0; i < da->u.stex.count; ++i) {
6554 if (da->u.stex.texSamplers[i].tex != db->u.stex.texSamplers[i].tex)
6555 return false;
6556 }
6557 break;
6558 case QRhiShaderResourceBinding::Sampler:
6559 if (da->u.stex.texSamplers[0].sampler != db->u.stex.texSamplers[0].sampler)
6560 return false;
6561 break;
6562 case QRhiShaderResourceBinding::ImageLoad:
6563 case QRhiShaderResourceBinding::ImageStore:
6564 case QRhiShaderResourceBinding::ImageLoadStore:
6565 if (da->u.simage.tex != db->u.simage.tex
6566 || da->u.simage.level != db->u.simage.level)
6567 {
6568 return false;
6569 }
6570 break;
6571 case QRhiShaderResourceBinding::BufferLoad:
6572 case QRhiShaderResourceBinding::BufferStore:
6573 case QRhiShaderResourceBinding::BufferLoadStore:
6574 if (da->u.sbuf.buf != db->u.sbuf.buf
6575 || da->u.sbuf.offset != db->u.sbuf.offset
6576 || da->u.sbuf.maybeSize != db->u.sbuf.maybeSize)
6577 {
6578 return false;
6579 }
6580 break;
6581 default:
6582 Q_UNREACHABLE_RETURN(false);
6583 }
6584
6585 return true;
6586}
6587
6588/*!
6589 \return \c false if all the bindings in the two QRhiShaderResourceBinding
6590 objects \a a and \a b are equal; otherwise returns \c true.
6591
6592 \relates QRhiShaderResourceBinding
6593 */
6594bool operator!=(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) noexcept
6595{
6596 return !(a == b);
6597}
6598
6599/*!
6600 \fn size_t qHash(const QRhiShaderResourceBinding &key, size_t seed)
6601 \qhashold{QRhiShaderResourceBinding}
6602 */
6603size_t qHash(const QRhiShaderResourceBinding &b, size_t seed) noexcept
6604{
6605 const QRhiShaderResourceBinding::Data *d = QRhiImplementation::shaderResourceBindingData(b);
6606 QtPrivate::QHashCombineWithSeed hash(seed);
6607 seed = hash(seed, d->binding);
6608 seed = hash(seed, d->stage);
6609 seed = hash(seed, d->type);
6610 switch (d->type) {
6611 case QRhiShaderResourceBinding::UniformBuffer:
6612 seed = hash(seed, reinterpret_cast<quintptr>(d->u.ubuf.buf));
6613 break;
6614 case QRhiShaderResourceBinding::SampledTexture:
6615 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].tex));
6616 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].sampler));
6617 break;
6618 case QRhiShaderResourceBinding::Texture:
6619 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].tex));
6620 break;
6621 case QRhiShaderResourceBinding::Sampler:
6622 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].sampler));
6623 break;
6624 case QRhiShaderResourceBinding::ImageLoad:
6625 case QRhiShaderResourceBinding::ImageStore:
6626 case QRhiShaderResourceBinding::ImageLoadStore:
6627 seed = hash(seed, reinterpret_cast<quintptr>(d->u.simage.tex));
6628 break;
6629 case QRhiShaderResourceBinding::BufferLoad:
6630 case QRhiShaderResourceBinding::BufferStore:
6631 case QRhiShaderResourceBinding::BufferLoadStore:
6632 seed = hash(seed, reinterpret_cast<quintptr>(d->u.sbuf.buf));
6633 break;
6634 }
6635 return seed;
6636}
6637
6638#ifndef QT_NO_DEBUG_STREAM
6639QDebug operator<<(QDebug dbg, const QRhiShaderResourceBinding &b)
6640{
6641 QDebugStateSaver saver(dbg);
6642 const QRhiShaderResourceBinding::Data *d = QRhiImplementation::shaderResourceBindingData(b);
6643 dbg.nospace() << "QRhiShaderResourceBinding("
6644 << "binding=" << d->binding
6645 << " stage=" << d->stage
6646 << " type=" << d->type;
6647 switch (d->type) {
6648 case QRhiShaderResourceBinding::UniformBuffer:
6649 dbg.nospace() << " UniformBuffer("
6650 << "buffer=" << d->u.ubuf.buf
6651 << " offset=" << d->u.ubuf.offset
6652 << " maybeSize=" << d->u.ubuf.maybeSize
6653 << ')';
6654 break;
6655 case QRhiShaderResourceBinding::SampledTexture:
6656 dbg.nospace() << " SampledTextures("
6657 << "count=" << d->u.stex.count;
6658 for (int i = 0; i < d->u.stex.count; ++i) {
6659 dbg.nospace() << " texture=" << d->u.stex.texSamplers[i].tex
6660 << " sampler=" << d->u.stex.texSamplers[i].sampler;
6661 }
6662 dbg.nospace() << ')';
6663 break;
6664 case QRhiShaderResourceBinding::Texture:
6665 dbg.nospace() << " Textures("
6666 << "count=" << d->u.stex.count;
6667 for (int i = 0; i < d->u.stex.count; ++i)
6668 dbg.nospace() << " texture=" << d->u.stex.texSamplers[i].tex;
6669 dbg.nospace() << ')';
6670 break;
6671 case QRhiShaderResourceBinding::Sampler:
6672 dbg.nospace() << " Sampler("
6673 << " sampler=" << d->u.stex.texSamplers[0].sampler
6674 << ')';
6675 break;
6676 case QRhiShaderResourceBinding::ImageLoad:
6677 dbg.nospace() << " ImageLoad("
6678 << "texture=" << d->u.simage.tex
6679 << " level=" << d->u.simage.level
6680 << ')';
6681 break;
6682 case QRhiShaderResourceBinding::ImageStore:
6683 dbg.nospace() << " ImageStore("
6684 << "texture=" << d->u.simage.tex
6685 << " level=" << d->u.simage.level
6686 << ')';
6687 break;
6688 case QRhiShaderResourceBinding::ImageLoadStore:
6689 dbg.nospace() << " ImageLoadStore("
6690 << "texture=" << d->u.simage.tex
6691 << " level=" << d->u.simage.level
6692 << ')';
6693 break;
6694 case QRhiShaderResourceBinding::BufferLoad:
6695 dbg.nospace() << " BufferLoad("
6696 << "buffer=" << d->u.sbuf.buf
6697 << " offset=" << d->u.sbuf.offset
6698 << " maybeSize=" << d->u.sbuf.maybeSize
6699 << ')';
6700 break;
6701 case QRhiShaderResourceBinding::BufferStore:
6702 dbg.nospace() << " BufferStore("
6703 << "buffer=" << d->u.sbuf.buf
6704 << " offset=" << d->u.sbuf.offset
6705 << " maybeSize=" << d->u.sbuf.maybeSize
6706 << ')';
6707 break;
6708 case QRhiShaderResourceBinding::BufferLoadStore:
6709 dbg.nospace() << " BufferLoadStore("
6710 << "buffer=" << d->u.sbuf.buf
6711 << " offset=" << d->u.sbuf.offset
6712 << " maybeSize=" << d->u.sbuf.maybeSize
6713 << ')';
6714 break;
6715 default:
6716 dbg.nospace() << " UNKNOWN()";
6717 break;
6718 }
6719 dbg.nospace() << ')';
6720 return dbg;
6721}
6722#endif
6723
6724#ifndef QT_NO_DEBUG_STREAM
6725QDebug operator<<(QDebug dbg, const QRhiShaderResourceBindings &srb)
6726{
6727 QDebugStateSaver saver(dbg);
6728 dbg.nospace() << "QRhiShaderResourceBindings("
6729 << srb.m_bindings
6730 << ')';
6731 return dbg;
6732}
6733#endif
6734
6735/*!
6736 \class QRhiGraphicsPipeline
6737 \inmodule QtGuiPrivate
6738 \inheaderfile rhi/qrhi.h
6739 \since 6.6
6740 \brief Graphics pipeline state resource.
6741
6742 Represents a graphics pipeline. What exactly this map to in the underlying
6743 native graphics API, varies. Where there is a concept of pipeline objects,
6744 for example with Vulkan, the QRhi backend will create such an object upon
6745 calling create(). Elsewhere, for example with OpenGL, the
6746 QRhiGraphicsPipeline may merely collect the various state, and create()'s
6747 main task is to set up the corresponding shader program, but deferring
6748 looking at any of the requested state to a later point.
6749
6750 As with all QRhiResource subclasses, the two-phased initialization pattern
6751 applies: setting any values via the setters, for example setDepthTest(), is
6752 only effective after calling create(). Avoid changing any values once the
6753 QRhiGraphicsPipeline has been initialized via create(). To change some
6754 state, set the new value and call create() again. However, that will
6755 effectively release all underlying native resources and create new ones. As
6756 a result, it may be a heavy, expensive operation. Rather, prefer creating
6757 multiple pipelines with the different states, and
6758 \l{QRhiCommandBuffer::setGraphicsPipeline()}{switch between them} when
6759 recording the render pass.
6760
6761 \note Setting the shader stages is mandatory. There must be at least one
6762 stage, and there must be a vertex stage.
6763
6764 \note Setting the shader resource bindings is mandatory. The referenced
6765 QRhiShaderResourceBindings must already have create() called on it by the
6766 time create() is called. Associating with a QRhiShaderResourceBindings that
6767 has no bindings is also valid, as long as no shader in any stage expects any
6768 resources. Using a QRhiShaderResourceBindings object that does not specify
6769 any actual resources (i.e., the buffers, textures, etc. for the binding
6770 points are set to \nullptr) is valid as well, as long as a
6771 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible}
6772 QRhiShaderResourceBindings, that specifies resources for all the bindings,
6773 is going to be set via
6774 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()} when
6775 recording the render pass.
6776
6777 \note Setting the render pass descriptor is mandatory. To obtain a
6778 QRhiRenderPassDescriptor that can be passed to setRenderPassDescriptor(),
6779 use either QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor() or
6780 QRhiSwapChain::newCompatibleRenderPassDescriptor().
6781
6782 \note Setting the vertex input layout is mandatory.
6783
6784 \note sampleCount() defaults to 1 and must match the sample count of the
6785 render target's color and depth stencil attachments.
6786
6787 \note The depth test, depth write, and stencil test are disabled by
6788 default. The face culling mode defaults to no culling.
6789
6790 \note stencilReadMask() and stencilWriteMask() apply to both faces. They
6791 both default to 0xFF.
6792
6793 \section2 Example usage
6794
6795 All settings of a graphics pipeline have defaults which might be suitable
6796 to many applications. Therefore a minimal example of creating a graphics
6797 pipeline could be the following. This assumes that the vertex shader takes
6798 a single \c{vec3 position} input at the input location 0. With the
6799 QRhiShaderResourceBindings and QRhiRenderPassDescriptor objects, plus the
6800 QShader collections for the vertex and fragment stages, a pipeline could be
6801 created like this:
6802
6803 \code
6804 QRhiShaderResourceBindings *srb;
6805 QRhiRenderPassDescriptor *rpDesc;
6806 QShader vs, fs;
6807 // ...
6808
6809 QRhiVertexInputLayout inputLayout;
6810 inputLayout.setBindings({ { 3 * sizeof(float) } });
6811 inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 } });
6812
6813 QRhiGraphicsPipeline *ps = rhi->newGraphicsPipeline();
6814 ps->setShaderStages({ { QRhiShaderStage::Vertex, vs }, { QRhiShaderStage::Fragment, fs } });
6815 ps->setVertexInputLayout(inputLayout);
6816 ps->setShaderResourceBindings(srb);
6817 ps->setRenderPassDescriptor(rpDesc);
6818 if (!ps->create()) { error(); }
6819 \endcode
6820
6821 The above code creates a pipeline object that uses the defaults for many
6822 settings and states. For example, it will use a \l Triangles topology, no
6823 backface culling, blending is disabled but color write is enabled for all
6824 four channels, depth test/write are disabled, stencil operations are
6825 disabled.
6826
6827 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
6828 for details.
6829
6830 \sa QRhiCommandBuffer, QRhi
6831 */
6832
6833/*!
6834 \enum QRhiGraphicsPipeline::Flag
6835
6836 Flag values for describing the dynamic state of the pipeline, and other
6837 options. The viewport is always dynamic.
6838
6839 \value UsesBlendConstants Indicates that a blend color constant will be set
6840 via QRhiCommandBuffer::setBlendConstants()
6841
6842 \value UsesStencilRef Indicates that a stencil reference value will be set
6843 via QRhiCommandBuffer::setStencilRef()
6844
6845 \value UsesScissor Indicates that a scissor rectangle will be set via
6846 QRhiCommandBuffer::setScissor()
6847
6848 \value CompileShadersWithDebugInfo Requests compiling shaders with debug
6849 information enabled. This is relevant only when runtime shader compilation
6850 from source code is involved, and only when the underlying infrastructure
6851 supports this. With concrete examples, this is not relevant with Vulkan and
6852 SPIR-V, because the GLSL-to-SPIR-V compilation does not happen at run
6853 time. On the other hand, consider Direct3D and HLSL, where there are
6854 multiple options: when the QShader packages ship with pre-compiled bytecode
6855 (\c DXBC), debug information is to be requested through the tool that
6856 generates the \c{.qsb} file, similarly to the case of Vulkan and
6857 SPIR-V. However, when having HLSL source code in the pre- or
6858 runtime-generated QShader packages, the first phase of compilation (HLSL
6859 source to intermediate format) happens at run time too, with this flag taken
6860 into account. Debug information is relevant in particular with tools like
6861 RenderDoc since it allows seeing the original source code when investigating
6862 the pipeline and when performing vertex or fragment shader debugging.
6863
6864 \value UsesShadingRate Indicates that a per-draw (per-pipeline) shading rate
6865 value will be set via QRhiCommandBuffer::setShadingRate(). Not specifying
6866 this flag and still calling setShadingRate() may lead to varying, unexpected
6867 results depending on the underlying graphics API.
6868 */
6869
6870/*!
6871 \enum QRhiGraphicsPipeline::Topology
6872 Specifies the primitive topology
6873
6874 \value Triangles (default)
6875 \value TriangleStrip
6876 \value TriangleFan (only available if QRhi::TriangleFanTopology is supported)
6877 \value Lines
6878 \value LineStrip
6879 \value Points
6880
6881 \value Patches (only available if QRhi::Tessellation is supported, and
6882 requires the tessellation stages to be present in the pipeline)
6883 */
6884
6885/*!
6886 \enum QRhiGraphicsPipeline::CullMode
6887 Specifies the culling mode
6888
6889 \value None No culling (default)
6890 \value Front Cull front faces
6891 \value Back Cull back faces
6892 */
6893
6894/*!
6895 \enum QRhiGraphicsPipeline::FrontFace
6896 Specifies the front face winding order
6897
6898 \value CCW Counter clockwise (default)
6899 \value CW Clockwise
6900 */
6901
6902/*!
6903 \enum QRhiGraphicsPipeline::ColorMaskComponent
6904 Flag values for specifying the color write mask
6905
6906 \value R
6907 \value G
6908 \value B
6909 \value A
6910 */
6911
6912/*!
6913 \enum QRhiGraphicsPipeline::BlendFactor
6914 Specifies the blend factor
6915
6916 \value Zero
6917 \value One
6918 \value SrcColor
6919 \value OneMinusSrcColor
6920 \value DstColor
6921 \value OneMinusDstColor
6922 \value SrcAlpha
6923 \value OneMinusSrcAlpha
6924 \value DstAlpha
6925 \value OneMinusDstAlpha
6926 \value ConstantColor
6927 \value OneMinusConstantColor
6928 \value ConstantAlpha
6929 \value OneMinusConstantAlpha
6930 \value SrcAlphaSaturate
6931 \value Src1Color
6932 \value OneMinusSrc1Color
6933 \value Src1Alpha
6934 \value OneMinusSrc1Alpha
6935 */
6936
6937/*!
6938 \enum QRhiGraphicsPipeline::BlendOp
6939 Specifies the blend operation
6940
6941 \value Add
6942 \value Subtract
6943 \value ReverseSubtract
6944 \value Min
6945 \value Max
6946 */
6947
6948/*!
6949 \enum QRhiGraphicsPipeline::CompareOp
6950 Specifies the depth or stencil comparison function
6951
6952 \value Never
6953 \value Less (default for depth)
6954 \value Equal
6955 \value LessOrEqual
6956 \value Greater
6957 \value NotEqual
6958 \value GreaterOrEqual
6959 \value Always (default for stencil)
6960 */
6961
6962/*!
6963 \enum QRhiGraphicsPipeline::StencilOp
6964 Specifies the stencil operation
6965
6966 \value StencilZero
6967 \value Keep (default)
6968 \value Replace
6969 \value IncrementAndClamp
6970 \value DecrementAndClamp
6971 \value Invert
6972 \value IncrementAndWrap
6973 \value DecrementAndWrap
6974 */
6975
6976/*!
6977 \enum QRhiGraphicsPipeline::PolygonMode
6978 \brief Specifies the polygon rasterization mode
6979
6980 Polygon Mode (Triangle Fill Mode in Metal, Fill Mode in D3D) specifies
6981 the fill mode used when rasterizing polygons. Polygons may be drawn as
6982 solids (Fill), or as a wire mesh (Line).
6983
6984 Support for non-fill polygon modes is optional and is indicated by the
6985 QRhi::NonFillPolygonMode feature. With OpenGL ES and some Vulkan
6986 implementations the feature will likely be reported as unsupported, which
6987 then means values other than Fill cannot be used.
6988
6989 \value Fill The interior of the polygon is filled (default)
6990 \value Line Boundary edges of the polygon are drawn as line segments.
6991 */
6992
6993/*!
6994 \struct QRhiGraphicsPipeline::TargetBlend
6995 \inmodule QtGuiPrivate
6996 \inheaderfile rhi/qrhi.h
6997 \since 6.6
6998 \brief Describes the blend state for one color attachment.
6999
7000 Defaults to color write enabled, blending disabled. The blend values are
7001 set up for pre-multiplied alpha (One, OneMinusSrcAlpha, One,
7002 OneMinusSrcAlpha) by default. This means that to get the alpha blending
7003 mode Qt Quick uses, it is enough to set the \c enable flag to true while
7004 leaving other values at their defaults.
7005
7006 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
7007 for details.
7008 */
7009
7010/*!
7011 \variable QRhiGraphicsPipeline::TargetBlend::colorWrite
7012 */
7013
7014/*!
7015 \variable QRhiGraphicsPipeline::TargetBlend::enable
7016 */
7017
7018/*!
7019 \variable QRhiGraphicsPipeline::TargetBlend::srcColor
7020 */
7021
7022/*!
7023 \variable QRhiGraphicsPipeline::TargetBlend::dstColor
7024 */
7025
7026/*!
7027 \variable QRhiGraphicsPipeline::TargetBlend::opColor
7028 */
7029
7030/*!
7031 \variable QRhiGraphicsPipeline::TargetBlend::srcAlpha
7032 */
7033
7034/*!
7035 \variable QRhiGraphicsPipeline::TargetBlend::dstAlpha
7036 */
7037
7038/*!
7039 \variable QRhiGraphicsPipeline::TargetBlend::opAlpha
7040 */
7041
7042/*!
7043 \struct QRhiGraphicsPipeline::StencilOpState
7044 \inmodule QtGuiPrivate
7045 \inheaderfile rhi/qrhi.h
7046 \since 6.6
7047 \brief Describes the stencil operation state.
7048
7049 The default-constructed StencilOpState has the following set:
7050 \list
7051 \li failOp - \l Keep
7052 \li depthFailOp - \l Keep
7053 \li passOp - \l Keep
7054 \li compareOp \l Always
7055 \endlist
7056
7057 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
7058 for details.
7059 */
7060
7061/*!
7062 \variable QRhiGraphicsPipeline::StencilOpState::failOp
7063 */
7064
7065/*!
7066 \variable QRhiGraphicsPipeline::StencilOpState::depthFailOp
7067 */
7068
7069/*!
7070 \variable QRhiGraphicsPipeline::StencilOpState::passOp
7071 */
7072
7073/*!
7074 \variable QRhiGraphicsPipeline::StencilOpState::compareOp
7075 */
7076
7077/*!
7078 \internal
7079 */
7080QRhiGraphicsPipeline::QRhiGraphicsPipeline(QRhiImplementation *rhi)
7081 : QRhiResource(rhi)
7082{
7083}
7084
7085/*!
7086 \return the resource type.
7087 */
7088QRhiResource::Type QRhiGraphicsPipeline::resourceType() const
7089{
7090 return GraphicsPipeline;
7091}
7092
7093/*!
7094 \fn virtual bool QRhiGraphicsPipeline::create() = 0
7095
7096 Creates the corresponding native graphics resources. If there are already
7097 resources present due to an earlier create() with no corresponding
7098 destroy(), then destroy() is called implicitly first.
7099
7100 \return \c true when successful, \c false when a graphics operation failed.
7101 Regardless of the return value, calling destroy() is always safe.
7102
7103 \note This may be, depending on the underlying graphics API, an expensive
7104 operation, especially when shaders get compiled/optimized from source or
7105 from an intermediate bytecode format to the GPU's own instruction set.
7106 Where applicable, the QRhi backend automatically sets up the relevant
7107 non-persistent facilities to accelerate this, for example the Vulkan
7108 backend automatically creates a \c VkPipelineCache to improve data reuse
7109 during the lifetime of the application.
7110
7111 \note Drivers may also employ various persistent (disk-based) caching
7112 strategies for shader and pipeline data, which is hidden to and is outside
7113 of Qt's control. In some cases, depending on the graphics API and the QRhi
7114 backend, there are facilities within QRhi for manually managing such a
7115 cache, allowing the retrieval of a serializable blob that can then be
7116 reloaded in the future runs of the application to ensure faster pipeline
7117 creation times. See QRhi::pipelineCacheData() and
7118 QRhi::setPipelineCacheData() for details. Note also that when working with
7119 a QRhi instance managed by a higher level Qt framework, such as Qt Quick,
7120 it is possible that such disk-based caching is taken care of automatically,
7121 for example QQuickWindow uses a disk-based pipeline cache by default (which
7122 comes in addition to any driver-level caching).
7123 */
7124
7125/*!
7126 \fn QRhiGraphicsPipeline::Flags QRhiGraphicsPipeline::flags() const
7127 \return the currently set flags.
7128 */
7129
7130/*!
7131 \fn void QRhiGraphicsPipeline::setFlags(Flags f)
7132 Sets the flags \a f.
7133 */
7134
7135/*!
7136 \fn QRhiGraphicsPipeline::Topology QRhiGraphicsPipeline::topology() const
7137 \return the currently set primitive topology.
7138 */
7139
7140/*!
7141 \fn void QRhiGraphicsPipeline::setTopology(Topology t)
7142 Sets the primitive topology \a t.
7143 */
7144
7145/*!
7146 \fn QRhiGraphicsPipeline::CullMode QRhiGraphicsPipeline::cullMode() const
7147 \return the currently set face culling mode.
7148 */
7149
7150/*!
7151 \fn void QRhiGraphicsPipeline::setCullMode(CullMode mode)
7152 Sets the specified face culling \a mode.
7153 */
7154
7155/*!
7156 \fn QRhiGraphicsPipeline::FrontFace QRhiGraphicsPipeline::frontFace() const
7157 \return the currently set front face mode.
7158 */
7159
7160/*!
7161 \fn void QRhiGraphicsPipeline::setFrontFace(FrontFace f)
7162 Sets the front face mode \a f.
7163 */
7164
7165/*!
7166 \fn void QRhiGraphicsPipeline::setTargetBlends(std::initializer_list<TargetBlend> list)
7167
7168 Sets the \a list of render target blend settings. This is a list because
7169 when multiple render targets are used (i.e., a QRhiTextureRenderTarget with
7170 more than one QRhiColorAttachment), there needs to be a TargetBlend
7171 structure per render target (color attachment).
7172
7173 By default there is one default-constructed TargetBlend set.
7174
7175 \sa QRhi::MaxColorAttachments
7176 */
7177
7178/*!
7179 \fn template<typename InputIterator> void QRhiGraphicsPipeline::setTargetBlends(InputIterator first, InputIterator last)
7180 Sets the list of render target blend settings from the iterators \a first and \a last.
7181 */
7182
7183/*!
7184 \fn const QRhiGraphicsPipeline::TargetBlend *QRhiGraphicsPipeline::cbeginTargetBlends() const
7185 \return a const iterator pointing to the first item in the render target blend setting list.
7186 */
7187
7188/*!
7189 \fn const QRhiGraphicsPipeline::TargetBlend *QRhiGraphicsPipeline::cendTargetBlends() const
7190 \return a const iterator pointing just after the last item in the render target blend setting list.
7191 */
7192
7193/*!
7194 \fn const QRhiGraphicsPipeline::TargetBlend *QRhiGraphicsPipeline::targetBlendAt(qsizetype index) const
7195 \return the render target blend setting at the specified \a index.
7196 */
7197
7198/*!
7199 \fn qsizetype QRhiGraphicsPipeline::targetBlendCount() const
7200 \return the number of render target blend settings.
7201 */
7202
7203/*!
7204 \fn bool QRhiGraphicsPipeline::hasDepthTest() const
7205 \return true if depth testing is enabled.
7206 */
7207
7208/*!
7209 \fn void QRhiGraphicsPipeline::setDepthTest(bool enable)
7210
7211 Enables or disables depth testing based on \a enable. Both depth test and
7212 the writing out of depth data are disabled by default.
7213
7214 \sa setDepthWrite()
7215 */
7216
7217/*!
7218 \fn bool QRhiGraphicsPipeline::hasDepthWrite() const
7219 \return true if depth write is enabled.
7220 */
7221
7222/*!
7223 \fn void QRhiGraphicsPipeline::setDepthWrite(bool enable)
7224
7225 Controls the writing out of depth data into the depth buffer based on
7226 \a enable. By default this is disabled. Depth write is typically enabled
7227 together with the depth test.
7228
7229 \note Enabling depth write without having depth testing enabled may not
7230 lead to the desired result, and should be avoided.
7231
7232 \sa setDepthTest()
7233 */
7234
7235/*!
7236 \fn bool QRhiGraphicsPipeline::hasDepthClamp() const
7237 \return true if depth clamp is enabled.
7238
7239 \since 6.11
7240 */
7241
7242/*!
7243 \fn void QRhiGraphicsPipeline::setDepthClamp(bool enable)
7244
7245 Enables depth clamping when \a enable is true. When depth clamping is
7246 enabled, primitives that would otherwise be clipped by the near or far
7247 clip plane are rasterized and their depth values are clamped to the
7248 depth range. When disabled (the default), such primitives are clipped.
7249
7250 \note This setting is ignored when the QRhi::DepthClamp feature is
7251 reported as unsupported.
7252
7253 \since 6.11
7254 */
7255
7256/*!
7257 \fn QRhiGraphicsPipeline::CompareOp QRhiGraphicsPipeline::depthOp() const
7258 \return the depth comparison function.
7259 */
7260
7261/*!
7262 \fn void QRhiGraphicsPipeline::setDepthOp(CompareOp op)
7263 Sets the depth comparison function \a op.
7264 */
7265
7266/*!
7267 \fn bool QRhiGraphicsPipeline::hasStencilTest() const
7268 \return true if stencil testing is enabled.
7269 */
7270
7271/*!
7272 \fn void QRhiGraphicsPipeline::setStencilTest(bool enable)
7273 Enables or disables stencil tests based on \a enable.
7274 By default this is disabled.
7275 */
7276
7277/*!
7278 \fn QRhiGraphicsPipeline::StencilOpState QRhiGraphicsPipeline::stencilFront() const
7279 \return the current stencil test state for front faces.
7280 */
7281
7282/*!
7283 \fn void QRhiGraphicsPipeline::setStencilFront(const StencilOpState &state)
7284 Sets the stencil test \a state for front faces.
7285 */
7286
7287/*!
7288 \fn QRhiGraphicsPipeline::StencilOpState QRhiGraphicsPipeline::stencilBack() const
7289 \return the current stencil test state for back faces.
7290 */
7291
7292/*!
7293 \fn void QRhiGraphicsPipeline::setStencilBack(const StencilOpState &state)
7294 Sets the stencil test \a state for back faces.
7295 */
7296
7297/*!
7298 \fn quint32 QRhiGraphicsPipeline::stencilReadMask() const
7299 \return the currrent stencil read mask.
7300 */
7301
7302/*!
7303 \fn void QRhiGraphicsPipeline::setStencilReadMask(quint32 mask)
7304 Sets the stencil read \a mask. The default value is 0xFF.
7305 */
7306
7307/*!
7308 \fn quint32 QRhiGraphicsPipeline::stencilWriteMask() const
7309 \return the current stencil write mask.
7310 */
7311
7312/*!
7313 \fn void QRhiGraphicsPipeline::setStencilWriteMask(quint32 mask)
7314 Sets the stencil write \a mask. The default value is 0xFF.
7315 */
7316
7317/*!
7318 \fn int QRhiGraphicsPipeline::sampleCount() const
7319 \return the currently set sample count. 1 means no multisample antialiasing.
7320 */
7321
7322/*!
7323 \fn void QRhiGraphicsPipeline::setSampleCount(int s)
7324
7325 Sets the sample count. Typical values for \a s are 1, 4, or 8. The pipeline
7326 must always be compatible with the render target, i.e. the sample counts
7327 must match.
7328
7329 \sa QRhi::supportedSampleCounts()
7330 */
7331
7332/*!
7333 \fn float QRhiGraphicsPipeline::lineWidth() const
7334 \return the currently set line width. The default is 1.0f.
7335 */
7336
7337/*!
7338 \fn void QRhiGraphicsPipeline::setLineWidth(float width)
7339
7340 Sets the line \a width. If the QRhi::WideLines feature is reported as
7341 unsupported at runtime, values other than 1.0f are ignored.
7342 */
7343
7344/*!
7345 \fn int QRhiGraphicsPipeline::depthBias() const
7346 \return the currently set depth bias.
7347 */
7348
7349/*!
7350 \fn void QRhiGraphicsPipeline::setDepthBias(int bias)
7351 Sets the depth \a bias. The default value is 0.
7352 */
7353
7354/*!
7355 \fn float QRhiGraphicsPipeline::slopeScaledDepthBias() const
7356 \return the currently set slope scaled depth bias.
7357 */
7358
7359/*!
7360 \fn void QRhiGraphicsPipeline::setSlopeScaledDepthBias(float bias)
7361 Sets the slope scaled depth \a bias. The default value is 0.
7362 */
7363
7364/*!
7365 \fn void QRhiGraphicsPipeline::setShaderStages(std::initializer_list<QRhiShaderStage> list)
7366 Sets the \a list of shader stages.
7367 */
7368
7369/*!
7370 \fn template<typename InputIterator> void QRhiGraphicsPipeline::setShaderStages(InputIterator first, InputIterator last)
7371 Sets the list of shader stages from the iterators \a first and \a last.
7372 */
7373
7374/*!
7375 \fn const QRhiShaderStage *QRhiGraphicsPipeline::cbeginShaderStages() const
7376 \return a const iterator pointing to the first item in the shader stage list.
7377 */
7378
7379/*!
7380 \fn const QRhiShaderStage *QRhiGraphicsPipeline::cendShaderStages() const
7381 \return a const iterator pointing just after the last item in the shader stage list.
7382 */
7383
7384/*!
7385 \fn const QRhiShaderStage *QRhiGraphicsPipeline::shaderStageAt(qsizetype index) const
7386 \return the shader stage at the specified \a index.
7387 */
7388
7389/*!
7390 \fn qsizetype QRhiGraphicsPipeline::shaderStageCount() const
7391 \return the number of shader stages in this pipeline.
7392 */
7393
7394/*!
7395 \fn QRhiVertexInputLayout QRhiGraphicsPipeline::vertexInputLayout() const
7396 \return the currently set vertex input layout specification.
7397 */
7398
7399/*!
7400 \fn void QRhiGraphicsPipeline::setVertexInputLayout(const QRhiVertexInputLayout &layout)
7401 Specifies the vertex input \a layout.
7402 */
7403
7404/*!
7405 \fn QRhiShaderResourceBindings *QRhiGraphicsPipeline::shaderResourceBindings() const
7406 \return the currently associated QRhiShaderResourceBindings object.
7407 */
7408
7409/*!
7410 \fn void QRhiGraphicsPipeline::setShaderResourceBindings(QRhiShaderResourceBindings *srb)
7411
7412 Associates with \a srb describing the resource binding layout and the
7413 resources (QRhiBuffer, QRhiTexture) themselves. The latter is optional,
7414 because only the layout matters during pipeline creation. Therefore, the \a
7415 srb passed in here can leave the actual buffer or texture objects
7416 unspecified (\nullptr) as long as there is another,
7417 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible}
7418 QRhiShaderResourceBindings bound via
7419 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()} before
7420 recording the draw calls.
7421 */
7422
7423/*!
7424 \fn QRhiRenderPassDescriptor *QRhiGraphicsPipeline::renderPassDescriptor() const
7425 \return the currently set QRhiRenderPassDescriptor.
7426 */
7427
7428/*!
7429 \fn void QRhiGraphicsPipeline::setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
7430 Associates with the specified QRhiRenderPassDescriptor \a desc.
7431 */
7432
7433/*!
7434 \fn int QRhiGraphicsPipeline::patchControlPointCount() const
7435 \return the currently set patch control point count.
7436 */
7437
7438/*!
7439 \fn void QRhiGraphicsPipeline::setPatchControlPointCount(int count)
7440
7441 Sets the number of patch control points to \a count. The default value is
7442 3. This is used only when the topology is set to \l Patches.
7443 */
7444
7445/*!
7446 \fn QRhiGraphicsPipeline::PolygonMode QRhiGraphicsPipeline::polygonMode() const
7447 \return the polygon mode.
7448 */
7449
7450/*!
7451 \fn void QRhiGraphicsPipeline::setPolygonMode(PolygonMode mode)
7452 Sets the polygon \a mode. The default is Fill.
7453
7454 \sa QRhi::NonFillPolygonMode
7455 */
7456
7457/*!
7458 \fn int QRhiGraphicsPipeline::multiViewCount() const
7459 \return the view count. The default is 0, indicating no multiview rendering.
7460 \since 6.7
7461 */
7462
7463/*!
7464 \fn void QRhiGraphicsPipeline::setMultiViewCount(int count)
7465 Sets the view \a count for multiview rendering. The default is 0,
7466 indicating no multiview rendering.
7467 \a count must be 2 or larger to trigger multiview rendering.
7468
7469 Multiview is only available when the \l{QRhi::MultiView}{MultiView feature}
7470 is reported as supported. The render target must be a 2D texture array, and
7471 the color attachment for the render target must have the same \a count set.
7472
7473 See QRhiColorAttachment::setMultiViewCount() for further details on
7474 multiview rendering.
7475
7476 \since 6.7
7477 \sa QRhi::MultiView, QRhiColorAttachment::setMultiViewCount()
7478 */
7479
7480/*!
7481 \class QRhiSwapChain
7482 \inmodule QtGuiPrivate
7483 \inheaderfile rhi/qrhi.h
7484 \since 6.6
7485 \brief Swapchain resource.
7486
7487 A swapchain enables presenting rendering results to a surface. A swapchain
7488 is typically backed by a set of color buffers. Of these, one is displayed
7489 at a time.
7490
7491 Below is a typical pattern for creating and managing a swapchain and some
7492 associated resources in order to render onto a QWindow:
7493
7494 \code
7495 void init()
7496 {
7497 sc = rhi->newSwapChain();
7498 ds = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
7499 QSize(), // no need to set the size here due to UsedWithSwapChainOnly
7500 1,
7501 QRhiRenderBuffer::UsedWithSwapChainOnly);
7502 sc->setWindow(window);
7503 sc->setDepthStencil(ds);
7504 rp = sc->newCompatibleRenderPassDescriptor();
7505 sc->setRenderPassDescriptor(rp);
7506 resizeSwapChain();
7507 }
7508
7509 void resizeSwapChain()
7510 {
7511 hasSwapChain = sc->createOrResize();
7512 }
7513
7514 void render()
7515 {
7516 if (!hasSwapChain || notExposed)
7517 return;
7518
7519 if (sc->currentPixelSize() != sc->surfacePixelSize() || newlyExposed) {
7520 resizeSwapChain();
7521 if (!hasSwapChain)
7522 return;
7523 newlyExposed = false;
7524 }
7525
7526 rhi->beginFrame(sc);
7527 // ...
7528 rhi->endFrame(sc);
7529 }
7530 \endcode
7531
7532 Avoid relying on QWindow resize events to resize swapchains, especially
7533 considering that surface sizes may not always fully match the QWindow
7534 reported dimensions. The safe, cross-platform approach is to do the check
7535 via surfacePixelSize() whenever starting a new frame.
7536
7537 Releasing the swapchain must happen while the QWindow and the underlying
7538 native window is fully up and running. Building on the previous example:
7539
7540 \code
7541 void releaseSwapChain()
7542 {
7543 if (hasSwapChain) {
7544 sc->destroy();
7545 hasSwapChain = false;
7546 }
7547 }
7548
7549 // assuming Window is our QWindow subclass
7550 bool Window::event(QEvent *e)
7551 {
7552 switch (e->type()) {
7553 case QEvent::UpdateRequest: // for QWindow::requestUpdate()
7554 render();
7555 break;
7556 case QEvent::PlatformSurface:
7557 if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed)
7558 releaseSwapChain();
7559 break;
7560 default:
7561 break;
7562 }
7563 return QWindow::event(e);
7564 }
7565 \endcode
7566
7567 Initializing the swapchain and starting to render the first frame cannot
7568 start at any time. The safe, cross-platform approach is to rely on expose
7569 events. QExposeEvent is a loosely specified event that is sent whenever a
7570 window gets mapped, obscured, and resized, depending on the platform.
7571
7572 \code
7573 void Window::exposeEvent(QExposeEvent *)
7574 {
7575 // initialize and start rendering when the window becomes usable for graphics purposes
7576 if (isExposed() && !running) {
7577 running = true;
7578 init();
7579 }
7580
7581 // stop pushing frames when not exposed or size becomes 0
7582 if ((!isExposed() || (hasSwapChain && sc->surfacePixelSize().isEmpty())) && running)
7583 notExposed = true;
7584
7585 // continue when exposed again and the surface has a valid size
7586 if (isExposed() && running && notExposed && !sc->surfacePixelSize().isEmpty()) {
7587 notExposed = false;
7588 newlyExposed = true;
7589 }
7590
7591 if (isExposed() && !sc->surfacePixelSize().isEmpty())
7592 render();
7593 }
7594 \endcode
7595
7596 Once the rendering has started, a simple way to request a new frame is
7597 QWindow::requestUpdate(). While on some platforms this is merely a small
7598 timer, on others it has a specific implementation: for instance on macOS or
7599 iOS it may be backed by
7600 \l{https://developer.apple.com/documentation/corevideo/cvdisplaylink?language=objc}{CVDisplayLink}.
7601 The example above is already prepared for update requests by handling
7602 QEvent::UpdateRequest.
7603
7604 While acting as a QRhiRenderTarget, QRhiSwapChain also manages a
7605 QRhiCommandBuffer. Calling QRhi::endFrame() submits the recorded commands
7606 and also enqueues a \c present request. The default behavior is to do this
7607 with a swap interval of 1, meaning synchronizing to the display's vertical
7608 refresh is enabled. Thus the rendering thread calling beginFrame() and
7609 endFrame() will get throttled to vsync. On some backends this can be
7610 disabled by passing QRhiSwapChain:NoVSync in flags().
7611
7612 Multisampling (MSAA) is handled transparently to the applications when
7613 requested via setSampleCount(). Where applicable, QRhiSwapChain will take
7614 care of creating additional color buffers and issuing a multisample resolve
7615 command at the end of a frame. For OpenGL, it is necessary to request the
7616 appropriate sample count also via QSurfaceFormat, by calling
7617 QSurfaceFormat::setDefaultFormat() before initializing the QRhi.
7618
7619 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
7620 for details.
7621 */
7622
7623/*!
7624 \enum QRhiSwapChain::Flag
7625 Flag values to describe swapchain properties
7626
7627 \value SurfaceHasPreMulAlpha Indicates that the target surface has
7628 transparency with premultiplied alpha. For example, this is what Qt Quick
7629 uses when the alpha channel is enabled on the target QWindow, because the
7630 scenegraph rendrerer always outputs fragments with alpha multiplied into
7631 the red, green, and blue values. To ensure identical behavior across
7632 platforms, always set QSurfaceFormat::alphaBufferSize() to a non-zero value
7633 on the target QWindow whenever this flag is set on the swapchain.
7634
7635 \value SurfaceHasNonPreMulAlpha Indicates the target surface has
7636 transparency with non-premultiplied alpha. Be aware that this may not be
7637 supported on some systems, if the system compositor always expects content
7638 with premultiplied alpha. In that case the behavior with this flag set is
7639 expected to be equivalent to SurfaceHasPreMulAlpha.
7640
7641 \value sRGB Requests to pick an sRGB format for the swapchain's color
7642 buffers and/or render target views, where applicable. Note that this
7643 implies that sRGB framebuffer update and blending will get enabled for all
7644 content targeting this swapchain, and opting out is not possible. For
7645 OpenGL, set \l{QSurfaceFormat::sRGBColorSpace}{sRGBColorSpace} on the
7646 QSurfaceFormat of the QWindow in addition. Applicable only when the
7647 swapchain format is set to QRhiSwapChain::SDR.
7648
7649 \value UsedAsTransferSource Indicates the swapchain will be used as the
7650 source of a readback in QRhiResourceUpdateBatch::readBackTexture().
7651
7652 \value NoVSync Requests disabling waiting for vertical sync, also avoiding
7653 throttling the rendering thread. The behavior is backend specific and
7654 applicable only where it is possible to control this. Some may ignore the
7655 request altogether. For OpenGL, try instead setting the swap interval to 0
7656 on the QWindow via QSurfaceFormat::setSwapInterval().
7657
7658 \value MinimalBufferCount Requests creating the swapchain with the minimum
7659 number of buffers, which is in practice 2, unless the graphics
7660 implementation has a higher minimum number than that. Only applicable with
7661 backends where such control is available via the graphics API, for example,
7662 Vulkan. By default it is up to the backend to decide what number of buffers
7663 it requests (in practice this is almost always either 2 or 3), and it is
7664 not the applications' concern. However, on Vulkan for instance the backend
7665 will likely prefer the higher number (3), for example to avoid odd
7666 performance issues with some Vulkan implementations on mobile devices. It
7667 could be that on some platforms it can prove to be beneficial to force the
7668 lower buffer count (2), so this flag allows forcing that. Note that all
7669 this has no effect on the number of frames kept in flight, so the CPU
7670 (QRhi) will still prepare frames at most \c{N - 1} frames ahead of the GPU,
7671 even when the swapchain image buffer count larger than \c N. (\c{N} =
7672 QRhi::FramesInFlight and typically 2).
7673 */
7674
7675/*!
7676 \enum QRhiSwapChain::Format
7677 Describes the swapchain format. The default format is SDR.
7678
7679 This enum is used with
7680 \l{QRhiSwapChain::isFormatSupported()}{isFormatSupported()} to check
7681 upfront if creating the swapchain with the given format is supported by the
7682 platform and the window's associated screen, and with
7683 \l{QRhiSwapChain::setFormat()}{setFormat()}
7684 to set the requested format in the swapchain before calling
7685 \l{QRhiSwapChain::createOrResize()}{createOrResize()} for the first time.
7686
7687 \value SDR 8-bit RGBA or BGRA, depending on the backend and platform. With
7688 OpenGL ES in particular, it could happen that the platform provides less
7689 than 8 bits (e.g. due to EGL and the QSurfaceFormat choosing a 565 or 444
7690 format - this is outside the control of QRhi). Standard dynamic range. May
7691 be combined with setting the QRhiSwapChain::sRGB flag.
7692
7693 \value HDRExtendedSrgbLinear 16-bit float RGBA, high dynamic range,
7694 extended linear sRGB (scRGB) color space. This involves Rec. 709 primaries
7695 (same as SDR/sRGB) and linear colors. Conversion to the display's native
7696 color space (such as, HDR10) is performed by the windowing system. On
7697 Windows this is the canonical color space of the system compositor, and is
7698 the recommended format for HDR swapchains in general on desktop platforms.
7699
7700 \value HDR10 10-bit unsigned int RGB or BGR with 2 bit alpha, high dynamic
7701 range, HDR10 (Rec. 2020) color space with an ST2084 PQ transfer function.
7702
7703 \value HDRExtendedDisplayP3Linear 16-bit float RGBA, high dynamic range,
7704 extended linear Display P3 color space. The primary choice for HDR on
7705 platforms such as iOS and VisionOS.
7706 */
7707
7708/*!
7709 \internal
7710 */
7711QRhiSwapChain::QRhiSwapChain(QRhiImplementation *rhi)
7712 : QRhiResource(rhi)
7713{
7714}
7715
7716/*!
7717 \return the resource type.
7718 */
7719QRhiResource::Type QRhiSwapChain::resourceType() const
7720{
7721 return SwapChain;
7722}
7723
7724/*!
7725 \fn QSize QRhiSwapChain::currentPixelSize() const
7726
7727 \return the size with which the swapchain was last successfully built. Use
7728 this to decide if createOrResize() needs to be called again: if
7729 \c{currentPixelSize() != surfacePixelSize()} then the swapchain needs to be
7730 resized.
7731
7732 \note Typical rendering logic will call this function to get the output
7733 size when starting to prepare a new frame, and base dependent calculations
7734 (such as, the viewport) on the size returned from this function.
7735
7736 While in many cases the value is the same as \c{QWindow::size() *
7737 QWindow::devicePixelRatio()}, relying on the QWindow-reported size is not
7738 guaranteed to be correct on all platforms and graphics API implementations.
7739 Using this function is therefore strongly recommended whenever there is a
7740 need to identify the dimensions, in pixels, of the output layer or surface.
7741
7742 This also has the added benefit of avoiding potential data races when QRhi
7743 is used on a dedicated rendering thread, because the need to call QWindow
7744 functions, that may then access data updated on the main thread, is
7745 avoided.
7746
7747 \sa surfacePixelSize()
7748 */
7749
7750/*!
7751 \fn virtual QSize QRhiSwapChain::surfacePixelSize() = 0
7752
7753 \return The size of the window's associated surface or layer.
7754
7755 \warning Do not assume this is the same as \c{QWindow::size() *
7756 QWindow::devicePixelRatio()}. With some graphics APIs and windowing system
7757 interfaces (for example, Vulkan) there is a theoretical possibility for a
7758 surface to assume a size different from the associated window. To support
7759 these cases, \b{rendering logic must always base size-derived calculations
7760 (such as, viewports) on the size reported from QRhiSwapChain, and never on
7761 the size queried from QWindow}.
7762
7763 \note \b{Can also be called before createOrResize(), if at least window() is
7764 already set. This in combination with currentPixelSize() allows to detect
7765 when a swapchain needs to be resized.} However, watch out for the fact that
7766 the size of the underlying native object (surface, layer, or similar) is
7767 "live", so whenever this function is called, it returns the latest value
7768 reported by the underlying implementation, without any atomicity guarantee.
7769 Therefore, using this function to determine pixel sizes for graphics
7770 resources that are used in a frame is strongly discouraged. Rely on
7771 currentPixelSize() instead which returns a size that is atomic and will not
7772 change between createOrResize() invocations.
7773
7774 \note For depth-stencil buffers used in combination with the swapchain's
7775 color buffers, it is strongly recommended to rely on the automatic sizing
7776 and rebuilding behavior provided by the
7777 QRhiRenderBuffer:UsedWithSwapChainOnly flag. Avoid querying the surface
7778 size via this function just to get a size that can be passed to
7779 QRhiRenderBuffer::setPixelSize() as that would suffer from the lack of
7780 atomicity as described above.
7781
7782 \sa currentPixelSize()
7783 */
7784
7785/*!
7786 \fn virtual bool QRhiSwapChain::isFormatSupported(Format f) = 0
7787
7788 \return true if the given swapchain format \a f is supported. SDR is always
7789 supported.
7790
7791 \note Can be called independently of createOrResize(), but window() must
7792 already be set. Calling without the window set may lead to unexpected
7793 results depending on the backend and platform (most likely false for any
7794 HDR format), because HDR format support is usually tied to the output
7795 (screen) to which the swapchain's associated window belongs at any given
7796 time. If the result is true for a HDR format, then creating the swapchain
7797 with that format is expected to succeed as long as the window is not moved
7798 to another screen in the meantime.
7799
7800 The main use of this function is to call it before the first
7801 createOrResize() after the window is already set. This allow the QRhi
7802 backends to perform platform or windowing system specific queries to
7803 determine if the window (and the screen it is on) is capable of true HDR
7804 output with the specified format.
7805
7806 When the format is reported as supported, call setFormat() to set the
7807 requested format and call createOrResize(). Be aware of the consequences
7808 however: successfully requesting a HDR format will involve having to deal
7809 with a different color space, possibly doing white level correction for
7810 non-HDR-aware content, adjusting tonemapping methods, adjusting offscreen
7811 render target settings, etc.
7812
7813 \sa setFormat()
7814 */
7815
7816/*!
7817 \fn virtual QRhiCommandBuffer *QRhiSwapChain::currentFrameCommandBuffer() = 0
7818
7819 \return a command buffer on which rendering commands and resource updates
7820 can be recorded within a \l{QRhi::beginFrame()}{beginFrame} -
7821 \l{QRhi::endFrame()}{endFrame} block, assuming beginFrame() was called with
7822 this swapchain.
7823
7824 \note The returned object is valid also after endFrame(), up until the next
7825 beginFrame(), but the returned command buffer should not be used to record
7826 any commands then. Rather, it can be used to query data collected during
7827 the frame (or previous frames), for example by calling
7828 \l{QRhiCommandBuffer::lastCompletedGpuTime()}{lastCompletedGpuTime()}.
7829
7830 \note The value must not be cached and reused between frames. The caller
7831 should not hold on to the returned object once
7832 \l{QRhi::beginFrame()}{beginFrame()} is called again. Instead, the command
7833 buffer object should be queried again by calling this function.
7834*/
7835
7836/*!
7837 \fn virtual QRhiRenderTarget *QRhiSwapChain::currentFrameRenderTarget() = 0
7838
7839 \return a render target that can used with beginPass() in order to render
7840 the swapchain's current backbuffer. Only valid within a
7841 QRhi::beginFrame() - QRhi::endFrame() block where beginFrame() was called
7842 with this swapchain.
7843
7844 \note the value must not be cached and reused between frames
7845 */
7846
7847/*!
7848 \enum QRhiSwapChain::StereoTargetBuffer
7849 Selects the backbuffer to use with a stereoscopic swapchain.
7850
7851 \value LeftBuffer
7852 \value RightBuffer
7853 */
7854
7855/*!
7856 \return a render target that can be used with beginPass() in order to
7857 render to the swapchain's left or right backbuffer. This overload should be
7858 used only with stereoscopic rendering, that is, when the associated QWindow
7859 is backed by two color buffers, one for each eye, instead of just one.
7860
7861 When stereoscopic rendering is not supported, the return value will be
7862 the default target. It is supported by all hardware backends except for Metal, in
7863 combination with \l QSurfaceFormat::StereoBuffers, assuming it is supported
7864 by the graphics and display driver stack at run time. Metal and Null backends
7865 are going to return the default render target from this overload.
7866
7867 \note the value must not be cached and reused between frames
7868 */
7869QRhiRenderTarget *QRhiSwapChain::currentFrameRenderTarget(StereoTargetBuffer targetBuffer)
7870{
7871 Q_UNUSED(targetBuffer);
7872 return currentFrameRenderTarget();
7873}
7874
7875/*!
7876 \fn virtual bool QRhiSwapChain::createOrResize() = 0
7877
7878 Creates the swapchain if not already done and resizes the swapchain buffers
7879 to match the current size of the targeted surface. Call this whenever the
7880 size of the target surface is different than before.
7881
7882 \note call destroy() only when the swapchain needs to be released
7883 completely, typically upon
7884 QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed. To perform resizing, just
7885 call createOrResize().
7886
7887 \return \c true when successful, \c false when a graphics operation failed.
7888 Regardless of the return value, calling destroy() is always safe.
7889 */
7890
7891/*!
7892 \fn QWindow *QRhiSwapChain::window() const
7893 \return the currently set window.
7894 */
7895
7896/*!
7897 \fn void QRhiSwapChain::setWindow(QWindow *window)
7898 Sets the \a window.
7899 */
7900
7901/*!
7902 \fn QRhiSwapChainProxyData QRhiSwapChain::proxyData() const
7903 \return the currently set proxy data.
7904 */
7905
7906/*!
7907 \fn void QRhiSwapChain::setProxyData(const QRhiSwapChainProxyData &d)
7908 Sets the proxy data \a d.
7909
7910 \sa QRhi::updateSwapChainProxyData()
7911 */
7912
7913/*!
7914 \fn QRhiSwapChain::Flags QRhiSwapChain::flags() const
7915 \return the currently set flags.
7916 */
7917
7918/*!
7919 \fn void QRhiSwapChain::setFlags(Flags f)
7920 Sets the flags \a f.
7921 */
7922
7923/*!
7924 \fn QRhiSwapChain::Format QRhiSwapChain::format() const
7925 \return the currently set format.
7926 */
7927
7928/*!
7929 \fn void QRhiSwapChain::setFormat(Format f)
7930 Sets the format \a f.
7931
7932 Avoid setting formats that are reported as unsupported from
7933 isFormatSupported(). Note that support for a given format may depend on the
7934 screen the swapchain's associated window is opened on. On some platforms,
7935 such as Windows and macOS, for HDR output to work it is necessary to have
7936 HDR output enabled in the display settings.
7937
7938 See isFormatSupported(), \l QRhiSwapChainHdrInfo, and \l Format for more
7939 information on high dynamic range output.
7940 */
7941
7942/*!
7943 \fn QRhiRenderBuffer *QRhiSwapChain::depthStencil() const
7944 \return the currently associated renderbuffer for depth-stencil.
7945 */
7946
7947/*!
7948 \fn void QRhiSwapChain::setDepthStencil(QRhiRenderBuffer *ds)
7949 Sets the renderbuffer \a ds for use as a depth-stencil buffer.
7950 */
7951
7952/*!
7953 \fn int QRhiSwapChain::sampleCount() const
7954 \return the currently set sample count. 1 means no multisample antialiasing.
7955 */
7956
7957/*!
7958 \fn void QRhiSwapChain::setSampleCount(int samples)
7959
7960 Sets the sample count. Common values for \a samples are 1 (no MSAA), 4 (4x
7961 MSAA), or 8 (8x MSAA).
7962
7963 \sa QRhi::supportedSampleCounts()
7964 */
7965
7966/*!
7967 \fn QRhiRenderPassDescriptor *QRhiSwapChain::renderPassDescriptor() const
7968 \return the currently associated QRhiRenderPassDescriptor object.
7969 */
7970
7971/*!
7972 \fn void QRhiSwapChain::setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
7973 Associates with the QRhiRenderPassDescriptor \a desc.
7974 */
7975
7976/*!
7977 \fn virtual QRhiRenderPassDescriptor *QRhiSwapChain::newCompatibleRenderPassDescriptor() = 0;
7978
7979 \return a new QRhiRenderPassDescriptor that is compatible with this swapchain.
7980
7981 The returned value is used in two ways: it can be passed to
7982 setRenderPassDescriptor() and
7983 QRhiGraphicsPipeline::setRenderPassDescriptor(). A render pass descriptor
7984 describes the attachments (color, depth/stencil) and the load/store
7985 behavior that can be affected by flags(). A QRhiGraphicsPipeline can only
7986 be used in combination with a swapchain that has a
7987 \l{QRhiRenderPassDescriptor::isCompatible()}{compatible}
7988 QRhiRenderPassDescriptor set.
7989
7990 \sa createOrResize()
7991 */
7992
7993/*!
7994 \fn QRhiShadingRateMap *QRhiSwapChain::shadingRateMap() const
7995 \return the currently set QRhiShadingRateMap. By default this is \nullptr.
7996 \since 6.9
7997 */
7998
7999/*!
8000 \fn void QRhiSwapChain::setShadingRateMap(QRhiShadingRateMap *map)
8001
8002 Associates with the specified QRhiShadingRateMap \a map. This is functional
8003 only when the \l QRhi::VariableRateShadingMap feature is reported as
8004 supported.
8005
8006 When QRhiCommandBuffer::setShadingRate() is also called, the higher of two
8007 the shading rates are used for each tile. There is currently no control
8008 offered over the combiner behavior.
8009
8010 \note Setting a shading rate map implies that a different, new
8011 QRhiRenderPassDescriptor is needed and some of the native swapchain objects
8012 must be rebuilt. Therefore, if the swapchain is already set up, call
8013 newCompatibleRenderPassDescriptor() and setRenderPassDescriptor() right
8014 after setShadingRateMap(). Then, createOrResize() must also be called again.
8015 This has rolling consequences, for example for graphics pipelines: those
8016 also need to be associated with the new QRhiRenderPassDescriptor and then
8017 rebuilt. See \l QRhiRenderPassDescriptor::serializedFormat() for some
8018 suggestions on how to deal with this. Remember to set the
8019 QRhiGraphicsPipeline::UsesShadingRate flag for them as well.
8020
8021 \since 6.9
8022 */
8023
8024/*!
8025 \struct QRhiSwapChainHdrInfo
8026 \inmodule QtGuiPrivate
8027 \inheaderfile rhi/qrhi.h
8028 \since 6.6
8029
8030 \brief Describes the high dynamic range related information of the
8031 swapchain's associated output.
8032
8033 To perform HDR-compatible tonemapping, where the target range is not [0,1],
8034 one often needs to know the maximum luminance of the display the
8035 swapchain's window is associated with. While this is often made
8036 user-configurable (think brightness, gamma and similar settings in games),
8037 it can be highly useful to set defaults based on the values reported by the
8038 display itself, thus providing a decent starting point.
8039
8040 There are some problems however: the information is exposed in different
8041 forms on different platforms, whereas with cross-platform graphics APIs
8042 there is often no associated solution at all, because managing such
8043 information is not in the scope of the API (and may rather be retrievable
8044 via other platform-specific means, if any).
8045
8046 With Metal on macOS/iOS, there is no luminance values exposed in the
8047 platform APIs. Instead, the maximum color component value, that would be
8048 1.0 in a non-HDR setup, is provided. The \c limitsType field indicates what
8049 kind of information is available. It is then up to the clients of QRhi to
8050 access the correct data from the \c limits union and use it as they see
8051 fit.
8052
8053 With an API like Vulkan, where there is no way to get such information, the
8054 values are always the built-in defaults.
8055
8056 Therefore, the struct returned from QRhiSwapChain::hdrInfo() contains
8057 either some hard-coded defaults or real values received from an API such as
8058 DXGI (IDXGIOutput6) or Cocoa (NSScreen). When no platform queries are
8059 available (or needs using platform facilities out of scope for QRhi), the
8060 hard-coded defaults are a maximum luminance of 1000 nits and an SDR white
8061 level of 200.
8062
8063 The struct also exposes the presumed luminance behavior of the platform and
8064 its compositor, to indicate what a color component value of 1.0 is treated
8065 as in a HDR color buffer. In some cases it will be necessary to perform
8066 color correction of non-HDR content composited with HDR content. To enable
8067 this, the SDR white level is queried from the system on some platforms
8068 (Windows) and exposed here.
8069
8070 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8071 for details.
8072
8073 \sa QRhiSwapChain::hdrInfo()
8074 */
8075
8076/*!
8077 \enum QRhiSwapChainHdrInfo::LimitsType
8078
8079 \value LuminanceInNits Indicates that the \l limits union has its
8080 \c luminanceInNits struct set
8081
8082 \value ColorComponentValue Indicates that the \l limits union has its
8083 \c colorComponentValue struct set
8084*/
8085
8086/*!
8087 \enum QRhiSwapChainHdrInfo::LuminanceBehavior
8088
8089 \value SceneReferred Indicates that the color value of 1.0 is interpreted
8090 as 80 nits. This is the behavior of HDR-enabled windows with the Windows
8091 compositor. See
8092 \l{https://learn.microsoft.com/en-us/windows/win32/direct3darticles/high-dynamic-range}{this
8093 page} for more information on HDR on Windows.
8094
8095 \value DisplayReferred Indicates that the color value of 1.0 is interpreted
8096 as the value of the SDR white. (which can be e.g. 200 nits, but will vary
8097 depending on screen brightness) This is the behavior of HDR-enabled windows
8098 on Apple platforms. See
8099 \l{https://developer.apple.com/documentation/metal/hdr_content/displaying_hdr_content_in_a_metal_layer}{this
8100 page} for more information on Apple's EDR system.
8101*/
8102
8103/*!
8104 \variable QRhiSwapChainHdrInfo::limitsType
8105
8106 With Metal on macOS/iOS, there is no luminance values exposed in the
8107 platform APIs. Instead, the maximum color component value, that would be
8108 1.0 in a non-HDR setup, is provided. This value indicates what kind of
8109 information is available in \l limits.
8110
8111 \sa QRhiSwapChain::hdrInfo()
8112*/
8113
8114/*!
8115 \variable QRhiSwapChainHdrInfo::limits
8116
8117 Contains the actual values queried from the graphics API or the platform.
8118 The type of data is indicated by \l limitsType. This is therefore a union.
8119 There are currently two options:
8120
8121 Luminance values in nits:
8122
8123 \code
8124 struct {
8125 float minLuminance;
8126 float maxLuminance;
8127 } luminanceInNits;
8128 \endcode
8129
8130 On Windows the minimum and maximum luminance depends on the screen
8131 brightness. While not relevant for desktops, on laptops the screen
8132 brightness may change at any time. Increasing brightness implies decreased
8133 maximum luminance. In addition, the results may also be dependent on the
8134 HDR Content Brightness set in Windows Settings' System/Display/HDR view,
8135 if there is such a setting.
8136
8137 Note however that the changes made to the laptop screen's brightness or in
8138 the system settings while the application is running are not necessarily
8139 reflected in the returned values, meaning calling hdrInfo() again may still
8140 return the same luminance range as before for the rest of the process'
8141 lifetime. The exact behavior is up to DXGI and Qt has no control over it.
8142
8143 \note The Windows compositor works in scene-referred mode for HDR content.
8144 A color component value of 1.0 corresponds to a luminance of 80 nits. When
8145 rendering non-HDR content (e.g. 2D UI elements), the correction of the
8146 white level is often necessary. (e.g., outputting the fragment color (1, 1,
8147 1) will likely lead to showing a shade of white that is too dim on-screen)
8148 See \l sdrWhiteLevel.
8149
8150 For macOS/iOS, the current maximum and potential maximum color
8151 component values are provided:
8152
8153 \code
8154 struct {
8155 float maxColorComponentValue;
8156 float maxPotentialColorComponentValue;
8157 } colorComponentValue;
8158 \endcode
8159
8160 The value may depend on the screen brightness, which on laptops means that
8161 the result may change in the next call to hdrInfo() if the brightness was
8162 changed in the meantime. The maximum screen brightness implies a maximum
8163 color value of 1.0.
8164
8165 \note Apple's EDR is display-referred. 1.0 corresponds to a luminance level
8166 of SDR white (e.g. 200 nits), the value of which varies based on the screen
8167 brightness and possibly other settings. The exact luminance value for that,
8168 or the maximum luminance of the display, are not exposed to the
8169 applications.
8170
8171 \note It has been observed that the color component values are not set to
8172 the correct larger-than-1 value right away on startup on some macOS
8173 systems, but the values tend to change during or after the first frame.
8174
8175 \sa QRhiSwapChain::hdrInfo()
8176*/
8177
8178/*!
8179 \variable QRhiSwapChainHdrInfo::luminanceBehavior
8180
8181 Describes the platform's presumed behavior with regards to color values.
8182
8183 \sa sdrWhiteLevel
8184 */
8185
8186/*!
8187 \variable QRhiSwapChainHdrInfo::sdrWhiteLevel
8188
8189 On Windows this is the dynamic SDR white level in nits. The value is
8190 dependent on the screen brightness (on laptops), and the SDR or HDR Content
8191 Brightness settings in the Windows settings' System/Display/HDR view.
8192
8193 To perform white level correction for non-HDR (SDR) content, such as 2D UI
8194 elemenents, multiply the final color with sdrWhiteLevel / 80.0 whenever
8195 \l luminanceBehavior is SceneReferred. (assuming Windows and a linear
8196 extended sRGB (scRGB) color space)
8197
8198 On other platforms the value is always a pre-defined value, 200. This may
8199 not match the system's actual SDR white level, but the value of this
8200 variable is not relevant in practice when the \l luminanceBehavior is
8201 DisplayReferred, because then the color component value of 1.0 refers to
8202 the SDR white by default.
8203
8204 \sa luminanceBehavior
8205*/
8206
8207/*!
8208 \return the HDR information for the associated display.
8209
8210 Do not assume that this is a cheap operation. Depending on the platform,
8211 this function makes various platform queries which may have a performance
8212 impact.
8213
8214 \note Can be called before createOrResize() as long as the window is
8215 \l{setWindow()}{set}.
8216
8217 \note What happens when moving a window with an initialized swapchain
8218 between displays (HDR to HDR with different characteristics, HDR to SDR,
8219 etc.) is not currently well-defined and depends heavily on the windowing
8220 system and compositor, with potentially varying behavior between platforms.
8221 Currently QRhi only guarantees that hdrInfo() returns valid data, if
8222 available, for the display to which the swapchain's associated window
8223 belonged at the time of createOrResize().
8224
8225 \sa QRhiSwapChainHdrInfo
8226 */
8227QRhiSwapChainHdrInfo QRhiSwapChain::hdrInfo()
8228{
8229 QRhiSwapChainHdrInfo info;
8230 info.limitsType = QRhiSwapChainHdrInfo::LuminanceInNits;
8231 info.limits.luminanceInNits.minLuminance = 0.0f;
8232 info.limits.luminanceInNits.maxLuminance = 1000.0f;
8233 info.luminanceBehavior = QRhiSwapChainHdrInfo::SceneReferred;
8234 info.sdrWhiteLevel = 200.0f;
8235 return info;
8236}
8237
8238#ifndef QT_NO_DEBUG_STREAM
8239QDebug operator<<(QDebug dbg, const QRhiSwapChainHdrInfo &info)
8240{
8241 QDebugStateSaver saver(dbg);
8242 dbg.nospace() << "QRhiSwapChainHdrInfo(";
8243 switch (info.limitsType) {
8245 dbg.nospace() << " minLuminance=" << info.limits.luminanceInNits.minLuminance
8246 << " maxLuminance=" << info.limits.luminanceInNits.maxLuminance;
8247 break;
8249 dbg.nospace() << " maxColorComponentValue=" << info.limits.colorComponentValue.maxColorComponentValue;
8250 dbg.nospace() << " maxPotentialColorComponentValue=" << info.limits.colorComponentValue.maxPotentialColorComponentValue;
8251 break;
8252 }
8253 switch (info.luminanceBehavior) {
8255 dbg.nospace() << " scene-referred, SDR white level=" << info.sdrWhiteLevel;
8256 break;
8258 dbg.nospace() << " display-referred";
8259 break;
8260 }
8261 dbg.nospace() << ')';
8262 return dbg;
8263}
8264#endif
8265
8266/*!
8267 \class QRhiComputePipeline
8268 \inmodule QtGuiPrivate
8269 \inheaderfile rhi/qrhi.h
8270 \since 6.6
8271 \brief Compute pipeline state resource.
8272
8273 \note Setting the shader resource bindings is mandatory. The referenced
8274 QRhiShaderResourceBindings must already have created() called on it by the
8275 time create() is called.
8276
8277 \note Setting the shader is mandatory.
8278
8279 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8280 for details.
8281 */
8282
8283/*!
8284 \enum QRhiComputePipeline::Flag
8285
8286 Flag values for describing pipeline options.
8287
8288 \value CompileShadersWithDebugInfo Requests compiling shaders with debug
8289 information enabled, when applicable. See
8290 QRhiGraphicsPipeline::CompileShadersWithDebugInfo for more information.
8291 */
8292
8293/*!
8294 \return the resource type.
8295 */
8296QRhiResource::Type QRhiComputePipeline::resourceType() const
8297{
8298 return ComputePipeline;
8299}
8300
8301/*!
8302 \internal
8303 */
8304QRhiComputePipeline::QRhiComputePipeline(QRhiImplementation *rhi)
8305 : QRhiResource(rhi)
8306{
8307}
8308
8309/*!
8310 \fn QRhiComputePipeline::Flags QRhiComputePipeline::flags() const
8311 \return the currently set flags.
8312 */
8313
8314/*!
8315 \fn void QRhiComputePipeline::setFlags(Flags f)
8316 Sets the flags \a f.
8317 */
8318
8319/*!
8320 \fn QRhiShaderStage QRhiComputePipeline::shaderStage() const
8321 \return the currently set shader.
8322 */
8323
8324/*!
8325 \fn void QRhiComputePipeline::setShaderStage(const QRhiShaderStage &stage)
8326
8327 Sets the shader to use. \a stage can only refer to the
8328 \l{QRhiShaderStage::Compute}{compute stage}.
8329 */
8330
8331/*!
8332 \fn QRhiShaderResourceBindings *QRhiComputePipeline::shaderResourceBindings() const
8333 \return the currently associated QRhiShaderResourceBindings object.
8334 */
8335
8336/*!
8337 \fn void QRhiComputePipeline::setShaderResourceBindings(QRhiShaderResourceBindings *srb)
8338
8339 Associates with \a srb describing the resource binding layout and the
8340 resources (QRhiBuffer, QRhiTexture) themselves. The latter is optional. As
8341 with graphics pipelines, the \a srb passed in here can leave the actual
8342 buffer or texture objects unspecified (\nullptr) as long as there is
8343 another,
8344 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible}
8345 QRhiShaderResourceBindings bound via
8346 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()} before
8347 recording the dispatch call.
8348 */
8349
8350/*!
8351 \struct QRhiIndirectDrawCommand
8352 \inmodule QtGuiPrivate
8353 \inheaderfile rhi/qrhi.h
8354 \since 6.12
8355 \brief Draw command.
8356
8357 A draw command that can be uploaded to a QRhiBuffer of usage
8358 QRhiBuffer::UsageFlag::IndirectBuffer.
8359
8360 \sa QRhiCommandBuffer::drawIndirect()
8361
8362 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8363 for details.
8364 */
8365
8366/*!
8367 \variable QRhiIndirectDrawCommand::vertexCount
8368*/
8369
8370/*!
8371 \variable QRhiIndirectDrawCommand::instanceCount
8372*/
8373
8374/*!
8375 \variable QRhiIndirectDrawCommand::firstVertex
8376*/
8377
8378/*!
8379 \variable QRhiIndirectDrawCommand::firstInstance
8380*/
8381
8382/*!
8383 \struct QRhiIndexedIndirectDrawCommand
8384 \inmodule QtGuiPrivate
8385 \inheaderfile rhi/qrhi.h
8386 \since 6.12
8387 \brief Indexed draw command.
8388
8389 An indexed draw command that can be uploaded to a QRhiBuffer of usage
8390 QRhiBuffer::UsageFlag::IndirectBuffer.
8391
8392 \sa QRhiCommandBuffer::drawIndexedIndirect()
8393
8394 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8395 for details.
8396 */
8397
8398/*!
8399 \variable QRhiIndexedIndirectDrawCommand::indexCount
8400*/
8401
8402/*!
8403 \variable QRhiIndexedIndirectDrawCommand::instanceCount
8404*/
8405
8406/*!
8407 \variable QRhiIndexedIndirectDrawCommand::firstIndex
8408*/
8409
8410/*!
8411 \variable QRhiIndexedIndirectDrawCommand::vertexOffset
8412*/
8413
8414/*!
8415 \variable QRhiIndexedIndirectDrawCommand::firstInstance
8416*/
8417
8418/*!
8419 \class QRhiCommandBuffer
8420 \inmodule QtGuiPrivate
8421 \inheaderfile rhi/qrhi.h
8422 \since 6.6
8423 \brief Command buffer resource.
8424
8425 Not creatable by applications at the moment. The only ways to obtain a
8426 valid QRhiCommandBuffer are to get it from the targeted swapchain via
8427 QRhiSwapChain::currentFrameCommandBuffer(), or, in case of rendering
8428 completely offscreen, initializing one via QRhi::beginOffscreenFrame().
8429
8430 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8431 for details.
8432 */
8433
8434/*!
8435 \enum QRhiCommandBuffer::IndexFormat
8436 Specifies the index data type
8437
8438 \value IndexUInt16 Unsigned 16-bit (quint16)
8439 \value IndexUInt32 Unsigned 32-bit (quint32)
8440 */
8441
8442/*!
8443 \enum QRhiCommandBuffer::BeginPassFlag
8444 Flag values for QRhi::beginPass()
8445
8446 \value ExternalContent Specifies that there will be a call to
8447 QRhiCommandBuffer::beginExternal() in this pass. Some backends, Vulkan in
8448 particular, will fail if this flag is not set and beginExternal() is still
8449 called.
8450
8451 \value DoNotTrackResourcesForCompute Specifies that there is no need to
8452 track resources used in this pass if the only purpose of such tracking is
8453 to generate barriers for compute. Implies that there are no compute passes
8454 in the frame. This is an optimization hint that may be taken into account
8455 by certain backends, OpenGL in particular, allowing them to skip certain
8456 operations. When this flag is set for a render pass in a frame, calling
8457 \l{QRhiCommandBuffer::beginComputePass()}{beginComputePass()} in that frame
8458 may lead to unexpected behavior, depending on the resource dependencies
8459 between the render and compute passes.
8460 */
8461
8462/*!
8463 \typedef QRhiCommandBuffer::DynamicOffset
8464
8465 Synonym for std::pair<int, quint32>. The first entry is the binding, the second
8466 is the offset in the buffer.
8467*/
8468
8469/*!
8470 \typedef QRhiCommandBuffer::VertexInput
8471
8472 Synonym for std::pair<QRhiBuffer *, quint32>. The second entry is an offset in
8473 the buffer specified by the first.
8474*/
8475
8476/*!
8477 \internal
8478 */
8479QRhiCommandBuffer::QRhiCommandBuffer(QRhiImplementation *rhi)
8480 : QRhiResource(rhi)
8481{
8482}
8483
8484/*!
8485 \return the resource type.
8486 */
8487QRhiResource::Type QRhiCommandBuffer::resourceType() const
8488{
8489 return CommandBuffer;
8490}
8491
8492static const char *resourceTypeStr(const QRhiResource *res)
8493{
8494 switch (res->resourceType()) {
8495 case QRhiResource::Buffer:
8496 return "Buffer";
8497 case QRhiResource::Texture:
8498 return "Texture";
8499 case QRhiResource::Sampler:
8500 return "Sampler";
8501 case QRhiResource::RenderBuffer:
8502 return "RenderBuffer";
8503 case QRhiResource::RenderPassDescriptor:
8504 return "RenderPassDescriptor";
8505 case QRhiResource::SwapChainRenderTarget:
8506 return "SwapChainRenderTarget";
8507 case QRhiResource::TextureRenderTarget:
8508 return "TextureRenderTarget";
8509 case QRhiResource::ShaderResourceBindings:
8510 return "ShaderResourceBindings";
8511 case QRhiResource::GraphicsPipeline:
8512 return "GraphicsPipeline";
8513 case QRhiResource::SwapChain:
8514 return "SwapChain";
8515 case QRhiResource::ComputePipeline:
8516 return "ComputePipeline";
8517 case QRhiResource::CommandBuffer:
8518 return "CommandBuffer";
8519 case QRhiResource::ShadingRateMap:
8520 return "ShadingRateMap";
8521 }
8522
8523 Q_UNREACHABLE_RETURN("");
8524}
8525
8526QRhiImplementation::~QRhiImplementation()
8527{
8528 qDeleteAll(resUpdPool);
8529
8530 // Be nice and show something about leaked stuff. Though we may not get
8531 // this far with some backends where the allocator or the api may check
8532 // and freak out for unfreed graphics objects in the derived dtor already.
8533#ifndef QT_NO_DEBUG
8534 // debug builds: just do it always
8535 static bool leakCheck = true;
8536#else
8537 // release builds: opt-in
8538 static bool leakCheck = qEnvironmentVariableIntValue("QT_RHI_LEAK_CHECK");
8539#endif
8540 if (!resources.isEmpty()) {
8541 if (leakCheck) {
8542 qWarning("QRhi %p going down with %d unreleased resources that own native graphics objects. This is not nice.",
8543 q, int(resources.size()));
8544 }
8545 for (auto it = resources.cbegin(), end = resources.cend(); it != end; ++it) {
8546 QRhiResource *res = it.key();
8547 const bool ownsNativeResources = it.value();
8548 if (leakCheck && ownsNativeResources)
8549 qWarning(" %s resource %p (%s)", resourceTypeStr(res), res, res->m_objectName.constData());
8550
8551 // Null out the resource's rhi pointer. This is why it makes sense to do null
8552 // checks in the destroy() implementations of the various resource types. It
8553 // allows to survive in bad applications that somehow manage to destroy a
8554 // resource of a QRhi after the QRhi itself.
8555 res->m_rhi = nullptr;
8556 }
8557 }
8558}
8559
8560bool QRhiImplementation::isCompressedFormat(QRhiTexture::Format format) const
8561{
8562 return (format >= QRhiTexture::BC1 && format <= QRhiTexture::BC7)
8563 || (format >= QRhiTexture::ETC2_RGB8 && format <= QRhiTexture::ETC2_RGBA8)
8564 || (format >= QRhiTexture::ASTC_4x4 && format <= QRhiTexture::ASTC_12x12);
8565}
8566
8567void QRhiImplementation::compressedFormatInfo(QRhiTexture::Format format, const QSize &size,
8568 quint32 *bpl, quint32 *byteSize,
8569 QSize *blockDim) const
8570{
8571 int xdim = 4;
8572 int ydim = 4;
8573 quint32 blockSize = 0;
8574
8575 switch (format) {
8576 case QRhiTexture::BC1:
8577 blockSize = 8;
8578 break;
8579 case QRhiTexture::BC2:
8580 blockSize = 16;
8581 break;
8582 case QRhiTexture::BC3:
8583 blockSize = 16;
8584 break;
8585 case QRhiTexture::BC4:
8586 blockSize = 8;
8587 break;
8588 case QRhiTexture::BC5:
8589 blockSize = 16;
8590 break;
8591 case QRhiTexture::BC6H:
8592 blockSize = 16;
8593 break;
8594 case QRhiTexture::BC7:
8595 blockSize = 16;
8596 break;
8597
8598 case QRhiTexture::ETC2_RGB8:
8599 blockSize = 8;
8600 break;
8601 case QRhiTexture::ETC2_RGB8A1:
8602 blockSize = 8;
8603 break;
8604 case QRhiTexture::ETC2_RGBA8:
8605 blockSize = 16;
8606 break;
8607
8608 case QRhiTexture::ASTC_4x4:
8609 blockSize = 16;
8610 break;
8611 case QRhiTexture::ASTC_5x4:
8612 blockSize = 16;
8613 xdim = 5;
8614 break;
8615 case QRhiTexture::ASTC_5x5:
8616 blockSize = 16;
8617 xdim = ydim = 5;
8618 break;
8619 case QRhiTexture::ASTC_6x5:
8620 blockSize = 16;
8621 xdim = 6;
8622 ydim = 5;
8623 break;
8624 case QRhiTexture::ASTC_6x6:
8625 blockSize = 16;
8626 xdim = ydim = 6;
8627 break;
8628 case QRhiTexture::ASTC_8x5:
8629 blockSize = 16;
8630 xdim = 8;
8631 ydim = 5;
8632 break;
8633 case QRhiTexture::ASTC_8x6:
8634 blockSize = 16;
8635 xdim = 8;
8636 ydim = 6;
8637 break;
8638 case QRhiTexture::ASTC_8x8:
8639 blockSize = 16;
8640 xdim = ydim = 8;
8641 break;
8642 case QRhiTexture::ASTC_10x5:
8643 blockSize = 16;
8644 xdim = 10;
8645 ydim = 5;
8646 break;
8647 case QRhiTexture::ASTC_10x6:
8648 blockSize = 16;
8649 xdim = 10;
8650 ydim = 6;
8651 break;
8652 case QRhiTexture::ASTC_10x8:
8653 blockSize = 16;
8654 xdim = 10;
8655 ydim = 8;
8656 break;
8657 case QRhiTexture::ASTC_10x10:
8658 blockSize = 16;
8659 xdim = ydim = 10;
8660 break;
8661 case QRhiTexture::ASTC_12x10:
8662 blockSize = 16;
8663 xdim = 12;
8664 ydim = 10;
8665 break;
8666 case QRhiTexture::ASTC_12x12:
8667 blockSize = 16;
8668 xdim = ydim = 12;
8669 break;
8670
8671 default:
8672 Q_UNREACHABLE();
8673 break;
8674 }
8675
8676 const quint32 wblocks = uint((size.width() + xdim - 1) / xdim);
8677 const quint32 hblocks = uint((size.height() + ydim - 1) / ydim);
8678
8679 if (bpl)
8680 *bpl = wblocks * blockSize;
8681 if (byteSize)
8682 *byteSize = wblocks * hblocks * blockSize;
8683 if (blockDim)
8684 *blockDim = QSize(xdim, ydim);
8685}
8686
8687void QRhiImplementation::textureFormatInfo(QRhiTexture::Format format, const QSize &size,
8688 quint32 *bpl, quint32 *byteSize, quint32 *bytesPerPixel) const
8689{
8690 if (isCompressedFormat(format)) {
8691 compressedFormatInfo(format, size, bpl, byteSize, nullptr);
8692 return;
8693 }
8694
8695 quint32 bpc = 0;
8696 switch (format) {
8697 case QRhiTexture::RGBA8:
8698 bpc = 4;
8699 break;
8700 case QRhiTexture::BGRA8:
8701 bpc = 4;
8702 break;
8703 case QRhiTexture::R8:
8704 bpc = 1;
8705 break;
8706 case QRhiTexture::RG8:
8707 bpc = 2;
8708 break;
8709 case QRhiTexture::R16:
8710 bpc = 2;
8711 break;
8712 case QRhiTexture::RG16:
8713 bpc = 4;
8714 break;
8715 case QRhiTexture::RED_OR_ALPHA8:
8716 bpc = 1;
8717 break;
8718
8719 case QRhiTexture::RGBA16F:
8720 bpc = 8;
8721 break;
8722 case QRhiTexture::RGBA32F:
8723 bpc = 16;
8724 break;
8725 case QRhiTexture::R16F:
8726 bpc = 2;
8727 break;
8728 case QRhiTexture::R32F:
8729 bpc = 4;
8730 break;
8731
8732 case QRhiTexture::RGB10A2:
8733 bpc = 4;
8734 break;
8735
8736 case QRhiTexture::D16:
8737 bpc = 2;
8738 break;
8739 case QRhiTexture::D24:
8740 case QRhiTexture::D24S8:
8741 case QRhiTexture::D32F:
8742 bpc = 4;
8743 break;
8744
8745 case QRhiTexture::D32FS8:
8746 bpc = 8;
8747 break;
8748
8749 case QRhiTexture::R8SI:
8750 case QRhiTexture::R8UI:
8751 bpc = 1;
8752 break;
8753 case QRhiTexture::R32SI:
8754 case QRhiTexture::R32UI:
8755 bpc = 4;
8756 break;
8757 case QRhiTexture::RG32SI:
8758 case QRhiTexture::RG32UI:
8759 bpc = 8;
8760 break;
8761 case QRhiTexture::RGBA32SI:
8762 case QRhiTexture::RGBA32UI:
8763 bpc = 16;
8764 break;
8765
8766 default:
8767 Q_UNREACHABLE();
8768 break;
8769 }
8770
8771 if (bpl)
8772 *bpl = uint(size.width()) * bpc;
8773 if (byteSize)
8774 *byteSize = uint(size.width() * size.height()) * bpc;
8775 if (bytesPerPixel)
8776 *bytesPerPixel = bpc;
8777}
8778
8779bool QRhiImplementation::isStencilSupportingFormat(QRhiTexture::Format format) const
8780{
8781 switch (format) {
8782 case QRhiTexture::D24S8:
8783 case QRhiTexture::D32FS8:
8784 return true;
8785 default:
8786 break;
8787 }
8788 return false;
8789}
8790
8791bool QRhiImplementation::sanityCheckGraphicsPipeline(QRhiGraphicsPipeline *ps)
8792{
8793 if (ps->cbeginShaderStages() == ps->cendShaderStages()) {
8794 qWarning("Cannot build a graphics pipeline without any stages");
8795 return false;
8796 }
8797
8798 bool hasVertexStage = false;
8799 for (auto it = ps->cbeginShaderStages(), itEnd = ps->cendShaderStages(); it != itEnd; ++it) {
8800 if (!it->shader().isValid()) {
8801 qWarning("Empty shader passed to graphics pipeline");
8802 return false;
8803 }
8804 if (it->type() == QRhiShaderStage::Vertex)
8805 hasVertexStage = true;
8806 }
8807 if (!hasVertexStage) {
8808 qWarning("Cannot build a graphics pipeline without a vertex stage");
8809 return false;
8810 }
8811
8812 if (!ps->renderPassDescriptor()) {
8813 qWarning("Cannot build a graphics pipeline without a QRhiRenderPassDescriptor");
8814 return false;
8815 }
8816
8817 if (!ps->shaderResourceBindings()) {
8818 qWarning("Cannot build a graphics pipeline without QRhiShaderResourceBindings");
8819 return false;
8820 }
8821
8822 return true;
8823}
8824
8825bool QRhiImplementation::sanityCheckShaderResourceBindings(QRhiShaderResourceBindings *srb)
8826{
8827#ifndef QT_NO_DEBUG
8828 bool bindingsOk = true;
8829 const int CHECKED_BINDINGS_COUNT = 64;
8830 bool bindingSeen[CHECKED_BINDINGS_COUNT] = {};
8831 for (auto it = srb->cbeginBindings(), end = srb->cendBindings(); it != end; ++it) {
8832 const int binding = shaderResourceBindingData(*it)->binding;
8833 if (binding >= CHECKED_BINDINGS_COUNT)
8834 continue;
8835 if (binding < 0) {
8836 qWarning("Invalid binding number %d", binding);
8837 bindingsOk = false;
8838 continue;
8839 }
8840 switch (shaderResourceBindingData(*it)->type) {
8841 case QRhiShaderResourceBinding::UniformBuffer:
8842 if (!bindingSeen[binding]) {
8843 bindingSeen[binding] = true;
8844 } else {
8845 qWarning("Uniform buffer duplicates an existing binding number %d", binding);
8846 bindingsOk = false;
8847 }
8848 break;
8849 case QRhiShaderResourceBinding::SampledTexture:
8850 if (!bindingSeen[binding]) {
8851 bindingSeen[binding] = true;
8852 } else {
8853 qWarning("Combined image sampler duplicates an existing binding number %d", binding);
8854 bindingsOk = false;
8855 }
8856 break;
8857 case QRhiShaderResourceBinding::Texture:
8858 if (!bindingSeen[binding]) {
8859 bindingSeen[binding] = true;
8860 } else {
8861 qWarning("Texture duplicates an existing binding number %d", binding);
8862 bindingsOk = false;
8863 }
8864 break;
8865 case QRhiShaderResourceBinding::Sampler:
8866 if (!bindingSeen[binding]) {
8867 bindingSeen[binding] = true;
8868 } else {
8869 qWarning("Sampler duplicates an existing binding number %d", binding);
8870 bindingsOk = false;
8871 }
8872 break;
8873 case QRhiShaderResourceBinding::ImageLoad:
8874 case QRhiShaderResourceBinding::ImageStore:
8875 case QRhiShaderResourceBinding::ImageLoadStore:
8876 if (!bindingSeen[binding]) {
8877 bindingSeen[binding] = true;
8878 } else {
8879 qWarning("Image duplicates an existing binding number %d", binding);
8880 bindingsOk = false;
8881 }
8882 break;
8883 case QRhiShaderResourceBinding::BufferLoad:
8884 case QRhiShaderResourceBinding::BufferStore:
8885 case QRhiShaderResourceBinding::BufferLoadStore:
8886 if (!bindingSeen[binding]) {
8887 bindingSeen[binding] = true;
8888 } else {
8889 qWarning("Buffer duplicates an existing binding number %d", binding);
8890 bindingsOk = false;
8891 }
8892 break;
8893 default:
8894 qWarning("Unknown binding type %d", int(shaderResourceBindingData(*it)->type));
8895 bindingsOk = false;
8896 break;
8897 }
8898 }
8899
8900 if (!bindingsOk) {
8901 qWarning() << *srb;
8902 return false;
8903 }
8904#else
8905 Q_UNUSED(srb);
8906#endif
8907 return true;
8908}
8909
8910bool QRhiImplementation::sanityCheckResourceOwnership(QRhiResource *maybeResource)
8911{
8912 if (!maybeResource)
8913 return true;
8914
8915 if (maybeResource->m_rhi->q != q) {
8916 qWarning("%s %p (%s) belongs to QRhi %p, but client code attempted to use it with QRhi %p. This is wrong.",
8917 resourceTypeStr(maybeResource),
8918 maybeResource,
8919 maybeResource->m_objectName.constData(),
8920 maybeResource->m_rhi->q,
8921 q);
8922 return false;
8923 }
8924
8925 return true;
8926}
8927
8928int QRhiImplementation::effectiveSampleCount(int sampleCount) const
8929{
8930 // Stay compatible with QSurfaceFormat and friends where samples == 0 means the same as 1.
8931 const int s = qBound(1, sampleCount, 64);
8932 const QList<int> supported = supportedSampleCounts();
8933 int result = 1;
8934
8935 // Stay compatible with Qt 5 in that requesting an unsupported sample count
8936 // is not an error (although we still do a categorized debug print about
8937 // this), and rather a supported value, preferably a close one, not just 1,
8938 // is used instead. This is actually deviating from Qt 5 as that performs a
8939 // clamping only and does not handle cases such as when sample count 2 is
8940 // not supported but 4 is. (OpenGL handles things like that gracefully,
8941 // other APIs may not, so improve this by picking the next largest, or in
8942 // absence of that, the largest value; this with the goal to not reduce
8943 // quality by rather picking a larger-than-requested value than a smaller one)
8944
8945 for (int i = 0, ie = supported.count(); i != ie; ++i) {
8946 // assumes the 'supported' list is sorted
8947 if (supported[i] >= s) {
8948 result = supported[i];
8949 break;
8950 }
8951 }
8952
8953 if (result != s) {
8954 if (result == 1 && !supported.isEmpty())
8955 result = supported.last();
8956 qCDebug(QRHI_LOG_INFO, "Attempted to set unsupported sample count %d, using %d instead",
8957 sampleCount, result);
8958 }
8959
8960 return result;
8961}
8962
8963/*!
8964 \internal
8965 */
8966QRhi::QRhi()
8967{
8968}
8969
8970/*!
8971 Destructor. Destroys the backend and releases resources.
8972 */
8973QRhi::~QRhi()
8974{
8975 if (!d)
8976 return;
8977
8978 d->runCleanup();
8979
8980 qDeleteAll(d->pendingDeleteResources);
8981 d->pendingDeleteResources.clear();
8982
8983 d->destroy();
8984 delete d;
8985}
8986
8987QRhiImplementation *QRhiImplementation::newInstance(QRhi::Implementation impl, QRhiInitParams *params, QRhiNativeHandles *importDevice)
8988{
8989 QRhiImplementation *d = nullptr;
8990
8991 switch (impl) {
8992 case QRhi::Null:
8993 d = new QRhiNull(static_cast<QRhiNullInitParams *>(params));
8994 break;
8995 case QRhi::Vulkan:
8996#if QT_CONFIG(vulkan)
8997 d = new QRhiVulkan(static_cast<QRhiVulkanInitParams *>(params),
8998 static_cast<QRhiVulkanNativeHandles *>(importDevice));
8999 break;
9000#else
9001 Q_UNUSED(importDevice);
9002 qWarning("This build of Qt has no Vulkan support");
9003 break;
9004#endif
9005 case QRhi::OpenGLES2:
9006#ifndef QT_NO_OPENGL
9007 d = new QRhiGles2(static_cast<QRhiGles2InitParams *>(params),
9008 static_cast<QRhiGles2NativeHandles *>(importDevice));
9009 break;
9010#else
9011 qWarning("This build of Qt has no OpenGL support");
9012 break;
9013#endif
9014 case QRhi::D3D11:
9015#ifdef Q_OS_WIN
9016 d = new QRhiD3D11(static_cast<QRhiD3D11InitParams *>(params),
9017 static_cast<QRhiD3D11NativeHandles *>(importDevice));
9018 break;
9019#else
9020 qWarning("This platform has no Direct3D 11 support");
9021 break;
9022#endif
9023 case QRhi::Metal:
9024#if QT_CONFIG(metal)
9025 d = new QRhiMetal(static_cast<QRhiMetalInitParams *>(params),
9026 static_cast<QRhiMetalNativeHandles *>(importDevice));
9027 break;
9028#else
9029 qWarning("This platform has no Metal support");
9030 break;
9031#endif
9032 case QRhi::D3D12:
9033#ifdef Q_OS_WIN
9034#ifdef QRHI_D3D12_AVAILABLE
9035 d = new QRhiD3D12(static_cast<QRhiD3D12InitParams *>(params),
9036 static_cast<QRhiD3D12NativeHandles *>(importDevice));
9037 break;
9038#else
9039 qWarning("Qt was built without Direct3D 12 support. "
9040 "This is likely due to having ancient SDK headers (such as d3d12.h) in the Qt build environment. "
9041 "Rebuild Qt with an SDK supporting D3D12 features introduced in Windows 10 version 1703, "
9042 "or use an MSVC build as those typically are built with more up-to-date SDKs.");
9043 break;
9044#endif
9045#else
9046 qWarning("This platform has no Direct3D 12 support");
9047 break;
9048#endif
9049 }
9050
9051 return d;
9052}
9053
9054void QRhiImplementation::prepareForCreate(QRhi *rhi, QRhi::Implementation impl, QRhi::Flags flags, QRhiAdapter *adapter)
9055{
9056 q = rhi;
9057
9058 debugMarkers = flags.testFlag(QRhi::EnableDebugMarkers);
9059
9060 implType = impl;
9061 implThread = QThread::currentThread();
9062
9063 requestedRhiAdapter = adapter;
9064}
9065
9066QRhi::AdapterList QRhiImplementation::enumerateAdaptersBeforeCreate(QRhiNativeHandles *) const
9067{
9068 return {};
9069}
9070
9071/*!
9072 \overload
9073
9074 Equivalent to create(\a impl, \a params, \a flags, \a importDevice, \c nullptr).
9075 */
9076QRhi *QRhi::create(Implementation impl, QRhiInitParams *params, Flags flags, QRhiNativeHandles *importDevice)
9077{
9078 return create(impl, params, flags, importDevice, nullptr);
9079}
9080
9081/*!
9082 \return a new QRhi instance with a backend for the graphics API specified
9083 by \a impl with the specified \a flags. \return \c nullptr if the
9084 function fails.
9085
9086 \a params must point to an instance of one of the backend-specific
9087 subclasses of QRhiInitParams, such as, QRhiVulkanInitParams,
9088 QRhiMetalInitParams, QRhiD3D11InitParams, QRhiD3D12InitParams,
9089 QRhiGles2InitParams. See these classes for examples on creating a QRhi.
9090
9091 QRhi by design does not implement any fallback logic: if the specified API
9092 cannot be initialized, create() will fail, with warnings printed on the
9093 debug output by the backends. The clients of QRhi, for example Qt Quick,
9094 may however provide additional logic that allow falling back to an API
9095 different than what was requested, depending on the platform. If the
9096 intention is just to test if initialization would succeed when calling
9097 create() at later point, it is preferable to use probe() instead of
9098 create(), because with some backends probing can be implemented in a more
9099 lightweight manner as opposed to create(), which performs full
9100 initialization of the infrastructure and is wasteful if that QRhi instance
9101 is then thrown immediately away.
9102
9103 \a importDevice allows using an already existing graphics device, without
9104 QRhi creating its own. When not null, this parameter must point to an
9105 instance of one of the subclasses of QRhiNativeHandles:
9106 QRhiVulkanNativeHandles, QRhiD3D11NativeHandles, QRhiD3D12NativeHandles,
9107 QRhiMetalNativeHandles, QRhiGles2NativeHandles. The exact details and
9108 semantics depend on the backand and the underlying graphics API.
9109
9110 Specifying a QRhiAdapter in \a adapter offers a transparent, cross-API
9111 alternative to passing in a \c VkPhysicalDevice via QRhiVulkanNativeHandles,
9112 or an adapter LUID via QRhiD3D12NativeHandles. The ownership of \a adapter
9113 is not taken. See enumerateAdapters() for more information on this approach.
9114
9115 \note \a importDevice and \a adapter cannot be both specified.
9116
9117 \sa probe()
9118 */
9119QRhi *QRhi::create(Implementation impl, QRhiInitParams *params, Flags flags, QRhiNativeHandles *importDevice, QRhiAdapter *adapter)
9120{
9121 if (adapter && importDevice)
9122 qWarning("adapter and importDevice should not both be non-null in QRhi::create()");
9123
9124 std::unique_ptr<QRhiImplementation> rd(QRhiImplementation::newInstance(impl, params, importDevice));
9125 if (!rd)
9126 return nullptr;
9127
9128 std::unique_ptr<QRhi> r(new QRhi);
9129 r->d = rd.release();
9130 r->d->prepareForCreate(r.get(), impl, flags, adapter);
9131 if (!r->d->create(flags))
9132 return nullptr;
9133
9134 return r.release();
9135}
9136
9137/*!
9138 \return true if create() can be expected to succeed when called the given
9139 \a impl and \a params.
9140
9141 For some backends this is equivalent to calling create(), checking its
9142 return value, and then destroying the resulting QRhi.
9143
9144 For others, in particular with Metal, there may be a specific probing
9145 implementation, which allows testing in a more lightweight manner without
9146 polluting the debug output with warnings upon failures.
9147
9148 \sa create()
9149 */
9150bool QRhi::probe(QRhi::Implementation impl, QRhiInitParams *params)
9151{
9152 bool ok = false;
9153
9154 // The only place currently where this makes sense is Metal, where the API
9155 // is simple enough so that a special probing function - doing nothing but
9156 // a MTLCreateSystemDefaultDevice - is reasonable. Elsewhere, just call
9157 // create() and then drop the result.
9158
9159 if (impl == Metal) {
9160#if QT_CONFIG(metal)
9161 ok = QRhiMetal::probe(static_cast<QRhiMetalInitParams *>(params));
9162#endif
9163 } else {
9164 QRhi *rhi = create(impl, params);
9165 ok = rhi != nullptr;
9166 delete rhi;
9167 }
9168 return ok;
9169}
9170
9171/*!
9172 \typedef QRhi::AdapterList
9173 \relates QRhi
9174 \since 6.10
9175
9176 Synonym for QVector<QRhiAdapter *>.
9177*/
9178
9179/*!
9180 \return the list of adapters (physical devices) present, or an empty list
9181 when such control is not available with a given graphics API.
9182
9183 Backends where such level of control is not available, the returned list is
9184 always empty. Thus an empty list does not indicate there are no graphics
9185 devices in the system, but that fine-grained control over selecting which
9186 one to use is not available.
9187
9188 Backends for Direct 3D 11, Direct 3D 12, and Vulkan can be expected to fully
9189 support enumerating adapters. Others may not. The backend is specified by \a
9190 impl. A QRhiAdapter returned from this function must only be used in a
9191 create() call with the same \a impl. Some underlying APIs may present
9192 further limitations, with Vulkan in particular the QRhiAdapter is specified
9193 to the QVulkanInstance (\c VkInstance).
9194
9195 The caller is expected to destroy the QRhiAdapter objects in the list. Apart
9196 from querying \l{QRhiAdapter::}{info()}, the only purpose of these objects is
9197 to be passed on to create(), or the corresponding functions in higher layers
9198 such as Qt Quick.
9199
9200 The following snippet, written specifically for Vulkan, shows how to
9201 enumerate the available physical devices and request to create a QRhi for
9202 the chosen one. This in practice is equivalent to passing in a \c
9203 VkPhysicalDevice via a QRhiVulkanNativeHandles to create(), but it involves
9204 less API-specific code on the application side:
9205
9206 \code
9207 QRhiVulkanInitParams initParams;
9208 initParams.inst = &vulkanInstance;
9209 QRhi::AdapterList adapters = QRhi::enumerateAdapters(QRhi::Vulkan, &initParams);
9210 QRhiAdapter *chosenAdapter = nullptr;
9211 for (QRhiAdapter *adapter : adapters) {
9212 if (looksGood(adapter->info())) {
9213 chosenAdapter = adapter;
9214 break;
9215 }
9216 }
9217 QRhi *rhi = QRhi::create(QRhi::Vulkan, &initParams, {}, nullptr, chosenAdapter);
9218 qDeleteAll(adapters);
9219 \endcode
9220
9221 Passing in \a params is required due to some of the underlying graphics
9222 APIs' design. With Vulkan in particular, the QVulkanInstance must be
9223 provided, since enumerating is not possible without it. Other fields in the
9224 backend-specific \a params will not actually be used by this function.
9225
9226 \a nativeHandles is optional. When specified, it must be a valid
9227 QRhiD3D11NativeHandles, QRhiD3D12NativeHandles, or QRhiVulkanNativeHandles,
9228 similarly to create(). However, unlike create(), only the physical device
9229 (in case of Vulkan) or the adapter LUID (in case of D3D) fields are used,
9230 all other fields are ignored. This can be used the restrict the results to a
9231 given adapter. The returned list will contain 1 or 0 elements in this case.
9232
9233 Note how in the previous code snippet the looksGood() function
9234 implementation cannot perform any platform-specific filtering based on the
9235 true adapter / physical device identity, such as the adapter LUID on Windows
9236 or the VkPhysicalDevice with Vulkan. This is because QRhiDriverInfo does not
9237 contain platform-specific data. Instead, use \a nativeHandles to get the
9238 results filtered already inside enumerateAdapters().
9239
9240 The following two snippets, using Direct 3D 12 as an example, are equivalent
9241 in practice:
9242
9243 \code
9244 // enumerateAdapters-based approach from Qt 6.10 on
9245 QRhiD3D12InitParams initParams;
9246 QRhiD3D12NativeHandles nativeHandles;
9247 nativeHandles.adapterLuidLow = luid.LowPart; // retrieved a LUID from somewhere, now pass it on to Qt
9248 nativeHandles.adapterLuidHigh = luid.HighPart;
9249 QRhi::AdapterList adapters = QRhi::enumerateAdapters(QRhi::D3D12, &initParams, &nativeHandles);
9250 if (adapters.isEmpty()) { qWarning("Requested adapter was not found"); }
9251 QRhi *rhi = QRhi::create(QRhi::D3D12, &initParams, {}, nullptr, adapters[0]);
9252 qDeleteAll(adapters);
9253 \endcode
9254
9255 \code
9256 // traditional approach, more lightweight
9257 QRhiD3D12InitParams initParams;
9258 QRhiD3D12NativeHandles nativeHandles;
9259 nativeHandles.adapterLuidLow = luid.LowPart; // retrieved a LUID from somewhere, now pass it on to Qt
9260 nativeHandles.adapterLuidHigh = luid.HighPart;
9261 QRhi *rhi = QRhi::create(QRhi::D3D12, &initParams, {}, &nativeHandles, nullptr);
9262 \endcode
9263
9264 \since 6.10
9265 \sa create()
9266 */
9267QRhi::AdapterList QRhi::enumerateAdapters(Implementation impl, QRhiInitParams *params, QRhiNativeHandles *nativeHandles)
9268{
9269 std::unique_ptr<QRhiImplementation> rd(QRhiImplementation::newInstance(impl, params, nullptr));
9270 if (!rd)
9271 return {};
9272
9273 return rd->enumerateAdaptersBeforeCreate(nativeHandles);
9274}
9275
9276/*!
9277 \struct QRhiSwapChainProxyData
9278 \inmodule QtGuiPrivate
9279 \inheaderfile rhi/qrhi.h
9280 \since 6.6
9281
9282 \brief Opaque data describing native objects needed to set up a swapchain.
9283
9284 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9285 for details.
9286
9287 \sa QRhi::updateSwapChainProxyData()
9288 */
9289
9290/*!
9291 Generates and returns a QRhiSwapChainProxyData struct containing opaque
9292 data specific to the backend and graphics API specified by \a impl. \a
9293 window is the QWindow a swapchain is targeting.
9294
9295 The returned struct can be passed to QRhiSwapChain::setProxyData(). This
9296 makes sense in threaded rendering systems: this static function is expected
9297 to be called on the \b{main (gui) thread}, unlike all QRhi operations, then
9298 transferred to the thread working with the QRhi and QRhiSwapChain and passed
9299 on to the swapchain. This allows doing native platform queries that are
9300 only safe to be called on the main thread, for example to query the
9301 CAMetalLayer from a NSView, and then passing on the data to the
9302 QRhiSwapChain living on the rendering thread. With the Metal example, doing
9303 the view.layer access on a dedicated rendering thread causes a warning in
9304 the Xcode Thread Checker. With the data proxy mechanism, this is avoided.
9305
9306 When threads are not involved, generating and passing on the
9307 QRhiSwapChainProxyData is not required: backends are guaranteed to be able
9308 to query whatever is needed on their own, and if everything lives on the
9309 main (gui) thread, that should be sufficient.
9310
9311 \note \a impl should match what the QRhi is created with. For example,
9312 calling with QRhi::Metal on a non-Apple platform will not generate any
9313 useful data.
9314 */
9315QRhiSwapChainProxyData QRhi::updateSwapChainProxyData(QRhi::Implementation impl, QWindow *window)
9316{
9317#if QT_CONFIG(metal)
9318 if (impl == Metal)
9319 return QRhiMetal::updateSwapChainProxyData(window);
9320#else
9321 Q_UNUSED(impl);
9322 Q_UNUSED(window);
9323#endif
9324 return {};
9325}
9326
9327/*!
9328 \return the backend type for this QRhi.
9329 */
9330QRhi::Implementation QRhi::backend() const
9331{
9332 return d->implType;
9333}
9334
9335/*!
9336 \return a friendly name for the backend \a impl, usually the name of the 3D
9337 API in use.
9338 */
9339const char *QRhi::backendName(Implementation impl)
9340{
9341 switch (impl) {
9342 case QRhi::Null:
9343 return "Null";
9344 case QRhi::Vulkan:
9345 return "Vulkan";
9346 case QRhi::OpenGLES2:
9347 return "OpenGL";
9348 case QRhi::D3D11:
9349 return "D3D11";
9350 case QRhi::Metal:
9351 return "Metal";
9352 case QRhi::D3D12:
9353 return "D3D12";
9354 }
9355
9356 Q_UNREACHABLE_RETURN("Unknown");
9357}
9358
9359/*!
9360 \return the backend type as string for this QRhi.
9361 */
9362const char *QRhi::backendName() const
9363{
9364 return backendName(d->implType);
9365}
9366
9367/*!
9368 \enum QRhiDriverInfo::DeviceType
9369 Specifies the graphics device's type, when the information is available.
9370
9371 In practice this is only applicable with Vulkan and Metal. With Direct 3D
9372 11 and 12, using an adapter with the software flag set leads to the value
9373 \c CpuDevice. Otherwise, and with OpenGL, the value is always UnknownDevice.
9374
9375 \value UnknownDevice
9376 \value IntegratedDevice
9377 \value DiscreteDevice
9378 \value ExternalDevice
9379 \value VirtualDevice
9380 \value CpuDevice
9381*/
9382
9383/*!
9384 \struct QRhiDriverInfo
9385 \inmodule QtGuiPrivate
9386 \inheaderfile rhi/qrhi.h
9387 \since 6.6
9388
9389 \brief Describes the physical device, adapter, or graphics API
9390 implementation that is used by an initialized QRhi.
9391
9392 Graphics APIs offer different levels and kinds of information. The only
9393 value that is available across all APIs is the deviceName, which is a
9394 freetext description of the physical device, adapter, or is a combination
9395 of the strings reported for \c{GL_VENDOR} + \c{GL_RENDERER} +
9396 \c{GL_VERSION}. The deviceId is always 0 for OpenGL. vendorId is always 0
9397 for OpenGL and Metal. deviceType is always UnknownDevice for OpenGL and
9398 Direct 3D.
9399
9400 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9401 for details.
9402 */
9403
9404/*!
9405 \variable QRhiDriverInfo::deviceName
9406
9407 \sa QRhi::driverInfo()
9408*/
9409
9410/*!
9411 \variable QRhiDriverInfo::deviceId
9412
9413 \sa QRhi::driverInfo()
9414*/
9415
9416/*!
9417 \variable QRhiDriverInfo::vendorId
9418
9419 \sa QRhi::driverInfo()
9420*/
9421
9422/*!
9423 \variable QRhiDriverInfo::deviceType
9424
9425 \sa QRhi::driverInfo(), QRhiDriverInfo::DeviceType
9426*/
9427
9428#ifndef QT_NO_DEBUG_STREAM
9429static inline const char *deviceTypeStr(QRhiDriverInfo::DeviceType type)
9430{
9431 switch (type) {
9432 case QRhiDriverInfo::UnknownDevice:
9433 return "Unknown";
9434 case QRhiDriverInfo::IntegratedDevice:
9435 return "Integrated";
9436 case QRhiDriverInfo::DiscreteDevice:
9437 return "Discrete";
9438 case QRhiDriverInfo::ExternalDevice:
9439 return "External";
9440 case QRhiDriverInfo::VirtualDevice:
9441 return "Virtual";
9442 case QRhiDriverInfo::CpuDevice:
9443 return "Cpu";
9444 }
9445
9446 Q_UNREACHABLE_RETURN(nullptr);
9447}
9448QDebug operator<<(QDebug dbg, const QRhiDriverInfo &info)
9449{
9450 QDebugStateSaver saver(dbg);
9451 dbg.nospace() << "QRhiDriverInfo(deviceName=" << info.deviceName
9452 << " deviceId=0x" << Qt::hex << info.deviceId
9453 << " vendorId=0x" << info.vendorId
9454 << " deviceType=" << deviceTypeStr(info.deviceType)
9455 << ')';
9456 return dbg;
9457}
9458#endif
9459
9460/*!
9461 \return metadata for the graphics device used by this successfully
9462 initialized QRhi instance.
9463 */
9464QRhiDriverInfo QRhi::driverInfo() const
9465{
9466 return d->driverInfo();
9467}
9468
9469/*!
9470 \class QRhiAdapter
9471 \inmodule QtGuiPrivate
9472 \inheaderfile rhi/qrhi.h
9473 \since 6.10
9474
9475 \brief Represents a physical graphics device.
9476
9477 Some QRhi backends target graphics APIs that expose the concept of \c
9478 adapters or \c{physical devices}. Call the static \l
9479 {QRhi::}{enumerateAdapters()} function to retrieve a list of the adapters
9480 present in the system. Pass one of the returned QRhiAdapter objects to \l
9481 {QRhi::}{create()} in order to request using the adapter or physical device
9482 the QRhiAdapter corresponds to. Other than exposing the QRhiDriverInfo,
9483 QRhiAdapter is to be treated as an opaque handle.
9484
9485 \note With Vulkan, the QRhiAdapter is valid only as long as the
9486 QVulkanInstance that was used for \l{QRhi::}{enumerateAdapters()} is valid.
9487 This also means that a QRhiAdapter is tied to the Vulkan instance
9488 (QVulkanInstance, \c VkInstance) and cannot be used in the context of
9489 another Vulkan instance.
9490
9491 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9492 for details.
9493 */
9494
9495/*!
9496 \fn virtual QRhiDriverInfo QRhiAdapter::info() const = 0
9497
9498 \return the corresponding QRhiDriverInfo.
9499 */
9500
9501/*!
9502 \internal
9503 */
9504QRhiAdapter::~QRhiAdapter()
9505{
9506}
9507
9508/*!
9509 \return the thread on which the QRhi was \l{QRhi::create()}{initialized}.
9510 */
9511QThread *QRhi::thread() const
9512{
9513 return d->implThread;
9514}
9515
9516/*!
9517 Registers a \a callback that is invoked when the QRhi is destroyed.
9518
9519 The callback will run with the graphics resource still available, so this
9520 provides an opportunity for the application to cleanly release QRhiResource
9521 instances belonging to the QRhi. This is particularly useful for managing
9522 the lifetime of resources stored in \c cache type of objects, where the
9523 cache holds QRhiResources or objects containing QRhiResources.
9524
9525 \sa ~QRhi()
9526 */
9527void QRhi::addCleanupCallback(const CleanupCallback &callback)
9528{
9529 d->addCleanupCallback(callback);
9530}
9531
9532/*!
9533 \overload
9534
9535 Registers \a callback to be invoked when the QRhi is destroyed. This
9536 overload takes an opaque pointer, \a key, that is used to ensure that a
9537 given callback is registered (and so called) only once.
9538
9539 \sa removeCleanupCallback()
9540 */
9541void QRhi::addCleanupCallback(const void *key, const CleanupCallback &callback)
9542{
9543 d->addCleanupCallback(key, callback);
9544}
9545
9546/*!
9547 Deregisters the callback with \a key. If no cleanup callback was registered
9548 with \a key, the function does nothing. Callbacks registered without a key
9549 cannot be removed.
9550
9551 \sa addCleanupCallback()
9552 */
9553void QRhi::removeCleanupCallback(const void *key)
9554{
9555 d->removeCleanupCallback(key);
9556}
9557
9558void QRhiImplementation::runCleanup()
9559{
9560 for (const QRhi::CleanupCallback &f : std::as_const(cleanupCallbacks))
9561 f(q);
9562
9563 cleanupCallbacks.clear();
9564
9565 for (auto it = keyedCleanupCallbacks.cbegin(), end = keyedCleanupCallbacks.cend(); it != end; ++it)
9566 it.value()(q);
9567
9568 keyedCleanupCallbacks.clear();
9569}
9570
9571/*!
9572 \class QRhiResourceUpdateBatch
9573 \inmodule QtGuiPrivate
9574 \inheaderfile rhi/qrhi.h
9575 \since 6.6
9576 \brief Records upload and copy type of operations.
9577
9578 With QRhi it is no longer possible to perform copy type of operations at
9579 arbitrary times. Instead, all such operations are recorded into batches
9580 that are then passed, most commonly, to QRhiCommandBuffer::beginPass().
9581 What then happens under the hood is hidden from the application: the
9582 underlying implementations can defer and implement these operations in
9583 various different ways.
9584
9585 A resource update batch owns no graphics resources and does not perform any
9586 actual operations on its own. It should rather be viewed as a command
9587 buffer for update, upload, and copy type of commands.
9588
9589 To get an available, empty batch from the pool, call
9590 QRhi::nextResourceUpdateBatch().
9591
9592 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9593 for details.
9594 */
9595
9596/*!
9597 \internal
9598 */
9599QRhiResourceUpdateBatch::QRhiResourceUpdateBatch(QRhiImplementation *rhi)
9600 : d(new QRhiResourceUpdateBatchPrivate)
9601{
9602 d->q = this;
9603 d->rhi = rhi;
9604}
9605
9606QRhiResourceUpdateBatch::~QRhiResourceUpdateBatch()
9607{
9608 delete d;
9609}
9610
9611/*!
9612 \return the batch to the pool. This should only be used when the batch is
9613 not passed to one of QRhiCommandBuffer::beginPass(),
9614 QRhiCommandBuffer::endPass(), or QRhiCommandBuffer::resourceUpdate()
9615 because these implicitly call destroy().
9616
9617 \note QRhiResourceUpdateBatch instances must never by \c deleted by
9618 applications.
9619 */
9620void QRhiResourceUpdateBatch::release()
9621{
9622 d->free();
9623}
9624
9625/*!
9626 Copies all queued operations from the \a other batch into this one.
9627
9628 \note \a other may no longer contain valid data after the merge operation,
9629 and must not be submitted, but it will still need to be released by calling
9630 release().
9631
9632 This allows for a convenient pattern where resource updates that are
9633 already known during the initialization step are collected into a batch
9634 that is then merged into another when starting to first render pass later
9635 on:
9636
9637 \code
9638 void init()
9639 {
9640 initialUpdates = rhi->nextResourceUpdateBatch();
9641 initialUpdates->uploadStaticBuffer(vbuf, vertexData);
9642 initialUpdates->uploadStaticBuffer(ibuf, indexData);
9643 // ...
9644 }
9645
9646 void render()
9647 {
9648 QRhiResourceUpdateBatch *resUpdates = rhi->nextResourceUpdateBatch();
9649 if (initialUpdates) {
9650 resUpdates->merge(initialUpdates);
9651 initialUpdates->release();
9652 initialUpdates = nullptr;
9653 }
9654 // resUpdates->updateDynamicBuffer(...);
9655 cb->beginPass(rt, clearCol, clearDs, resUpdates);
9656 }
9657 \endcode
9658 */
9659void QRhiResourceUpdateBatch::merge(QRhiResourceUpdateBatch *other)
9660{
9661 d->merge(other->d);
9662}
9663
9664/*!
9665 \return true until the number of buffer and texture operations enqueued
9666 onto this batch is below a reasonable limit.
9667
9668 The return value is false when the number of buffer and/or texture
9669 operations added to this batch have reached, or are about to reach, a
9670 certain limit. The batch is fully functional afterwards as well, but may
9671 need to allocate additional memory. Therefore, a renderer that collects
9672 lots of buffer and texture updates in a single batch when preparing a frame
9673 may want to consider \l{QRhiCommandBuffer::resourceUpdate()}{submitting the
9674 batch} and \l{QRhi::nextResourceUpdateBatch()}{starting a new one} when
9675 this function returns false.
9676 */
9677bool QRhiResourceUpdateBatch::hasOptimalCapacity() const
9678{
9679 return d->hasOptimalCapacity();
9680}
9681
9682/*!
9683 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9684 QRhiBuffer::Dynamic.
9685
9686 The region is specified \a offset and \a size. The actual bytes to write
9687 are specified by \a data which must have at least \a size bytes available.
9688
9689 \a data is copied and can safely be destroyed or changed once this function
9690 returns.
9691
9692 \note If host writes are involved, which is the case with
9693 updateDynamicBuffer() typically as such buffers are backed by host visible
9694 memory with most backends, they may accumulate within a frame. Thus pass 1
9695 reading a region changed by a batch passed to pass 2 may see the changes
9696 specified in pass 2's update batch.
9697
9698 \note QRhi transparently manages double buffering in order to prevent
9699 stalling the graphics pipeline. The fact that a QRhiBuffer may have
9700 multiple native buffer objects underneath can be safely ignored when using
9701 the QRhi and QRhiResourceUpdateBatch.
9702 */
9703void QRhiResourceUpdateBatch::updateDynamicBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
9704{
9705 if (size > 0) {
9706 const int idx = d->activeBufferOpCount++;
9707 const int opListSize = d->bufferOps.size();
9708 if (idx < opListSize)
9709 QRhiResourceUpdateBatchPrivate::BufferOp::changeToDynamicUpdate(&d->bufferOps[idx], buf, offset, size, data);
9710 else
9711 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::dynamicUpdate(buf, offset, size, data));
9712 }
9713}
9714
9715/*!
9716 \overload
9717 \since 6.10
9718
9719 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9720 QRhiBuffer::Dynamic.
9721
9722 \a data is moved into the batch instead of copied with this overload.
9723 */
9724void QRhiResourceUpdateBatch::updateDynamicBuffer(QRhiBuffer *buf, quint32 offset, QByteArray data)
9725{
9726 if (!data.isEmpty()) {
9727 const int idx = d->activeBufferOpCount++;
9728 const int opListSize = d->bufferOps.size();
9729 if (idx < opListSize)
9730 QRhiResourceUpdateBatchPrivate::BufferOp::changeToDynamicUpdate(&d->bufferOps[idx], buf, offset, std::move(data));
9731 else
9732 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::dynamicUpdate(buf, offset, std::move(data)));
9733 }
9734}
9735
9736/*!
9737 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9738 QRhiBuffer::Immutable or QRhiBuffer::Static.
9739
9740 The region is specified \a offset and \a size. The actual bytes to write
9741 are specified by \a data which must have at least \a size bytes available.
9742
9743 \a data is copied and can safely be destroyed or changed once this function
9744 returns.
9745 */
9746void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
9747{
9748 if (size > 0) {
9749 const int idx = d->activeBufferOpCount++;
9750 if (idx < d->bufferOps.size())
9751 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, offset, size, data);
9752 else
9753 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, offset, size, data));
9754 }
9755}
9756
9757/*!
9758 \overload
9759 \since 6.10
9760
9761 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9762 QRhiBuffer::Immutable or QRhiBuffer::Static.
9763
9764 \a data is moved into the batch instead of copied with this overload.
9765 */
9766void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, QByteArray data)
9767{
9768 if (!data.isEmpty()) {
9769 const int idx = d->activeBufferOpCount++;
9770 if (idx < d->bufferOps.size())
9771 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, offset, std::move(data));
9772 else
9773 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, offset, std::move(data)));
9774 }
9775}
9776
9777/*!
9778 \overload
9779
9780 Enqueues updating the entire QRhiBuffer \a buf created with the type
9781 QRhiBuffer::Immutable or QRhiBuffer::Static.
9782 */
9783void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, const void *data)
9784{
9785 if (buf->size() > 0) {
9786 const int idx = d->activeBufferOpCount++;
9787 if (idx < d->bufferOps.size())
9788 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, 0, 0, data);
9789 else
9790 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, 0, 0, data));
9791 }
9792}
9793
9794/*!
9795 \overload
9796 \since 6.10
9797
9798 Enqueues updating the entire QRhiBuffer \a buf created with the type
9799 QRhiBuffer::Immutable or QRhiBuffer::Static.
9800
9801 \a data is moved into the batch instead of copied with this overload.
9802
9803 \a data size must equal the size of \a buf.
9804 */
9805void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, QByteArray data)
9806{
9807 if (buf->size() > 0 && quint32(data.size()) == buf->size()) {
9808 const int idx = d->activeBufferOpCount++;
9809 if (idx < d->bufferOps.size())
9810 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, 0, std::move(data));
9811 else
9812 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, 0, std::move(data)));
9813 }
9814}
9815
9816/*!
9817 Enqueues reading back a region of the QRhiBuffer \a buf. The size of the
9818 region is specified by \a size in bytes, \a offset is the offset in bytes
9819 to start reading from.
9820
9821 A readback is asynchronous. \a result contains a callback that is invoked
9822 when the operation has completed. The data is provided in
9823 QRhiReadbackResult::data. Upon successful completion that QByteArray
9824 will have a size equal to \a size. On failure the QByteArray will be empty.
9825
9826 \note Reading buffers with a usage different than QRhiBuffer::UniformBuffer
9827 is supported only when the QRhi::ReadBackNonUniformBuffer feature is
9828 reported as supported.
9829
9830 \note The asynchronous readback is guaranteed to have completed when one of
9831 the following conditions is met: \l{QRhi::finish()}{finish()} has been
9832 called; or, at least \c N frames have been \l{QRhi::endFrame()}{submitted},
9833 including the frame that issued the readback operation, and the
9834 \l{QRhi::beginFrame()}{recording of a new frame} has been started, where \c
9835 N is the \l{QRhi::resourceLimit()}{resource limit value} returned for
9836 QRhi::MaxAsyncReadbackFrames.
9837
9838 \sa readBackTexture(), QRhi::isFeatureSupported(), QRhi::resourceLimit()
9839 */
9840void QRhiResourceUpdateBatch::readBackBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, QRhiReadbackResult *result)
9841{
9842 const int idx = d->activeBufferOpCount++;
9843 if (idx < d->bufferOps.size())
9844 d->bufferOps[idx] = QRhiResourceUpdateBatchPrivate::BufferOp::read(buf, offset, size, result);
9845 else
9846 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::read(buf, offset, size, result));
9847}
9848
9849/*!
9850 Enqueues uploading the image data for one or more mip levels in one or more
9851 layers of the texture \a tex.
9852
9853 The details of the copy (source QImage or compressed texture data, regions,
9854 target layers and levels) are described in \a desc.
9855 */
9856void QRhiResourceUpdateBatch::uploadTexture(QRhiTexture *tex, const QRhiTextureUploadDescription &desc)
9857{
9858 if (desc.cbeginEntries() != desc.cendEntries()) {
9859 const int idx = d->activeTextureOpCount++;
9860 if (idx < d->textureOps.size())
9861 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::upload(tex, desc);
9862 else
9863 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::upload(tex, desc));
9864 }
9865}
9866
9867/*!
9868 Enqueues uploading the image data for mip level 0 of layer 0 of the texture
9869 \a tex.
9870
9871 \a tex must have an uncompressed format. Its format must also be compatible
9872 with the QImage::format() of \a image. The source data is given in \a
9873 image.
9874 */
9875void QRhiResourceUpdateBatch::uploadTexture(QRhiTexture *tex, const QImage &image)
9876{
9877 uploadTexture(tex,
9878 QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(image)));
9879}
9880
9881/*!
9882 Enqueues a texture-to-texture copy operation from \a src into \a dst as
9883 described by \a desc.
9884
9885 \note The source texture \a src must be created with
9886 QRhiTexture::UsedAsTransferSource.
9887
9888 \note The format of the textures must match. With most graphics
9889 APIs the data is copied as-is without any format conversions. If
9890 \a dst and \a src are created with different formats, unspecified
9891 issues may arise.
9892 */
9893void QRhiResourceUpdateBatch::copyTexture(QRhiTexture *dst, QRhiTexture *src, const QRhiTextureCopyDescription &desc)
9894{
9895 const int idx = d->activeTextureOpCount++;
9896 if (idx < d->textureOps.size())
9897 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::copy(dst, src, desc);
9898 else
9899 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::copy(dst, src, desc));
9900}
9901
9902/*!
9903 Enqueues a texture-to-host copy operation as described by \a rb.
9904
9905 Normally \a rb will specify a QRhiTexture as the source. However, when the
9906 swapchain in the current frame was created with
9907 QRhiSwapChain::UsedAsTransferSource, it can also be the source of the
9908 readback. For this, leave the texture set to null in \a rb.
9909
9910 Unlike other operations, the results here need to be processed by the
9911 application. Therefore, \a result provides not just the data but also a
9912 callback as operations on the batch are asynchronous by nature:
9913
9914 \code
9915 rhi->beginFrame(swapchain);
9916 cb->beginPass(swapchain->currentFrameRenderTarget(), colorClear, dsClear);
9917 // ...
9918 QRhiReadbackResult *rbResult = new QRhiReadbackResult;
9919 rbResult->completed = [rbResult] {
9920 {
9921 const QImage::Format fmt = QImage::Format_RGBA8888_Premultiplied; // fits QRhiTexture::RGBA8
9922 const uchar *p = reinterpret_cast<const uchar *>(rbResult->data.constData());
9923 QImage image(p, rbResult->pixelSize.width(), rbResult->pixelSize.height(), fmt);
9924 image.save("result.png");
9925 }
9926 delete rbResult;
9927 };
9928 QRhiResourceUpdateBatch *u = nextResourceUpdateBatch();
9929 QRhiReadbackDescription rb; // no texture -> uses the current backbuffer of sc
9930 u->readBackTexture(rb, rbResult);
9931 cb->endPass(u);
9932 rhi->endFrame(swapchain);
9933 \endcode
9934
9935 \note The texture must be created with QRhiTexture::UsedAsTransferSource.
9936
9937 \note Multisample textures cannot be read back.
9938
9939 \note The readback returns raw byte data, in order to allow the applications
9940 to interpret it in any way they see fit. Be aware of the blending settings
9941 of rendering code: if the blending is set up to rely on premultiplied alpha,
9942 the results of the readback must also be interpreted as Premultiplied.
9943
9944 \note When interpreting the resulting raw data, be aware that the readback
9945 happens with a byte ordered format. A \l{QRhiTexture::RGBA8}{RGBA8} texture
9946 maps therefore to byte ordered QImage formats, such as,
9947 QImage::Format_RGBA8888.
9948
9949 \note The asynchronous readback is guaranteed to have completed when one of
9950 the following conditions is met: \l{QRhi::finish()}{finish()} has been
9951 called; or, at least \c N frames have been \l{QRhi::endFrame()}{submitted},
9952 including the frame that issued the readback operation, and the
9953 \l{QRhi::beginFrame()}{recording of a new frame} has been started, where \c
9954 N is the \l{QRhi::resourceLimit()}{resource limit value} returned for
9955 QRhi::MaxAsyncReadbackFrames.
9956
9957 A single readback operation copies one mip level of one layer (cubemap face
9958 or 3D slice or texture array element) at a time. The level and layer are
9959 specified by the respective fields in \a rb.
9960
9961 \sa readBackBuffer(), QRhi::resourceLimit()
9962 */
9963void QRhiResourceUpdateBatch::readBackTexture(const QRhiReadbackDescription &rb, QRhiReadbackResult *result)
9964{
9965 const int idx = d->activeTextureOpCount++;
9966 if (idx < d->textureOps.size())
9967 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::read(rb, result);
9968 else
9969 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::read(rb, result));
9970}
9971
9972/*!
9973 Enqueues a mipmap generation operation for the specified texture \a tex.
9974
9975 Both 2D and cube textures are supported.
9976
9977 \note The texture must be created with QRhiTexture::MipMapped and
9978 QRhiTexture::UsedWithGenerateMips.
9979
9980 \warning QRhi cannot guarantee that mipmaps can be generated for all
9981 supported texture formats. For example, QRhiTexture::RGBA32F is not a \c
9982 filterable format in OpenGL ES 3.0 and Metal on iOS, and therefore the
9983 mipmap generation request may fail. RGBA8 and RGBA16F are typically
9984 filterable, so it is recommended to use these formats when mipmap generation
9985 is desired.
9986 */
9987void QRhiResourceUpdateBatch::generateMips(QRhiTexture *tex)
9988{
9989 const int idx = d->activeTextureOpCount++;
9990 if (idx < d->textureOps.size())
9991 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::genMips(tex);
9992 else
9993 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::genMips(tex));
9994}
9995
9996/*!
9997 \return an available, empty batch to which copy type of operations can be
9998 recorded.
9999
10000 \note the return value is not owned by the caller and must never be
10001 destroyed. Instead, the batch is returned the pool for reuse by passing
10002 it to QRhiCommandBuffer::beginPass(), QRhiCommandBuffer::endPass(), or
10003 QRhiCommandBuffer::resourceUpdate(), or by calling
10004 QRhiResourceUpdateBatch::release() on it.
10005
10006 \note Can be called outside beginFrame() - endFrame() as well since a batch
10007 instance just collects data on its own, it does not perform any operations.
10008
10009 Due to not being tied to a frame being recorded, the following sequence is
10010 valid for example:
10011
10012 \code
10013 rhi->beginFrame(swapchain);
10014 QRhiResourceUpdateBatch *u = rhi->nextResourceUpdateBatch();
10015 u->uploadStaticBuffer(buf, data);
10016 // ... do not commit the batch
10017 rhi->endFrame();
10018 // u stays valid (assuming buf stays valid as well)
10019 rhi->beginFrame(swapchain);
10020 swapchain->currentFrameCommandBuffer()->resourceUpdate(u);
10021 // ... draw with buf
10022 rhi->endFrame();
10023 \endcode
10024
10025 \warning The maximum number of batches per QRhi is 64. When this limit is
10026 reached, the function will return null until a batch is returned to the
10027 pool.
10028 */
10029QRhiResourceUpdateBatch *QRhi::nextResourceUpdateBatch()
10030{
10031 // By default we prefer spreading out the utilization of the worst case 64
10032 // (but typically 4) batches as much as possible, meaning we won't pick the
10033 // first one even if it's free, but prefer picking one after the last picked
10034 // one. Relevant due to implicit sharing (the backend may hold on to the
10035 // QRhiBufferData until frame no. current+FramesInFlight-1, but
10036 // implementations may vary), combined with the desire to reuse container
10037 // and QRhiBufferData allocations in bufferOps instead of flooding every
10038 // frame with allocs. See free(). In typical Qt Quick scenes this leads to
10039 // eventually seeding all 4 (or more) resource batches with buffer operation
10040 // data allocations which may (*) then be reused in subsequent frames. This
10041 // comes at the expense of using more memory, but has proven good results
10042 // when (CPU) profiling typical Quick/Quick3D apps.
10043 //
10044 // (*) Due to implicit sharing(ish), the exact behavior is unpredictable. If
10045 // a backend holds on to the QRhiBufferData for, e.g., a dynamic buffer
10046 // update, and then there is a new assign() for that same QRhiBufferData
10047 // while the refcount is still 2, it will "detach" (without contents) and
10048 // there is no reuse of the alloc. This is mitigated by the 'choose the one
10049 // afer the last picked one' logic when handing out batches.
10050
10051 auto nextFreeBatch = [this]() -> QRhiResourceUpdateBatch * {
10052 auto isFree = [this](int i) -> QRhiResourceUpdateBatch * {
10053 const quint64 mask = 1ULL << quint64(i);
10054 if (!(d->resUpdPoolMap & mask)) {
10055 d->resUpdPoolMap |= mask;
10056 QRhiResourceUpdateBatch *u = d->resUpdPool[i];
10057 QRhiResourceUpdateBatchPrivate::get(u)->poolIndex = i;
10058 d->lastResUpdIdx = i;
10059 return u;
10060 }
10061 return nullptr;
10062 };
10063 const int poolSize = d->resUpdPool.size();
10064 for (int i = d->lastResUpdIdx + 1; i < poolSize; ++i) {
10065 if (QRhiResourceUpdateBatch *u = isFree(i))
10066 return u;
10067 }
10068 for (int i = 0; i <= d->lastResUpdIdx; ++i) {
10069 if (QRhiResourceUpdateBatch *u = isFree(i))
10070 return u;
10071 }
10072 return nullptr;
10073 };
10074
10075 QRhiResourceUpdateBatch *u = nextFreeBatch();
10076 if (!u) {
10077 const int oldSize = d->resUpdPool.size();
10078 // 4, 8, 12, ..., up to 64
10079 const int newSize = oldSize + qMin(4, qMax(0, 64 - oldSize));
10080 d->resUpdPool.resize(newSize);
10081 for (int i = oldSize; i < newSize; ++i)
10082 d->resUpdPool[i] = new QRhiResourceUpdateBatch(d);
10083 u = nextFreeBatch();
10084 if (!u)
10085 qWarning("Resource update batch pool exhausted (max is 64)");
10086 }
10087
10088 return u;
10089}
10090
10092{
10093 Q_ASSERT(poolIndex >= 0 && rhi->resUpdPool[poolIndex] == q);
10094
10095 quint32 bufferDataTotal = 0;
10096 quint32 bufferLargeAllocTotal = 0;
10097 for (const BufferOp &op : std::as_const(bufferOps)) {
10098 bufferDataTotal += op.data.size();
10099 bufferLargeAllocTotal += op.data.largeAlloc(); // alloc when > 1 KB
10100 }
10101
10102 if (QRHI_LOG_RUB().isDebugEnabled()) {
10103 qDebug() << "[rub] release to pool upd.batch #" << poolIndex
10104 << "/ bufferOps active" << activeBufferOpCount
10105 << "of" << bufferOps.count()
10106 << "data" << bufferDataTotal
10107 << "largeAlloc" << bufferLargeAllocTotal
10108 << "textureOps active" << activeTextureOpCount
10109 << "of" << textureOps.count();
10110 }
10111
10114
10115 const quint64 mask = 1ULL << quint64(poolIndex);
10116 rhi->resUpdPoolMap &= ~mask;
10117 poolIndex = -1;
10118
10119 // textureOps is cleared, to not keep the potentially large image pixel
10120 // data alive, but it is expected that the container keeps the list alloc
10121 // at least. Only trimOpList() goes for the more aggressive route with squeeze.
10122 textureOps.clear();
10123
10124 // bufferOps is not touched in many cases, to allow reusing allocations
10125 // (incl. in the elements' QRhiBufferData) as much as possible when this
10126 // batch is used again in the future, which is important for performance, in
10127 // particular with Qt Quick where it is easy for scenes to produce lots of,
10128 // typically small buffer changes on every frame.
10129 //
10130 // However, ensure that even in the unlikely case of having the max number
10131 // of batches (64) created in resUpdPool, no more than 64 MB in total is
10132 // used up by buffer data just to help future reuse. For simplicity, if
10133 // there is more than 1 MB data -> clear. Applications with frequent, huge
10134 // buffer updates probably have other bottlenecks anyway.
10135 if (bufferLargeAllocTotal > 1024 * 1024)
10136 bufferOps.clear();
10137}
10138
10140{
10141 int combinedSize = activeBufferOpCount + other->activeBufferOpCount;
10142 if (bufferOps.size() < combinedSize)
10143 bufferOps.resize(combinedSize);
10144 for (int i = activeBufferOpCount; i < combinedSize; ++i)
10145 bufferOps[i] = std::move(other->bufferOps[i - activeBufferOpCount]);
10147
10148 combinedSize = activeTextureOpCount + other->activeTextureOpCount;
10149 if (textureOps.size() < combinedSize)
10150 textureOps.resize(combinedSize);
10151 for (int i = activeTextureOpCount; i < combinedSize; ++i)
10152 textureOps[i] = std::move(other->textureOps[i - activeTextureOpCount]);
10154}
10155
10161
10163{
10164 // Unlike free(), this is expected to aggressively deallocate all memory
10165 // used by both the buffer and texture operation lists. (i.e. using
10166 // squeeze() to only keep the stack prealloc of the QVLAs)
10167 //
10168 // This (e.g. just the destruction of bufferOps elements) may have a
10169 // non-negligible performance impact e.g. with Qt Quick with scenes where
10170 // there are lots of buffer operations per frame.
10171
10173 bufferOps.clear();
10174 bufferOps.squeeze();
10175
10177 textureOps.clear();
10178 textureOps.squeeze();
10179}
10180
10181/*!
10182 Sometimes committing resource updates is necessary or just more convenient
10183 without starting a render pass. Calling this function with \a
10184 resourceUpdates is an alternative to passing \a resourceUpdates to a
10185 beginPass() call (or endPass(), which would be typical in case of readbacks).
10186
10187 \note Cannot be called inside a pass.
10188 */
10189void QRhiCommandBuffer::resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates)
10190{
10191 if (resourceUpdates)
10192 m_rhi->resourceUpdate(this, resourceUpdates);
10193}
10194
10195/*!
10196 Records starting a new render pass targeting the render target \a rt.
10197
10198 \a resourceUpdates, when not null, specifies a resource update batch that
10199 is to be committed and then released.
10200
10201 The color and depth/stencil buffers of the render target are normally
10202 cleared. The clear values are specified in \a colorClearValue and \a
10203 depthStencilClearValue. The exception is when the render target was created
10204 with QRhiTextureRenderTarget::PreserveColorContents and/or
10205 QRhiTextureRenderTarget::PreserveDepthStencilContents. The clear values are
10206 ignored then.
10207
10208 \note Enabling preserved color or depth contents leads to decreased
10209 performance depending on the underlying hardware. Mobile GPUs with tiled
10210 architecture benefit from not having to reload the previous contents into
10211 the tile buffer. Similarly, a QRhiTextureRenderTarget with a QRhiTexture as
10212 the depth buffer is less efficient than a QRhiRenderBuffer since using a
10213 depth texture triggers requiring writing the data out to it, while with
10214 renderbuffers this is not needed (as the API does not allow sampling or
10215 reading from a renderbuffer).
10216
10217 \note Do not assume that any state or resource bindings persist between
10218 passes.
10219
10220 \note The QRhiCommandBuffer's \c set and \c draw functions can only be
10221 called inside a pass. Also, with the exception of setGraphicsPipeline(),
10222 they expect to have a pipeline set already on the command buffer.
10223 Unspecified issues may arise otherwise, depending on the backend.
10224
10225 If \a rt is a QRhiTextureRenderTarget, beginPass() performs a check to see
10226 if the texture and renderbuffer objects referenced from the render target
10227 are up-to-date. This is similar to what setShaderResources() does for
10228 QRhiShaderResourceBindings. If any of the attachments had been rebuilt
10229 since QRhiTextureRenderTarget::create(), an implicit call to create() is
10230 made on \a rt. Therefore, if \a rt has a QRhiTexture color attachment \c
10231 texture, and one needs to make the texture a different size, the following
10232 is then valid:
10233 \code
10234 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ { texture } });
10235 rt->create();
10236 // ...
10237 texture->setPixelSize(new_size);
10238 texture->create();
10239 cb->beginPass(rt, colorClear, dsClear); // this is ok, no explicit rt->create() is required before
10240 \endcode
10241
10242 \a flags allow controlling certain advanced functionality. One commonly used
10243 flag is \c ExternalContents. This should be specified whenever
10244 beginExternal() will be called within the pass started by this function.
10245
10246 \sa endPass(), BeginPassFlags
10247 */
10248void QRhiCommandBuffer::beginPass(QRhiRenderTarget *rt,
10249 const QColor &colorClearValue,
10250 const QRhiDepthStencilClearValue &depthStencilClearValue,
10251 QRhiResourceUpdateBatch *resourceUpdates,
10252 BeginPassFlags flags)
10253{
10254 m_rhi->beginPass(this, rt, colorClearValue, depthStencilClearValue, resourceUpdates, flags);
10255}
10256
10257/*!
10258 Records ending the current render pass.
10259
10260 \a resourceUpdates, when not null, specifies a resource update batch that
10261 is to be committed and then released.
10262
10263 \sa beginPass()
10264 */
10265void QRhiCommandBuffer::endPass(QRhiResourceUpdateBatch *resourceUpdates)
10266{
10267 m_rhi->endPass(this, resourceUpdates);
10268}
10269
10270/*!
10271 Records setting a new graphics pipeline \a ps.
10272
10273 \note This function must be called before recording other \c set or \c draw
10274 commands on the command buffer.
10275
10276 \note QRhi will optimize out unnecessary invocations within a pass, so
10277 therefore overoptimizing to avoid calls to this function is not necessary
10278 on the applications' side.
10279
10280 \note This function can only be called inside a render pass, meaning
10281 between a beginPass() and endPass() call.
10282
10283 \note The new graphics pipeline \a ps must be a valid pointer.
10284
10285 Setting a graphics pipeline that does not have the
10286 \l{QRhiGraphicsPipeline::}{UsesScissor} flag will either disable scissoring,
10287 with graphics APIs where that is applicable, or set the scissor rectangle to
10288 match the viewport that was last set (with graphics APIs where scissoring is
10289 effectively always active), in order to ensure a uniform behavior across QRhi
10290 backends.
10291 */
10292void QRhiCommandBuffer::setGraphicsPipeline(QRhiGraphicsPipeline *ps)
10293{
10294 Q_ASSERT(ps != nullptr);
10295 m_rhi->setGraphicsPipeline(this, ps);
10296}
10297
10298/*!
10299 Records binding a set of shader resources, such as, uniform buffers or
10300 textures, that are made visible to one or more shader stages.
10301
10302 \a srb can be null in which case the current graphics or compute pipeline's
10303 associated QRhiShaderResourceBindings is used. When \a srb is non-null, it
10304 must be
10305 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible},
10306 meaning the layout (number of bindings, the type and binding number of each
10307 binding) must fully match the QRhiShaderResourceBindings that was
10308 associated with the pipeline at the time of calling the pipeline's create().
10309
10310 There are cases when a seemingly unnecessary setShaderResources() call is
10311 mandatory: when rebuilding a resource referenced from \a srb, for example
10312 changing the size of a QRhiBuffer followed by a QRhiBuffer::create(), this
10313 is the place where associated native objects (such as descriptor sets in
10314 case of Vulkan) are updated to refer to the current native resources that
10315 back the QRhiBuffer, QRhiTexture, QRhiSampler objects referenced from \a
10316 srb. In this case setShaderResources() must be called even if \a srb is
10317 the same as in the last call.
10318
10319 When \a srb is not null, the QRhiShaderResourceBindings object the pipeline
10320 was built with in create() is guaranteed to be not accessed in any form. In
10321 fact, it does not need to be valid even at this point: destroying the
10322 pipeline's associated srb after create() and instead explicitly specifying
10323 another, \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout
10324 compatible} one in every setShaderResources() call is valid.
10325
10326 \a dynamicOffsets allows specifying buffer offsets for uniform buffers that
10327 were associated with \a srb via
10328 QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(). This is
10329 different from providing the offset in the \a srb itself: dynamic offsets
10330 do not require building a new QRhiShaderResourceBindings for every
10331 different offset, can avoid writing the underlying descriptors (with
10332 backends where applicable), and so they may be more efficient. Each element
10333 of \a dynamicOffsets is a \c binding - \c offset pair.
10334 \a dynamicOffsetCount specifies the number of elements in \a dynamicOffsets.
10335
10336 \note All offsets in \a dynamicOffsets must be byte aligned to the value
10337 returned from QRhi::ubufAlignment().
10338
10339 \note Some backends may limit the number of supported dynamic offsets.
10340 Avoid using a \a dynamicOffsetCount larger than 8.
10341
10342 \note QRhi will optimize out unnecessary invocations within a pass (taking
10343 the conditions described above into account), so therefore overoptimizing
10344 to avoid calls to this function is not necessary on the applications' side.
10345
10346 \note This function can only be called inside a render or compute pass,
10347 meaning between a beginPass() and endPass(), or beginComputePass() and
10348 endComputePass().
10349 */
10350void QRhiCommandBuffer::setShaderResources(QRhiShaderResourceBindings *srb,
10351 int dynamicOffsetCount,
10352 const DynamicOffset *dynamicOffsets)
10353{
10354 m_rhi->setShaderResources(this, srb, dynamicOffsetCount, dynamicOffsets);
10355}
10356
10357/*!
10358 Records vertex input bindings.
10359
10360 The index buffer used by subsequent drawIndexed() commands is specified by
10361 \a indexBuf, \a indexOffset, and \a indexFormat. \a indexBuf can be set to
10362 null when indexed drawing is not needed.
10363
10364 Vertex buffer bindings are batched. \a startBinding specifies the first
10365 binding number. The recorded command then binds each buffer from \a
10366 bindings to the binding point \c{startBinding + i} where \c i is the index
10367 in \a bindings. Each element in \a bindings specifies a QRhiBuffer and an
10368 offset.
10369
10370 \note Some backends may limit the number of vertex buffer bindings. Avoid
10371 using a \a bindingCount larger than 8.
10372
10373 Superfluous vertex input and index changes in the same pass are ignored
10374 automatically with most backends and therefore applications do not need to
10375 overoptimize to avoid calls to this function.
10376
10377 \note This function can only be called inside a render pass, meaning
10378 between a beginPass() and endPass() call.
10379
10380 As a simple example, take a vertex shader with two inputs:
10381
10382 \badcode
10383 layout(location = 0) in vec4 position;
10384 layout(location = 1) in vec3 color;
10385 \endcode
10386
10387 and assume we have the data available in interleaved format, using only 2
10388 floats for position (so 5 floats per vertex: x, y, r, g, b). A QRhiGraphicsPipeline for
10389 this shader can then be created using the input layout:
10390
10391 \code
10392 QRhiVertexInputLayout inputLayout;
10393 inputLayout.setBindings({
10394 { 5 * sizeof(float) }
10395 });
10396 inputLayout.setAttributes({
10397 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
10398 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
10399 });
10400 \endcode
10401
10402 Here there is one buffer binding (binding number 0), with two inputs
10403 referencing it. When recording the pass, once the pipeline is set, the
10404 vertex bindings can be specified simply like the following, assuming vbuf
10405 is the QRhiBuffer with all the interleaved position+color data:
10406
10407 \code
10408 const QRhiCommandBuffer::VertexInput vbufBinding(vbuf, 0);
10409 cb->setVertexInput(0, 1, &vbufBinding);
10410 \endcode
10411 */
10412void QRhiCommandBuffer::setVertexInput(int startBinding, int bindingCount, const VertexInput *bindings,
10413 QRhiBuffer *indexBuf, quint32 indexOffset,
10414 IndexFormat indexFormat)
10415{
10416 m_rhi->setVertexInput(this, startBinding, bindingCount, bindings, indexBuf, indexOffset, indexFormat);
10417}
10418
10419/*!
10420 Records setting the active viewport rectangle specified in \a viewport.
10421
10422 With backends where the underlying graphics API has scissoring always
10423 enabled, this function also sets the scissor to match the viewport whenever
10424 the active QRhiGraphicsPipeline does not have
10425 \l{QRhiGraphicsPipeline::UsesScissor}{UsesScissor} set.
10426
10427 \note QRhi assumes OpenGL-style viewport coordinates, meaning x and y are
10428 bottom-left.
10429
10430 \note This function can only be called inside a render pass, meaning
10431 between a beginPass() and endPass() call.
10432 */
10433void QRhiCommandBuffer::setViewport(const QRhiViewport &viewport)
10434{
10435 m_rhi->setViewport(this, viewport);
10436}
10437
10438/*!
10439 Records setting the active scissor rectangle specified in \a scissor.
10440
10441 This can only be called when the bound pipeline has
10442 \l{QRhiGraphicsPipeline::UsesScissor}{UsesScissor} set. When the flag is
10443 set on the active pipeline, this function must be called because scissor
10444 testing will get enabled and so a scissor rectangle must be provided.
10445
10446 \note QRhi assumes OpenGL-style viewport coordinates, meaning x and y are
10447 bottom-left.
10448
10449 \note This function can only be called inside a render pass, meaning
10450 between a beginPass() and endPass() call.
10451 */
10452void QRhiCommandBuffer::setScissor(const QRhiScissor &scissor)
10453{
10454 m_rhi->setScissor(this, scissor);
10455}
10456
10457/*!
10458 Records setting the active blend constants to \a c.
10459
10460 This can only be called when the bound pipeline has
10461 QRhiGraphicsPipeline::UsesBlendConstants set.
10462
10463 \note This function can only be called inside a render pass, meaning
10464 between a beginPass() and endPass() call.
10465 */
10466void QRhiCommandBuffer::setBlendConstants(const QColor &c)
10467{
10468 m_rhi->setBlendConstants(this, c);
10469}
10470
10471/*!
10472 Records setting the active stencil reference value to \a refValue.
10473
10474 This can only be called when the bound pipeline has
10475 QRhiGraphicsPipeline::UsesStencilRef set.
10476
10477 \note This function can only be called inside a render pass, meaning between
10478 a beginPass() and endPass() call.
10479 */
10480void QRhiCommandBuffer::setStencilRef(quint32 refValue)
10481{
10482 m_rhi->setStencilRef(this, refValue);
10483}
10484
10485/*!
10486 Sets the shading rate for the following draw calls to \a coarsePixelSize.
10487
10488 The default is 1x1.
10489
10490 Functional only when the \l QRhi::VariableRateShading feature is reported as
10491 supported and the QRhiGraphicsPipeline(s) bound on the command buffer were
10492 declaring \l QRhiGraphicsPipeline::UsesShadingRate when creating them.
10493
10494 Call \l QRhi::supportedShadingRates() to check what shading rates are
10495 supported for a given sample count.
10496
10497 When both a QRhiShadingRateMap and this function is in use, the higher of
10498 two the shading rates are used for each tile. There is currently no control
10499 offered over the combiner behavior.
10500
10501 \since 6.9
10502 */
10503void QRhiCommandBuffer::setShadingRate(const QSize &coarsePixelSize)
10504{
10505 m_rhi->setShadingRate(this, coarsePixelSize);
10506}
10507
10508/*!
10509 Records a non-indexed draw.
10510
10511 The number of vertices is specified in \a vertexCount. For instanced
10512 drawing set \a instanceCount to a value other than 1. \a firstVertex is the
10513 index of the first vertex to draw. When drawing multiple instances, the
10514 first instance ID is specified by \a firstInstance.
10515
10516 \note \a firstInstance may not be supported, and is ignored when the
10517 QRhi::BaseInstance feature is reported as not supported. The first instance
10518 ID is always 0 in that case. QRhi::BaseInstance is never supported with
10519 OpenGL at the moment, mainly due to OpenGL ES limitations, and therefore
10520 portable applications should not be designed to rely on this argument.
10521
10522 \note Shaders that need to access the index of the current vertex or
10523 instance must use \c gl_VertexIndex and \c gl_InstanceIndex, i.e., the
10524 Vulkan-compatible built-in variables, instead of \c gl_VertexID and \c
10525 gl_InstanceID.
10526
10527 \note When \a firstInstance is non-zero, \c gl_InstanceIndex will not
10528 include the base value with some of the underlying 3D APIs. This is
10529 indicated by the QRhi::InstanceIndexIncludesBaseInstance feature. If relying
10530 on a base instance value cannot be avoided, applications are advised to pass
10531 in the value as a uniform conditionally based on what that feature reports,
10532 and add it to \c gl_InstanceIndex in the shader.
10533
10534 \note This function can only be called inside a render pass, meaning
10535 between a beginPass() and endPass() call.
10536 */
10537void QRhiCommandBuffer::draw(quint32 vertexCount,
10538 quint32 instanceCount,
10539 quint32 firstVertex,
10540 quint32 firstInstance)
10541{
10542 m_rhi->draw(this, vertexCount, instanceCount, firstVertex, firstInstance);
10543}
10544
10545/*!
10546 Records an indexed draw.
10547
10548 The number of vertices is specified in \a indexCount. \a firstIndex is the
10549 base index. The effective offset in the index buffer is given by
10550 \c{indexOffset + firstIndex * n} where \c n is 2 or 4 depending on the
10551 index element type. \c indexOffset is specified in setVertexInput().
10552
10553 \note The effective offset in the index buffer must be 4 byte aligned with
10554 some backends (for example, Metal). With these backends the
10555 \l{QRhi::NonFourAlignedEffectiveIndexBufferOffset}{NonFourAlignedEffectiveIndexBufferOffset}
10556 feature will be reported as not-supported.
10557
10558 \a vertexOffset (also called \c{base vertex}) is a signed value that is
10559 added to the element index before indexing into the vertex buffer. Support
10560 for this is not always available, and the value is ignored when the feature
10561 QRhi::BaseVertex is reported as unsupported.
10562
10563 For instanced drawing set \a instanceCount to a value other than 1. When
10564 drawing multiple instances, the first instance ID is specified by \a
10565 firstInstance.
10566
10567 \note \a firstInstance may not be supported, and is ignored when the
10568 QRhi::BaseInstance feature is reported as not supported. The first instance
10569 ID is always 0 in that case. QRhi::BaseInstance is never supported with
10570 OpenGL at the moment, mainly due to OpenGL ES limitations, and therefore
10571 portable applications should not be designed to rely on this argument.
10572
10573 \note Shaders that need to access the index of the current vertex or
10574 instance must use \c gl_VertexIndex and \c gl_InstanceIndex, i.e., the
10575 Vulkan-compatible built-in variables, instead of \c gl_VertexID and \c
10576 gl_InstanceID.
10577
10578 \note When \a firstInstance is non-zero, \c gl_InstanceIndex will not
10579 include the base value with some of the underlying 3D APIs. This is
10580 indicated by the QRhi::InstanceIndexIncludesBaseInstance feature. If relying
10581 on a base instance value cannot be avoided, applications are advised to pass
10582 in the value as a uniform conditionally based on what that feature reports,
10583 and add it to \c gl_InstanceIndex in the shader.
10584
10585 \note This function can only be called inside a render pass, meaning
10586 between a beginPass() and endPass() call.
10587 */
10588void QRhiCommandBuffer::drawIndexed(quint32 indexCount,
10589 quint32 instanceCount,
10590 quint32 firstIndex,
10591 qint32 vertexOffset,
10592 quint32 firstInstance)
10593{
10594 m_rhi->drawIndexed(this, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
10595}
10596
10597/*!
10598 Records a non-indexed, indirect draw.
10599
10600 The draw parameters are provided by the buffer specified in \a indirectBuffer,
10601 which must contain an array of elements of type QRhiIndirectDrawCommand.
10602 The parameters in QRhiIndirectDrawCommand have the same meaning as in draw().
10603
10604 The offset, in bytes, from which the parameters are read in the buffer is specified
10605 by \a indirectBufferOffset.
10606
10607 \a drawCount specifies the number of such draw commands to issue.
10608
10609 \a stride indicates the byte size of each individual draw command structure
10610 in the buffer. This allows interleaving custom data between commands if needed.
10611 The value must be a multiple of 4 and greater than or equal to sizeof(QRhiIndirectDrawCommand).
10612
10613 \note A \a drawCount value greater than 1 is only natively supported if the
10614 QRhi::DrawIndirectMulti feature is reported as supported and stride is the default.
10615 Otherwise, this function emulates multi-draw by recording multiple draw calls,
10616 offering no performance benefit over repeated draw() calls.
10617
10618 \note This function can only be called inside a render pass, meaning
10619 between a beginPass() and endPass() call.
10620
10621 \since 6.12
10622 */
10623void QRhiCommandBuffer::drawIndirect(QRhiBuffer *indirectBuffer,
10624 quint32 indirectBufferOffset,
10625 quint32 drawCount,
10626 quint32 stride)
10627{
10628 Q_ASSERT(indirectBuffer);
10629 Q_ASSERT(indirectBuffer->usage().testFlag(QRhiBuffer::IndirectBuffer));
10630 Q_ASSERT_X((indirectBufferOffset & 3u) == 0u, Q_FUNC_INFO, "indirectBufferOffset must be a multiple of 4");
10631 Q_ASSERT(stride >= sizeof(QRhiIndirectDrawCommand));
10632 Q_ASSERT_X((stride & 3u) == 0u, Q_FUNC_INFO, "stride must be a multiple of 4");
10633 m_rhi->drawIndirect(this, indirectBuffer, indirectBufferOffset, drawCount, stride);
10634}
10635
10636/*!
10637 Records an indexed, indirect draw.
10638
10639 The draw parameters are provided by the buffer specified in \a indirectBuffer,
10640 which must contain an array of elements of type QRhiIndexedIndirectDrawCommand.
10641 The parameters in QRhiIndexedIndirectDrawCommand have the same meaning as in drawIndexed().
10642
10643 The offset, in bytes, from which the parameters are read in the buffer is specified
10644 by \a indirectBufferOffset.
10645
10646 \a drawCount specifies the number of such draw commands to issue.
10647
10648 \a stride indicates the byte size of each individual draw command structure
10649 in the buffer. This allows interleaving custom data between commands if needed.
10650 The value must be a multiple of 4 and greater than or equal to sizeof(QRhiIndexedIndirectDrawCommand).
10651
10652 \note A \a drawCount value greater than 1 is only natively supported if the
10653 QRhi::DrawIndirectMulti feature is reported as supported and stride is the default.
10654 Otherwise, this function emulates multi-draw by recording multiple draw calls,
10655 offering no performance benefit over repeated draw() calls.
10656
10657 \note This function can only be called inside a render pass, meaning
10658 between a beginPass() and endPass() call.
10659
10660 \since 6.12
10661 */
10662void QRhiCommandBuffer::drawIndexedIndirect(QRhiBuffer *indirectBuffer,
10663 quint32 indirectBufferOffset,
10664 quint32 drawCount,
10665 quint32 stride)
10666{
10667 Q_ASSERT(indirectBuffer);
10668 Q_ASSERT(indirectBuffer->usage().testFlag(QRhiBuffer::IndirectBuffer));
10669 Q_ASSERT_X((indirectBufferOffset & 3u) == 0u, Q_FUNC_INFO, "indirectBufferOffset must be a multiple of 4");
10670 Q_ASSERT(stride >= sizeof(QRhiIndexedIndirectDrawCommand));
10671 Q_ASSERT_X((stride & 3u) == 0u, Q_FUNC_INFO, "stride must be a multiple of 4");
10672 m_rhi->drawIndexedIndirect(this, indirectBuffer, indirectBufferOffset, drawCount, stride);
10673}
10674
10675/*!
10676 Records a named debug group on the command buffer with the specified \a
10677 name. This is shown in graphics debugging tools such as
10678 \l{https://renderdoc.org/}{RenderDoc} and
10679 \l{https://developer.apple.com/xcode/}{XCode}. The end of the grouping is
10680 indicated by debugMarkEnd().
10681
10682 \note Ignored when QRhi::DebugMarkers are not supported or
10683 QRhi::EnableDebugMarkers is not set.
10684
10685 \note Can be called anywhere within the frame, both inside and outside of passes.
10686 */
10687void QRhiCommandBuffer::debugMarkBegin(const QByteArray &name)
10688{
10689 m_rhi->debugMarkBegin(this, name);
10690}
10691
10692/*!
10693 Records the end of a debug group.
10694
10695 \note Ignored when QRhi::DebugMarkers are not supported or
10696 QRhi::EnableDebugMarkers is not set.
10697
10698 \note Can be called anywhere within the frame, both inside and outside of passes.
10699 */
10700void QRhiCommandBuffer::debugMarkEnd()
10701{
10702 m_rhi->debugMarkEnd(this);
10703}
10704
10705/*!
10706 Inserts a debug message \a msg into the command stream.
10707
10708 \note Ignored when QRhi::DebugMarkers are not supported or
10709 QRhi::EnableDebugMarkers is not set.
10710
10711 \note With some backends debugMarkMsg() is only supported inside a pass and
10712 is ignored when called outside a pass. With others it is recorded anywhere
10713 within the frame.
10714 */
10715void QRhiCommandBuffer::debugMarkMsg(const QByteArray &msg)
10716{
10717 m_rhi->debugMarkMsg(this, msg);
10718}
10719
10720/*!
10721 Records starting a new compute pass.
10722
10723 \a resourceUpdates, when not null, specifies a resource update batch that
10724 is to be committed and then released.
10725
10726 \note Do not assume that any state or resource bindings persist between
10727 passes.
10728
10729 \note A compute pass can record setComputePipeline(), setShaderResources(),
10730 and dispatch() calls, not graphics ones. General functionality, such as,
10731 debug markers and beginExternal() is available both in render and compute
10732 passes.
10733
10734 \note Compute is only available when the \l{QRhi::Compute}{Compute} feature
10735 is reported as supported.
10736
10737 \a flags is not currently used.
10738 */
10739void QRhiCommandBuffer::beginComputePass(QRhiResourceUpdateBatch *resourceUpdates, BeginPassFlags flags)
10740{
10741 m_rhi->beginComputePass(this, resourceUpdates, flags);
10742}
10743
10744/*!
10745 Records ending the current compute pass.
10746
10747 \a resourceUpdates, when not null, specifies a resource update batch that
10748 is to be committed and then released.
10749 */
10750void QRhiCommandBuffer::endComputePass(QRhiResourceUpdateBatch *resourceUpdates)
10751{
10752 m_rhi->endComputePass(this, resourceUpdates);
10753}
10754
10755/*!
10756 Records setting a new compute pipeline \a ps.
10757
10758 \note This function must be called before recording setShaderResources() or
10759 dispatch() commands on the command buffer.
10760
10761 \note QRhi will optimize out unnecessary invocations within a pass, so
10762 therefore overoptimizing to avoid calls to this function is not necessary
10763 on the applications' side.
10764
10765 \note This function can only be called inside a compute pass, meaning
10766 between a beginComputePass() and endComputePass() call.
10767 */
10768void QRhiCommandBuffer::setComputePipeline(QRhiComputePipeline *ps)
10769{
10770 m_rhi->setComputePipeline(this, ps);
10771}
10772
10773/*!
10774 Records dispatching compute work items, with \a x, \a y, and \a z
10775 specifying the number of local workgroups in the corresponding dimension.
10776
10777 \note This function can only be called inside a compute pass, meaning
10778 between a beginComputePass() and endComputePass() call.
10779
10780 \note \a x, \a y, and \a z must fit the limits from the underlying graphics
10781 API implementation at run time. The maximum values are typically 65535.
10782
10783 \note Watch out for possible limits on the local workgroup size as well.
10784 This is specified in the shader, for example: \c{layout(local_size_x = 16,
10785 local_size_y = 16) in;}. For example, with OpenGL the minimum value mandated
10786 by the specification for the number of invocations in a single local work
10787 group (the product of \c local_size_x, \c local_size_y, and \c local_size_z)
10788 is 1024, while with OpenGL ES (3.1) the value may be as low as 128. This
10789 means that the example given above may be rejected by some OpenGL ES
10790 implementations as the number of invocations is 256.
10791 */
10792void QRhiCommandBuffer::dispatch(int x, int y, int z)
10793{
10794 m_rhi->dispatch(this, x, y, z);
10795}
10796
10797/*!
10798 \return a pointer to a backend-specific QRhiNativeHandles subclass, such as
10799 QRhiVulkanCommandBufferNativeHandles. The returned value is \nullptr when
10800 exposing the underlying native resources is not supported by, or not
10801 applicable to, the backend.
10802
10803 \sa QRhiVulkanCommandBufferNativeHandles,
10804 QRhiMetalCommandBufferNativeHandles, beginExternal(), endExternal()
10805 */
10806const QRhiNativeHandles *QRhiCommandBuffer::nativeHandles()
10807{
10808 return m_rhi->nativeHandles(this);
10809}
10810
10811/*!
10812 To be called when the application before the application is about to
10813 enqueue commands to the current pass' command buffer by calling graphics
10814 API functions directly.
10815
10816 \note This is only available when the intent was declared upfront in
10817 beginPass() or beginComputePass(). Therefore this function must only be
10818 called when the pass recording was started with specifying
10819 QRhiCommandBuffer::ExternalContent.
10820
10821 With Vulkan, Metal, or Direct3D 12 one can query the native command buffer
10822 or encoder objects via nativeHandles() and enqueue commands to them. With
10823 OpenGL or Direct3D 11 the (device) context can be retrieved from
10824 QRhi::nativeHandles(). However, this must never be done without ensuring
10825 the QRhiCommandBuffer's state stays up-to-date. Hence the requirement for
10826 wrapping any externally added command recording between beginExternal() and
10827 endExternal(). Conceptually this is the same as QPainter's
10828 \l{QPainter::beginNativePainting()}{beginNativePainting()} and
10829 \l{QPainter::endNativePainting()}{endNativePainting()} functions.
10830
10831 For OpenGL in particular, this function has an additional task: it makes
10832 sure the context is made current on the current thread.
10833
10834 \note Once beginExternal() is called, no other render pass specific
10835 functions (\c set* or \c draw*) must be called on the
10836 QRhiCommandBuffer until endExternal().
10837
10838 \warning Some backends may return a native command buffer object from
10839 QRhiCommandBuffer::nativeHandles() that is different from the primary one
10840 when inside a beginExternal() - endExternal() block. Therefore it is
10841 important to (re)query the native command buffer object after calling
10842 beginExternal(). In practical terms this means that with Vulkan for example
10843 the externally recorded Vulkan commands are placed onto a secondary command
10844 buffer (with VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT).
10845 nativeHandles() returns this secondary command buffer when called between
10846 begin/endExternal.
10847
10848 \sa endExternal(), nativeHandles()
10849 */
10850void QRhiCommandBuffer::beginExternal()
10851{
10852 m_rhi->beginExternal(this);
10853}
10854
10855/*!
10856 To be called once the externally added commands are recorded to the command
10857 buffer or context.
10858
10859 \note All QRhiCommandBuffer state must be assumed as invalid after calling
10860 this function. Pipelines, vertex and index buffers, and other state must be
10861 set again if more draw calls are recorded after the external commands.
10862
10863 \sa beginExternal(), nativeHandles()
10864 */
10865void QRhiCommandBuffer::endExternal()
10866{
10867 m_rhi->endExternal(this);
10868}
10869
10870/*!
10871 \return the last available timestamp, in seconds, when
10872 \l QRhi::EnableTimestamps was enabled when creating the QRhi. The value
10873 indicates the elapsed time on the GPU during the last completed frame.
10874
10875 \note Do not expect results other than 0 when the QRhi::Timestamps feature
10876 is not reported as supported, or when QRhi::EnableTimestamps was not passed
10877 to QRhi::create(). There are exceptions to this, because with some graphics
10878 APIs (Metal) timings are available without having to perform extra
10879 operations (timestamp queries), but portable applications should always
10880 consciously opt-in to timestamp collection when they know it is needed, and
10881 call this function accordingly.
10882
10883 Care must be exercised with the interpretation of the value, as its
10884 precision and granularity is often not controlled by Qt, and depends on the
10885 underlying graphics API and its implementation. In particular, comparing
10886 the values between different graphics APIs and hardware is discouraged and
10887 may be meaningless.
10888
10889 When the frame was recorded with \l{QRhi::beginFrame()}{beginFrame()} and
10890 \l{QRhi::endFrame()}{endFrame()}, i.e., with a swapchain, the timing values
10891 will likely become available asynchronously. The returned value may
10892 therefore be 0 (e.g., for the first 1-2 frames) or the last known value
10893 referring to some previous frame. The value my also
10894 become 0 again under certain conditions, such as when resizing the window.
10895 It can be expected that the most up-to-date available value is retrieved in
10896 beginFrame() and becomes queriable via this function once beginFrame()
10897 returns.
10898
10899 \note Do not assume that the value refers to the previous
10900 (\c{currently_recorded - 1}) frame. It may refer to \c{currently_recorded -
10901 2} or \c{currently_recorded - 3} as well. The exact behavior may depend on
10902 the graphics API and its implementation.
10903
10904 On the other hand, with offscreen frames the returned value is up-to-date
10905 once \l{QRhi::endOffscreenFrame()}{endOffscreenFrame()} returns, because
10906 offscreen frames reduce GPU pipelining and wait the the commands to be
10907 complete.
10908
10909 \note This means that, unlike with swapchain frames, with offscreen frames
10910 the returned value is guaranteed to refer to the frame that has just been
10911 submitted and completed. (assuming this function is called after
10912 endOffscreenFrame() but before the next beginOffscreenFrame())
10913
10914 Watch out for the consequences of GPU frequency scaling and GPU clock
10915 changes, depending on the platform. For example, on Windows the returned
10916 timing may vary in a quite wide range between frames with modern graphics
10917 cards, even when submitting frames with a similar, or the same workload.
10918 This is out of scope for Qt to control and solve, generally speaking.
10919 However, the D3D12 backend automatically calls
10920 \l{https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-setstablepowerstate}{ID3D12Device::SetStablePowerState()}
10921 whenever the environment variable \c QT_D3D_STABLE_POWER_STATE is set to a
10922 non-zero value. This can greatly stabilize the result. It can also have a
10923 non-insignificant effect on the CPU-side timings measured via QElapsedTimer
10924 for example, especially when offscreen frames are involved.
10925
10926 \note Do not and never ship applications to production with
10927 \c QT_D3D_STABLE_POWER_STATE set. See the Windows API documentation for details.
10928
10929 \sa QRhi::Timestamps, QRhi::EnableTimestamps
10930 */
10931double QRhiCommandBuffer::lastCompletedGpuTime()
10932{
10933 return m_rhi->lastCompletedGpuTime(this);
10934}
10935
10936/*!
10937 \return the value (typically an offset) \a v aligned to the uniform buffer
10938 alignment given by by ubufAlignment().
10939 */
10940int QRhi::ubufAligned(int v) const
10941{
10942 const int byteAlign = ubufAlignment();
10943 return (v + byteAlign - 1) & ~(byteAlign - 1);
10944}
10945
10946/*!
10947 \return the number of mip levels for a given \a size.
10948 */
10949int QRhi::mipLevelsForSize(const QSize &size)
10950{
10951 return qFloor(std::log2(qMax(size.width(), size.height()))) + 1;
10952}
10953
10954/*!
10955 \return the texture image size for a given \a mipLevel, calculated based on
10956 the level 0 size given in \a baseLevelSize.
10957 */
10958QSize QRhi::sizeForMipLevel(int mipLevel, const QSize &baseLevelSize)
10959{
10960 const int w = qMax(1, baseLevelSize.width() >> mipLevel);
10961 const int h = qMax(1, baseLevelSize.height() >> mipLevel);
10962 return QSize(w, h);
10963}
10964
10965/*!
10966 \return \c true if the underlying graphics API has the Y axis pointing up
10967 in framebuffers and images.
10968
10969 In practice this is \c true for OpenGL only.
10970 */
10971bool QRhi::isYUpInFramebuffer() const
10972{
10973 return d->isYUpInFramebuffer();
10974}
10975
10976/*!
10977 \return \c true if the underlying graphics API has the Y axis pointing up
10978 in its normalized device coordinate system.
10979
10980 In practice this is \c false for Vulkan only.
10981
10982 \note clipSpaceCorrMatrix() includes the corresponding adjustment (to make
10983 Y point up) in its returned matrix.
10984 */
10985bool QRhi::isYUpInNDC() const
10986{
10987 return d->isYUpInNDC();
10988}
10989
10990/*!
10991 \return \c true if the underlying graphics API uses depth range [0, 1] in
10992 clip space.
10993
10994 In practice this is \c false for OpenGL only, because OpenGL uses a
10995 post-projection depth range of [-1, 1]. (not to be confused with the
10996 NDC-to-window mapping controlled by glDepthRange(), which uses a range of
10997 [0, 1], unless overridden by the QRhiViewport) In some OpenGL versions
10998 glClipControl() could be used to change this, but the OpenGL backend of
10999 QRhi does not use that function as it is not available in OpenGL ES or
11000 OpenGL versions lower than 4.5.
11001
11002 \note clipSpaceCorrMatrix() includes the corresponding adjustment in its
11003 returned matrix. Therefore, many users of QRhi do not need to take any
11004 further measures apart from pre-multiplying their projection matrices with
11005 clipSpaceCorrMatrix(). However, some graphics techniques, such as, some
11006 types of shadow mapping, involve working with and outputting depth values
11007 in the shaders. These will need to query and take the value of this
11008 function into account as appropriate.
11009 */
11010bool QRhi::isClipDepthZeroToOne() const
11011{
11012 return d->isClipDepthZeroToOne();
11013}
11014
11015/*!
11016 \return a matrix that can be used to allow applications keep using
11017 OpenGL-targeted vertex data and perspective projection matrices (such as,
11018 the ones generated by QMatrix4x4::perspective()), regardless of the active
11019 QRhi backend.
11020
11021 In a typical renderer, once \c{this_matrix * mvp} is used instead of just
11022 \c mvp, vertex data with Y up and viewports with depth range 0 - 1 can be
11023 used without considering what backend (and so graphics API) is going to be
11024 used at run time. This way branching based on isYUpInNDC() and
11025 isClipDepthZeroToOne() can be avoided (although such logic may still become
11026 required when implementing certain advanced graphics techniques).
11027
11028 See
11029 \l{https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/}{this
11030 page} for a discussion of the topic from Vulkan perspective.
11031 */
11032QMatrix4x4 QRhi::clipSpaceCorrMatrix() const
11033{
11034 return d->clipSpaceCorrMatrix();
11035}
11036
11037/*!
11038 \return \c true if the specified texture \a format modified by \a flags is
11039 supported.
11040
11041 The query is supported both for uncompressed and compressed formats.
11042 */
11043bool QRhi::isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const
11044{
11045 return d->isTextureFormatSupported(format, flags);
11046}
11047
11048/*!
11049 \return \c true if the specified \a feature is supported
11050 */
11051bool QRhi::isFeatureSupported(QRhi::Feature feature) const
11052{
11053 return d->isFeatureSupported(feature);
11054}
11055
11056/*!
11057 \return the value for the specified resource \a limit.
11058
11059 The values are expected to be queried by the backends upon initialization,
11060 meaning calling this function is a light operation.
11061 */
11062int QRhi::resourceLimit(ResourceLimit limit) const
11063{
11064 return d->resourceLimit(limit);
11065}
11066
11067/*!
11068 \return a pointer to the backend-specific collection of native objects
11069 for the device, context, and similar concepts used by the backend.
11070
11071 Cast to QRhiVulkanNativeHandles, QRhiD3D11NativeHandles,
11072 QRhiD3D12NativeHandles, QRhiGles2NativeHandles, or QRhiMetalNativeHandles
11073 as appropriate.
11074
11075 \note No ownership is transferred, neither for the returned pointer nor for
11076 any native objects.
11077 */
11078const QRhiNativeHandles *QRhi::nativeHandles()
11079{
11080 return d->nativeHandles();
11081}
11082
11083/*!
11084 With OpenGL this makes the OpenGL context current on the current thread.
11085 The function has no effect with other backends.
11086
11087 Calling this function is relevant typically in Qt framework code, when one
11088 has to ensure external OpenGL code provided by the application can still
11089 run like it did before with direct usage of OpenGL, as long as the QRhi is
11090 using the OpenGL backend.
11091
11092 \return false when failed, similarly to QOpenGLContext::makeCurrent(). When
11093 the operation failed, isDeviceLost() can be called to determine if there
11094 was a loss of context situation. Such a check is equivalent to checking via
11095 QOpenGLContext::isValid().
11096
11097 \sa QOpenGLContext::makeCurrent(), QOpenGLContext::isValid()
11098 */
11099bool QRhi::makeThreadLocalNativeContextCurrent()
11100{
11101 return d->makeThreadLocalNativeContextCurrent();
11102}
11103
11104/*!
11105 With backends and graphics APIs where applicable, this function allows to
11106 provide additional arguments to the \b next submission of commands to the
11107 graphics command queue.
11108
11109 In particular, with Vulkan this allows passing in a list of Vulkan semaphore
11110 objects for \c vkQueueSubmit() to signal and wait on. \a params must then be
11111 a \l QRhiVulkanQueueSubmitParams. This becomes essential in certain advanced
11112 use cases, such as when performing native Vulkan calls that involve having
11113 to wait on and signal VkSemaphores that the application's custom Vulkan
11114 rendering or compute code manages. In addition, this also allows specifying
11115 additional semaphores to wait on in the next \c vkQueuePresentKHR().
11116
11117 \note This function affects the next queue submission only, which will
11118 happen in endFrame(), endOffscreenFrame(), or finish(). The enqueuing of
11119 present happens in endFrame().
11120
11121 With many other backends the implementation of this function is a no-op.
11122
11123 \since 6.9
11124 */
11125void QRhi::setQueueSubmitParams(QRhiNativeHandles *params)
11126{
11127 d->setQueueSubmitParams(params);
11128}
11129
11130/*!
11131 Attempts to release resources in the backend's caches. This can include both
11132 CPU and GPU resources. Only memory and resources that can be recreated
11133 automatically are in scope. As an example, if the backend's
11134 QRhiGraphicsPipeline implementation maintains a cache of shader compilation
11135 results, calling this function leads to emptying that cache, thus
11136 potentially freeing up memory and graphics resources.
11137
11138 Calling this function makes sense in resource constrained environments,
11139 where at a certain point there is a need to ensure minimal resource usage,
11140 at the expense of performance.
11141 */
11142void QRhi::releaseCachedResources()
11143{
11144 d->releaseCachedResources();
11145
11146 for (QRhiResourceUpdateBatch *u : d->resUpdPool) {
11147 if (u->d->poolIndex < 0)
11148 u->d->trimOpLists();
11149 }
11150}
11151
11152/*!
11153 \return true if the graphics device was lost.
11154
11155 The loss of the device is typically detected in beginFrame(), endFrame() or
11156 QRhiSwapChain::createOrResize(), depending on the backend and the underlying
11157 native APIs. The most common is endFrame() because that is where presenting
11158 happens. With some backends QRhiSwapChain::createOrResize() can also fail
11159 due to a device loss. Therefore this function is provided as a generic way
11160 to check if a device loss was detected by a previous operation.
11161
11162 When the device is lost, no further operations should be done via the QRhi.
11163 Rather, all QRhi resources should be released, followed by destroying the
11164 QRhi. A new QRhi can then be attempted to be created. If successful, all
11165 graphics resources must be reinitialized. If not, try again later,
11166 repeatedly.
11167
11168 While simple applications may decide to not care about device loss,
11169 on the commonly used desktop platforms a device loss can happen
11170 due to a variety of reasons, including physically disconnecting the
11171 graphics adapter, disabling the device or driver, uninstalling or upgrading
11172 the graphics driver, or due to errors that lead to a graphics device reset.
11173 Some of these can happen under perfectly normal circumstances as well, for
11174 example the upgrade of the graphics driver to a newer version is a common
11175 task that can happen at any time while a Qt application is running. Users
11176 may very well expect applications to be able to survive this, even when the
11177 application is actively using an API like OpenGL or Direct3D.
11178
11179 Qt's own frameworks built on top of QRhi, such as, Qt Quick, can be
11180 expected to handle and take appropriate measures when a device loss occurs.
11181 If the data for graphics resources, such as textures and buffers, are still
11182 available on the CPU side, such an event may not be noticeable on the
11183 application level at all since graphics resources can seamlessly be
11184 reinitialized then. However, applications and libraries working directly
11185 with QRhi are expected to be prepared to check and handle device loss
11186 situations themselves.
11187
11188 \note With OpenGL, applications may need to opt-in to context reset
11189 notifications by setting QSurfaceFormat::ResetNotification on the
11190 QOpenGLContext. This is typically done by enabling the flag in
11191 QRhiGles2InitParams::format. Keep in mind however that some systems may
11192 generate context resets situations even when this flag is not set.
11193 */
11194bool QRhi::isDeviceLost() const
11195{
11196 return d->isDeviceLost();
11197}
11198
11199/*!
11200 \return a binary data blob with data collected from the
11201 QRhiGraphicsPipeline and QRhiComputePipeline successfully created during
11202 the lifetime of this QRhi.
11203
11204 By saving and then, in subsequent runs of the same application, reloading
11205 the cache data, pipeline and shader creation times can potentially be
11206 reduced. What exactly the cache and its serialized version includes is not
11207 specified, is always specific to the backend used, and in some cases also
11208 dependent on the particular implementation of the graphics API.
11209
11210 When the PipelineCacheDataLoadSave is reported as unsupported, the returned
11211 QByteArray is empty.
11212
11213 When the EnablePipelineCacheDataSave flag was not specified when calling
11214 create(), the returned QByteArray may be empty, even when the
11215 PipelineCacheDataLoadSave feature is supported.
11216
11217 When the returned data is non-empty, it is always specific to the Qt
11218 version and QRhi backend. In addition, in some cases there is a strong
11219 dependency to the graphics device and the exact driver version used. QRhi
11220 takes care of adding the appropriate header and safeguards that ensure that
11221 the data can always be passed safely to setPipelineCacheData(), therefore
11222 attempting to load data from a run on another version of a driver will be
11223 handled safely and gracefully.
11224
11225 \note Calling releaseCachedResources() may, depending on the backend, clear
11226 the pipeline data collected. A subsequent call to this function may then
11227 not return any data.
11228
11229 See EnablePipelineCacheDataSave for further details about this feature.
11230
11231 \note Minimize the number of calls to this function. Retrieving the blob is
11232 not always a cheap operation, and therefore this function should only be
11233 called at a low frequency, ideally only once e.g. when closing the
11234 application.
11235
11236 \sa setPipelineCacheData(), create(), isFeatureSupported()
11237 */
11238QByteArray QRhi::pipelineCacheData()
11239{
11240 return d->pipelineCacheData();
11241}
11242
11243/*!
11244 Loads \a data into the pipeline cache, when applicable.
11245
11246 When the PipelineCacheDataLoadSave is reported as unsupported, the function
11247 is safe to call, but has no effect.
11248
11249 The blob returned by pipelineCacheData() is always specific to the Qt
11250 version, the QRhi backend, and, in some cases, also to the graphics device,
11251 and a given version of the graphics driver. QRhi takes care of adding the
11252 appropriate header and safeguards that ensure that the data can always be
11253 passed safely to this function. If there is a mismatch, e.g. because the
11254 driver has been upgraded to a newer version, or because the data was
11255 generated from a different QRhi backend, a warning is printed and \a data
11256 is safely ignored.
11257
11258 With Vulkan, this maps directly to VkPipelineCache. Calling this function
11259 creates a new Vulkan pipeline cache object, with its initial data sourced
11260 from \a data. The pipeline cache object is then used by all subsequently
11261 created QRhiGraphicsPipeline and QRhiComputePipeline objects, thus
11262 accelerating, potentially, the pipeline creation.
11263
11264 With other APIs there is no real pipeline cache, but they may provide a
11265 cache with bytecode from shader compilations (D3D) or program binaries
11266 (OpenGL). In applications that perform a lot of shader compilation from
11267 source at run time this can provide a significant boost in subsequent runs
11268 if the "pipeline cache" is pre-seeded from an earlier run using this
11269 function.
11270
11271 \note QRhi cannot give any guarantees that \a data has an effect on the
11272 pipeline and shader creation performance. With APIs like Vulkan, it is up
11273 to the driver to decide if \a data is used for some purpose, or if it is
11274 ignored.
11275
11276 See EnablePipelineCacheDataSave for further details about this feature.
11277
11278 \note This mechanism offered by QRhi is independent of the drivers' own
11279 internal caching mechanism, if any. This means that, depending on the
11280 graphics API and its implementation, the exact effects of retrieving and
11281 then reloading \a data are not predictable. Improved performance may not be
11282 visible at all in case other caching mechanisms outside of Qt's control are
11283 already active.
11284
11285 \note Minimize the number of calls to this function. Loading the blob is
11286 not always a cheap operation, and therefore this function should only be
11287 called at a low frequency, ideally only once e.g. when starting the
11288 application.
11289
11290 \warning Serialized pipeline cache data is assumed to be trusted content. Qt
11291 performs robust parsing of the header and metadata included in \a data,
11292 application developers are however advised to never pass in data from
11293 untrusted sources.
11294
11295 \sa pipelineCacheData(), isFeatureSupported()
11296 */
11297void QRhi::setPipelineCacheData(const QByteArray &data)
11298{
11299 d->setPipelineCacheData(data);
11300}
11301
11302/*!
11303 \struct QRhiStats
11304 \inmodule QtGuiPrivate
11305 \inheaderfile rhi/qrhi.h
11306 \since 6.6
11307
11308 \brief Statistics provided from the underlying memory allocator.
11309
11310 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
11311 for details.
11312 */
11313
11314/*!
11315 \variable QRhiStats::totalPipelineCreationTime
11316
11317 The total time in milliseconds spent in graphics and compute pipeline
11318 creation, which usually involves shader compilation or cache lookups, and
11319 potentially expensive processing.
11320
11321 \note The value should not be compared between different backends since the
11322 concept of "pipelines" and what exactly happens under the hood during, for
11323 instance, a call to QRhiGraphicsPipeline::create(), differ greatly between
11324 graphics APIs and their implementations.
11325
11326 \sa QRhi::statistics()
11327*/
11328
11329/*!
11330 \variable QRhiStats::blockCount
11331
11332 Statistic reported from the Vulkan or D3D12 memory allocator.
11333
11334 \sa QRhi::statistics()
11335*/
11336
11337/*!
11338 \variable QRhiStats::allocCount
11339
11340 Statistic reported from the Vulkan or D3D12 memory allocator.
11341
11342 \sa QRhi::statistics()
11343*/
11344
11345/*!
11346 \variable QRhiStats::usedBytes
11347
11348 Statistic reported from the Vulkan or D3D12 memory allocator.
11349
11350 \sa QRhi::statistics()
11351*/
11352
11353/*!
11354 \variable QRhiStats::unusedBytes
11355
11356 Statistic reported from the Vulkan or D3D12 memory allocator.
11357
11358 \sa QRhi::statistics()
11359*/
11360
11361/*!
11362 \variable QRhiStats::totalUsageBytes
11363
11364 Valid only with D3D12 currently. Matches IDXGIAdapter3::QueryVideoMemoryInfo().
11365
11366 \sa QRhi::statistics()
11367*/
11368
11369#ifndef QT_NO_DEBUG_STREAM
11370QDebug operator<<(QDebug dbg, const QRhiStats &info)
11371{
11372 QDebugStateSaver saver(dbg);
11373 dbg.nospace() << "QRhiStats("
11374 << "totalPipelineCreationTime=" << info.totalPipelineCreationTime
11375 << " blockCount=" << info.blockCount
11376 << " allocCount=" << info.allocCount
11377 << " usedBytes=" << info.usedBytes
11378 << " unusedBytes=" << info.unusedBytes
11379 << " totalUsageBytes=" << info.totalUsageBytes
11380 << ')';
11381 return dbg;
11382}
11383#endif
11384
11385/*!
11386 Gathers and returns statistics about the timings and allocations of
11387 graphics resources.
11388
11389 Data about memory allocations is only available with some backends, where
11390 such operations are under Qt's control. With graphics APIs where there is
11391 no lower level control over resource memory allocations, this will never be
11392 supported and all relevant fields in the results are 0.
11393
11394 With Vulkan in particular, the values are valid always, and are queried
11395 from the underlying memory allocator library. This gives an insight into
11396 the memory requirements of the active buffers and textures.
11397
11398 The same is true for Direct 3D 12. In addition to the memory allocator
11399 library's statistics, here the result also includes a \c totalUsageBytes
11400 field which reports the total size including additional resources that are
11401 not under the memory allocator library's control (swapchain buffers,
11402 descriptor heaps, etc.), as reported by DXGI.
11403
11404 The values correspond to all types of memory used, combined. (i.e. video +
11405 system in case of a discreet GPU)
11406
11407 Additional data, such as the total time in milliseconds spent in graphics
11408 and compute pipeline creation (which usually involves shader compilation or
11409 cache lookups, and potentially expensive processing) is available with most
11410 backends.
11411
11412 \note The elapsed times for operations such as pipeline creation may be
11413 affected by various factors. The results should not be compared between
11414 different backends since the concept of "pipelines" and what exactly
11415 happens under the hood during, for instance, a call to
11416 QRhiGraphicsPipeline::create(), differ greatly between graphics APIs and
11417 their implementations.
11418
11419 \note Additionally, many drivers will likely employ various caching
11420 strategies for shaders, programs, pipelines. (independently of Qt's own
11421 similar facilities, such as setPipelineCacheData() or the OpenGL-specific
11422 program binary disk cache). Because such internal behavior is transparent
11423 to the API client, Qt and QRhi have no knowledge or control over the exact
11424 caching strategy, persistency, invalidation of the cached data, etc. When
11425 reading timings, such as the time spent on pipeline creation, the potential
11426 presence and unspecified behavior of driver-level caching mechanisms should
11427 be kept in mind.
11428 */
11429QRhiStats QRhi::statistics() const
11430{
11431 return d->statistics();
11432}
11433
11434/*!
11435 \return a new graphics pipeline resource.
11436
11437 \sa QRhiResource::destroy()
11438 */
11439QRhiGraphicsPipeline *QRhi::newGraphicsPipeline()
11440{
11441 return d->createGraphicsPipeline();
11442}
11443
11444/*!
11445 \return a new compute pipeline resource.
11446
11447 \note Compute is only available when the \l{QRhi::Compute}{Compute} feature
11448 is reported as supported.
11449
11450 \sa QRhiResource::destroy()
11451 */
11452QRhiComputePipeline *QRhi::newComputePipeline()
11453{
11454 return d->createComputePipeline();
11455}
11456
11457/*!
11458 \return a new shader resource binding collection resource.
11459
11460 \sa QRhiResource::destroy()
11461 */
11462QRhiShaderResourceBindings *QRhi::newShaderResourceBindings()
11463{
11464 return d->createShaderResourceBindings();
11465}
11466
11467/*!
11468 \return a new buffer with the specified \a type, \a usage, and \a size.
11469
11470 \note Some \a usage and \a type combinations may not be supported by all
11471 backends. See \l{QRhiBuffer::UsageFlag}{UsageFlags} and
11472 \l{QRhi::NonDynamicUniformBuffers}{the feature flags}.
11473
11474 \note Backends may choose to allocate buffers bigger than \a size. This is
11475 done transparently to applications, so there are no special restrictions on
11476 the value of \a size. QRhiBuffer::size() will always report back the value
11477 that was requested in \a size.
11478
11479 \sa QRhiResource::destroy()
11480 */
11481QRhiBuffer *QRhi::newBuffer(QRhiBuffer::Type type,
11482 QRhiBuffer::UsageFlags usage,
11483 quint32 size)
11484{
11485 return d->createBuffer(type, usage, size);
11486}
11487
11488/*!
11489 \return a new renderbuffer with the specified \a type, \a pixelSize, \a
11490 sampleCount, and \a flags.
11491
11492 When \a backingFormatHint is set to a texture format other than
11493 QRhiTexture::UnknownFormat, it may be used by the backend to decide what
11494 format to use for the storage backing the renderbuffer.
11495
11496 \note \a backingFormatHint becomes relevant typically when multisampling
11497 and floating point texture formats are involved: rendering into a
11498 multisample QRhiRenderBuffer and then resolving into a non-RGBA8
11499 QRhiTexture implies (with some graphics APIs) that the storage backing the
11500 QRhiRenderBuffer uses the matching non-RGBA8 format. That means that
11501 passing a format like QRhiTexture::RGBA32F is important, because backends
11502 will typically opt for QRhiTexture::RGBA8 by default, which would then
11503 break later on due to attempting to set up RGBA8->RGBA32F multisample
11504 resolve in the color attachment(s) of the QRhiTextureRenderTarget.
11505
11506 \sa QRhiResource::destroy()
11507 */
11508QRhiRenderBuffer *QRhi::newRenderBuffer(QRhiRenderBuffer::Type type,
11509 const QSize &pixelSize,
11510 int sampleCount,
11511 QRhiRenderBuffer::Flags flags,
11512 QRhiTexture::Format backingFormatHint)
11513{
11514 return d->createRenderBuffer(type, pixelSize, sampleCount, flags, backingFormatHint);
11515}
11516
11517/*!
11518 \return a new 1D or 2D texture with the specified \a format, \a pixelSize, \a
11519 sampleCount, and \a flags.
11520
11521 A 1D texture array must have QRhiTexture::OneDimensional set in \a flags. This
11522 function will implicitly set this flag if the \a pixelSize height is 0.
11523
11524 \note \a format specifies the requested internal and external format,
11525 meaning the data to be uploaded to the texture will need to be in a
11526 compatible format, while the native texture may (but is not guaranteed to,
11527 in case of OpenGL at least) use this format internally.
11528
11529 \note 1D textures are only functional when the OneDimensionalTextures feature is
11530 reported as supported at run time. Further, mipmaps on 1D textures are only
11531 functional when the OneDimensionalTextureMipmaps feature is reported at run time.
11532
11533 \sa QRhiResource::destroy()
11534 */
11535QRhiTexture *QRhi::newTexture(QRhiTexture::Format format,
11536 const QSize &pixelSize,
11537 int sampleCount,
11538 QRhiTexture::Flags flags)
11539{
11540 if (pixelSize.height() == 0)
11541 flags |= QRhiTexture::OneDimensional;
11542
11543 return d->createTexture(format, pixelSize, 1, 0, sampleCount, flags);
11544}
11545
11546/*!
11547 \return a new 1D, 2D or 3D texture with the specified \a format, \a width, \a
11548 height, \a depth, \a sampleCount, and \a flags.
11549
11550 This overload is suitable for 3D textures because it allows specifying \a
11551 depth. A 3D texture must have QRhiTexture::ThreeDimensional set in \a
11552 flags, but using this overload that can be omitted because the flag is set
11553 implicitly whenever \a depth is greater than 0. For 1D, 2D and cube textures \a
11554 depth should be set to 0.
11555
11556 A 1D texture must have QRhiTexture::OneDimensional set in \a flags. This overload
11557 will implicitly set this flag if both \a height and \a depth are 0.
11558
11559 \note 3D textures are only functional when the ThreeDimensionalTextures
11560 feature is reported as supported at run time.
11561
11562 \note 1D textures are only functional when the OneDimensionalTextures feature is
11563 reported as supported at run time. Further, mipmaps on 1D textures are only
11564 functional when the OneDimensionalTextureMipmaps feature is reported at run time.
11565
11566 \overload
11567 */
11568QRhiTexture *QRhi::newTexture(QRhiTexture::Format format,
11569 int width, int height, int depth,
11570 int sampleCount,
11571 QRhiTexture::Flags flags)
11572{
11573 if (depth > 0)
11574 flags |= QRhiTexture::ThreeDimensional;
11575
11576 if (height == 0 && depth == 0)
11577 flags |= QRhiTexture::OneDimensional;
11578
11579 return d->createTexture(format, QSize(width, height), depth, 0, sampleCount, flags);
11580}
11581
11582/*!
11583 \return a new 1D or 2D texture array with the specified \a format, \a arraySize,
11584 \a pixelSize, \a sampleCount, and \a flags.
11585
11586 This function implicitly sets QRhiTexture::TextureArray in \a flags.
11587
11588 A 1D texture array must have QRhiTexture::OneDimensional set in \a flags. This
11589 function will implicitly set this flag if the \a pixelSize height is 0.
11590
11591 \note Do not confuse texture arrays with arrays of textures. A QRhiTexture
11592 created by this function is usable with 1D or 2D array samplers in the shader, for
11593 example: \c{layout(binding = 1) uniform sampler2DArray texArr;}. Arrays of
11594 textures refers to a list of textures that are exposed to the shader via
11595 QRhiShaderResourceBinding::sampledTextures() and a count > 1, and declared
11596 in the shader for example like this: \c{layout(binding = 1) uniform
11597 sampler2D textures[4];}
11598
11599 \note This is only functional when the TextureArrays feature is reported as
11600 supported at run time.
11601
11602 \note 1D textures are only functional when the OneDimensionalTextures feature is
11603 reported as supported at run time. Further, mipmaps on 1D textures are only
11604 functional when the OneDimensionalTextureMipmaps feature is reported at run time.
11605
11606
11607 \sa newTexture()
11608 */
11609QRhiTexture *QRhi::newTextureArray(QRhiTexture::Format format,
11610 int arraySize,
11611 const QSize &pixelSize,
11612 int sampleCount,
11613 QRhiTexture::Flags flags)
11614{
11615 flags |= QRhiTexture::TextureArray;
11616
11617 if (pixelSize.height() == 0)
11618 flags |= QRhiTexture::OneDimensional;
11619
11620 return d->createTexture(format, pixelSize, 1, arraySize, sampleCount, flags);
11621}
11622
11623/*!
11624 \return a new sampler with the specified magnification filter \a magFilter,
11625 minification filter \a minFilter, mipmapping mode \a mipmapMode, and the
11626 addressing (wrap) modes \a addressU, \a addressV, and \a addressW.
11627
11628 \note Setting \a mipmapMode to a value other than \c None implies that
11629 images for all relevant mip levels will be provided either via
11630 \l{QRhiResourceUpdateBatch::uploadTexture()}{texture uploads} or by calling
11631 \l{QRhiResourceUpdateBatch::generateMips()}{generateMips()} on the texture
11632 that is used with this sampler. Attempting to use the sampler with a
11633 texture that has no data for all relevant mip levels will lead to rendering
11634 errors, with the exact behavior dependent on the underlying graphics API.
11635
11636 \sa QRhiResource::destroy()
11637 */
11638QRhiSampler *QRhi::newSampler(QRhiSampler::Filter magFilter,
11639 QRhiSampler::Filter minFilter,
11640 QRhiSampler::Filter mipmapMode,
11641 QRhiSampler::AddressMode addressU,
11642 QRhiSampler::AddressMode addressV,
11643 QRhiSampler::AddressMode addressW)
11644{
11645 return d->createSampler(magFilter, minFilter, mipmapMode, addressU, addressV, addressW);
11646}
11647
11648/*!
11649 \return a new shading rate map object.
11650
11651 \since 6.9
11652 */
11653QRhiShadingRateMap *QRhi::newShadingRateMap()
11654{
11655 return d->createShadingRateMap();
11656}
11657
11658/*!
11659 \return a new texture render target with color and depth/stencil
11660 attachments given in \a desc, and with the specified \a flags.
11661
11662 \sa QRhiResource::destroy()
11663 */
11664
11665QRhiTextureRenderTarget *QRhi::newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
11666 QRhiTextureRenderTarget::Flags flags)
11667{
11668 return d->createTextureRenderTarget(desc, flags);
11669}
11670
11671/*!
11672 \return a new swapchain.
11673
11674 \sa QRhiResource::destroy(), QRhiSwapChain::createOrResize()
11675 */
11676QRhiSwapChain *QRhi::newSwapChain()
11677{
11678 return d->createSwapChain();
11679}
11680
11681/*!
11682 Starts a new frame targeting the next available buffer of \a swapChain.
11683
11684 A frame consists of resource updates and one or more render and compute
11685 passes.
11686
11687 \a flags can indicate certain special cases.
11688
11689 The high level pattern of rendering into a QWindow using a swapchain:
11690
11691 \list
11692
11693 \li Create a swapchain.
11694
11695 \li Call QRhiSwapChain::createOrResize() whenever the surface size is
11696 different than before.
11697
11698 \li Call QRhiSwapChain::destroy() on
11699 QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed.
11700
11701 \li Then on every frame:
11702 \badcode
11703 beginFrame(sc);
11704 updates = nextResourceUpdateBatch();
11705 updates->...
11706 QRhiCommandBuffer *cb = sc->currentFrameCommandBuffer();
11707 cb->beginPass(sc->currentFrameRenderTarget(), colorClear, dsClear, updates);
11708 ...
11709 cb->endPass();
11710 ... // more passes as necessary
11711 endFrame(sc);
11712 \endcode
11713
11714 \endlist
11715
11716 \return QRhi::FrameOpSuccess on success, or another QRhi::FrameOpResult
11717 value on failure. Some of these should be treated as soft, "try again
11718 later" type of errors: When QRhi::FrameOpSwapChainOutOfDate is returned,
11719 the swapchain is to be resized or updated by calling
11720 QRhiSwapChain::createOrResize(). The application should then attempt to
11721 generate a new frame. QRhi::FrameOpDeviceLost means the graphics device is
11722 lost but this may also be recoverable by releasing all resources, including
11723 the QRhi itself, and then recreating all resources. See isDeviceLost() for
11724 further discussion.
11725
11726 \sa endFrame(), beginOffscreenFrame(), isDeviceLost()
11727 */
11728QRhi::FrameOpResult QRhi::beginFrame(QRhiSwapChain *swapChain, BeginFrameFlags flags)
11729{
11730 if (d->inFrame)
11731 qWarning("Attempted to call beginFrame() within a still active frame; ignored");
11732
11733 qCDebug(QRHI_LOG_RUB) << "[rub] new frame";
11734
11735 QRhi::FrameOpResult r = !d->inFrame ? d->beginFrame(swapChain, flags) : FrameOpSuccess;
11736 if (r == FrameOpSuccess)
11737 d->inFrame = true;
11738
11739 return r;
11740}
11741
11742/*!
11743 Ends, commits, and presents a frame that was started in the last
11744 beginFrame() on \a swapChain.
11745
11746 Double (or triple) buffering is managed internally by the QRhiSwapChain and
11747 QRhi.
11748
11749 \a flags can optionally be used to change the behavior in certain ways.
11750 Passing QRhi::SkipPresent skips queuing the Present command or calling
11751 swapBuffers.
11752
11753 \return QRhi::FrameOpSuccess on success, or another QRhi::FrameOpResult
11754 value on failure. Some of these should be treated as soft, "try again
11755 later" type of errors: When QRhi::FrameOpSwapChainOutOfDate is returned,
11756 the swapchain is to be resized or updated by calling
11757 QRhiSwapChain::createOrResize(). The application should then attempt to
11758 generate a new frame. QRhi::FrameOpDeviceLost means the graphics device is
11759 lost but this may also be recoverable by releasing all resources, including
11760 the QRhi itself, and then recreating all resources. See isDeviceLost() for
11761 further discussion.
11762
11763 \sa beginFrame(), isDeviceLost()
11764 */
11765QRhi::FrameOpResult QRhi::endFrame(QRhiSwapChain *swapChain, EndFrameFlags flags)
11766{
11767 if (!d->inFrame)
11768 qWarning("Attempted to call endFrame() without an active frame; ignored");
11769
11770 QRhi::FrameOpResult r = d->inFrame ? d->endFrame(swapChain, flags) : FrameOpSuccess;
11771 d->inFrame = false;
11772 // deleteLater is a high level QRhi concept the backends know
11773 // nothing about - handle it here.
11774 qDeleteAll(d->pendingDeleteResources);
11775 d->pendingDeleteResources.clear();
11776
11777 return r;
11778}
11779
11780/*!
11781 \return true when there is an active frame, meaning there was a
11782 beginFrame() (or beginOffscreenFrame()) with no corresponding endFrame()
11783 (or endOffscreenFrame()) yet.
11784
11785 \sa currentFrameSlot(), beginFrame(), endFrame()
11786 */
11787bool QRhi::isRecordingFrame() const
11788{
11789 return d->inFrame;
11790}
11791
11792/*!
11793 \return the current frame slot index while recording a frame. Unspecified
11794 when called outside an active frame (that is, when isRecordingFrame() is \c
11795 false).
11796
11797 With backends like Vulkan or Metal, it is the responsibility of the QRhi
11798 backend to block whenever starting a new frame and finding the CPU is
11799 already \c{FramesInFlight - 1} frames ahead of the GPU (because the command
11800 buffer submitted in frame no. \c{current} - \c{FramesInFlight} has not yet
11801 completed).
11802
11803 Resources that tend to change between frames (such as, the native buffer
11804 object backing a QRhiBuffer with type QRhiBuffer::Dynamic) exist in
11805 multiple versions, so that each frame, that can be submitted while a
11806 previous one is still being processed, works with its own copy, thus
11807 avoiding the need to stall the pipeline when preparing the frame. (The
11808 contents of a resource that may still be in use in the GPU should not be
11809 touched, but simply always waiting for the previous frame to finish would
11810 reduce GPU utilization and ultimately, performance and efficiency.)
11811
11812 Conceptually this is somewhat similar to copy-on-write schemes used by some
11813 C++ containers and other types. It may also be similar to what an OpenGL or
11814 Direct 3D 11 implementation performs internally for certain type of objects.
11815
11816 In practice, such double (or triple) buffering resources is realized in
11817 the Vulkan, Metal, and similar QRhi backends by having a fixed number of
11818 native resource (such as, VkBuffer) \c slots behind a QRhiResource. That
11819 can then be indexed by a frame slot index running 0, 1, ..,
11820 FramesInFlight-1, and then wrapping around.
11821
11822 All this is managed transparently to the users of QRhi. However,
11823 applications that integrate rendering done directly with the graphics API
11824 may want to perform a similar double or triple buffering of their own
11825 graphics resources. That is then most easily achieved by knowing the values
11826 of the maximum number of in-flight frames (retrievable via resourceLimit())
11827 and the current frame (slot) index (returned by this function).
11828
11829 \sa isRecordingFrame(), beginFrame(), endFrame()
11830 */
11831int QRhi::currentFrameSlot() const
11832{
11833 return d->currentFrameSlot;
11834}
11835
11836/*!
11837 Starts a new offscreen frame. Provides a command buffer suitable for
11838 recording rendering commands in \a cb. \a flags is used to indicate
11839 certain special cases, just like with beginFrame().
11840
11841 \note The QRhiCommandBuffer stored to *cb is not owned by the caller.
11842
11843 Rendering without a swapchain is possible as well. The typical use case is
11844 to use it in completely offscreen applications, e.g. to generate image
11845 sequences by rendering and reading back without ever showing a window.
11846
11847 Usage in on-screen applications (so beginFrame, endFrame,
11848 beginOffscreenFrame, endOffscreenFrame, beginFrame, ...) is possible too
11849 but it does reduce parallelism so it should be done only infrequently.
11850
11851 Offscreen frames do not let the CPU potentially generate another frame
11852 while the GPU is still processing the previous one. This has the side
11853 effect that if readbacks are scheduled, the results are guaranteed to be
11854 available once endOffscreenFrame() returns. That is not the case with
11855 frames targeting a swapchain: there the GPU is potentially better utilized,
11856 but working with readback operations needs more care from the application
11857 because endFrame(), unlike endOffscreenFrame(), does not guarantee that the
11858 results from the readback are available at that point.
11859
11860 The skeleton of rendering a frame without a swapchain and then reading the
11861 frame contents back could look like the following:
11862
11863 \code
11864 QRhiReadbackResult rbResult;
11865 QRhiCommandBuffer *cb;
11866 rhi->beginOffscreenFrame(&cb);
11867 cb->beginPass(rt, colorClear, dsClear);
11868 // ...
11869 u = nextResourceUpdateBatch();
11870 u->readBackTexture(rb, &rbResult);
11871 cb->endPass(u);
11872 rhi->endOffscreenFrame();
11873 // image data available in rbResult
11874 \endcode
11875
11876 \sa endOffscreenFrame(), beginFrame()
11877 */
11878QRhi::FrameOpResult QRhi::beginOffscreenFrame(QRhiCommandBuffer **cb, BeginFrameFlags flags)
11879{
11880 if (d->inFrame)
11881 qWarning("Attempted to call beginOffscreenFrame() within a still active frame; ignored");
11882
11883 qCDebug(QRHI_LOG_RUB) << "[rub] new offscreen frame";
11884
11885 QRhi::FrameOpResult r = !d->inFrame ? d->beginOffscreenFrame(cb, flags) : FrameOpSuccess;
11886 if (r == FrameOpSuccess)
11887 d->inFrame = true;
11888
11889 return r;
11890}
11891
11892/*!
11893 Ends, submits, and waits for the offscreen frame.
11894
11895 \a flags is not currently used.
11896
11897 \sa beginOffscreenFrame()
11898 */
11899QRhi::FrameOpResult QRhi::endOffscreenFrame(EndFrameFlags flags)
11900{
11901 if (!d->inFrame)
11902 qWarning("Attempted to call endOffscreenFrame() without an active frame; ignored");
11903
11904 QRhi::FrameOpResult r = d->inFrame ? d->endOffscreenFrame(flags) : FrameOpSuccess;
11905 d->inFrame = false;
11906 qDeleteAll(d->pendingDeleteResources);
11907 d->pendingDeleteResources.clear();
11908
11909 return r;
11910}
11911
11912/*!
11913 Waits for any work on the graphics queue (where applicable) to complete,
11914 then executes all deferred operations, like completing readbacks and
11915 resource releases. Can be called inside and outside of a frame, but not
11916 inside a pass. Inside a frame it implies submitting any work on the
11917 command buffer.
11918
11919 \note Avoid this function. One case where it may be needed is when the
11920 results of an enqueued readback in a swapchain-based frame are needed at a
11921 fixed given point and so waiting for the results is desired.
11922 */
11923QRhi::FrameOpResult QRhi::finish()
11924{
11925 return d->finish();
11926}
11927
11928/*!
11929 \return the list of supported sample counts.
11930
11931 A typical example would be (1, 2, 4, 8).
11932
11933 With some backend this list of supported values is fixed in advance, while
11934 with some others the (physical) device properties indicate what is
11935 supported at run time.
11936
11937 \sa QRhiRenderBuffer::setSampleCount(), QRhiTexture::setSampleCount(),
11938 QRhiGraphicsPipeline::setSampleCount(), QRhiSwapChain::setSampleCount()
11939 */
11940QList<int> QRhi::supportedSampleCounts() const
11941{
11942 return d->supportedSampleCounts();
11943}
11944
11945/*!
11946 \return the minimum uniform buffer offset alignment in bytes. This is
11947 typically 256.
11948
11949 Attempting to bind a uniform buffer region with an offset not aligned to
11950 this value will lead to failures depending on the backend and the
11951 underlying graphics API.
11952
11953 \sa ubufAligned()
11954 */
11955int QRhi::ubufAlignment() const
11956{
11957 return d->ubufAlignment();
11958}
11959
11960/*!
11961 \return The list of supported variable shading rates for the specified \a sampleCount.
11962
11963 1x1 is always supported.
11964
11965 \since 6.9
11966 */
11967QList<QSize> QRhi::supportedShadingRates(int sampleCount) const
11968{
11969 return d->supportedShadingRates(sampleCount);
11970}
11971
11972Q_CONSTINIT static QBasicAtomicInteger<QRhiGlobalObjectIdGenerator::Type> counter = Q_BASIC_ATOMIC_INITIALIZER(0);
11973
11974QRhiGlobalObjectIdGenerator::Type QRhiGlobalObjectIdGenerator::newId()
11975{
11976 return counter.fetchAndAddRelaxed(1) + 1;
11977}
11978
11980{
11981 return m_buffers.isEmpty() && m_textures.isEmpty();
11982}
11983
11985{
11986 m_buffers.clear();
11987 m_textures.clear();
11988}
11989
11995
11996void QRhiPassResourceTracker::registerBuffer(QRhiBuffer *buf, int slot, BufferAccess *access, BufferStage *stage,
11997 const UsageState &state)
11998{
11999 auto it = m_buffers.find(buf);
12000 if (it != m_buffers.end()) {
12001 Buffer &b = it->second;
12002 if (Q_UNLIKELY(b.access != *access)) {
12003 const QByteArray name = buf->name();
12004 qWarning("Buffer %p (%s) used with different accesses within the same pass, this is not allowed.",
12005 buf, name.constData());
12006 return;
12007 }
12008 if (b.stage != *stage) {
12009 b.stage = earlierStage(b.stage, *stage);
12010 *stage = b.stage;
12011 }
12012 return;
12013 }
12014
12015 Buffer b;
12016 b.slot = slot;
12017 b.access = *access;
12018 b.stage = *stage;
12019 b.stateAtPassBegin = state; // first use -> initial state
12020 m_buffers.insert(buf, b);
12021}
12022
12028
12035
12037 const UsageState &state)
12038{
12039 auto it = m_textures.find(tex);
12040 if (it != m_textures.end()) {
12041 Texture &t = it->second;
12042 if (t.access != *access) {
12043 // Different subresources of a texture may be used for both load
12044 // and store in the same pass. (think reading from one mip level
12045 // and writing to another one in a compute shader) This we can
12046 // handle by treating the entire resource as read-write.
12047 if (Q_LIKELY(isImageLoadStore(t.access) && isImageLoadStore(*access))) {
12049 *access = t.access;
12050 } else {
12051 const QByteArray name = tex->name();
12052 qWarning("Texture %p (%s) used with different accesses within the same pass, this is not allowed.",
12053 tex, name.constData());
12054 }
12055 }
12056 if (t.stage != *stage) {
12057 t.stage = earlierStage(t.stage, *stage);
12058 *stage = t.stage;
12059 }
12060 return;
12061 }
12062
12063 Texture t;
12064 t.access = *access;
12065 t.stage = *stage;
12066 t.stateAtPassBegin = state; // first use -> initial state
12067 m_textures.insert(tex, t);
12068}
12069
12070QRhiPassResourceTracker::BufferStage QRhiPassResourceTracker::toPassTrackerBufferStage(QRhiShaderResourceBinding::StageFlags stages)
12071{
12072 // pick the earlier stage (as this is going to be dstAccessMask)
12073 if (stages.testFlag(QRhiShaderResourceBinding::VertexStage))
12075 if (stages.testFlag(QRhiShaderResourceBinding::TessellationControlStage))
12077 if (stages.testFlag(QRhiShaderResourceBinding::TessellationEvaluationStage))
12079 if (stages.testFlag(QRhiShaderResourceBinding::FragmentStage))
12081 if (stages.testFlag(QRhiShaderResourceBinding::ComputeStage))
12083 if (stages.testFlag(QRhiShaderResourceBinding::GeometryStage))
12085
12086 Q_UNREACHABLE_RETURN(QRhiPassResourceTracker::BufVertexStage);
12087}
12088
12089QRhiPassResourceTracker::TextureStage QRhiPassResourceTracker::toPassTrackerTextureStage(QRhiShaderResourceBinding::StageFlags stages)
12090{
12091 // pick the earlier stage (as this is going to be dstAccessMask)
12092 if (stages.testFlag(QRhiShaderResourceBinding::VertexStage))
12094 if (stages.testFlag(QRhiShaderResourceBinding::TessellationControlStage))
12096 if (stages.testFlag(QRhiShaderResourceBinding::TessellationEvaluationStage))
12098 if (stages.testFlag(QRhiShaderResourceBinding::FragmentStage))
12100 if (stages.testFlag(QRhiShaderResourceBinding::ComputeStage))
12102 if (stages.testFlag(QRhiShaderResourceBinding::GeometryStage))
12104
12105 Q_UNREACHABLE_RETURN(QRhiPassResourceTracker::TexVertexStage);
12106}
12107
12108QSize QRhiImplementation::clampedSubResourceUploadSize(QSize size, QPoint dstPos, int level, QSize textureSizeAtLevelZero, bool warn)
12109{
12110 const QSize subResSize = q->sizeForMipLevel(level, textureSizeAtLevelZero);
12111 const bool outOfBoundsHoriz = dstPos.x() + size.width() > subResSize.width();
12112 const bool outOfBoundsVert = dstPos.y() + size.height() > subResSize.height();
12113 if (Q_UNLIKELY(outOfBoundsHoriz || outOfBoundsVert)) {
12114 if (warn) {
12115 qWarning("Invalid texture upload issued; size %dx%d dst.position %d,%d dst.subresource size %dx%d; size will be clamped",
12116 size.width(), size.height(), dstPos.x(), dstPos.y(), subResSize.width(), subResSize.height());
12117 }
12118 if (outOfBoundsHoriz)
12119 size.setWidth(subResSize.width() - dstPos.x());
12120 if (outOfBoundsVert)
12121 size.setHeight(subResSize.height() - dstPos.y());
12122 }
12123 return size;
12124}
12125
12126QT_END_NAMESPACE
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:803
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:814
bool isEmpty() const
Definition qrhi.cpp:11979
void registerBuffer(QRhiBuffer *buf, int slot, BufferAccess *access, BufferStage *stage, const UsageState &state)
Definition qrhi.cpp:11996
void registerTexture(QRhiTexture *tex, TextureAccess *access, TextureStage *stage, const UsageState &state)
Definition qrhi.cpp:12036
QRhiImplementation * rhi
Definition qrhi_p.h:589
static const int BUFFER_OPS_STATIC_ALLOC
Definition qrhi_p.h:581
void merge(QRhiResourceUpdateBatchPrivate *other)
Definition qrhi.cpp:10139
QRhiResourceUpdateBatch * q
Definition qrhi_p.h:588
static const int TEXTURE_OPS_STATIC_ALLOC
Definition qrhi_p.h:585
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
static const char * resourceTypeStr(const QRhiResource *res)
Definition qrhi.cpp:8492
static QRhiPassResourceTracker::BufferStage earlierStage(QRhiPassResourceTracker::BufferStage a, QRhiPassResourceTracker::BufferStage b)
Definition qrhi.cpp:11990
QDebug operator<<(QDebug dbg, const QRhiSwapChainHdrInfo &info)
Definition qrhi.cpp:8239
static bool isImageLoadStore(QRhiPassResourceTracker::TextureAccess access)
Definition qrhi.cpp:12029
static const char * deviceTypeStr(QRhiDriverInfo::DeviceType type)
\variable QRhiDriverInfo::deviceName
Definition qrhi.cpp:9429
static QRhiPassResourceTracker::TextureStage earlierStage(QRhiPassResourceTracker::TextureStage a, QRhiPassResourceTracker::TextureStage b)
Definition qrhi.cpp:12023
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:192
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1557
LimitsType limitsType
Definition qrhi.h:1568
float maxPotentialColorComponentValue
Definition qrhi.h:1576
LuminanceBehavior luminanceBehavior
Definition qrhi.h:1579
float maxColorComponentValue
Definition qrhi.h:1575