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
4#include "qrhi_p.h"
5#include <qmath.h>
6#include <QLoggingCategory>
7#include "private/qloggingregistry_p.h"
8
9#include "qrhinull_p.h"
10#ifndef QT_NO_OPENGL
11#include "qrhigles2_p.h"
12#endif
13#if QT_CONFIG(vulkan)
14#include "qrhivulkan_p.h"
15#endif
16#ifdef Q_OS_WIN
17#include "qrhid3d11_p.h"
18#include "qrhid3d12_p.h"
19#endif
20#if QT_CONFIG(metal)
21#include "qrhimetal_p.h"
22#endif
23
24#include <memory>
25
26QT_BEGIN_NAMESPACE
27
28// Play nice with QSG_INFO since that is still the most commonly used
29// way to get graphics info printed from Qt Quick apps, and the Quick
30// scenegraph is our primary user.
31Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(QRHI_LOG_INFO, "QSG_INFO", "qt.rhi.general")
32
33Q_LOGGING_CATEGORY(QRHI_LOG_RUB, "qt.rhi.rub")
34
35/*!
36 \class QRhi
37 \ingroup painting-3D
38 \inmodule QtGuiPrivate
39 \inheaderfile rhi/qrhi.h
40 \since 6.6
41
42 \brief Accelerated 2D/3D graphics API abstraction.
43
44 The Qt Rendering Hardware Interface is an abstraction for hardware accelerated
45 graphics APIs, such as, \l{https://www.khronos.org/opengl/}{OpenGL},
46 \l{https://www.khronos.org/opengles/}{OpenGL ES},
47 \l{https://docs.microsoft.com/en-us/windows/desktop/direct3d}{Direct3D},
48 \l{https://developer.apple.com/metal/}{Metal}, and
49 \l{https://www.khronos.org/vulkan/}{Vulkan}.
50
51 \warning The QRhi family of classes in the Qt Gui module, including QShader
52 and QShaderDescription, offer limited compatibility guarantees. There are
53 no source or binary compatibility guarantees for these classes, meaning the
54 API is only guaranteed to work with the Qt version the application was
55 developed against. Source incompatible changes are however aimed to be kept
56 at a minimum and will only be made in minor releases (6.7, 6.8, and so on).
57 To use these classes in an application, link to
58 \c{Qt::GuiPrivate} (if using CMake), and include the headers with the \c
59 rhi prefix, for example \c{#include <rhi/qrhi.h>}.
60
61 Each QRhi instance is backed by a backend for a specific graphics API. The
62 selection of the backend is a run time choice and is up to the application
63 or library that creates the QRhi instance. Some backends are available on
64 multiple platforms (OpenGL, Vulkan, Null), while APIs specific to a given
65 platform are only available when running on the platform in question (Metal
66 on macOS/iOS, Direct3D on Windows).
67
68 The available backends currently are:
69
70 \list
71
72 \li OpenGL 2.1 / OpenGL ES 2.0 or newer. Some extensions and newer core
73 specification features are utilized when present, for example to enable
74 multisample framebuffers or compute shaders. Operating in core profile
75 contexts is supported as well. If necessary, applications can query the
76 \l{QRhi::Feature}{feature flags} at runtime to check for features that are
77 not supported in the OpenGL context backing the QRhi. The OpenGL backend
78 builds on QOpenGLContext, QOpenGLFunctions, and the related cross-platform
79 infrastructure of the Qt GUI module.
80
81 \li Direct3D 11.2 and newer (with DXGI 1.3 and newer), using Shader Model
82 5.0 or newer. When the D3D runtime has no support for 11.2 features or
83 Shader Model 5.0, initialization using an accelerated graphics device will
84 fail, but using the
85 \l{https://learn.microsoft.com/en-us/windows/win32/direct3darticles/directx-warp}{software
86 adapter} is still an option.
87
88 \li Direct3D 12 on Windows 10 version 1703 and newer, with Shader Model 5.0
89 or newer. Qt requires ID3D12Device2 to be present, hence the requirement
90 for at least version 1703 of Windows 10. The D3D12 device is by default
91 created with specifying a minimum feature level of
92 \c{D3D_FEATURE_LEVEL_11_0}.
93
94 \li Metal 1.2 or newer.
95
96 \li Vulkan 1.0 or newer, optionally utilizing some Vulkan 1.1 level
97 features.
98
99 \li Null, a "dummy" backend that issues no graphics calls at all.
100
101 \endlist
102
103 In order to allow shader code to be written once in Qt applications and
104 libraries, all shaders are expected to be written in a single language
105 which is then compiled into SPIR-V. Versions for various shading language
106 are then generated from that, together with reflection information (inputs,
107 outputs, shader resources). This is then packed into easily and efficiently
108 serializable QShader instances. The compilers and tools to generate such
109 shaders are not part of QRhi and the Qt GUI module, but the core classes
110 for using such shaders, QShader and QShaderDescription, are. The APIs and
111 tools for performing compilation and translation are part of the Qt Shader
112 Tools module.
113
114 See the \l{RHI Window Example} for an introductory example of creating a
115 portable, cross-platform application that performs accelerated 3D rendering
116 onto a QWindow using QRhi.
117
118 \section1 An Impression of the API
119
120 To provide a quick look at the API with a short yet complete example that
121 does not involve window-related setup, the following is a complete,
122 runnable cross-platform application that renders 20 frames off-screen, and
123 then saves the generated images to files after reading back the texture
124 contents from the GPU. For an example that renders on-screen, which then
125 involves setting up a QWindow and a swapchain, refer to the
126 \l{RHI Window Example}.
127
128 For brevity, the initialization of the QRhi is done based on the platform:
129 the sample code here chooses Direct 3D 12 on Windows, Metal on macOS and
130 iOS, and Vulkan otherwise. OpenGL and Direct 3D 11 are never used by this
131 application, but support for those could be introduced with a few
132 additional lines.
133
134 \snippet rhioffscreen/main.cpp 0
135
136 The result of the application is 20 \c PNG images (frame0.png -
137 frame19.png). These contain a rotating triangle with varying opacity over a
138 green background.
139
140 The vertex and fragment shaders are expected to be processed and packaged
141 into \c{.qsb} files. The Vulkan-compatible GLSL source code is the
142 following:
143
144 \e color.vert
145 \snippet rhioffscreen/color.vert 0
146
147 \e color.frag
148 \snippet rhioffscreen/color.frag 0
149
150 To manually compile and transpile these shaders to a number of targets
151 (SPIR-V, HLSL, MSL, GLSL) and generate the \c{.qsb} files the application
152 loads at run time, run \c{qsb --qt6 color.vert -o color.vert.qsb} and
153 \c{qsb --qt6 color.frag -o color.frag.qsb}. Alternatively, the Qt Shader
154 Tools module offers build system integration for CMake, the
155 \c qt_add_shaders() CMake function, that can achieve the same at build time.
156
157 \section1 Security Considerations
158
159 All data consumed by QRhi and related classes such as QShader are considered
160 trusted content.
161
162 \warning Application developers are advised to carefully consider the
163 potential implications before allowing the feeding of user-provided content
164 that is not part of the application and is not under the developers'
165 control. (this includes all vertex/index data, shaders, pipeline and draw
166 call parameters, etc.)
167
168 \section1 Design Fundamentals
169
170 A QRhi cannot be instantiated directly. Instead, use the create()
171 function. Delete the QRhi instance normally to release the graphics device.
172
173 \section2 Resources
174
175 Instances of classes deriving from QRhiResource, such as, QRhiBuffer,
176 QRhiTexture, etc., encapsulate zero, one, or more native graphics
177 resources. Instances of such classes are always created via the \c new
178 functions of the QRhi, such as, newBuffer(), newTexture(),
179 newTextureRenderTarget(), newSwapChain().
180
181 \code
182 QRhiBuffer *vbuf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
183 if (!vbuf->create()) { error(); }
184 // ...
185 delete vbuf;
186 \endcode
187
188 \list
189
190 \li The returned value from functions like newBuffer() is always owned by
191 the caller.
192
193 \li Just creating an instance of a QRhiResource subclass never allocates or
194 initializes any native resources. That is only done when calling the
195 \c create() function of a subclass, for example, QRhiBuffer::create() or
196 QRhiTexture::create().
197
198 \li The exceptions are
199 QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor(),
200 QRhiSwapChain::newCompatibleRenderPassDescriptor(), and
201 QRhiRenderPassDescriptor::newCompatibleRenderPassDescriptor(). There is no
202 \c create() operation for these and the returned object is immediately
203 active.
204
205 \li The resource objects themselves are treated as immutable: once a
206 resource has create() called, changing any parameters via the setters, such as,
207 QRhiTexture::setPixelSize(), has no effect, unless the underlying native
208 resource is released and \c create() is called again. See more about resource
209 reuse in the sections below.
210
211 \li The underlying native resources are scheduled for releasing by the
212 QRhiResource destructor, or by calling QRhiResource::destroy(). Backends
213 often queue release requests and defer executing them to an unspecified
214 time, this is hidden from the applications. This way applications do not
215 have to worry about releasing native resources that may still be in use by
216 an in-flight frame.
217
218 \li Note that this does not mean that a QRhiResource can freely be
219 destroy()'ed or deleted within a frame (that is, in a
220 \l{QRhi::beginFrame()}{beginFrame()} - \l{QRhi::endFrame()}{endFrame()}
221 section). As a general rule, all referenced QRhiResource objects must stay
222 unchanged until the frame is submitted by calling
223 \l{QRhi::endFrame()}{endFrame()}. To ease this,
224 QRhiResource::deleteLater() is provided as a convenience.
225
226 \endlist
227
228 \section2 Command buffers and deferred command execution
229
230 Regardless of the design and capabilities of the underlying graphics API,
231 all QRhi backends implement some level of command buffers. No
232 QRhiCommandBuffer function issues any native bind or draw command (such as,
233 \c glDrawElements) directly. Commands are always recorded in a queue,
234 either native or provided by the QRhi backend. The command buffer is
235 submitted, and so execution starts only upon QRhi::endFrame() or
236 QRhi::finish().
237
238 The deferred nature has consequences for some types of objects. For example,
239 writing to a dynamic buffer multiple times within a frame, in case such
240 buffers are backed by host-visible memory, will result in making the
241 results of all writes are visible to all draw calls in the command buffer
242 of the frame, regardless of when the dynamic buffer update was recorded
243 relative to a draw call.
244
245 Furthermore, instances of QRhiResource subclasses must be treated immutable
246 within a frame in which they are referenced in any way. Create
247 all resources upfront, before starting to record commands for the next
248 frame. Reusing a QRhiResource instance within a frame (by calling \c create()
249 then referencing it again in the same \c{beginFrame - endFrame} section)
250 should be avoided as it may lead to unexpected results, depending on the
251 backend.
252
253 As a general rule, all referenced QRhiResource objects must stay valid and
254 unmodified until the frame is submitted by calling
255 \l{QRhi::endFrame()}{endFrame()}. On the other hand, calling
256 \l{QRhiResource::destroy()}{destroy()} or deleting the QRhiResource are
257 always safe once the frame is submitted, regardless of the status of the
258 underlying native resources (which may still be in use by the GPU - but
259 that is taken care of internally).
260
261 Unlike APIs like OpenGL, upload and copy type of commands cannot be mixed
262 with draw commands. The typical renderer will involve a sequence similar to
263 the following:
264
265 \list
266 \li (re)create resources
267 \li begin frame
268 \li record/issue uploads and copies
269 \li start recording a render pass
270 \li record draw calls
271 \li end render pass
272 \li end frame
273 \endlist
274
275 Recording copy type of operations happens via QRhiResourceUpdateBatch. Such
276 operations are committed typically on
277 \l{QRhiCommandBuffer::beginPass()}{beginPass()}.
278
279 When working with legacy rendering engines designed for OpenGL, the
280 migration to QRhi often involves redesigning from having a single \c render
281 step (that performs copies and uploads, clears buffers, and issues draw
282 calls, all mixed together) to a clearly separated, two phase \c prepare -
283 \c render setup where the \c render step only starts a renderpass and
284 records draw calls, while all resource creation and queuing of updates,
285 uploads and copies happens beforehand, in the \c prepare step.
286
287 QRhi does not at the moment allow freely creating and submitting command
288 buffers. This may be lifted in the future to some extent, in particular if
289 compute support is introduced, but the model of well defined
290 \c{frame-start} and \c{frame-end} points, combined with a dedicated,
291 "frame" command buffer, where \c{frame-end} implies presenting, is going to
292 remain the primary way of operating since this is what fits Qt's various UI
293 technologies best.
294
295 \section2 Threading
296
297 A QRhi instance and the associated resources can be created and used on any
298 thread but all usage must be limited to that one single thread. When
299 rendering to multiple QWindows in an application, having a dedicated thread
300 and QRhi instance for each window is often advisable, as this can eliminate
301 issues with unexpected throttling caused by presenting to multiple windows.
302 Conceptually that is then the same as how Qt Quick scene graph's threaded
303 render loop operates when working directly with OpenGL: one thread for each
304 window, one QOpenGLContext for each thread. When moving onto QRhi,
305 QOpenGLContext is replaced by QRhi, making the migration straightforward.
306
307 When it comes to externally created native objects, such as OpenGL contexts
308 passed in via QRhiGles2NativeHandles, it is up to the application to ensure
309 they are not misused by other threads.
310
311 Resources are not shareable between QRhi instances. This is an intentional
312 choice since QRhi hides most queue, command buffer, and resource
313 synchronization related tasks, and provides no API for them. Safe and
314 efficient concurrent use of graphics resources from multiple threads is
315 tied to those concepts, however, and is thus a topic that is currently out
316 of scope, but may be introduced in the future.
317
318 \note The Metal backend requires that an autorelease pool is available on
319 the rendering thread, ideally wrapping each iteration of the render loop.
320 This needs no action from the users of QRhi when rendering on the main
321 (gui) thread, but becomes important when a separate, dedicated render
322 thread is used.
323
324 \section2 Resource synchronization
325
326 QRhi does not expose APIs for resource barriers or image layout
327 transitions. Such synchronization is done implicitly by the backends, where
328 applicable (for example, Vulkan), by tracking resource usage as necessary.
329 Buffer and image barriers are inserted before render or compute passes
330 transparently to the application.
331
332 \note Resources within a render or compute pass are expected to be bound to
333 a single usage during that pass. For example, a buffer can be used as
334 vertex, index, uniform, or storage buffer, but not a combination of them
335 within a single pass. However, it is perfectly fine to use a buffer as a
336 storage buffer in a compute pass, and then as a vertex buffer in a render
337 pass, for example, assuming the buffer declared both usages upon creation.
338
339 \note Textures have this rule relaxed in certain cases, because using two
340 subresources (typically two different mip levels) of the same texture for
341 different access (one for load, one for store) is supported even within the
342 same pass.
343
344 \section2 Resource reuse
345
346 From the user's point of view a QRhiResource is reusable immediately after
347 calling QRhiResource::destroy(). With the exception of swapchains, calling
348 \c create() on an already created object does an implicit \c destroy(). This
349 provides a handy shortcut to reuse a QRhiResource instance with different
350 parameters, with a new native graphics object underneath.
351
352 The importance of reusing the same object lies in the fact that some
353 objects reference other objects: for example, a QRhiShaderResourceBindings
354 can reference QRhiBuffer, QRhiTexture, and QRhiSampler instances. If in a
355 later frame one of these buffers need to be resized or a sampler parameter
356 needs changing, destroying and creating a whole new QRhiBuffer or
357 QRhiSampler would invalidate all references to the old instance. By just
358 changing the appropriate parameters via QRhiBuffer::setSize() or similar
359 and then calling QRhiBuffer::create(), everything works as expected and
360 there is no need to touch the QRhiShaderResourceBindings at all, even
361 though there is a good chance that under the hood the QRhiBuffer is now
362 backed by a whole new native buffer.
363
364 \code
365 QRhiBuffer *ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 256);
366 ubuf->create();
367
368 QRhiShaderResourceBindings *srb = rhi->newShaderResourceBindings()
369 srb->setBindings({
370 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, ubuf)
371 });
372 srb->create();
373
374 // ...
375
376 // now in a later frame we need to grow the buffer to a larger size
377 ubuf->setSize(512);
378 ubuf->create(); // same as ubuf->destroy(); ubuf->create();
379
380 // srb needs no changes whatsoever, any references in it to ubuf
381 // stay valid. When it comes to internal details, such as that
382 // ubuf may now be backed by a completely different native buffer
383 // resource, that is is recognized and handled automatically by the
384 // next setShaderResources().
385 \endcode
386
387 QRhiTextureRenderTarget offers the same contract: calling
388 QRhiCommandBuffer::beginPass() is safe even when one of the render target's
389 associated textures or renderbuffers has been rebuilt (by calling \c
390 create() on it) since the creation of the render target object. This allows
391 the application to resize a texture by setting a new pixel size on the
392 QRhiTexture and calling create(), thus creating a whole new native texture
393 resource underneath, without having to update the QRhiTextureRenderTarget
394 as that will be done implicitly in beginPass().
395
396 \section2 Pooled objects
397
398 In addition to resources, there are pooled objects as well, such as,
399 QRhiResourceUpdateBatch. An instance is retrieved via a \c next function,
400 such as, nextResourceUpdateBatch(). The caller does not own the returned
401 instance in this case. The only valid way of operating here is calling
402 functions on the QRhiResourceUpdateBatch and then passing it to
403 QRhiCommandBuffer::beginPass() or QRhiCommandBuffer::endPass(). These
404 functions take care of returning the batch to the pool. Alternatively, a
405 batch can be "canceled" and returned to the pool without processing by
406 calling QRhiResourceUpdateBatch::release().
407
408 A typical pattern is thus:
409
410 \code
411 QRhiResourceUpdateBatch *resUpdates = rhi->nextResourceUpdateBatch();
412 // ...
413 resUpdates->updateDynamicBuffer(ubuf, 0, 64, mvp.constData());
414 if (!image.isNull()) {
415 resUpdates->uploadTexture(texture, image);
416 image = QImage();
417 }
418 // ...
419 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
420 // note the last argument
421 cb->beginPass(swapchain->currentFrameRenderTarget(), clearCol, clearDs, resUpdates);
422 \endcode
423
424 \section2 Swapchain specifics
425
426 QRhiSwapChain features some special semantics due to the peculiar nature of
427 swapchains.
428
429 \list
430
431 \li It has no \c create() but rather a QRhiSwapChain::createOrResize().
432 Repeatedly calling this function is \b not the same as calling
433 QRhiSwapChain::destroy() followed by QRhiSwapChain::createOrResize(). This
434 is because swapchains often have ways to handle the case where buffers need
435 to be resized in a manner that is more efficient than a brute force
436 destroying and recreating from scratch.
437
438 \li An active QRhiSwapChain must be released by calling
439 \l{QRhiSwapChain::destroy()}{destroy()}, or by destroying the object, before
440 the QWindow's underlying QPlatformWindow, and so the associated native
441 window object, is destroyed. It should not be postponed because releasing
442 the swapchain may become problematic (and with some APIs, like Vulkan, is
443 explicitly disallowed) when the native window is not around anymore, for
444 example because the QPlatformWindow got destroyed upon getting a
445 QWindow::close(). Therefore, releasing the swapchain must happen whenever
446 the targeted QWindow sends the
447 QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed event. If the event does
448 not arrive before the destruction of the QWindow - this can happen when
449 using QCoreApplication::quit() -, then check QWindow::handle() after the
450 event loop exits and invoke the swapchain release when non-null (meaning
451 the underlying native window is still around).
452
453 \endlist
454
455 \section2 Ownership
456
457 The general rule is no ownership transfer. Creating a QRhi with an already
458 existing graphics device does not mean the QRhi takes ownership of the
459 device object. Similarly, ownership is not given away when a device or
460 texture object is "exported" via QRhi::nativeHandles() or
461 QRhiTexture::nativeTexture(). Most importantly, passing pointers in structs
462 and via setters does not transfer ownership.
463
464 \section1 Troubleshooting and Profiling
465
466 \section2 Error reporting
467
468 Functions such as \l QRhi::create() and the resource classes' \c create()
469 member functions (e.g., \l QRhiBuffer::create()) indicate failure with the
470 return value (\nullptr or
471 \c false, respectively). When working with QShader, \l QShader::fromSerialized()
472 returns an invalid QShader (for which \l{QShader::isValid()}{isValid()} returns
473 \c false) when the data passed to the function cannot be successfully deserialized.
474 Some functions, beginFrame() in particular, may also sometimes report "soft failures",
475 such as \l FrameOpSwapChainOutOfDate, which do not indicate an unrecoverable error,
476 but rather should be seen as a "try again later" response.
477
478 Warnings and errors may get printed at any time to the debug output via
479 qWarning(). It is therefore always advisable to inspect the output of the
480 application.
481
482 Additional debug messages can be enabled via the following logging
483 categories. Messages from these categories are not printed by default
484 unless explicitly enabled via QLoggingCategory or the \c QT_LOGGING_RULES
485 environment variable. For better interoperation with Qt Quick, the
486 environment variable \c{QSG_INFO} also enables these debug prints.
487
488 \list
489 \li \c{qt.rhi.general}
490 \endlist
491
492 Additionally, applications can query the \l{QRhi::backendName()}{QRhi
493 backend name} and
494 \l{QRhi::driverInfo()}{graphics device information} from a successfully
495 initialized QRhi. This can then be printed to the user or stored in the
496 application logs even in production builds, if desired.
497
498 \section2 Investigating rendering problems
499
500 When the rendering results are not as expected, or the application is
501 experiencing problems, always consider checking with the the native 3D
502 APIs' debug and validation facilities. QRhi itself features limited error
503 checking since replicating the already existing, vast amount of
504 functionality in the underlying layers is not reasonable.
505
506 \list
507
508 \li For Vulkan, controlling the
509 \l{https://github.com/KhronosGroup/Vulkan-ValidationLayers}{Vulkan
510 Validation Layers} is not in the scope of the QRhi, but rather can be
511 achieved by configuring the \l QVulkanInstance with the appropriate layers.
512 For example, call \c{instance.setLayers({ "VK_LAYER_KHRONOS_validation" });}
513 before invoking \l{QVulkanInstance::create()}{create()} on the QVulkanInstance.
514 (note that this assumes that the validation layers are actually installed
515 and available, e.g. from the Vulkan SDK) By default, QVulkanInstance conveniently
516 redirects the Vulkan debug messages to qDebug, meaning the validation messages get
517 printed just like other Qt warnings.
518
519 \li With Direct 3D 11 and 12, a graphics device with the debug layer
520 enabled can be requested by toggling the \c enableDebugLayer flag in the
521 appropriate \l{QRhiD3D11InitParams}{init params struct}. The messages appear on the
522 debug output, which is visible in Qt Creator's messages panel or via a tool
523 such as \l{https://learn.microsoft.com/en-us/sysinternals/downloads/debugview}{DebugView}.
524
525 \li For Metal, controlling Metal Validation is outside of QRhi's scope.
526 Rather, to enable validation, run the application with the environment
527 variable \c{METAL_DEVICE_WRAPPER_TYPE=1} set, or run the application within
528 XCode. There may also be further settings and environment variable in modern
529 XCode and macOS versions. See for instance
530 \l{https://developer.apple.com/documentation/metal/diagnosing_metal_programming_issues_early}{this
531 page}.
532
533 \endlist
534
535 \section2 Frame captures and performance profiling
536
537 A Qt application rendering with QRhi to a window while relying on a 3D API
538 under the hood, is, from the windowing and graphics pipeline perspective at
539 least, no different from any other (non-Qt) applications using the same 3D
540 API. This means that tools and practices for debugging and profiling
541 applications involving 3D graphics, such as games, all apply to such a Qt
542 application as well.
543
544 A few examples of tools that can provide insights into the rendering
545 internals of Qt applications that use QRhi, which includes Qt Quick and Qt
546 Quick 3D based projects as well:
547
548 \list
549
550 \li \l{https://renderdoc.org/}{RenderDoc} allows taking frame captures and
551 introspecting the recorded commands and pipeline state on Windows and Linux
552 for applications using OpenGL, Vulkan, D3D11, or D3D12. When trying to
553 figure out why some parts of the 3D scene do not show up as expected,
554 RenderDoc is often a fast and efficient way to check the pipeline stages
555 and the related state and discover the missing or incorrect value. It is
556 also a tool that is actively used when developing Qt itself.
557
558 \li For NVIDIA-based systems,
559 \l{https://developer.nvidia.com/nsight-graphics}{Nsight Graphics} provides
560 a graphics debugger tool on Windows and Linux. In addition to investigating the commands
561 in the frame and the pipeline, the vendor-specific tools allow looking at timings and
562 hardware performance information, which is not something simple frame captures can provide.
563
564 \li For AMD-based systems, the \l{https://gpuopen.com/rgp/}{Radeon GPU
565 Profiler} can be used to gain deeper insights into the application's
566 rendering and its performance.
567
568 \li As QRhi supports Direct 3D 12, using
569 \l{https://devblogs.microsoft.com/pix/download/}{PIX}, a performance tuning
570 and debugging tool for DirectX 12 games on Windows is an option as well.
571
572 \li On macOS,
573 \l{https://developer.apple.com/documentation/metal/debugging_tools/viewing_your_gpu_workload_with_the_metal_debugger}{the
574 XCode Metal debugger} can be used to take and introspect frame
575 captures, to investigate performance details, and debug shaders. In macOS 13 it is also possible
576 to enable an overlay that displays frame rate and other information for any Metal-based window by
577 setting the environment variable \c{MTL_HUD_ENABLED=1}.
578
579 \endlist
580
581 On mobile and embedded platforms, there may be vendor and platform-specific
582 tools, provided by the GPU or SoC vendor, available to perform performance
583 profiling of application using OpenGL ES or Vulkan.
584
585 When capturing frames, remember that objects and groups of commands can be
586 named via debug markers, as long as \l{QRhi::EnableDebugMarkers}{debug
587 markers were enabled} for the QRhi, and the graphics API in use supports
588 this. To annotate the command stream, call
589 \l{QRhiCommandBuffer::debugMarkBegin()}{debugMarkBegin()},
590 \l{QRhiCommandBuffer::debugMarkEnd()}{debugMarkEnd()} and/or
591 \l{QRhiCommandBuffer::debugMarkMsg()}{debugMarkMsg()}.
592 This can be particularly useful in larger frames with multiple render passes.
593 Resources are named by calling \l{QRhiResource::setName()}{setName()} before create().
594
595 To perform basic timing measurements on the CPU and GPU side within the
596 application, \l QElapsedTimer and
597 \l QRhiCommandBuffer::lastCompletedGpuTime() can be used. The latter is
598 only available with select graphics APIs at the moment and requires opting
599 in via the \l QRhi::EnableTimestamps flag.
600
601 \section2 Resource leak checking
602
603 When destroying a QRhi object without properly destroying all buffers,
604 textures, and other resources created from it, warnings about this are
605 printed to the debug output whenever the application is a debug build, or
606 when the \c QT_RHI_LEAK_CHECK environment variable is set to a non-zero
607 value. This is a simple way to discover design issues around resource
608 handling within the application rendering logic. Note however that some
609 platforms and underlying graphics APIs may perform their own allocation and
610 resource leak detection as well, over which Qt will have no direct control.
611 For example, when using Vulkan, the memory allocator may raise failing
612 assertions in debug builds when resources that own graphics memory
613 allocations are not destroyed before the QRhi. In addition, the Vulkan
614 validation layer, when enabled, will issue warnings about native graphics
615 resources that were not released. Similarly, with Direct 3D warnings may
616 get printed about unreleased COM objects when the application does not
617 destroy the QRhi and its resources in the correct order.
618
619 \sa {RHI Window Example}, QRhiCommandBuffer, QRhiResourceUpdateBatch,
620 QRhiShaderResourceBindings, QShader, QRhiBuffer, QRhiTexture,
621 QRhiRenderBuffer, QRhiSampler, QRhiTextureRenderTarget,
622 QRhiGraphicsPipeline, QRhiComputePipeline, QRhiSwapChain
623 */
624
625/*!
626 \enum QRhi::Implementation
627 Describes which graphics API-specific backend gets used by a QRhi instance.
628
629 \value Null
630 \value Vulkan
631 \value OpenGLES2
632 \value D3D11
633 \value D3D12
634 \value Metal
635 */
636
637/*!
638 \enum QRhi::Flag
639 Describes what special features to enable.
640
641 \value EnableDebugMarkers Enables debug marker groups. Without this frame
642 debugging features like making debug groups and custom resource name
643 visible in external GPU debugging tools will not be available and functions
644 like QRhiCommandBuffer::debugMarkBegin() will become no-ops. Avoid enabling
645 in production builds as it may involve a small performance impact. Has no
646 effect when the QRhi::DebugMarkers feature is not reported as supported.
647
648 \value EnableTimestamps Enables GPU timestamp collection. When not set,
649 QRhiCommandBuffer::lastCompletedGpuTime() always returns 0. Enable this
650 only when needed since there may be a small amount of extra work involved
651 (e.g. timestamp queries), depending on the underlying graphics API. Has no
652 effect when the QRhi::Timestamps feature is not reported as supported.
653
654 \value PreferSoftwareRenderer Indicates that backends should prefer
655 choosing an adapter or physical device that renders in software on the CPU.
656 For example, with Direct3D there is typically a "Basic Render Driver"
657 adapter available with \c{DXGI_ADAPTER_FLAG_SOFTWARE}. Setting this flag
658 requests the backend to choose that adapter over any other, as long as no
659 specific adapter was forced by other backend-specific means. With Vulkan
660 this maps to preferring physical devices with
661 \c{VK_PHYSICAL_DEVICE_TYPE_CPU}. When not available, or when it is not
662 possible to decide if an adapter/device is software-based, this flag is
663 ignored. It may also be ignored with graphics APIs that have no concept and
664 means of enumerating adapters/devices.
665
666 \value EnablePipelineCacheDataSave Enables retrieving the pipeline cache
667 contents, where applicable. When not set, pipelineCacheData() will return
668 an empty blob always. With backends where retrieving and restoring the
669 pipeline cache contents is not supported, the flag has no effect and the
670 serialized cache data is always empty. The flag provides an opt-in
671 mechanism because the cost of maintaining the related data structures is
672 not insignificant with some backends. With Vulkan this feature maps
673 directly to VkPipelineCache, vkGetPipelineCacheData and
674 VkPipelineCacheCreateInfo::pInitialData. With Direct3D 11 there is no real
675 pipline cache, but the results of HLSL->DXBC compilations are stored and
676 can be serialized/deserialized via this mechanism. This allows skipping the
677 time consuming D3DCompile() in future runs of the applications for shaders
678 that come with HLSL source instead of offline pre-compiled bytecode. This
679 can provide a huge boost in startup and load times, if there is a lot of
680 HLSL source compilation happening. With OpenGL the "pipeline cache" is
681 simulated by retrieving and loading shader program binaries (if supported
682 by the driver). With OpenGL there are additional, disk-based caching
683 mechanisms for shader/program binaries provided by Qt. Writing to those may
684 get disabled whenever this flag is set since storing program binaries to
685 multiple caches is not sensible.
686
687 \value SuppressSmokeTestWarnings Indicates that, with backends where this
688 is relevant, certain, non-fatal QRhi::create() failures should not
689 produce qWarning() calls. For example, with D3D11, passing this flag
690 makes a number of warning messages (that appear due to QRhi::create()
691 failing) to become categorized debug prints instead under the commonly used
692 \c{qt.rhi.general} logging category. This can be used by engines, such as
693 Qt Quick, that feature fallback logic, i.e. they retry calling create()
694 with a different set of flags (such as, \l PreferSoftwareRenderer), in order
695 to hide the unconditional warnings from the output that would be printed
696 when the first create() attempt had failed.
697 */
698
699/*!
700 \enum QRhi::FrameOpResult
701 Describes the result of operations that can have a soft failure.
702
703 \value FrameOpSuccess Success
704
705 \value FrameOpError Unspecified error
706
707 \value FrameOpSwapChainOutOfDate The swapchain is in an inconsistent state
708 internally. This can be recoverable by attempting to repeat the operation
709 (such as, beginFrame()) later.
710
711 \value FrameOpDeviceLost The graphics device was lost. This can be
712 recoverable by attempting to repeat the operation (such as, beginFrame())
713 after releasing and reinitializing all objects backed by native graphics
714 resources. See isDeviceLost().
715 */
716
717/*!
718 \enum QRhi::Feature
719 Flag values to indicate what features are supported by the backend currently in use.
720
721 \value MultisampleTexture Indicates that textures with a sample count larger
722 than 1 are supported. In practice this feature will be unsupported with
723 OpenGL ES versions older than 3.1, and OpenGL older than 3.0.
724
725 \value MultisampleRenderBuffer Indicates that renderbuffers with a sample
726 count larger than 1 are supported. In practice this feature will be
727 unsupported with OpenGL ES 2.0, and may also be unsupported with OpenGL 2.x
728 unless the relevant extensions are present.
729
730 \value DebugMarkers Indicates that debug marker groups (and so
731 QRhiCommandBuffer::debugMarkBegin()) are supported.
732
733 \value Timestamps Indicates that command buffer timestamps are supported.
734 Relevant for QRhiCommandBuffer::lastCompletedGpuTime(). This can be
735 expected to be supported on Metal, Vulkan, Direct 3D 11 and 12, and OpenGL
736 contexts of version 3.3 or newer. However, with some of these APIs support
737 for timestamp queries is technically optional, and therefore it cannot be
738 guaranteed that this feature is always supported with every implementation
739 of them.
740
741 \value Instancing Indicates that instanced drawing is supported. In
742 practice this feature will be unsupported with OpenGL ES 2.0 and OpenGL
743 3.2 or older.
744
745 \value CustomInstanceStepRate Indicates that instance step rates other
746 than 1 are supported. In practice this feature will always be unsupported
747 with OpenGL. In addition, running with Vulkan 1.0 without
748 VK_EXT_vertex_attribute_divisor will also lead to reporting false for this
749 feature.
750
751 \value PrimitiveRestart Indicates that restarting the assembly of
752 primitives when encountering an index value of 0xFFFF
753 (\l{QRhiCommandBuffer::IndexUInt16}{IndexUInt16}) or 0xFFFFFFFF
754 (\l{QRhiCommandBuffer::IndexUInt32}{IndexUInt32}) is enabled, for certain
755 primitive topologies at least. QRhi will try to enable this with all
756 backends, but in some cases it will not be supported. Dynamically
757 controlling primitive restart is not possible since with some APIs
758 primitive restart with a fixed index is always on. Applications must assume
759 that whenever this feature is reported as supported, the above mentioned
760 index values \c may be treated specially, depending on the topology. The
761 only two topologies where primitive restart is guaranteed to behave
762 identically across backends, as long as this feature is reported as
763 supported, are \l{QRhiGraphicsPipeline::LineStrip}{LineStrip} and
764 \l{QRhiGraphicsPipeline::TriangleStrip}{TriangleStrip}.
765
766 \value NonDynamicUniformBuffers Indicates that creating buffers with the
767 usage \l{QRhiBuffer::UniformBuffer}{UniformBuffer} and the types
768 \l{QRhiBuffer::Immutable}{Immutable} or \l{QRhiBuffer::Static}{Static} is
769 supported. When reported as unsupported, uniform (constant) buffers must be
770 created as \l{QRhiBuffer::Dynamic}{Dynamic}. (which is recommended
771 regardless)
772
773 \value NonFourAlignedEffectiveIndexBufferOffset Indicates that effective
774 index buffer offsets (\c{indexOffset + firstIndex * indexComponentSize})
775 that are not 4 byte aligned are supported. When not supported, attempting
776 to issue a \l{QRhiCommandBuffer::drawIndexed()}{drawIndexed()} with a
777 non-aligned effective offset may lead to unspecified behavior. Relevant in
778 particular for Metal, where this will be reported as unsupported.
779
780 \value NPOTTextureRepeat Indicates that the
781 \l{QRhiSampler::Repeat}{Repeat} wrap mode and mipmap filtering modes are
782 supported for textures with a non-power-of-two size. In practice this can
783 only be false with OpenGL ES 2.0 implementations without
784 \c{GL_OES_texture_npot}.
785
786 \value RedOrAlpha8IsRed Indicates that the
787 \l{QRhiTexture::RED_OR_ALPHA8}{RED_OR_ALPHA8} format maps to a one
788 component 8-bit \c red format. This is the case for all backends except
789 OpenGL when using either OpenGL ES or a non-core profile context. There
790 \c{GL_ALPHA}, a one component 8-bit \c alpha format, is used
791 instead. Using the special texture format allows having a single code
792 path for creating textures, leaving it up to the backend to decide the
793 actual format, while the feature flag can be used to pick the
794 appropriate shader variant for sampling the texture.
795
796 \value ElementIndexUint Indicates that 32-bit unsigned integer elements are
797 supported in the index buffer. In practice this is true everywhere except
798 when running on plain OpenGL ES 2.0 implementations without the necessary
799 extension. When false, only 16-bit unsigned elements are supported in the
800 index buffer.
801
802 \value Compute Indicates that compute shaders, image load/store, and
803 storage buffers are supported. OpenGL older than 4.3 and OpenGL ES older
804 than 3.1 have no compute support.
805
806 \value WideLines Indicates that lines with a width other than 1 are
807 supported. When reported as not supported, the line width set on the
808 graphics pipeline state is ignored. This can always be false with some
809 backends (D3D11, D3D12, Metal). With Vulkan, the value depends on the
810 implementation. With OpenGL, wide lines are not supported in core profile
811 contexts.
812
813 \value VertexShaderPointSize Indicates that the size of rasterized points
814 set via \c{gl_PointSize} in the vertex shader is taken into account. When
815 reported as not supported, drawing points with a size other than 1 is not
816 supported. Setting \c{gl_PointSize} in the shader is still valid then, but
817 is ignored. (for example, when generating HLSL, the assignment is silently
818 dropped from the generated code) Note that some APIs (Metal, Vulkan)
819 require the point size to be set in the shader explicitly whenever drawing
820 points, even when the size is 1, as they do not automatically default to 1.
821
822 \value BaseVertex Indicates that
823 \l{QRhiCommandBuffer::drawIndexed()}{drawIndexed()} supports the \c
824 vertexOffset argument. When reported as not supported, the vertexOffset
825 value in an indexed draw is ignored. In practice this feature will be
826 unsupported with OpenGL and OpenGL ES versions lower than 3.2, and with
827 Metal on older iOS devices, including the iOS Simulator.
828
829 \value BaseInstance Indicates that instanced draw commands support the \c
830 firstInstance argument. When reported as not supported, the firstInstance
831 value is ignored and the instance ID starts from 0. In practice this feature
832 will be unsupported with with Metal on older iOS devices, including the iOS
833 Simulator, and all versions of OpenGL. The latter is due to OpenGL ES not
834 supporting draw calls with a base instance at all. Currently QRhi's OpenGL
835 backend does not implement the functionality for OpenGL (non-ES) either,
836 because portable applications cannot rely on a non-zero base instance in
837 practice due to GLES. If the application still chooses to do so, it should
838 be aware of the InstanceIndexIncludesBaseInstance feature as well.
839
840 \value TriangleFanTopology Indicates that QRhiGraphicsPipeline::setTopology()
841 supports QRhiGraphicsPipeline::TriangleFan. In practice this feature will be
842 unsupported with Metal and Direct 3D 11/12.
843
844 \value ReadBackNonUniformBuffer Indicates that
845 \l{QRhiResourceUpdateBatch::readBackBuffer()}{reading buffer contents} is
846 supported for QRhiBuffer instances with a usage different than
847 UniformBuffer. In practice this feature will be unsupported with OpenGL ES
848 2.0.
849
850 \value ReadBackNonBaseMipLevel Indicates that specifying a mip level other
851 than 0 is supported when reading back texture contents. When not supported,
852 specifying a non-zero level in QRhiReadbackDescription leads to returning
853 an all-zero image. In practice this feature will be unsupported with OpenGL
854 ES 2.0.
855
856 \value TexelFetch Indicates that texelFetch() and textureLod() are available
857 in shaders. In practice this will be reported as unsupported with OpenGL ES
858 2.0 and OpenGL 2.x contexts, because GLSL 100 es and versions before 130 do
859 not support these functions.
860
861 \value RenderToNonBaseMipLevel Indicates that specifying a mip level other
862 than 0 is supported when creating a QRhiTextureRenderTarget with a
863 QRhiTexture as its color attachment. When not supported, create() will fail
864 whenever the target mip level is not zero. In practice this feature will be
865 unsupported with OpenGL ES 2.0.
866
867 \value IntAttributes Indicates that specifying input attributes with
868 signed and unsigned integer types for a shader pipeline is supported. When
869 not supported, build() will succeed but just show a warning message and the
870 values of the target attributes will be broken. In practice this feature
871 will be unsupported with OpenGL ES 2.0 and OpenGL 2.x.
872
873 \value ScreenSpaceDerivatives Indicates that functions such as dFdx(),
874 dFdy(), and fwidth() are supported in shaders. In practice this feature will
875 be unsupported with OpenGL ES 2.0 without the GL_OES_standard_derivatives
876 extension.
877
878 \value ReadBackAnyTextureFormat Indicates that reading back texture
879 contents can be expected to work for any QRhiTexture::Format. Backends
880 other than OpenGL can be expected to return true for this feature. When
881 reported as false, which will typically happen with OpenGL, only the
882 formats QRhiTexture::RGBA8 and QRhiTexture::BGRA8 are guaranteed to be
883 supported for readbacks. In addition, with OpenGL, but not OpenGL ES,
884 reading back the 1 byte per component formats QRhiTexture::R8 and
885 QRhiTexture::RED_OR_ALPHA8 are supported as well. Reading back floating
886 point formats QRhiTexture::RGBA16F and RGBA32F may work too with OpenGL, as
887 long as the implementation provides support for these, but QRhi can give no
888 guarantees, as indicated by this flag.
889
890 \value PipelineCacheDataLoadSave Indicates that the pipelineCacheData() and
891 setPipelineCacheData() functions are functional. When not supported, the
892 functions will not perform any action, the retrieved blob is always empty,
893 and thus no benefits can be expected from retrieving and, during a
894 subsequent run of the application, reloading the pipeline cache content.
895
896 \value ImageDataStride Indicates that specifying a custom stride (row
897 length) for raw image data in texture uploads is supported. When not
898 supported (which can happen when the underlying API is OpenGL ES 2.0 without
899 support for GL_UNPACK_ROW_LENGTH),
900 QRhiTextureSubresourceUploadDescription::setDataStride() must not be used.
901
902 \value RenderBufferImport Indicates that QRhiRenderBuffer::createFrom() is
903 supported. For most graphics APIs this is not sensible because
904 QRhiRenderBuffer encapsulates texture objects internally, just like
905 QRhiTexture. With OpenGL however, renderbuffer object exist as a separate
906 object type in the API, and in certain environments (for example, where one
907 may want to associated a renderbuffer object with an EGLImage object) it is
908 important to allow wrapping an existing OpenGL renderbuffer object with a
909 QRhiRenderBuffer.
910
911 \value ThreeDimensionalTextures Indicates that 3D textures are supported.
912 In practice this feature will be unsupported with OpenGL and OpenGL ES
913 versions lower than 3.0.
914
915 \value RenderTo3DTextureSlice Indicates that rendering to a slice in a 3D
916 texture is supported. This can be unsupported with Vulkan 1.0 due to
917 relying on VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT which is a Vulkan 1.1
918 feature.
919
920 \value TextureArrays Indicates that texture arrays are supported and
921 QRhi::newTextureArray() is functional. Note that even when texture arrays
922 are not supported, arrays of textures are still available as those are two
923 independent features.
924
925 \value Tessellation Indicates that the tessellation control and evaluation
926 stages are supported. When reported as supported, the topology of a
927 QRhiGraphicsPipeline can be set to
928 \l{QRhiGraphicsPipeline::Patches}{Patches}, the number of control points
929 can be set via
930 \l{QRhiGraphicsPipeline::setPatchControlPointCount()}{setPatchControlPointCount()},
931 and shaders for tessellation control and evaluation can be specified in the
932 QRhiShaderStage list. Tessellation shaders have portability issues between
933 APIs (for example, translating GLSL/SPIR-V to HLSL is problematic due to
934 the way hull shaders are structured, whereas Metal uses a somewhat
935 different tessellation pipeline than others), and therefore unexpected
936 issues may still arise, even though basic functionality is implemented
937 across all the underlying APIs. For Direct 3D in particular, handwritten
938 HLSL hull and domain shaders must be injected into each QShader for the
939 tessellation control and evaluation stages, respectively, since qsb cannot
940 generate these from SPIR-V. Note that isoline tessellation should be
941 avoided as it will not be supported by all backends. The maximum patch
942 control point count portable between backends is 32.
943
944 \value GeometryShader Indicates that the geometry shader stage is supported.
945 When supported, a geometry shader can be specified in the QRhiShaderStage
946 list. Geometry Shaders are considered an experimental feature in QRhi and
947 can only be expected to be supported with Vulkan, Direct 3D 11 and 12,
948 OpenGL (3.2+) and OpenGL ES (3.2+), assuming the implementation reports it
949 as supported at run time. Starting with Qt 6.11 geometry shaders are
950 automatically translated to HLSL, and therefore no injection of handwritten
951 HLSL geometry shaders is necessary anymore (but note that gl_in and
952 expressions such as gl_in[0].gl_Position are not supported; rather, pass the
953 position as an output variable from the vertex shader). Geometry shaders are
954 not supported with Metal.
955
956 \value TextureArrayRange Indicates that for
957 \l{QRhi::newTextureArray()}{texture arrays} it is possible to specify a
958 range that is exposed to the shaders. Normally all array layers are exposed
959 and it is up to the shader to select the layer (via the third coordinate
960 passed to texture() when sampling the \c sampler2DArray). When supported,
961 calling QRhiTexture::setArrayRangeStart() and
962 QRhiTexture::setArrayRangeLength() before
963 \l{QRhiTexture::create()}{building} or
964 \l{QRhiTexture::createFrom()}{importing} the native texture has an effect,
965 and leads to selecting only the specified range from the array. This will
966 be necessary in special cases, such as when working with accelerated video
967 decoding and Direct 3D 11, because a texture array with both
968 \c{D3D11_BIND_DECODER} and \c{D3D11_BIND_SHADER_RESOURCE} on it is only
969 usable as a shader resource if a single array layer is selected. Note that
970 all this is applicable only when the texture is used as a
971 QRhiShaderResourceBinding::SampledTexture or
972 QRhiShaderResourceBinding::Texture shader resource, and is not compatible
973 with image load/store. This feature is only available with some backends as
974 it does not map well to all graphics APIs, and it is only meant to provide
975 support for special cases anyhow. In practice the feature can be expected to
976 be supported with Direct3D 11/12 and Vulkan.
977
978 \value NonFillPolygonMode Indicates that setting a PolygonMode other than
979 the default Fill is supported for QRhiGraphicsPipeline. A common use case
980 for changing the mode to Line is to get wireframe rendering. This however
981 is not available as a core OpenGL ES feature, and is optional with Vulkan
982 as well as some mobile GPUs may not offer the feature.
983
984 \value OneDimensionalTextures Indicates that 1D textures are supported.
985 In practice this feature will be unsupported on OpenGL ES.
986
987 \value OneDimensionalTextureMipmaps Indicates that generating 1D texture
988 mipmaps are supported. In practice this feature will be unsupported on
989 backends that do not report support for
990 \l{OneDimensionalTextures}, Metal, and Direct 3D 12.
991
992 \value HalfAttributes Indicates that specifying input attributes with half
993 precision (16bit) floating point types for a shader pipeline is supported.
994 When not supported, build() will succeed but just show a warning message
995 and the values of the target attributes will be broken. In practice this
996 feature will be unsupported in some OpenGL ES 2.0 and OpenGL 2.x
997 implementations. Note that while Direct3D 11/12 does support half precision
998 input attributes, it does not support the half3 type. The D3D backends pass
999 half3 attributes as half4. To ensure cross platform compatibility, half3
1000 inputs should be padded to 8 bytes.
1001
1002 \value RenderToOneDimensionalTexture Indicates that 1D texture render
1003 targets are supported. In practice this feature will be unsupported on
1004 backends that do not report support for
1005 \l{OneDimensionalTextures}, and Metal.
1006
1007 \value ThreeDimensionalTextureMipmaps Indicates that generating 3D texture
1008 mipmaps are supported. This is typically supported with all backends starting
1009 with Qt 6.10.
1010
1011 \value MultiView Indicates that multiview, see e.g.
1012 \l{https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_multiview.html}{VK_KHR_multiview}
1013 is supported. With OpenGL ES 2.0, Direct 3D 11, and OpenGL (ES)
1014 implementations without \c{GL_OVR_multiview2} this feature will not be
1015 supported. With Vulkan 1.1 and newer, and Direct 3D 12 multiview is
1016 typically supported. When reported as supported, creating a
1017 QRhiTextureRenderTarget with a QRhiColorAttachment that references a texture
1018 array and has \l{QRhiColorAttachment::setMultiViewCount()}{multiViewCount}
1019 set enables recording a render pass that uses multiview rendering. In addition,
1020 any QRhiGraphicsPipeline used in that render pass must have
1021 \l{QRhiGraphicsPipeline::setMultiViewCount()}{the same view count set}. Note that
1022 multiview is only available in combination with 2D texture arrays. It cannot
1023 be used to optimize the rendering into individual textures (e.g. two, for
1024 the left and right eyes). Rather, the target of a multiview render pass is
1025 always a texture array, automatically rendering to the layer (array element)
1026 corresponding to each view. Therefore this feature implies \l TextureArrays
1027 as well. Multiview rendering is not supported in combination with
1028 tessellation or geometry shaders. See QRhiColorAttachment::setMultiViewCount()
1029 for further details on multiview rendering. This enum value has been introduced in Qt 6.7.
1030
1031 \value TextureViewFormat Indicates that setting a
1032 \l{QRhiTexture::setWriteViewFormat()}{view format} on a QRhiTexture is
1033 effective. When reported as supported, setting the read (sampling) or write
1034 (render target / image load-store) view mode changes the texture's viewing
1035 format. When unsupported, setting a view format has no effect. Note that Qt
1036 has no knowledge or control over format compatibility or resource view rules
1037 in the underlying 3D API and its implementation. Passing in unsuitable,
1038 incompatible formats may lead to errors and unspecified behavior. This is
1039 provided mainly to allow "casting" rendering into a texture created with an
1040 sRGB format to non-sRGB to avoid the unwanted linear->sRGB conversion on
1041 shader writes. Other types of casting may or may not be functional,
1042 depending on the underlying API. Currently implemented for Vulkan and Direct
1043 3D 12. With D3D12 the feature is available only if
1044 \c CastingFullyTypedFormatSupported is supported, see
1045 \l{https://microsoft.github.io/DirectX-Specs/d3d/RelaxedCasting.html} (and
1046 note that QRhi always uses fully typed formats for textures.) This enum
1047 value has been introduced in Qt 6.8.
1048
1049 \value ResolveDepthStencil Indicates that resolving a multisample depth or
1050 depth-stencil texture is supported. Otherwise,
1051 \l{QRhiTextureRenderTargetDescription::setDepthResolveTexture()}{setting a
1052 depth resolve texture} is not functional and must be avoided. Direct 3D 11
1053 and 12 have no support for resolving depth/depth-stencil formats, and
1054 therefore this feature will never be supported with those. Vulkan 1.0 has no
1055 API to request resolving a depth-stencil attachment. Therefore, with Vulkan
1056 this feature will only be supported with Vulkan 1.2 and up, and on 1.1
1057 implementations with the appropriate extensions present. This feature is
1058 provided for the rare case when resolving into a non-multisample depth
1059 texture becomes necessary, for example when rendering into an
1060 OpenXR-provided depth texture (XR_KHR_composition_layer_depth). This enum
1061 value has been introduced in Qt 6.8.
1062
1063 \value VariableRateShading Indicates that per-draw (per-pipeline) variable
1064 rate shading is supported. When reported as supported, \l
1065 QRhiCommandBuffer::setShadingRate() is functional and has an effect for
1066 QRhiGraphicsPipeline objects that declared \l
1067 QRhiGraphicsPipeline::UsesShadingRate in their flags. Call \l
1068 QRhi::supportedShadingRates() to check which rates are supported. (1x1 is
1069 always supported, other typical values are 2x2, 1x2, 2x1, 2x4, 4x2, 4x4).
1070 This feature can be expected to be supported with Direct 3D 12 and Vulkan,
1071 assuming the implementation and GPU used at run time supports VRS. This enum
1072 value has been introduced in Qt 6.9.
1073
1074 \value VariableRateShadingMap Indicates that image-based specification of
1075 the shading rate is possible. The "image" is not necessarily a texture, it
1076 may be a native 3D API object, depending on the underlying backend and
1077 graphics API at run time. In practice this feature can be expected to be
1078 supported with Direct 3D 12, Vulkan, and Metal, assuming the GPU is modern
1079 enough to support VRS. To check if D3D12/Vulkan-style image-based VRS is
1080 suspported, use VariableRateShadingMapWithTexture instead. When this feature
1081 is reported as supported, there are two possibilities: when
1082 VariableRateShadingMapWithTexture is also true, then QRhiShadingRateMap
1083 consumes QRhiTexture objects via the createFrom() overload taking a
1084 QRhiTexture argument. When VariableRateShadingMapWithTexture is false, then
1085 QRhiShadingRateMap consumes some other type of native objects, for example
1086 an MTLRasterizationRateMap in case of Metal. Use the createFrom() overload
1087 taking a NativeShadingRateMap in this case. This enum value has been
1088 introduced in Qt 6.9.
1089
1090 \value VariableRateShadingMapWithTexture Indicates that image-based
1091 specification of the shading rate is supported via regular textures. In
1092 practice this may be supported with Direct 3D 12 and Vulkan. This enum value
1093 has been introduced in Qt 6.9.
1094
1095 \value PerRenderTargetBlending Indicates that per rendertarget blending is
1096 supported i.e. different render targets in MRT framebuffer can have different
1097 blending modes. In practice this can be expected to be supported everywhere
1098 except OpenGL ES, where it is only available with GLES 3.2 implementations.
1099 This enum value has been introduced in Qt 6.9.
1100
1101 \value SampleVariables Indicates that gl_SampleID, gl_SamplePosition,
1102 gl_SampleMaskIn and gl_SampleMask variables are available in fragment shaders.
1103 In practice this can be expected to be supported everywhere except OpenGL ES,
1104 where it is only available with GLES 3.2 implementations.
1105 This enum value has been introduced in Qt 6.9.
1106
1107 \value InstanceIndexIncludesBaseInstance Indicates that \c gl_InstanceIndex
1108 includes the base instance (the \c firstInstance argument in draw calls) in
1109 its value. When this feature is unsupported, but BaseInstance is, it
1110 indicates that \c gl_InstanceIndex always starts at 0, not the base value.
1111 In practice this will be the case for Direct 3D 11 and 12 at the moment.
1112 With Vulkan and Metal this feature is expected to be reported as supported
1113 always. This enum value has been introduced in Qt 6.11.
1114 */
1115
1116/*!
1117 \enum QRhi::BeginFrameFlag
1118 Flag values for QRhi::beginFrame()
1119 */
1120
1121/*!
1122 \enum QRhi::EndFrameFlag
1123 Flag values for QRhi::endFrame()
1124
1125 \value SkipPresent Specifies that no present command is to be queued or no
1126 swapBuffers call is to be made. This way no image is presented. Generating
1127 multiple frames with all having this flag set is not recommended (except,
1128 for example, for benchmarking purposes - but keep in mind that backends may
1129 behave differently when it comes to waiting for command completion without
1130 presenting so the results are not comparable between them)
1131 */
1132
1133/*!
1134 \enum QRhi::ResourceLimit
1135 Describes the resource limit to query.
1136
1137 \value TextureSizeMin Minimum texture width and height. This is typically
1138 1. The minimum texture size is handled gracefully, meaning attempting to
1139 create a texture with an empty size will instead create a texture with the
1140 minimum size.
1141
1142 \value TextureSizeMax Maximum texture width and height. This depends on the
1143 graphics API and sometimes the platform or implementation as well.
1144 Typically the value is in the range 4096 - 16384. Attempting to create
1145 textures larger than this is expected to fail.
1146
1147 \value MaxColorAttachments The maximum number of color attachments for a
1148 QRhiTextureRenderTarget, in case multiple render targets are supported. When
1149 MRT is not supported, the value is 1. Otherwise this is typically 8, but
1150 watch out for the fact that OpenGL only mandates 4 as the minimum, and that
1151 is what some OpenGL ES implementations provide.
1152
1153 \value FramesInFlight The number of frames the backend may keep "in
1154 flight": with backends like Vulkan or Metal, it is the responsibility of
1155 QRhi to block whenever starting a new frame and finding the CPU is already
1156 \c{N - 1} frames ahead of the GPU (because the command buffer submitted in
1157 frame no. \c{current} - \c{N} has not yet completed). The value N is what
1158 is returned from here, and is typically 2. This can be relevant to
1159 applications that integrate rendering done directly with the graphics API,
1160 as such rendering code may want to perform double (if the value is 2)
1161 buffering for resources, such as, buffers, similarly to the QRhi backends
1162 themselves. The current frame slot index (a value running 0, 1, .., N-1,
1163 then wrapping around) is retrievable from QRhi::currentFrameSlot(). The
1164 value is 1 for backends where the graphics API offers no such low level
1165 control over the command submission process. Note that pipelining may still
1166 happen even when this value is 1 (some backends, such as D3D11, are
1167 designed to attempt to enable this, for instance, by using an update
1168 strategy for uniform buffers that does not stall the pipeline), but that is
1169 then not controlled by QRhi and so not reflected here in the API.
1170
1171 \value MaxAsyncReadbackFrames The number of \l{QRhi::endFrame()}{submitted}
1172 frames (including the one that contains the readback) after which an
1173 asynchronous texture or buffer readback is guaranteed to complete upon
1174 \l{QRhi::beginFrame()}{starting a new frame}.
1175
1176 \value MaxThreadGroupsPerDimension The maximum number of compute
1177 work/thread groups that can be dispatched. Effectively the maximum value
1178 for the arguments of QRhiCommandBuffer::dispatch(). Typically 65535.
1179
1180 \value MaxThreadsPerThreadGroup The maximum number of invocations in a
1181 single local work group, or in other terminology, the maximum number of
1182 threads in a thread group. Effectively the maximum value for the product of
1183 \c local_size_x, \c local_size_y, and \c local_size_z in the compute
1184 shader. Typical values are 128, 256, 512, 1024, or 1536. Watch out that
1185 both OpenGL ES and Vulkan specify only 128 as the minimum required limit
1186 for implementations. While uncommon for Vulkan, some OpenGL ES 3.1
1187 implementations for mobile/embedded devices only support the spec-mandated
1188 minimum value.
1189
1190 \value MaxThreadGroupX The maximum size of a work/thread group in the X
1191 dimension. Effectively the maximum value of \c local_size_x in the compute
1192 shader. Typically 256 or 1024.
1193
1194 \value MaxThreadGroupY The maximum size of a work/thread group in the Y
1195 dimension. Effectively the maximum value of \c local_size_y in the compute
1196 shader. Typically 256 or 1024.
1197
1198 \value MaxThreadGroupZ The maximum size of a work/thread group in the Z
1199 dimension. Effectively the maximum value of \c local_size_z in the compute
1200 shader. Typically 64 or 256.
1201
1202 \value TextureArraySizeMax Maximum texture array size. Typically in range
1203 256 - 2048. Attempting to \l{QRhi::newTextureArray()}{create a texture
1204 array} with more elements will likely fail.
1205
1206 \value MaxUniformBufferRange The number of bytes that can be exposed from a
1207 uniform buffer to the shaders at once. On OpenGL ES 2.0 and 3.0
1208 implementations this may be as low as 3584 bytes (224 four component, 32
1209 bits per component vectors). Elsewhere the value is typically 16384 (1024
1210 vec4s) or 65536 (4096 vec4s).
1211
1212 \value MaxVertexInputs The number of input attributes to the vertex shader.
1213 The location in a QRhiVertexInputAttribute must be in range \c{[0,
1214 MaxVertexInputs-1]}. The value may be as low as 8 with OpenGL ES 2.0.
1215 Elsewhere, typical values are 16, 31, or 32.
1216
1217 \value MaxVertexOutputs The maximum number of outputs (4 component vector
1218 \c out variables) from the vertex shader. The value may be as low as 8 with
1219 OpenGL ES 2.0, and 15 with OpenGL ES 3.0 and some Metal devices. Elsewhere,
1220 a typical value is 32.
1221
1222 \value ShadingRateImageTileSize The tile size for shading rate textures. 0
1223 if the QRhi::VariableRateShadingMapWithTexture feature is not supported.
1224 Otherwise a value such as 16, indicating, for example, a tile size of 16x16.
1225 Each byte in the (R8UI) shading rate texture defines then the shading rate
1226 for a tile of 16x16 pixels. See \l QRhiShadingRateMap for details.
1227 */
1228
1229/*!
1230 \class QRhiInitParams
1231 \inmodule QtGuiPrivate
1232 \inheaderfile rhi/qrhi.h
1233 \since 6.6
1234 \brief Base class for backend-specific initialization parameters.
1235
1236 Contains fields that are relevant to all backends.
1237
1238 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1239 for details.
1240 */
1241
1242/*!
1243 \class QRhiDepthStencilClearValue
1244 \inmodule QtGuiPrivate
1245 \inheaderfile rhi/qrhi.h
1246 \since 6.6
1247 \brief Specifies clear values for a depth or stencil buffer.
1248
1249 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1250 for details.
1251 */
1252
1253/*!
1254 \fn QRhiDepthStencilClearValue::QRhiDepthStencilClearValue() = default
1255
1256 Constructs a depth/stencil clear value with depth clear value 1.0f and
1257 stencil clear value 0.
1258 */
1259
1260/*!
1261 Constructs a depth/stencil clear value with depth clear value \a d and
1262 stencil clear value \a s.
1263 */
1264QRhiDepthStencilClearValue::QRhiDepthStencilClearValue(float d, quint32 s)
1265 : m_d(d),
1266 m_s(s)
1267{
1268}
1269
1270/*!
1271 \fn float QRhiDepthStencilClearValue::depthClearValue() const
1272 \return the depth clear value. In most cases this is 1.0f.
1273 */
1274
1275/*!
1276 \fn void QRhiDepthStencilClearValue::setDepthClearValue(float d)
1277 Sets the depth clear value to \a d.
1278 */
1279
1280/*!
1281 \fn quint32 QRhiDepthStencilClearValue::stencilClearValue() const
1282 \return the stencil clear value. In most cases this is 0.
1283 */
1284
1285/*!
1286 \fn void QRhiDepthStencilClearValue::setStencilClearValue(quint32 s)
1287 Sets the stencil clear value to \a s.
1288 */
1289
1290/*!
1291 \fn bool QRhiDepthStencilClearValue::operator==(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) noexcept
1292
1293 \return \c true if the values in the two QRhiDepthStencilClearValue objects
1294 \a a and \a b are equal.
1295 */
1296
1297/*!
1298 \fn bool QRhiDepthStencilClearValue::operator!=(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) noexcept
1299
1300 \return \c false if the values in the two QRhiDepthStencilClearValue
1301 objects \a a and \a b are equal; otherwise returns \c true.
1302
1303*/
1304
1305/*!
1306 \fn size_t QRhiDepthStencilClearValue::qHash(const QRhiDepthStencilClearValue &key, size_t seed)
1307 \qhash{QRhiDepthStencilClearValue}
1308 */
1309
1310#ifndef QT_NO_DEBUG_STREAM
1311QDebug operator<<(QDebug dbg, const QRhiDepthStencilClearValue &v)
1312{
1313 QDebugStateSaver saver(dbg);
1314 dbg.nospace() << "QRhiDepthStencilClearValue(depth-clear=" << v.depthClearValue()
1315 << " stencil-clear=" << v.stencilClearValue()
1316 << ')';
1317 return dbg;
1318}
1319#endif
1320
1321/*!
1322 \class QRhiViewport
1323 \inmodule QtGuiPrivate
1324 \inheaderfile rhi/qrhi.h
1325 \since 6.6
1326 \brief Specifies a viewport rectangle.
1327
1328 Used with QRhiCommandBuffer::setViewport().
1329
1330 QRhi assumes OpenGL-style viewport coordinates, meaning x and y are
1331 bottom-left. Negative width or height are not allowed.
1332
1333 Typical usage is like the following:
1334
1335 \code
1336 const QSize outputSizeInPixels = swapchain->currentPixelSize();
1337 const QRhiViewport viewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height());
1338 cb->beginPass(swapchain->currentFrameRenderTarget(), Qt::black, { 1.0f, 0 });
1339 cb->setGraphicsPipeline(ps);
1340 cb->setViewport(viewport);
1341 // ...
1342 \endcode
1343
1344 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1345 for details.
1346
1347 \sa QRhiCommandBuffer::setViewport(), QRhi::clipSpaceCorrMatrix(), QRhiScissor
1348 */
1349
1350/*!
1351 \fn QRhiViewport::QRhiViewport() = default
1352
1353 Constructs a viewport description with an empty rectangle and a depth range
1354 of 0.0f - 1.0f.
1355
1356 \sa QRhi::clipSpaceCorrMatrix()
1357 */
1358
1359/*!
1360 Constructs a viewport description with the rectangle specified by \a x, \a
1361 y, \a w, \a h and the depth range \a minDepth and \a maxDepth.
1362
1363 \note \a x and \a y are assumed to be the bottom-left position. \a w and \a
1364 h should not be negative, the viewport will be ignored by
1365 QRhiCommandBuffer::setViewport() otherwise.
1366
1367 \sa QRhi::clipSpaceCorrMatrix()
1368 */
1369QRhiViewport::QRhiViewport(float x, float y, float w, float h, float minDepth, float maxDepth)
1370 : m_rect { { x, y, w, h } },
1371 m_minDepth(minDepth),
1372 m_maxDepth(maxDepth)
1373{
1374}
1375
1376/*!
1377 \fn std::array<float, 4> QRhiViewport::viewport() const
1378 \return the viewport x, y, width, and height.
1379 */
1380
1381/*!
1382 \fn void QRhiViewport::setViewport(float x, float y, float w, float h)
1383 Sets the viewport's position and size to \a x, \a y, \a w, and \a h.
1384
1385 \note Viewports are specified in a coordinate system that has its origin in
1386 the bottom-left.
1387 */
1388
1389/*!
1390 \fn float QRhiViewport::minDepth() const
1391 \return the minDepth value of the depth range of the viewport.
1392 */
1393
1394/*!
1395 \fn void QRhiViewport::setMinDepth(float minDepth)
1396 Sets the \a minDepth of the depth range of the viewport.
1397 By default this is set to 0.0f.
1398 */
1399
1400/*!
1401 \fn float QRhiViewport::maxDepth() const
1402 \return the maxDepth value of the depth range of the viewport.
1403 */
1404
1405/*!
1406 \fn void QRhiViewport::setMaxDepth(float maxDepth)
1407 Sets the \a maxDepth of the depth range of the viewport.
1408 By default this is set to 1.0f.
1409 */
1410
1411/*!
1412 \fn bool QRhiViewport::operator==(const QRhiViewport &a, const QRhiViewport &b) noexcept
1413
1414 \return \c true if the values in the two QRhiViewport objects
1415 \a a and \a b are equal.
1416 */
1417
1418/*!
1419 \fn bool QRhiViewport::operator!=(const QRhiViewport &a, const QRhiViewport &b) noexcept
1420
1421 \return \c false if the values in the two QRhiViewport
1422 objects \a a and \a b are equal; otherwise returns \c true.
1423*/
1424
1425/*!
1426 \fn size_t QRhiViewport::qHash(const QRhiViewport &key, size_t seed)
1427 \qhash{QRhiViewport}
1428 */
1429
1430#ifndef QT_NO_DEBUG_STREAM
1431QDebug operator<<(QDebug dbg, const QRhiViewport &v)
1432{
1433 QDebugStateSaver saver(dbg);
1434 const std::array<float, 4> r = v.viewport();
1435 dbg.nospace() << "QRhiViewport(bottom-left-x=" << r[0]
1436 << " bottom-left-y=" << r[1]
1437 << " width=" << r[2]
1438 << " height=" << r[3]
1439 << " minDepth=" << v.minDepth()
1440 << " maxDepth=" << v.maxDepth()
1441 << ')';
1442 return dbg;
1443}
1444#endif
1445
1446/*!
1447 \class QRhiScissor
1448 \inmodule QtGuiPrivate
1449 \inheaderfile rhi/qrhi.h
1450 \since 6.6
1451 \brief Specifies a scissor rectangle.
1452
1453 Used with QRhiCommandBuffer::setScissor(). Setting a scissor rectangle is
1454 only possible with a QRhiGraphicsPipeline that has
1455 QRhiGraphicsPipeline::UsesScissor set.
1456
1457 QRhi assumes OpenGL-style scissor coordinates, meaning x and y are
1458 bottom-left. Negative width or height are not allowed. However, apart from
1459 that, the flexible OpenGL semantics apply: negative x and y, partially out
1460 of bounds rectangles, etc. will be handled gracefully, clamping as
1461 appropriate. Therefore, any rendering logic targeting OpenGL can feed
1462 scissor rectangles into QRhiScissor as-is, without any adaptation.
1463
1464 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1465 for details.
1466
1467 \sa QRhiCommandBuffer::setScissor(), QRhiViewport
1468 */
1469
1470/*!
1471 \fn QRhiScissor::QRhiScissor() = default
1472
1473 Constructs an empty scissor.
1474 */
1475
1476/*!
1477 Constructs a scissor with the rectangle specified by \a x, \a y, \a w, and
1478 \a h.
1479
1480 \note \a x and \a y are assumed to be the bottom-left position. Negative \a w
1481 or \a h are not allowed, such scissor rectangles will be ignored by
1482 QRhiCommandBuffer. Other than that, the flexible OpenGL semantics apply:
1483 negative x and y, partially out of bounds rectangles, etc. will be handled
1484 gracefully, clamping as appropriate.
1485 */
1486QRhiScissor::QRhiScissor(int x, int y, int w, int h)
1487 : m_rect { { x, y, w, h } }
1488{
1489}
1490
1491/*!
1492 \fn std::array<int, 4> QRhiScissor::scissor() const
1493 \return the scissor position and size.
1494 */
1495
1496/*!
1497 \fn void QRhiScissor::setScissor(int x, int y, int w, int h)
1498 Sets the scissor position and size to \a x, \a y, \a w, \a h.
1499
1500 \note The position is always expected to be specified in a coordinate
1501 system that has its origin in the bottom-left corner, like OpenGL.
1502 */
1503
1504/*!
1505 \fn bool QRhiScissor::operator==(const QRhiScissor &a, const QRhiScissor &b) noexcept
1506
1507 \return \c true if the values in the two QRhiScissor objects
1508 \a a and \a b are equal.
1509 */
1510
1511/*!
1512 \fn bool QRhiScissor::operator!=(const QRhiScissor &a, const QRhiScissor &b) noexcept
1513
1514 \return \c false if the values in the two QRhiScissor
1515 objects \a a and \a b are equal; otherwise returns \c true.
1516*/
1517
1518/*!
1519 \fn size_t QRhiScissor::qHash(const QRhiScissor &key, size_t seed)
1520 \qhash{QRhiScissor}
1521 */
1522
1523#ifndef QT_NO_DEBUG_STREAM
1524QDebug operator<<(QDebug dbg, const QRhiScissor &s)
1525{
1526 QDebugStateSaver saver(dbg);
1527 const std::array<int, 4> r = s.scissor();
1528 dbg.nospace() << "QRhiScissor(bottom-left-x=" << r[0]
1529 << " bottom-left-y=" << r[1]
1530 << " width=" << r[2]
1531 << " height=" << r[3]
1532 << ')';
1533 return dbg;
1534}
1535#endif
1536
1537/*!
1538 \class QRhiVertexInputBinding
1539 \inmodule QtGuiPrivate
1540 \inheaderfile rhi/qrhi.h
1541 \since 6.6
1542 \brief Describes a vertex input binding.
1543
1544 Specifies the stride (in bytes, must be a multiple of 4), the
1545 classification and optionally the instance step rate.
1546
1547 As an example, assume a vertex shader with the following inputs:
1548
1549 \badcode
1550 layout(location = 0) in vec4 position;
1551 layout(location = 1) in vec2 texcoord;
1552 \endcode
1553
1554 Now let's assume also that 3 component vertex positions \c{(x, y, z)} and 2
1555 component texture coordinates \c{(u, v)} are provided in a non-interleaved
1556 format in a buffer (or separate buffers even). Defining two bindings
1557 could then be done like this:
1558
1559 \code
1560 QRhiVertexInputLayout inputLayout;
1561 inputLayout.setBindings({
1562 { 3 * sizeof(float) },
1563 { 2 * sizeof(float) }
1564 });
1565 \endcode
1566
1567 Only the stride is interesting here since instancing is not used. The
1568 binding number is given by the index of the QRhiVertexInputBinding
1569 element in the bindings vector of the QRhiVertexInputLayout.
1570
1571 Once a graphics pipeline with this vertex input layout is bound, the vertex
1572 inputs could be set up like the following for drawing a cube with 36
1573 vertices, assuming we have a single buffer with first the positions and
1574 then the texture coordinates:
1575
1576 \code
1577 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
1578 { cubeBuf, 0 },
1579 { cubeBuf, 36 * 3 * sizeof(float) }
1580 };
1581 cb->setVertexInput(0, 2, vbufBindings);
1582 \endcode
1583
1584 Note how the index defined by \c {startBinding + i}, where \c i is the
1585 index in the second argument of
1586 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()}, matches the
1587 index of the corresponding entry in the \c bindings vector of the
1588 QRhiVertexInputLayout.
1589
1590 \note the stride must always be a multiple of 4.
1591
1592 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1593 for details.
1594
1595 \sa QRhiCommandBuffer::setVertexInput()
1596 */
1597
1598/*!
1599 \enum QRhiVertexInputBinding::Classification
1600 Describes the input data classification.
1601
1602 \value PerVertex Data is per-vertex
1603 \value PerInstance Data is per-instance
1604 */
1605
1606/*!
1607 \fn QRhiVertexInputBinding::QRhiVertexInputBinding() = default
1608
1609 Constructs a default vertex input binding description.
1610 */
1611
1612/*!
1613 Constructs a vertex input binding description with the specified \a stride,
1614 classification \a cls, and instance step rate \a stepRate.
1615
1616 \note \a stepRate other than 1 is only supported when
1617 QRhi::CustomInstanceStepRate is reported to be supported.
1618 */
1619QRhiVertexInputBinding::QRhiVertexInputBinding(quint32 stride, Classification cls, quint32 stepRate)
1620 : m_stride(stride),
1621 m_classification(cls),
1622 m_instanceStepRate(stepRate)
1623{
1624}
1625
1626/*!
1627 \fn quint32 QRhiVertexInputBinding::stride() const
1628 \return the stride in bytes.
1629 */
1630
1631/*!
1632 \fn void QRhiVertexInputBinding::setStride(quint32 s)
1633 Sets the stride to \a s.
1634 */
1635
1636/*!
1637 \fn QRhiVertexInputBinding::Classification QRhiVertexInputBinding::classification() const
1638 \return the input data classification.
1639 */
1640
1641/*!
1642 \fn void QRhiVertexInputBinding::setClassification(Classification c)
1643 Sets the input data classification \a c. By default this is set to PerVertex.
1644 */
1645
1646/*!
1647 \fn quint32 QRhiVertexInputBinding::instanceStepRate() const
1648 \return the instance step rate.
1649 */
1650
1651/*!
1652 \fn void QRhiVertexInputBinding::setInstanceStepRate(quint32 rate)
1653 Sets the instance step \a rate. By default this is set to 1.
1654 */
1655
1656/*!
1657 \fn bool QRhiVertexInputBinding::operator==(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) noexcept
1658
1659 \return \c true if the values in the two QRhiVertexInputBinding objects
1660 \a a and \a b are equal.
1661 */
1662
1663/*!
1664 \fn bool QRhiVertexInputBinding::operator!=(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) noexcept
1665
1666 \return \c false if the values in the two QRhiVertexInputBinding
1667 objects \a a and \a b are equal; otherwise returns \c true.
1668*/
1669
1670/*!
1671 \fn size_t QRhiVertexInputBinding::qHash(const QRhiVertexInputBinding &key, size_t seed)
1672 \qhash{QRhiVertexInputBinding}
1673 */
1674
1675#ifndef QT_NO_DEBUG_STREAM
1676QDebug operator<<(QDebug dbg, const QRhiVertexInputBinding &b)
1677{
1678 QDebugStateSaver saver(dbg);
1679 dbg.nospace() << "QRhiVertexInputBinding(stride=" << b.stride()
1680 << " cls=" << b.classification()
1681 << " step-rate=" << b.instanceStepRate()
1682 << ')';
1683 return dbg;
1684}
1685#endif
1686
1687/*!
1688 \class QRhiVertexInputAttribute
1689 \inmodule QtGuiPrivate
1690 \inheaderfile rhi/qrhi.h
1691 \since 6.6
1692 \brief Describes a single vertex input element.
1693
1694 The members specify the binding number, location, format, and offset for a
1695 single vertex input element.
1696
1697 \note For HLSL it is assumed that the vertex shader translated from SPIR-V
1698 uses
1699 \c{TEXCOORD<location>} as the semantic for each input. Hence no separate
1700 semantic name and index.
1701
1702 As an example, assume a vertex shader with the following inputs:
1703
1704 \badcode
1705 layout(location = 0) in vec4 position;
1706 layout(location = 1) in vec2 texcoord;
1707 \endcode
1708
1709 Now let's assume that we have 3 component vertex positions \c{(x, y, z)}
1710 and 2 component texture coordinates \c{(u, v)} are provided in a
1711 non-interleaved format in a buffer (or separate buffers even). Once two
1712 bindings are defined, the attributes could be specified as:
1713
1714 \code
1715 QRhiVertexInputLayout inputLayout;
1716 inputLayout.setBindings({
1717 { 3 * sizeof(float) },
1718 { 2 * sizeof(float) }
1719 });
1720 inputLayout.setAttributes({
1721 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
1722 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
1723 });
1724 \endcode
1725
1726 Once a graphics pipeline with this vertex input layout is bound, the vertex
1727 inputs could be set up like the following for drawing a cube with 36
1728 vertices, assuming we have a single buffer with first the positions and
1729 then the texture coordinates:
1730
1731 \code
1732 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
1733 { cubeBuf, 0 },
1734 { cubeBuf, 36 * 3 * sizeof(float) }
1735 };
1736 cb->setVertexInput(0, 2, vbufBindings);
1737 \endcode
1738
1739 When working with interleaved data, there will typically be just one
1740 binding, with multiple attributes referring to that same buffer binding
1741 point:
1742
1743 \code
1744 QRhiVertexInputLayout inputLayout;
1745 inputLayout.setBindings({
1746 { 5 * sizeof(float) }
1747 });
1748 inputLayout.setAttributes({
1749 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
1750 { 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) }
1751 });
1752 \endcode
1753
1754 and then:
1755
1756 \code
1757 const QRhiCommandBuffer::VertexInput vbufBinding(interleavedCubeBuf, 0);
1758 cb->setVertexInput(0, 1, &vbufBinding);
1759 \endcode
1760
1761 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
1762 for details.
1763
1764 \sa QRhiCommandBuffer::setVertexInput()
1765 */
1766
1767/*!
1768 \enum QRhiVertexInputAttribute::Format
1769 Specifies the type of the element data.
1770
1771 \value Float4 Four component float vector
1772 \value Float3 Three component float vector
1773 \value Float2 Two component float vector
1774 \value Float Float
1775 \value UNormByte4 Four component normalized unsigned byte vector
1776 \value UNormByte2 Two component normalized unsigned byte vector
1777 \value UNormByte Normalized unsigned byte
1778 \value UInt4 Four component unsigned integer vector
1779 \value UInt3 Three component unsigned integer vector
1780 \value UInt2 Two component unsigned integer vector
1781 \value UInt Unsigned integer
1782 \value SInt4 Four component signed integer vector
1783 \value SInt3 Three component signed integer vector
1784 \value SInt2 Two component signed integer vector
1785 \value SInt Signed integer
1786 \value Half4 Four component half precision (16 bit) float vector
1787 \value Half3 Three component half precision (16 bit) float vector
1788 \value Half2 Two component half precision (16 bit) float vector
1789 \value Half Half precision (16 bit) float
1790 \value UShort4 Four component unsigned short (16 bit) integer vector
1791 \value UShort3 Three component unsigned short (16 bit) integer vector
1792 \value UShort2 Two component unsigned short (16 bit) integer vector
1793 \value UShort Unsigned short (16 bit) integer
1794 \value SShort4 Four component signed short (16 bit) integer vector
1795 \value SShort3 Three component signed short (16 bit) integer vector
1796 \value SShort2 Two component signed short (16 bit) integer vector
1797 \value SShort Signed short (16 bit) integer
1798
1799 \note Support for half precision floating point attributes is indicated at
1800 run time by the QRhi::Feature::HalfAttributes feature flag.
1801
1802 \note Direct3D 11/12 supports 16 bit input attributes, but does not support
1803 the Half3, UShort3 or SShort3 types. The D3D backends pass through Half3 as
1804 Half4, UShort3 as UShort4, and SShort3 as SShort4. To ensure cross platform
1805 compatibility, 16 bit inputs should be padded to 8 bytes.
1806 */
1807
1808/*!
1809 \fn QRhiVertexInputAttribute::QRhiVertexInputAttribute() = default
1810
1811 Constructs a default vertex input attribute description.
1812 */
1813
1814/*!
1815 Constructs a vertex input attribute description with the specified \a
1816 binding number, \a location, \a format, and \a offset.
1817
1818 \a matrixSlice should be -1 except when this attribute corresponds to a row
1819 or column of a matrix (for example, a 4x4 matrix becomes 4 vec4s, consuming
1820 4 consecutive vertex input locations), in which case it is the index of the
1821 row or column. \c{location - matrixSlice} must always be equal to the \c
1822 location for the first row or column of the unrolled matrix.
1823 */
1824QRhiVertexInputAttribute::QRhiVertexInputAttribute(int binding, int location, Format format, quint32 offset, int matrixSlice)
1825 : m_binding(binding),
1826 m_location(location),
1827 m_format(format),
1828 m_offset(offset),
1829 m_matrixSlice(matrixSlice)
1830{
1831}
1832
1833/*!
1834 \fn int QRhiVertexInputAttribute::binding() const
1835 \return the binding point index.
1836 */
1837
1838/*!
1839 \fn void QRhiVertexInputAttribute::setBinding(int b)
1840 Sets the binding point index to \a b.
1841 By default this is set to 0.
1842 */
1843
1844/*!
1845 \fn int QRhiVertexInputAttribute::location() const
1846 \return the location of the vertex input element.
1847 */
1848
1849/*!
1850 \fn void QRhiVertexInputAttribute::setLocation(int loc)
1851 Sets the location of the vertex input element to \a loc.
1852 By default this is set to 0.
1853 */
1854
1855/*!
1856 \fn QRhiVertexInputAttribute::Format QRhiVertexInputAttribute::format() const
1857 \return the format of the vertex input element.
1858 */
1859
1860/*!
1861 \fn void QRhiVertexInputAttribute::setFormat(Format f)
1862 Sets the format of the vertex input element to \a f.
1863 By default this is set to Float4.
1864 */
1865
1866/*!
1867 \fn quint32 QRhiVertexInputAttribute::offset() const
1868 \return the byte offset for the input element.
1869 */
1870
1871/*!
1872 \fn void QRhiVertexInputAttribute::setOffset(quint32 ofs)
1873 Sets the byte offset for the input element to \a ofs.
1874 */
1875
1876/*!
1877 \fn int QRhiVertexInputAttribute::matrixSlice() const
1878
1879 \return the matrix slice if the input element corresponds to a row or
1880 column of a matrix, or -1 if not relevant.
1881 */
1882
1883/*!
1884 \fn void QRhiVertexInputAttribute::setMatrixSlice(int slice)
1885
1886 Sets the matrix \a slice. By default this is set to -1, and should be set
1887 to a >= 0 value only when this attribute corresponds to a row or column of
1888 a matrix (for example, a 4x4 matrix becomes 4 vec4s, consuming 4
1889 consecutive vertex input locations), in which case it is the index of the
1890 row or column. \c{location - matrixSlice} must always be equal to the \c
1891 location for the first row or column of the unrolled matrix.
1892 */
1893
1894/*!
1895 \fn bool QRhiVertexInputAttribute::operator==(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) noexcept
1896
1897 \return \c true if the values in the two QRhiVertexInputAttribute objects
1898 \a a and \a b are equal.
1899 */
1900
1901/*!
1902 \fn bool QRhiVertexInputAttribute::operator!=(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) noexcept
1903
1904 \return \c false if the values in the two QRhiVertexInputAttribute
1905 objects \a a and \a b are equal; otherwise returns \c true.
1906*/
1907
1908/*!
1909 \fn size_t QRhiVertexInputAttribute::qHash(const QRhiVertexInputAttribute &key, size_t seed)
1910 \qhash{QRhiVertexInputAttribute}
1911 */
1912
1913#ifndef QT_NO_DEBUG_STREAM
1914QDebug operator<<(QDebug dbg, const QRhiVertexInputAttribute &a)
1915{
1916 QDebugStateSaver saver(dbg);
1917 dbg.nospace() << "QRhiVertexInputAttribute(binding=" << a.binding()
1918 << " location=" << a.location()
1919 << " format=" << a.format()
1920 << " offset=" << a.offset()
1921 << ')';
1922 return dbg;
1923}
1924#endif
1925
1926QRhiVertexInputAttribute::Format QRhiImplementation::shaderDescVariableFormatToVertexInputFormat(QShaderDescription::VariableType type) const
1927{
1928 switch (type) {
1929 case QShaderDescription::Vec4:
1930 return QRhiVertexInputAttribute::Float4;
1931 case QShaderDescription::Vec3:
1932 return QRhiVertexInputAttribute::Float3;
1933 case QShaderDescription::Vec2:
1934 return QRhiVertexInputAttribute::Float2;
1935 case QShaderDescription::Float:
1936 return QRhiVertexInputAttribute::Float;
1937
1938 case QShaderDescription::Int4:
1939 return QRhiVertexInputAttribute::SInt4;
1940 case QShaderDescription::Int3:
1941 return QRhiVertexInputAttribute::SInt3;
1942 case QShaderDescription::Int2:
1943 return QRhiVertexInputAttribute::SInt2;
1944 case QShaderDescription::Int:
1945 return QRhiVertexInputAttribute::SInt;
1946
1947 case QShaderDescription::Uint4:
1948 return QRhiVertexInputAttribute::UInt4;
1949 case QShaderDescription::Uint3:
1950 return QRhiVertexInputAttribute::UInt3;
1951 case QShaderDescription::Uint2:
1952 return QRhiVertexInputAttribute::UInt2;
1953 case QShaderDescription::Uint:
1954 return QRhiVertexInputAttribute::UInt;
1955
1956 case QShaderDescription::Half4:
1957 return QRhiVertexInputAttribute::Half4;
1958 case QShaderDescription::Half3:
1959 return QRhiVertexInputAttribute::Half3;
1960 case QShaderDescription::Half2:
1961 return QRhiVertexInputAttribute::Half2;
1962 case QShaderDescription::Half:
1963 return QRhiVertexInputAttribute::Half;
1964
1965 default:
1966 Q_UNREACHABLE_RETURN(QRhiVertexInputAttribute::Float);
1967 }
1968}
1969
1970quint32 QRhiImplementation::byteSizePerVertexForVertexInputFormat(QRhiVertexInputAttribute::Format format) const
1971{
1972 switch (format) {
1973 case QRhiVertexInputAttribute::Float4:
1974 return 4 * sizeof(float);
1975 case QRhiVertexInputAttribute::Float3:
1976 return 4 * sizeof(float); // vec3 still takes 16 bytes
1977 case QRhiVertexInputAttribute::Float2:
1978 return 2 * sizeof(float);
1979 case QRhiVertexInputAttribute::Float:
1980 return sizeof(float);
1981
1982 case QRhiVertexInputAttribute::UNormByte4:
1983 return 4 * sizeof(quint8);
1984 case QRhiVertexInputAttribute::UNormByte2:
1985 return 2 * sizeof(quint8);
1986 case QRhiVertexInputAttribute::UNormByte:
1987 return sizeof(quint8);
1988
1989 case QRhiVertexInputAttribute::UInt4:
1990 return 4 * sizeof(quint32);
1991 case QRhiVertexInputAttribute::UInt3:
1992 return 4 * sizeof(quint32); // ivec3 still takes 16 bytes
1993 case QRhiVertexInputAttribute::UInt2:
1994 return 2 * sizeof(quint32);
1995 case QRhiVertexInputAttribute::UInt:
1996 return sizeof(quint32);
1997
1998 case QRhiVertexInputAttribute::SInt4:
1999 return 4 * sizeof(qint32);
2000 case QRhiVertexInputAttribute::SInt3:
2001 return 4 * sizeof(qint32); // uvec3 still takes 16 bytes
2002 case QRhiVertexInputAttribute::SInt2:
2003 return 2 * sizeof(qint32);
2004 case QRhiVertexInputAttribute::SInt:
2005 return sizeof(qint32);
2006
2007 case QRhiVertexInputAttribute::Half4:
2008 return 4 * sizeof(qfloat16);
2009 case QRhiVertexInputAttribute::Half3:
2010 return 4 * sizeof(qfloat16); // half3 still takes 8 bytes
2011 case QRhiVertexInputAttribute::Half2:
2012 return 2 * sizeof(qfloat16);
2013 case QRhiVertexInputAttribute::Half:
2014 return sizeof(qfloat16);
2015
2016 case QRhiVertexInputAttribute::UShort4:
2017 return 4 * sizeof(quint16);
2018 case QRhiVertexInputAttribute::UShort3:
2019 return 4 * sizeof(quint16); // ivec3 still takes 8 bytes
2020 case QRhiVertexInputAttribute::UShort2:
2021 return 2 * sizeof(quint16);
2022 case QRhiVertexInputAttribute::UShort:
2023 return sizeof(quint16);
2024
2025 case QRhiVertexInputAttribute::SShort4:
2026 return 4 * sizeof(qint16);
2027 case QRhiVertexInputAttribute::SShort3:
2028 return 4 * sizeof(qint16); // uvec3 still takes 8 bytes
2029 case QRhiVertexInputAttribute::SShort2:
2030 return 2 * sizeof(qint16);
2031 case QRhiVertexInputAttribute::SShort:
2032 return sizeof(qint16);
2033
2034 default:
2035 Q_UNREACHABLE_RETURN(1);
2036 }
2037}
2038
2039/*!
2040 \class QRhiVertexInputLayout
2041 \inmodule QtGuiPrivate
2042 \inheaderfile rhi/qrhi.h
2043 \since 6.6
2044 \brief Describes the layout of vertex inputs consumed by a vertex shader.
2045
2046 The vertex input layout is defined by the collections of
2047 QRhiVertexInputBinding and QRhiVertexInputAttribute.
2048
2049 As an example, let's assume that we have a single buffer with 3 component
2050 vertex positions and 2 component UV coordinates interleaved (\c x, \c y, \c
2051 z, \c u, \c v), that the position and UV are expected at input locations 0
2052 and 1 by the vertex shader, and that the vertex buffer will be bound at
2053 binding point 0 using
2054 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()} later on:
2055
2056 \code
2057 QRhiVertexInputLayout inputLayout;
2058 inputLayout.setBindings({
2059 { 5 * sizeof(float) }
2060 });
2061 inputLayout.setAttributes({
2062 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
2063 { 0, 1, QRhiVertexInputAttribute::Float2, 3 * sizeof(float) }
2064 });
2065 \endcode
2066
2067 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2068 for details.
2069 */
2070
2071/*!
2072 \fn QRhiVertexInputLayout::QRhiVertexInputLayout() = default
2073
2074 Constructs an empty vertex input layout description.
2075 */
2076
2077/*!
2078 \fn void QRhiVertexInputLayout::setBindings(std::initializer_list<QRhiVertexInputBinding> list)
2079 Sets the bindings from the specified \a list.
2080 */
2081
2082/*!
2083 \fn template<typename InputIterator> void QRhiVertexInputLayout::setBindings(InputIterator first, InputIterator last)
2084 Sets the bindings using the iterators \a first and \a last.
2085 */
2086
2087/*!
2088 \fn const QRhiVertexInputBinding *QRhiVertexInputLayout::cbeginBindings() const
2089 \return a const iterator pointing to the first item in the binding list.
2090 */
2091
2092/*!
2093 \fn const QRhiVertexInputBinding *QRhiVertexInputLayout::cendBindings() const
2094 \return a const iterator pointing just after the last item in the binding list.
2095 */
2096
2097/*!
2098 \fn const QRhiVertexInputBinding *QRhiVertexInputLayout::bindingAt(qsizetype index) const
2099 \return the binding at the given \a index.
2100 */
2101
2102/*!
2103 \fn qsizetype QRhiVertexInputLayout::bindingCount() const
2104 \return the number of bindings.
2105 */
2106
2107/*!
2108 \fn void QRhiVertexInputLayout::setAttributes(std::initializer_list<QRhiVertexInputAttribute> list)
2109 Sets the attributes from the specified \a list.
2110 */
2111
2112/*!
2113 \fn template<typename InputIterator> void QRhiVertexInputLayout::setAttributes(InputIterator first, InputIterator last)
2114 Sets the attributes using the iterators \a first and \a last.
2115 */
2116
2117/*!
2118 \fn const QRhiVertexInputAttribute *QRhiVertexInputLayout::cbeginAttributes() const
2119 \return a const iterator pointing to the first item in the attribute list.
2120 */
2121
2122/*!
2123 \fn const QRhiVertexInputAttribute *QRhiVertexInputLayout::cendAttributes() const
2124 \return a const iterator pointing just after the last item in the attribute list.
2125 */
2126
2127/*!
2128 \fn const QRhiVertexInputAttribute *QRhiVertexInputLayout::attributeAt(qsizetype index) const
2129 \return the attribute at the given \a index.
2130 */
2131
2132/*!
2133 \fn qsizetype QRhiVertexInputLayout::attributeCount() const
2134 \return the number of attributes.
2135 */
2136
2137/*!
2138 \fn bool QRhiVertexInputLayout::operator==(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) noexcept
2139
2140 \return \c true if the values in the two QRhiVertexInputLayout objects
2141 \a a and \a b are equal.
2142 */
2143
2144/*!
2145 \fn bool QRhiVertexInputLayout::operator!=(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) noexcept
2146
2147 \return \c false if the values in the two QRhiVertexInputLayout
2148 objects \a a and \a b are equal; otherwise returns \c true.
2149*/
2150
2151/*!
2152 \fn size_t QRhiVertexInputLayout::qHash(const QRhiVertexInputLayout &key, size_t seed)
2153 \qhash{QRhiVertexInputLayout}
2154 */
2155
2156#ifndef QT_NO_DEBUG_STREAM
2157QDebug operator<<(QDebug dbg, const QRhiVertexInputLayout &v)
2158{
2159 QDebugStateSaver saver(dbg);
2160 dbg.nospace() << "QRhiVertexInputLayout(bindings=" << v.m_bindings
2161 << " attributes=" << v.m_attributes
2162 << ')';
2163 return dbg;
2164}
2165#endif
2166
2167/*!
2168 \class QRhiShaderStage
2169 \inmodule QtGuiPrivate
2170 \inheaderfile rhi/qrhi.h
2171 \since 6.6
2172 \brief Specifies the type and the shader code for a shader stage in the pipeline.
2173
2174 When setting up a QRhiGraphicsPipeline, a collection of shader stages are
2175 specified. The QRhiShaderStage contains a QShader and some associated
2176 metadata, such as the graphics pipeline stage, and the
2177 \l{QShader::Variant}{shader variant} to select. There is no need to specify
2178 the shader language or version because the QRhi backend in use at runtime
2179 will take care of choosing the appropriate shader version from the
2180 collection within the QShader.
2181
2182 The typical usage is in combination with
2183 QRhiGraphicsPipeline::setShaderStages(), shown here with a simple approach
2184 to load the QShader from \c{.qsb} files generated offline or at build time:
2185
2186 \code
2187 QShader getShader(const QString &name)
2188 {
2189 QFile f(name);
2190 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
2191 }
2192
2193 QShader vs = getShader("material.vert.qsb");
2194 QShader fs = getShader("material.frag.qsb");
2195 pipeline->setShaderStages({
2196 { QRhiShaderStage::Vertex, vs },
2197 { QRhiShaderStage::Fragment, fs }
2198 });
2199 \endcode
2200
2201 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2202 for details.
2203 */
2204
2205/*!
2206 \enum QRhiShaderStage::Type
2207 Specifies the type of the shader stage.
2208
2209 \value Vertex Vertex stage
2210
2211 \value TessellationControl Tessellation control (hull shader) stage. Must
2212 be used only when the QRhi::Tessellation feature is supported.
2213
2214 \value TessellationEvaluation Tessellation evaluation (domain shader)
2215 stage. Must be used only when the QRhi::Tessellation feature is supported.
2216
2217 \value Fragment Fragment (pixel shader) stage
2218
2219 \value Compute Compute stage. Must be used only when the QRhi::Compute
2220 feature is supported.
2221
2222 \value Geometry Geometry stage. Must be used only when the
2223 QRhi::GeometryShader feature is supported.
2224 */
2225
2226/*!
2227 \fn QRhiShaderStage::QRhiShaderStage() = default
2228
2229 Constructs a shader stage description for the vertex stage with an empty
2230 QShader.
2231 */
2232
2233/*!
2234 \fn QRhiShaderStage::Type QRhiShaderStage::type() const
2235 \return the type of the stage.
2236 */
2237
2238/*!
2239 \fn void QRhiShaderStage::setType(Type t)
2240
2241 Sets the type of the stage to \a t. Setters should rarely be needed in
2242 pratice. Most applications will likely use the QRhiShaderStage constructor
2243 in most cases.
2244 */
2245
2246/*!
2247 \fn QShader QRhiShaderStage::shader() const
2248 \return the QShader to be used for this stage in the graphics pipeline.
2249 */
2250
2251/*!
2252 \fn void QRhiShaderStage::setShader(const QShader &s)
2253 Sets the shader collection \a s.
2254 */
2255
2256/*!
2257 \fn QShader::Variant QRhiShaderStage::shaderVariant() const
2258 \return the requested shader variant.
2259 */
2260
2261/*!
2262 \fn void QRhiShaderStage::setShaderVariant(QShader::Variant v)
2263 Sets the requested shader variant \a v.
2264 */
2265
2266/*!
2267 Constructs a shader stage description with the \a type of the stage and the
2268 \a shader.
2269
2270 The shader variant \a v defaults to QShader::StandardShader. A
2271 QShader contains multiple source and binary versions of a shader.
2272 In addition, it can also contain variants of the shader with slightly
2273 modified code. \a v can then be used to select the desired variant.
2274 */
2275QRhiShaderStage::QRhiShaderStage(Type type, const QShader &shader, QShader::Variant v)
2276 : m_type(type),
2277 m_shader(shader),
2278 m_shaderVariant(v)
2279{
2280}
2281
2282/*!
2283 \fn bool QRhiShaderStage::operator==(const QRhiShaderStage &a, const QRhiShaderStage &b) noexcept
2284
2285 \return \c true if the values in the two QRhiShaderStage objects
2286 \a a and \a b are equal.
2287 */
2288
2289/*!
2290 \fn bool QRhiShaderStage::operator!=(const QRhiShaderStage &a, const QRhiShaderStage &b) noexcept
2291
2292 \return \c false if the values in the two QRhiShaderStage
2293 objects \a a and \a b are equal; otherwise returns \c true.
2294*/
2295
2296/*!
2297 \fn size_t QRhiShaderStage::qHash(const QRhiShaderStage &key, size_t seed)
2298 \qhash{QRhiShaderStage}
2299 */
2300
2301#ifndef QT_NO_DEBUG_STREAM
2302QDebug operator<<(QDebug dbg, const QRhiShaderStage &s)
2303{
2304 QDebugStateSaver saver(dbg);
2305 dbg.nospace() << "QRhiShaderStage(type=" << s.type()
2306 << " shader=" << s.shader()
2307 << " variant=" << s.shaderVariant()
2308 << ')';
2309 return dbg;
2310}
2311#endif
2312
2313/*!
2314 \class QRhiColorAttachment
2315 \inmodule QtGuiPrivate
2316 \inheaderfile rhi/qrhi.h
2317 \since 6.6
2318 \brief Describes the a single color attachment of a render target.
2319
2320 A color attachment is either a QRhiTexture or a QRhiRenderBuffer. The
2321 former, i.e. when texture() is set, is used in most cases.
2322 QRhiColorAttachment is commonly used in combination with
2323 QRhiTextureRenderTargetDescription.
2324
2325 \note texture() and renderBuffer() cannot be both set (be non-null at the
2326 same time).
2327
2328 Setting renderBuffer instead is recommended only when multisampling is
2329 needed. Relying on QRhi::MultisampleRenderBuffer is a better choice than
2330 QRhi::MultisampleTexture in practice since the former is available in more
2331 run time configurations (e.g. when running on OpenGL ES 3.0 which has no
2332 support for multisample textures, but does support multisample
2333 renderbuffers).
2334
2335 When targeting a non-multisample texture, the layer() and level() indicate
2336 the targeted layer (face index \c{0-5} for cubemaps) and mip level. For 3D
2337 textures layer() specifies the slice (one 2D image within the 3D texture)
2338 to render to. For texture arrays layer() is the array index.
2339
2340 When texture() or renderBuffer() is multisample, resolveTexture() can be
2341 set optionally. When set, samples are resolved automatically into that
2342 (non-multisample) texture at the end of the render pass. When rendering
2343 into a multisample renderbuffers, this is the only way to get resolved,
2344 non-multisample content out of them. Multisample textures allow sampling in
2345 shaders so for them this is just one option.
2346
2347 \note when resolving is enabled, the multisample data may not be written
2348 out at all. This means that the multisample texture() must not be used
2349 afterwards with shaders for sampling when resolveTexture() is set.
2350
2351 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2352 for details.
2353
2354 \sa QRhiTextureRenderTargetDescription
2355 */
2356
2357/*!
2358 \fn QRhiColorAttachment::QRhiColorAttachment() = default
2359
2360 Constructs an empty color attachment description.
2361 */
2362
2363/*!
2364 Constructs a color attachment description that specifies \a texture as the
2365 associated color buffer.
2366 */
2367QRhiColorAttachment::QRhiColorAttachment(QRhiTexture *texture)
2368 : m_texture(texture)
2369{
2370}
2371
2372/*!
2373 Constructs a color attachment description that specifies \a renderBuffer as
2374 the associated color buffer.
2375 */
2376QRhiColorAttachment::QRhiColorAttachment(QRhiRenderBuffer *renderBuffer)
2377 : m_renderBuffer(renderBuffer)
2378{
2379}
2380
2381/*!
2382 \fn QRhiTexture *QRhiColorAttachment::texture() const
2383
2384 \return the texture this attachment description references, or \nullptr if
2385 there is none.
2386 */
2387
2388/*!
2389 \fn void QRhiColorAttachment::setTexture(QRhiTexture *tex)
2390
2391 Sets the texture \a tex.
2392
2393 \note texture() and renderBuffer() cannot be both set (be non-null at the
2394 same time).
2395 */
2396
2397/*!
2398 \fn QRhiRenderBuffer *QRhiColorAttachment::renderBuffer() const
2399
2400 \return the renderbuffer this attachment description references, or
2401 \nullptr if there is none.
2402
2403 In practice associating a QRhiRenderBuffer with a QRhiColorAttachment makes
2404 the most sense when setting up multisample rendering via a multisample
2405 \l{QRhiRenderBuffer::Type}{color} renderbuffer that is then resolved into a
2406 non-multisample texture at the end of the render pass.
2407 */
2408
2409/*!
2410 \fn void QRhiColorAttachment::setRenderBuffer(QRhiRenderBuffer *rb)
2411
2412 Sets the renderbuffer \a rb.
2413
2414 \note texture() and renderBuffer() cannot be both set (be non-null at the
2415 same time).
2416 */
2417
2418/*!
2419 \fn int QRhiColorAttachment::layer() const
2420 \return the layer index (cubemap face or array layer). 0 by default.
2421 */
2422
2423/*!
2424 \fn void QRhiColorAttachment::setLayer(int layer)
2425 Sets the \a layer index.
2426 */
2427
2428/*!
2429 \fn int QRhiColorAttachment::level() const
2430 \return the mip level. 0 by default.
2431 */
2432
2433/*!
2434 \fn void QRhiColorAttachment::setLevel(int level)
2435 Sets the mip \a level.
2436 */
2437
2438/*!
2439 \fn QRhiTexture *QRhiColorAttachment::resolveTexture() const
2440
2441 \return the resolve texture this attachment description references, or
2442 \nullptr if there is none.
2443
2444 Setting a non-null resolve texture is applicable when the attachment
2445 references a multisample texture or renderbuffer. The QRhiTexture in the
2446 resolveTexture() is then a non-multisample 2D texture (or texture array)
2447 with the same size (but a sample count of 1). The multisample content is
2448 automatically resolved into this texture at the end of each render pass.
2449 */
2450
2451/*!
2452 \fn void QRhiColorAttachment::setResolveTexture(QRhiTexture *tex)
2453
2454 Sets the resolve texture \a tex.
2455
2456 \a tex is expected to be a 2D texture or a 2D texture array. In either
2457 case, resolving targets a single mip level of a single layer (array
2458 element) of \a tex. The mip level and array layer are specified by
2459 resolveLevel() and resolveLayer().
2460
2461 An exception is \l{setMultiViewCount()}{multiview}: when the color
2462 attachment is associated with a texture array and multiview is enabled, the
2463 resolve texture must also be a texture array with sufficient elements for
2464 all views. In this case all elements that correspond to views are resolved
2465 automatically; the behavior is similar to the following pseudo-code:
2466 \badcode
2467 for (i = 0; i < multiViewCount(); ++i)
2468 resolve texture's layer() + i into resolveTexture's resolveLayer() + i
2469 \endcode
2470
2471 Setting a non-multisample texture to resolve a multisample texture or
2472 renderbuffer automatically at the end of the render pass is often
2473 preferable to working with multisample textures (and not setting a resolve
2474 texture), because it avoids the need for writing dedicated fragment shaders
2475 that work exclusively with multisample textures (\c sampler2DMS, \c
2476 texelFetch, etc.), and rather allows using the same shader as one would if
2477 the attachment's texture was not multisampled to begin with. This comes at
2478 the expense of an additional resource (the non-multisample \a tex).
2479 */
2480
2481/*!
2482 \fn int QRhiColorAttachment::resolveLayer() const
2483 \return the currently set resolve texture layer. Defaults to 0.
2484 */
2485
2486/*!
2487 \fn void QRhiColorAttachment::setResolveLayer(int layer)
2488 Sets the resolve texture \a layer to use.
2489 */
2490
2491/*!
2492 \fn int QRhiColorAttachment::resolveLevel() const
2493 \return the currently set resolve texture mip level. Defaults to 0.
2494 */
2495
2496/*!
2497 \fn void QRhiColorAttachment::setResolveLevel(int level)
2498 Sets the resolve texture mip \a level to use.
2499 */
2500
2501/*!
2502 \fn int QRhiColorAttachment::multiViewCount() const
2503
2504 \return the currently set number of views. Defaults to 0 which indicates
2505 the render target with this color attachment is not going to be used with
2506 multiview rendering.
2507
2508 \since 6.7
2509 */
2510
2511/*!
2512 \fn void QRhiColorAttachment::setMultiViewCount(int count)
2513
2514 Sets the view \a count. Setting a value larger than 1 indicates that the
2515 render target with this color attachment is going to be used with multiview
2516 rendering. The default value is 0. Values smaller than 2 indicate no
2517 multiview rendering.
2518
2519 When \a count is set to \c 2 or greater, the color attachment must be
2520 associated with a 2D texture array. layer() and multiViewCount() together
2521 define the range of texture array elements that are targeted during
2522 multiview rendering.
2523
2524 For example, if \c layer is \c 0 and \c multiViewCount is \c 2, the texture
2525 array must have 2 (or more) elements, and the multiview rendering will
2526 target elements 0 and 1. The \c{gl_ViewIndex} variable in the shaders has a
2527 value of \c 0 or \c 1 then, where view \c 0 corresponds to the texture array
2528 element \c 0, and view \c 1 to the array element \c 1.
2529
2530 \note Setting a \a count larger than 1, using a texture array as texture(),
2531 and calling \l{QRhiCommandBuffer::beginPass()}{beginPass()} on a
2532 QRhiTextureRenderTarget with this color attachment implies multiview
2533 rendering for the entire render pass. multiViewCount() should not be set
2534 unless multiview rendering is wanted. Multiview cannot be used with texture
2535 types other than 2D texture arrays. (although 3D textures may work,
2536 depending on the graphics API and backend; applications are nonetheless
2537 advised not to rely on that and only use 2D texture arrays as the render
2538 targets of multiview rendering)
2539
2540 See
2541 \l{https://registry.khronos.org/OpenGL/extensions/OVR/OVR_multiview.txt}{GL_OVR_multiview}
2542 for more details regarding multiview rendering. Do note that Qt requires
2543 \l{https://registry.khronos.org/OpenGL/extensions/OVR/OVR_multiview2.txt}{GL_OVR_multiview2}
2544 as well, when running on OpenGL (ES).
2545
2546 Multiview rendering is available only when the
2547 \l{QRhi::MultiView}{MultiView} feature is reported as supported from
2548 \l{QRhi::isFeatureSupported()}{isFeatureSupported()}.
2549
2550 \note For portability, be aware of limitations that exist for multiview
2551 rendering with some of the graphics APIs. It is recommended that multiview
2552 render passes do not rely on any of the features that
2553 \l{https://registry.khronos.org/OpenGL/extensions/OVR/OVR_multiview.txt}{GL_OVR_multiview}
2554 declares as unsupported. The one exception is shader stage outputs other
2555 than \c{gl_Position} depending on \c{gl_ViewIndex}: that can be relied on
2556 (even with OpenGL) because QRhi never reports multiview as supported without
2557 \c{GL_OVR_multiview2} also being present.
2558
2559 \note Multiview rendering is not supported in combination with tessellation
2560 or geometry shaders, even though some implementations of some graphics APIs
2561 may allow this.
2562
2563 \since 6.7
2564 */
2565
2566/*!
2567 \class QRhiTextureRenderTargetDescription
2568 \inmodule QtGuiPrivate
2569 \inheaderfile rhi/qrhi.h
2570 \since 6.6
2571 \brief Describes the color and depth or depth/stencil attachments of a render target.
2572
2573 A texture render target has zero or more textures as color attachments,
2574 zero or one renderbuffer as combined depth/stencil buffer or zero or one
2575 texture as depth buffer.
2576
2577 \note depthStencilBuffer() and depthTexture() cannot be both set (cannot be
2578 non-null at the same time).
2579
2580 Let's look at some example usages in combination with
2581 QRhiTextureRenderTarget.
2582
2583 Due to the constructors, the targeting a texture (and no depth/stencil
2584 buffer) is simple:
2585
2586 \code
2587 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(256, 256), 1, QRhiTexture::RenderTarget);
2588 texture->create();
2589 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ texture }));
2590 \endcode
2591
2592 The following creates a texture render target that is set up to target mip
2593 level #2 of a texture:
2594
2595 \code
2596 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget | QRhiTexture::MipMapped);
2597 texture->create();
2598 QRhiColorAttachment colorAtt(texture);
2599 colorAtt.setLevel(2);
2600 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ colorAtt });
2601 \endcode
2602
2603 Another example, this time to render into a depth texture:
2604
2605 \code
2606 QRhiTexture *shadowMap = rhi->newTexture(QRhiTexture::D32F, QSize(1024, 1024), 1, QRhiTexture::RenderTarget);
2607 shadowMap->create();
2608 QRhiTextureRenderTargetDescription rtDesc;
2609 rtDesc.setDepthTexture(shadowMap);
2610 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget(rtDesc);
2611 \endcode
2612
2613 A very common case, having a texture as the color attachment and a
2614 renderbuffer as depth/stencil to enable depth testing:
2615
2616 \code
2617 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
2618 texture->create();
2619 QRhiRenderBuffer *depthStencil = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, QSize(512, 512));
2620 depthStencil->create();
2621 QRhiTextureRenderTargetDescription rtDesc({ texture }, depthStencil);
2622 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget(rtDesc);
2623 \endcode
2624
2625 Finally, to enable multisample rendering in a portable manner (so also
2626 supporting OpenGL ES 3.0), using a QRhiRenderBuffer as the (multisample)
2627 color buffer and then resolving into a regular (non-multisample) 2D
2628 texture. To enable depth testing, a depth-stencil buffer, which also must
2629 use the same sample count, is used as well:
2630
2631 \code
2632 QRhiRenderBuffer *colorBuffer = rhi->newRenderBuffer(QRhiRenderBuffer::Color, QSize(512, 512), 4); // 4x MSAA
2633 colorBuffer->create();
2634 QRhiRenderBuffer *depthStencil = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, QSize(512, 512), 4);
2635 depthStencil->create();
2636 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
2637 texture->create();
2638 QRhiColorAttachment colorAtt(colorBuffer);
2639 colorAtt.setResolveTexture(texture);
2640 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ colorAtt, depthStencil });
2641 \endcode
2642
2643 \note when multisample resolving is enabled, the multisample data may not be
2644 written out at all. This means that the multisample texture in a color
2645 attachment must not be used afterwards with shaders for sampling (or other
2646 purposes) whenever a resolve texture is set, since the multisample color
2647 buffer is merely an intermediate storage then that gets no data written back
2648 on some GPU architectures at all. See
2649 \l{QRhiTextureRenderTarget::Flag}{PreserveColorContents} for more details.
2650
2651 \note When using setDepthTexture(), not setDepthStencilBuffer(), and the
2652 depth (stencil) data is not of interest afterwards, set the
2653 DoNotStoreDepthStencilContents flag on the QRhiTextureRenderTarget. This
2654 allows indicating to the underlying 3D API that the depth/stencil data can
2655 be discarded, leading potentially to better performance with tiled GPU
2656 architectures. When the depth-stencil buffer is a QRhiRenderBuffer (and also
2657 for the multisample color texture, see previous note) this is implicit, but
2658 with a depth (stencil) QRhiTexture the intention needs to be declared
2659 explicitly. By default QRhi assumes that the data is of interest (e.g., the
2660 depth texture is sampled in a shader afterwards).
2661
2662 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2663 for details.
2664
2665 \sa QRhiColorAttachment, QRhiTextureRenderTarget
2666 */
2667
2668/*!
2669 \fn QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription() = default
2670
2671 Constructs an empty texture render target description.
2672 */
2673
2674/*!
2675 Constructs a texture render target description with one attachment
2676 described by \a colorAttachment.
2677 */
2678QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment)
2679{
2680 m_colorAttachments.append(colorAttachment);
2681}
2682
2683/*!
2684 Constructs a texture render target description with two attachments, a
2685 color attachment described by \a colorAttachment, and a depth/stencil
2686 attachment with \a depthStencilBuffer.
2687 */
2688QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment,
2689 QRhiRenderBuffer *depthStencilBuffer)
2690 : m_depthStencilBuffer(depthStencilBuffer)
2691{
2692 m_colorAttachments.append(colorAttachment);
2693}
2694
2695/*!
2696 Constructs a texture render target description with two attachments, a
2697 color attachment described by \a colorAttachment, and a depth attachment
2698 with \a depthTexture.
2699
2700 \note \a depthTexture must have a suitable format, such as QRhiTexture::D16
2701 or QRhiTexture::D32F.
2702 */
2703QRhiTextureRenderTargetDescription::QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment,
2704 QRhiTexture *depthTexture)
2705 : m_depthTexture(depthTexture)
2706{
2707 m_colorAttachments.append(colorAttachment);
2708}
2709
2710/*!
2711 \fn void QRhiTextureRenderTargetDescription::setColorAttachments(std::initializer_list<QRhiColorAttachment> list)
2712 Sets the \a list of color attachments.
2713 */
2714
2715/*!
2716 \fn template<typename InputIterator> void QRhiTextureRenderTargetDescription::setColorAttachments(InputIterator first, InputIterator last)
2717 Sets the list of color attachments via the iterators \a first and \a last.
2718 */
2719
2720/*!
2721 \fn const QRhiColorAttachment *QRhiTextureRenderTargetDescription::cbeginColorAttachments() const
2722 \return a const iterator pointing to the first item in the attachment list.
2723 */
2724
2725/*!
2726 \fn const QRhiColorAttachment *QRhiTextureRenderTargetDescription::cendColorAttachments() const
2727 \return a const iterator pointing just after the last item in the attachment list.
2728 */
2729
2730/*!
2731 \fn const QRhiColorAttachment *QRhiTextureRenderTargetDescription::colorAttachmentAt(qsizetype index) const
2732 \return the color attachment at the specified \a index.
2733 */
2734
2735/*!
2736 \fn qsizetype QRhiTextureRenderTargetDescription::colorAttachmentCount() const
2737 \return the number of currently set color attachments.
2738 */
2739
2740/*!
2741 \fn QRhiRenderBuffer *QRhiTextureRenderTargetDescription::depthStencilBuffer() const
2742 \return the renderbuffer used as depth-stencil buffer, or \nullptr if none was set.
2743 */
2744
2745/*!
2746 \fn void QRhiTextureRenderTargetDescription::setDepthStencilBuffer(QRhiRenderBuffer *renderBuffer)
2747
2748 Sets the \a renderBuffer for depth-stencil. Not mandatory, e.g. when no
2749 depth test/write or stencil-related features are used within any graphics
2750 pipelines in any of the render passes for this render target, it can be
2751 left set to \nullptr.
2752
2753 \note depthStencilBuffer() and depthTexture() cannot be both set (cannot be
2754 non-null at the same time).
2755
2756 Using a QRhiRenderBuffer over a 2D QRhiTexture as the depth or
2757 depth/stencil buffer is very common, and is the recommended approach for
2758 applications. Using a QRhiTexture, and so setDepthTexture() becomes
2759 relevant if the depth data is meant to be accessed (e.g. sampled in a
2760 shader) afterwards, or when
2761 \l{QRhiColorAttachment::setMultiViewCount()}{multiview rendering} is
2762 involved (because then the depth texture must be a texture array).
2763
2764 \sa setDepthTexture()
2765 */
2766
2767/*!
2768 \fn QRhiTexture *QRhiTextureRenderTargetDescription::depthTexture() const
2769 \return the currently referenced depth texture, or \nullptr if none was set.
2770 */
2771
2772/*!
2773 \fn void QRhiTextureRenderTargetDescription::setDepthTexture(QRhiTexture *texture)
2774
2775 Sets the \a texture for depth-stencil. This is an alternative to
2776 setDepthStencilBuffer(), where instead of a QRhiRenderBuffer a QRhiTexture
2777 with a suitable type (e.g., QRhiTexture::D32F) is provided.
2778
2779 \note depthStencilBuffer() and depthTexture() cannot be both set (cannot be
2780 non-null at the same time).
2781
2782 \a texture can either be a 2D texture or a 2D texture array (when texture
2783 arrays are supported). Specifying a texture array is relevant in particular
2784 with
2785 \l{QRhiColorAttachment::setMultiViewCount()}{multiview rendering}.
2786
2787 \note If \a texture is a format with a stencil component, such as
2788 \l QRhiTexture::D24S8, it will serve as the stencil buffer as well.
2789
2790 \sa setDepthStencilBuffer()
2791 */
2792
2793/*!
2794 \fn QRhiTexture *QRhiTextureRenderTargetDescription::depthResolveTexture() const
2795
2796 \return the texture to which a multisample depth (or depth-stencil) texture
2797 (or texture array) is resolved to. \nullptr if there is none, which is the
2798 most common case.
2799
2800 \since 6.8
2801 \sa QRhiColorAttachment::resolveTexture(), depthTexture()
2802 */
2803
2804/*!
2805 \fn void QRhiTextureRenderTargetDescription::setDepthResolveTexture(QRhiTexture *tex)
2806
2807 Sets the depth (or depth-stencil) resolve texture \a tex.
2808
2809 \a tex is expected to be a 2D texture or a 2D texture array with a format
2810 matching the texture set via setDepthTexture().
2811
2812 \note Resolving depth (or depth-stencil) data is only functional when the
2813 \l QRhi::ResolveDepthStencil feature is reported as supported at run time.
2814 Support for depth-stencil resolve is not universally available among the
2815 graphics APIs. Designs assuming unconditional availability of depth-stencil
2816 resolve are therefore non-portable, and should be avoided.
2817
2818 \note As an additional limitation for OpenGL ES in particular, setting a
2819 depth resolve texture may only be functional in combination with
2820 setDepthTexture(), not with setDepthStencilBuffer().
2821
2822 \since 6.8
2823 \sa QRhiColorAttachment::setResolveTexture(), setDepthTexture()
2824 */
2825
2826/*!
2827 \fn QRhiShadingRateMap *QRhiTextureRenderTargetDescription::shadingRateMap() const
2828 \return the currently set QRhiShadingRateMap. By default this is \nullptr.
2829 \since 6.9
2830 */
2831
2832/*!
2833 \fn void QRhiTextureRenderTargetDescription::setShadingRateMap(QRhiShadingRateMap *map)
2834
2835 Associates with the specified QRhiShadingRateMap \a map. This is functional
2836 only when the \l QRhi::VariableRateShadingMap feature is reported as
2837 supported.
2838
2839 When QRhiCommandBuffer::setShadingRate() is also called, the higher of two
2840 the shading rates are used for each tile. There is currently no control
2841 offered over the combiner behavior.
2842
2843 \note When the render target had already been built (create() was called
2844 successfully), setting a shading rate map implies that a different, new
2845 QRhiRenderPassDescriptor is needed and thus a rebuild is needed. Call
2846 setRenderPassDescriptor() again (outside of a render pass) and then rebuild
2847 by calling create(). This has other rolling consequences as well, for
2848 example for graphics pipelines: those also need to be associated with the
2849 new QRhiRenderPassDescriptor and then rebuilt. See \l
2850 QRhiRenderPassDescriptor::serializedFormat() for some suggestions on how to
2851 deal with this. Remember to set the QRhiGraphicsPipeline::UsesShadingRate
2852 flag as well.
2853
2854 \since 6.9
2855 */
2856
2857/*!
2858 \class QRhiTextureSubresourceUploadDescription
2859 \inmodule QtGuiPrivate
2860 \inheaderfile rhi/qrhi.h
2861 \since 6.6
2862 \brief Describes the source for one mip level in a layer in a texture upload operation.
2863
2864 The source content is specified either as a QImage or as a raw blob. The
2865 former is only allowed for uncompressed textures with a format that can be
2866 mapped to QImage, while the latter is supported for all formats, including
2867 floating point and compressed.
2868
2869 \note image() and data() cannot be both set at the same time.
2870
2871 destinationTopLeft() specifies the top-left corner of the target
2872 rectangle. Defaults to (0, 0).
2873
2874 An empty sourceSize() (the default) indicates that size is assumed to be
2875 the size of the subresource. With QImage-based uploads this implies that
2876 the size of the source image() must match the subresource. When providing
2877 raw data instead, sufficient number of bytes must be provided in data().
2878
2879 sourceTopLeft() is supported only for QImage-based uploads, and specifies
2880 the top-left corner of the source rectangle.
2881
2882 \note Setting sourceSize() or sourceTopLeft() may trigger a QImage copy
2883 internally, depending on the format and the backend.
2884
2885 When providing raw data, and the stride is not specified via
2886 setDataStride(), the stride (row pitch, row length in bytes) of the
2887 provided data must be equal to \c{width * pixelSize} where \c pixelSize is
2888 the number of bytes used for one pixel, and there must be no additional
2889 padding between rows. There is no row start alignment requirement.
2890
2891 When there is unused data at the end of each row in the input raw data,
2892 call setDataStride() with the total number of bytes per row. The stride
2893 must always be a multiple of the number of bytes for one pixel. The row
2894 stride is only applicable to image data for textures with an uncompressed
2895 format.
2896
2897 \note The format of the source data must be compatible with the texture
2898 format. With many graphics APIs the data is copied as-is into a staging
2899 buffer, there is no intermediate format conversion provided by QRhi. This
2900 applies to floating point formats as well, with, for example, RGBA16F
2901 requiring half floats in the source data.
2902
2903 \note Setting the stride via setDataStride() is only functional when
2904 QRhi::ImageDataStride is reported as
2905 \l{QRhi::isFeatureSupported()}{supported}. In practice this can be expected
2906 to be supported everywhere except for OpenGL ES 2.0.
2907
2908 \note When a QImage is given, the stride returned from
2909 QImage::bytesPerLine() is taken into account automatically.
2910
2911 \warning When a QImage is given and the QImage does not own the underlying
2912 pixel data, it is up to the caller to ensure that the associated data stays
2913 valid until the end of the frame. (just submitting the resource update batch
2914 is not sufficient, the data must stay valid until QRhi::endFrame() is called
2915 in order to be portable across all backends) If this cannot be ensured, the
2916 caller is strongly encouraged to call QImage::detach() on the image before
2917 passing it to uploadTexture().
2918
2919 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
2920 for details.
2921
2922 \sa QRhiTextureUploadDescription
2923 */
2924
2925/*!
2926 \fn QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription() = default
2927
2928 Constructs an empty subresource description.
2929
2930 \note an empty QRhiTextureSubresourceUploadDescription is not useful on its
2931 own and should not be submitted to a QRhiTextureUploadEntry. At minimum
2932 image or data must be set first.
2933 */
2934
2935/*!
2936 Constructs a mip level description with a \a image.
2937
2938 The \l{QImage::size()}{size} of \a image must match the size of the mip
2939 level. For level 0 that is the \l{QRhiTexture::pixelSize()}{texture size}.
2940
2941 The bit depth of \a image must be compatible with the
2942 \l{QRhiTexture::Format}{texture format}.
2943
2944 To describe a partial upload, call setSourceSize(), setSourceTopLeft(), or
2945 setDestinationTopLeft() afterwards.
2946 */
2947QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription(const QImage &image)
2948 : m_image(image)
2949{
2950}
2951
2952/*!
2953 Constructs a mip level description with the image data is specified by \a
2954 data and \a size. This is suitable for floating point and compressed
2955 formats as well.
2956
2957 \a data can safely be destroyed or changed once this function returns.
2958 */
2959QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription(const void *data, quint32 size)
2960 : m_data(reinterpret_cast<const char *>(data), size)
2961{
2962}
2963
2964/*!
2965 Constructs a mip level description with the image data specified by \a
2966 data. This is suitable for floating point and compressed formats as well.
2967 */
2968QRhiTextureSubresourceUploadDescription::QRhiTextureSubresourceUploadDescription(const QByteArray &data)
2969 : m_data(data)
2970{
2971}
2972
2973/*!
2974 \fn QImage QRhiTextureSubresourceUploadDescription::image() const
2975 \return the currently set QImage.
2976 */
2977
2978/*!
2979 \fn void QRhiTextureSubresourceUploadDescription::setImage(const QImage &image)
2980
2981 Sets \a image.
2982 Upon textures loading, the image data will be read as is, with no formats conversions.
2983
2984 \note image() and data() cannot be both set at the same time.
2985 */
2986
2987/*!
2988 \fn QByteArray QRhiTextureSubresourceUploadDescription::data() const
2989 \return the currently set raw pixel data.
2990 */
2991
2992/*!
2993 \fn void QRhiTextureSubresourceUploadDescription::setData(const QByteArray &data)
2994
2995 Sets \a data.
2996
2997 \note image() and data() cannot be both set at the same time.
2998 */
2999
3000/*!
3001 \fn quint32 QRhiTextureSubresourceUploadDescription::dataStride() const
3002 \return the currently set data stride.
3003 */
3004
3005/*!
3006 \fn void QRhiTextureSubresourceUploadDescription::setDataStride(quint32 stride)
3007
3008 Sets the data \a stride in bytes. By default this is 0 and not always
3009 relevant. When providing raw data(), and the stride is not specified via
3010 setDataStride(), the stride (row pitch, row length in bytes) of the
3011 provided data must be equal to \c{width * pixelSize} where \c pixelSize is
3012 the number of bytes used for one pixel, and there must be no additional
3013 padding between rows. Otherwise, if there is additional space between the
3014 lines, set a non-zero \a stride. All this is applicable only when raw image
3015 data is provided, and is not necessary when working QImage since that has
3016 its own \l{QImage::bytesPerLine()}{stride} value.
3017
3018 \note Setting the stride via setDataStride() is only functional when
3019 QRhi::ImageDataStride is reported as
3020 \l{QRhi::isFeatureSupported()}{supported}.
3021
3022 \note When a QImage is given, the stride returned from
3023 QImage::bytesPerLine() is taken into account automatically and therefore
3024 there is no need to set the data stride manually.
3025 */
3026
3027/*!
3028 \fn QPoint QRhiTextureSubresourceUploadDescription::destinationTopLeft() const
3029 \return the currently set destination top-left position. Defaults to (0, 0).
3030 */
3031
3032/*!
3033 \fn void QRhiTextureSubresourceUploadDescription::setDestinationTopLeft(const QPoint &p)
3034 Sets the destination top-left position \a p.
3035 */
3036
3037/*!
3038 \fn QSize QRhiTextureSubresourceUploadDescription::sourceSize() const
3039
3040 \return the source size in pixels. Defaults to a default-constructed QSize,
3041 which indicates the entire subresource.
3042 */
3043
3044/*!
3045 \fn void QRhiTextureSubresourceUploadDescription::setSourceSize(const QSize &size)
3046
3047 Sets the source \a size in pixels.
3048
3049 \note Setting sourceSize() or sourceTopLeft() may trigger a QImage copy
3050 internally, depending on the format and the backend.
3051 */
3052
3053/*!
3054 \fn QPoint QRhiTextureSubresourceUploadDescription::sourceTopLeft() const
3055 \return the currently set source top-left position. Defaults to (0, 0).
3056 */
3057
3058/*!
3059 \fn void QRhiTextureSubresourceUploadDescription::setSourceTopLeft(const QPoint &p)
3060
3061 Sets the source top-left position \a p.
3062
3063 \note Setting sourceSize() or sourceTopLeft() may trigger a QImage copy
3064 internally, depending on the format and the backend.
3065 */
3066
3067/*!
3068 \class QRhiTextureUploadEntry
3069 \inmodule QtGuiPrivate
3070 \inheaderfile rhi/qrhi.h
3071 \since 6.6
3072
3073 \brief Describes one layer (face for cubemaps, slice for 3D textures,
3074 element for texture arrays) in a texture upload operation.
3075
3076 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3077 for details.
3078 */
3079
3080/*!
3081 \fn QRhiTextureUploadEntry::QRhiTextureUploadEntry()
3082
3083 Constructs an empty QRhiTextureUploadEntry targeting layer 0 and level 0.
3084
3085 \note an empty QRhiTextureUploadEntry should not be submitted without
3086 setting a QRhiTextureSubresourceUploadDescription via setDescription()
3087 first.
3088 */
3089
3090/*!
3091 Constructs a QRhiTextureUploadEntry targeting the given \a layer and mip
3092 \a level, with the subresource contents described by \a desc.
3093 */
3094QRhiTextureUploadEntry::QRhiTextureUploadEntry(int layer, int level,
3095 const QRhiTextureSubresourceUploadDescription &desc)
3096 : m_layer(layer),
3097 m_level(level),
3098 m_desc(desc)
3099{
3100}
3101
3102/*!
3103 \fn int QRhiTextureUploadEntry::layer() const
3104 \return the currently set layer index (cubemap face, array layer). Defaults to 0.
3105 */
3106
3107/*!
3108 \fn void QRhiTextureUploadEntry::setLayer(int layer)
3109 Sets the \a layer.
3110 */
3111
3112/*!
3113 \fn int QRhiTextureUploadEntry::level() const
3114 \return the currently set mip level. Defaults to 0.
3115 */
3116
3117/*!
3118 \fn void QRhiTextureUploadEntry::setLevel(int level)
3119 Sets the mip \a level.
3120 */
3121
3122/*!
3123 \fn QRhiTextureSubresourceUploadDescription QRhiTextureUploadEntry::description() const
3124 \return the currently set subresource description.
3125 */
3126
3127/*!
3128 \fn void QRhiTextureUploadEntry::setDescription(const QRhiTextureSubresourceUploadDescription &desc)
3129 Sets the subresource description \a desc.
3130 */
3131
3132/*!
3133 \class QRhiTextureUploadDescription
3134 \inmodule QtGuiPrivate
3135 \inheaderfile rhi/qrhi.h
3136 \since 6.6
3137 \brief Describes a texture upload operation.
3138
3139 Used with QRhiResourceUpdateBatch::uploadTexture(). That function has two
3140 variants: one taking a QImage and one taking a
3141 QRhiTextureUploadDescription. The former is a convenience version,
3142 internally creating a QRhiTextureUploadDescription with a single image
3143 targeting level 0 for layer 0.
3144
3145 An example of the the common, simple case of wanting to upload the contents
3146 of a QImage to a QRhiTexture with a matching pixel size:
3147
3148 \code
3149 QImage image(256, 256, QImage::Format_RGBA8888);
3150 image.fill(Qt::green); // or could use a QPainter targeting image
3151 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(256, 256));
3152 texture->create();
3153 QRhiResourceUpdateBatch *u = rhi->nextResourceUpdateBatch();
3154 u->uploadTexture(texture, image);
3155 \endcode
3156
3157 When cubemaps, pre-generated mip images, compressed textures, or partial
3158 uploads are involved, applications will have to use this class instead.
3159
3160 QRhiTextureUploadDescription also enables specifying batched uploads, which
3161 are useful for example when generating an atlas or glyph cache texture:
3162 multiple, partial uploads for the same subresource (meaning the same layer
3163 and level) are supported, and can be, depending on the backend and the
3164 underlying graphics API, more efficient when batched into the same
3165 QRhiTextureUploadDescription as opposed to issuing individual
3166 \l{QRhiResourceUpdateBatch::uploadTexture()}{uploadTexture()} commands for
3167 each of them.
3168
3169 \note Cubemaps have one layer for each of the six faces in the order +X,
3170 -X, +Y, -Y, +Z, -Z.
3171
3172 For example, specifying the faces of a cubemap could look like the following:
3173
3174 \code
3175 QImage faces[6];
3176 // ...
3177 QVarLengthArray<QRhiTextureUploadEntry, 6> entries;
3178 for (int i = 0; i < 6; ++i)
3179 entries.append(QRhiTextureUploadEntry(i, 0, faces[i]));
3180 QRhiTextureUploadDescription desc;
3181 desc.setEntries(entries.cbegin(), entries.cend());
3182 resourceUpdates->uploadTexture(texture, desc);
3183 \endcode
3184
3185 Another example that specifies mip images for a compressed texture:
3186
3187 \code
3188 QList<QRhiTextureUploadEntry> entries;
3189 const int mipCount = rhi->mipLevelsForSize(compressedTexture->pixelSize());
3190 for (int level = 0; level < mipCount; ++level) {
3191 const QByteArray compressedDataForLevel = ..
3192 entries.append(QRhiTextureUploadEntry(0, level, compressedDataForLevel));
3193 }
3194 QRhiTextureUploadDescription desc;
3195 desc.setEntries(entries.cbegin(), entries.cend());
3196 resourceUpdates->uploadTexture(compressedTexture, desc);
3197 \endcode
3198
3199 With partial uploads targeting the same subresource, it is recommended to
3200 batch them into a single upload request, whenever possible:
3201
3202 \code
3203 QRhiTextureSubresourceUploadDescription subresDesc(image);
3204 subresDesc.setSourceSize(QSize(10, 10));
3205 subResDesc.setDestinationTopLeft(QPoint(50, 40));
3206 QRhiTextureUploadEntry entry(0, 0, subresDesc); // layer 0, level 0
3207
3208 QRhiTextureSubresourceUploadDescription subresDesc2(image);
3209 subresDesc2.setSourceSize(QSize(30, 40));
3210 subResDesc2.setDestinationTopLeft(QPoint(100, 200));
3211 QRhiTextureUploadEntry entry2(0, 0, subresDesc2); // layer 0, level 0, i.e. same subresource
3212
3213 QRhiTextureUploadDescription desc({ entry, entry2});
3214 resourceUpdates->uploadTexture(texture, desc);
3215 \endcode
3216
3217 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3218 for details.
3219
3220 \sa QRhiResourceUpdateBatch
3221 */
3222
3223/*!
3224 \fn QRhiTextureUploadDescription::QRhiTextureUploadDescription()
3225
3226 Constructs an empty texture upload description.
3227 */
3228
3229/*!
3230 Constructs a texture upload description with a single subresource upload
3231 described by \a entry.
3232 */
3233QRhiTextureUploadDescription::QRhiTextureUploadDescription(const QRhiTextureUploadEntry &entry)
3234{
3235 m_entries.append(entry);
3236}
3237
3238/*!
3239 Constructs a texture upload description with the specified \a list of entries.
3240
3241 \note \a list can also contain multiple QRhiTextureUploadEntry elements
3242 with the same layer and level. This makes sense when those uploads are
3243 partial, meaning their subresource description has a source size or image
3244 smaller than the subresource dimensions, and can be more efficient than
3245 issuing separate uploadTexture()'s.
3246 */
3247QRhiTextureUploadDescription::QRhiTextureUploadDescription(std::initializer_list<QRhiTextureUploadEntry> list)
3248 : m_entries(list)
3249{
3250}
3251
3252/*!
3253 \fn void QRhiTextureUploadDescription::setEntries(std::initializer_list<QRhiTextureUploadEntry> list)
3254 Sets the \a list of entries.
3255 */
3256
3257/*!
3258 \fn template<typename InputIterator> void QRhiTextureUploadDescription::setEntries(InputIterator first, InputIterator last)
3259 Sets the list of entries using the iterators \a first and \a last.
3260 */
3261
3262/*!
3263 \fn const QRhiTextureUploadEntry *QRhiTextureUploadDescription::cbeginEntries() const
3264 \return a const iterator pointing to the first item in the entry list.
3265 */
3266
3267/*!
3268 \fn const QRhiTextureUploadEntry *QRhiTextureUploadDescription::cendEntries() const
3269 \return a const iterator pointing just after the last item in the entry list.
3270 */
3271
3272/*!
3273 \fn const QRhiTextureUploadEntry *QRhiTextureUploadDescription::entryAt(qsizetype index) const
3274 \return the entry at \a index.
3275 */
3276
3277/*!
3278 \fn qsizetype QRhiTextureUploadDescription::entryCount() const
3279 \return the number of entries.
3280 */
3281
3282/*!
3283 \class QRhiTextureCopyDescription
3284 \inmodule QtGuiPrivate
3285 \inheaderfile rhi/qrhi.h
3286 \since 6.6
3287 \brief Describes a texture-to-texture copy operation.
3288
3289 An empty pixelSize() indicates that the entire subresource is to be copied.
3290 A default constructed copy description therefore leads to copying the
3291 entire subresource at level 0 of layer 0.
3292
3293 \note The source texture must be created with
3294 QRhiTexture::UsedAsTransferSource.
3295
3296 \note The source and destination rectangles defined by pixelSize(),
3297 sourceTopLeft(), and destinationTopLeft() must fit the source and
3298 destination textures, respectively. The behavior is undefined otherwise.
3299
3300 With cubemaps, 3D textures, and texture arrays one face or slice can be
3301 copied at a time. The face or slice is specified by the source and
3302 destination layer indices. With mipmapped textures one mip level can be
3303 copied at a time. The source and destination layer and mip level indices can
3304 differ, but the size and position must be carefully controlled to avoid out
3305 of bounds copies, in which case the behavior is undefined.
3306
3307 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3308 for details.
3309 */
3310
3311/*!
3312 \fn QRhiTextureCopyDescription::QRhiTextureCopyDescription()
3313
3314 Constructs an empty texture copy description.
3315 */
3316
3317/*!
3318 \fn QSize QRhiTextureCopyDescription::pixelSize() const
3319 \return the size of the region to copy.
3320
3321 \note An empty pixelSize() indicates that the entire subresource is to be
3322 copied. A default constructed copy description therefore leads to copying
3323 the entire subresource at level 0 of layer 0.
3324 */
3325
3326/*!
3327 \fn void QRhiTextureCopyDescription::setPixelSize(const QSize &sz)
3328 Sets the size of the region to copy to \a sz.
3329 */
3330
3331/*!
3332 \fn int QRhiTextureCopyDescription::sourceLayer() const
3333 \return the source array layer (cubemap face or array layer index). Defaults to 0.
3334 */
3335
3336/*!
3337 \fn void QRhiTextureCopyDescription::setSourceLayer(int layer)
3338 Sets the source array \a layer.
3339 */
3340
3341/*!
3342 \fn int QRhiTextureCopyDescription::sourceLevel() const
3343 \return the source mip level. Defaults to 0.
3344 */
3345
3346/*!
3347 \fn void QRhiTextureCopyDescription::setSourceLevel(int level)
3348 Sets the source mip \a level.
3349 */
3350
3351/*!
3352 \fn QPoint QRhiTextureCopyDescription::sourceTopLeft() const
3353 \return the source top-left position (in pixels). Defaults to (0, 0).
3354 */
3355
3356/*!
3357 \fn void QRhiTextureCopyDescription::setSourceTopLeft(const QPoint &p)
3358 Sets the source top-left position to \a p.
3359 */
3360
3361/*!
3362 \fn int QRhiTextureCopyDescription::destinationLayer() const
3363 \return the destination array layer (cubemap face or array layer index). Default to 0.
3364 */
3365
3366/*!
3367 \fn void QRhiTextureCopyDescription::setDestinationLayer(int layer)
3368 Sets the destination array \a layer.
3369 */
3370
3371/*!
3372 \fn int QRhiTextureCopyDescription::destinationLevel() const
3373 \return the destionation mip level. Defaults to 0.
3374 */
3375
3376/*!
3377 \fn void QRhiTextureCopyDescription::setDestinationLevel(int level)
3378 Sets the destination mip \a level.
3379 */
3380
3381/*!
3382 \fn QPoint QRhiTextureCopyDescription::destinationTopLeft() const
3383 \return the destionation top-left position in pixels. Defaults to (0, 0).
3384 */
3385
3386/*!
3387 \fn void QRhiTextureCopyDescription::setDestinationTopLeft(const QPoint &p)
3388 Sets the destination top-left position \a p.
3389 */
3390
3391/*!
3392 \class QRhiReadbackDescription
3393 \inmodule QtGuiPrivate
3394 \inheaderfile rhi/qrhi.h
3395 \since 6.6
3396 \brief Describes a readback (reading back texture contents from possibly GPU-only memory) operation.
3397
3398 The source of the readback operation is either a QRhiTexture or the
3399 current backbuffer of the currently targeted QRhiSwapChain. When
3400 texture() is not set, the swapchain is used. Otherwise the specified
3401 QRhiTexture is treated as the source.
3402
3403 \note Textures used in readbacks must be created with
3404 QRhiTexture::UsedAsTransferSource.
3405
3406 \note Swapchains used in readbacks must be created with
3407 QRhiSwapChain::UsedAsTransferSource.
3408
3409 layer() and level() are only applicable when the source is a QRhiTexture.
3410
3411 \note Multisample textures cannot be read back. Readbacks are supported for
3412 multisample swapchain buffers however.
3413
3414 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3415 for details.
3416 */
3417
3418/*!
3419 \fn QRhiReadbackDescription::QRhiReadbackDescription() = default
3420
3421 Constructs an empty texture readback description.
3422
3423 \note The source texture is set to null by default, which is still a valid
3424 readback: it specifies that the backbuffer of the current swapchain is to
3425 be read back. (current meaning the frame's target swapchain at the time of
3426 committing the QRhiResourceUpdateBatch with the
3427 \l{QRhiResourceUpdateBatch::readBackTexture()}{texture readback} on it)
3428 */
3429
3430/*!
3431 Constructs an texture readback description that specifies that level 0 of
3432 layer 0 of \a texture is to be read back.
3433
3434 \note \a texture can also be null in which case this constructor is
3435 identical to the argumentless variant.
3436 */
3437QRhiReadbackDescription::QRhiReadbackDescription(QRhiTexture *texture)
3438 : m_texture(texture)
3439{
3440}
3441
3442/*!
3443 \fn QRhiTexture *QRhiReadbackDescription::texture() const
3444
3445 \return the QRhiTexture that is read back. Can be left set to \nullptr
3446 which indicates that the backbuffer of the current swapchain is to be used
3447 instead.
3448 */
3449
3450/*!
3451 \fn void QRhiReadbackDescription::setTexture(QRhiTexture *tex)
3452
3453 Sets the texture \a tex as the source of the readback operation.
3454
3455 Setting \nullptr is valid too, in which case the current swapchain's
3456 current backbuffer is used. (but then the readback cannot be issued in a
3457 non-swapchain-based frame)
3458
3459 \note Multisample textures cannot be read back. Readbacks are supported for
3460 multisample swapchain buffers however.
3461
3462 \note Textures used in readbacks must be created with
3463 QRhiTexture::UsedAsTransferSource.
3464
3465 \note Swapchains used in readbacks must be created with
3466 QRhiSwapChain::UsedAsTransferSource.
3467 */
3468
3469/*!
3470 \fn int QRhiReadbackDescription::layer() const
3471
3472 \return the currently set array layer (cubemap face, array index). Defaults to 0.
3473
3474 Applicable only when the source of the readback is a QRhiTexture.
3475 */
3476
3477/*!
3478 \fn void QRhiReadbackDescription::setLayer(int layer)
3479 Sets the array \a layer to read back.
3480 */
3481
3482/*!
3483 \fn int QRhiReadbackDescription::level() const
3484
3485 \return the currently set mip level. Defaults to 0.
3486
3487 Applicable only when the source of the readback is a QRhiTexture.
3488 */
3489
3490/*!
3491 \fn void QRhiReadbackDescription::setLevel(int level)
3492 Sets the mip \a level to read back.
3493 */
3494
3495/*!
3496 \fn const QRect &QRhiReadbackDescription::rect() const
3497 \since 6.10
3498
3499 \return the rectangle to read back. Defaults to an invalid rectangle.
3500
3501 If invalid, the entire texture or swapchain backbuffer is read back.
3502 */
3503
3504/*!
3505 \fn void QRhiReadbackDescription::setRect(const QRect &rectangle)
3506 \since 6.10
3507
3508 Sets the \a rectangle to read back.
3509 */
3510
3511/*!
3512 \class QRhiReadbackResult
3513 \inmodule QtGuiPrivate
3514 \inheaderfile rhi/qrhi.h
3515 \since 6.6
3516 \brief Describes the results of a potentially asynchronous buffer or texture readback operation.
3517
3518 When \l completed is set, the function is invoked when the \l data is
3519 available. \l format and \l pixelSize are set upon completion together with
3520 \l data.
3521
3522 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3523 for details.
3524 */
3525
3526/*!
3527 \variable QRhiReadbackResult::completed
3528
3529 Callback that is invoked upon completion, on the thread the QRhi operates
3530 on. Can be left set to \nullptr, in which case no callback is invoked.
3531 */
3532
3533/*!
3534 \variable QRhiReadbackResult::format
3535
3536 Valid only for textures, the texture format.
3537 */
3538
3539/*!
3540 \variable QRhiReadbackResult::pixelSize
3541
3542 Valid only for textures, the size in pixels.
3543 */
3544
3545/*!
3546 \variable QRhiReadbackResult::data
3547
3548 The buffer or image data.
3549
3550 \sa QRhiResourceUpdateBatch::readBackTexture(), QRhiResourceUpdateBatch::readBackBuffer()
3551 */
3552
3553
3554/*!
3555 \class QRhiNativeHandles
3556 \inmodule QtGuiPrivate
3557 \inheaderfile rhi/qrhi.h
3558 \since 6.6
3559 \brief Base class for classes exposing backend-specific collections of native resource objects.
3560
3561 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3562 for details.
3563 */
3564
3565/*!
3566 \class QRhiResource
3567 \inmodule QtGuiPrivate
3568 \inheaderfile rhi/qrhi.h
3569 \since 6.6
3570 \brief Base class for classes encapsulating native resource objects.
3571
3572 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3573 for details.
3574 */
3575
3576/*!
3577 \enum QRhiResource::Type
3578 Specifies type of the resource.
3579
3580 \value Buffer
3581 \value Texture
3582 \value Sampler
3583 \value RenderBuffer
3584 \value RenderPassDescriptor
3585 \value SwapChainRenderTarget
3586 \value TextureRenderTarget
3587 \value ShaderResourceBindings
3588 \value GraphicsPipeline
3589 \value SwapChain
3590 \value ComputePipeline
3591 \value CommandBuffer
3592 \value ShadingRateMap
3593 */
3594
3595/*!
3596 \fn virtual QRhiResource::Type QRhiResource::resourceType() const = 0
3597
3598 \return the type of the resource.
3599 */
3600
3601/*!
3602 \internal
3603 */
3604QRhiResource::QRhiResource(QRhiImplementation *rhi)
3605 : m_rhi(rhi)
3606{
3607 m_id = QRhiGlobalObjectIdGenerator::newId();
3608}
3609
3610/*!
3611 Destructor.
3612
3613 Releases (or requests deferred releasing of) the underlying native graphics
3614 resources, if there are any.
3615
3616 \note Resources referenced by commands for the current frame should not be
3617 released until the frame is submitted by QRhi::endFrame().
3618
3619 \sa destroy()
3620 */
3621QRhiResource::~QRhiResource()
3622{
3623 // destroy() cannot be called here, due to virtuals; it is up to the
3624 // subclasses to do that.
3625}
3626
3627/*!
3628 \fn virtual void QRhiResource::destroy() = 0
3629
3630 Releases (or requests deferred releasing of) the underlying native graphics
3631 resources. Safe to call multiple times, subsequent invocations will be a
3632 no-op then.
3633
3634 Once destroy() is called, the QRhiResource instance can be reused, by
3635 calling \c create() again. That will then result in creating new native
3636 graphics resources underneath.
3637
3638 \note Resources referenced by commands for the current frame should not be
3639 released until the frame is submitted by QRhi::endFrame().
3640
3641 The QRhiResource destructor also performs the same task, so calling this
3642 function is not necessary before deleting a QRhiResource.
3643
3644 \sa deleteLater()
3645 */
3646
3647/*!
3648 When called without a frame being recorded, this function is equivalent to
3649 deleting the object. Between a QRhi::beginFrame() and QRhi::endFrame()
3650 however the behavior is different: the QRhiResource will not be destroyed
3651 until the frame is submitted via QRhi::endFrame(), thus satisfying the QRhi
3652 requirement of not altering QRhiResource objects that are referenced by the
3653 frame being recorded.
3654
3655 If the QRhi that created this object is already destroyed, the object is
3656 deleted immediately.
3657
3658 Using deleteLater() can be a useful convenience in many cases, and it
3659 complements the low-level guarantee (that the underlying native graphics
3660 objects are never destroyed until it is safe to do so and it is known for
3661 sure that they are not used by the GPU in an still in-flight frame), by
3662 offering a way to make sure the C++ object instances (of QRhiBuffer,
3663 QRhiTexture, etc.) themselves also stay valid until the end of the current
3664 frame.
3665
3666 The following example shows a convenient way of creating a throwaway buffer
3667 that is only used in one frame and gets automatically released in
3668 endFrame(). (when it comes to the underlying native buffer(s), the usual
3669 guarantee applies: the QRhi backend defers the releasing of those until it
3670 is guaranteed that the frame in which the buffer is accessed by the GPU has
3671 completed)
3672
3673 \code
3674 rhi->beginFrame(swapchain);
3675 QRhiBuffer *buf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, 256);
3676 buf->deleteLater(); // !
3677 u = rhi->nextResourceUpdateBatch();
3678 u->uploadStaticBuffer(buf, data);
3679 // ... draw with buf
3680 rhi->endFrame();
3681 \endcode
3682
3683 \sa destroy()
3684 */
3685void QRhiResource::deleteLater()
3686{
3687 if (m_rhi)
3688 m_rhi->addDeleteLater(this);
3689 else
3690 delete this;
3691}
3692
3693/*!
3694 \return the currently set object name. By default the name is empty.
3695 */
3696QByteArray QRhiResource::name() const
3697{
3698 return m_objectName;
3699}
3700
3701/*!
3702 Sets a \a name for the object.
3703
3704 This allows getting descriptive names for the native graphics
3705 resources visible in graphics debugging tools, such as
3706 \l{https://renderdoc.org/}{RenderDoc} and
3707 \l{https://developer.apple.com/xcode/}{XCode}.
3708
3709 When it comes to naming native objects by relaying the name via the
3710 appropriate graphics API, note that the name is ignored when
3711 QRhi::DebugMarkers are not supported, and may, depending on the backend,
3712 also be ignored when QRhi::EnableDebugMarkers is not set.
3713
3714 \note The name may be ignored for objects other than buffers,
3715 renderbuffers, and textures, depending on the backend.
3716
3717 \note The name may be modified. For slotted resources, such as a QRhiBuffer
3718 backed by multiple native buffers, QRhi will append a suffix to make the
3719 underlying native buffers easily distinguishable from each other.
3720 */
3721void QRhiResource::setName(const QByteArray &name)
3722{
3723 m_objectName = name;
3724}
3725
3726/*!
3727 \return the global, unique identifier of this QRhiResource.
3728
3729 User code rarely needs to deal with the value directly. It is used
3730 internally for tracking and bookkeeping purposes.
3731 */
3732quint64 QRhiResource::globalResourceId() const
3733{
3734 return m_id;
3735}
3736
3737/*!
3738 \return the QRhi that created this resource.
3739
3740 If the QRhi that created this object is already destroyed, the result is
3741 \nullptr.
3742 */
3743QRhi *QRhiResource::rhi() const
3744{
3745 return m_rhi ? m_rhi->q : nullptr;
3746}
3747
3748/*!
3749 \class QRhiBuffer
3750 \inmodule QtGuiPrivate
3751 \inheaderfile rhi/qrhi.h
3752 \since 6.6
3753 \brief Vertex, index, or uniform (constant) buffer resource.
3754
3755 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
3756 for details.
3757
3758 A QRhiBuffer encapsulates zero, one, or more native buffer objects (such as
3759 a \c VkBuffer or \c MTLBuffer). With some graphics APIs and backends
3760 certain types of buffers may not use a native buffer object at all (e.g.
3761 OpenGL if uniform buffer objects are not used), but this is transparent to
3762 the user of the QRhiBuffer API. Similarly, the fact that some types of
3763 buffers may use two or three native buffers underneath, in order to allow
3764 efficient per-frame content update without stalling the GPU pipeline, is
3765 mostly invisible to the applications and libraries.
3766
3767 A QRhiBuffer instance is always created by calling
3768 \l{QRhi::newBuffer()}{the QRhi's newBuffer() function}. This creates no
3769 native graphics resources. To do that, call create() after setting the
3770 appropriate options, such as the type, usage flags, size, although in most cases these
3771 are already set based on the arguments passed to
3772 \l{QRhi::newBuffer()}{newBuffer()}.
3773
3774 \section2 Example usage
3775
3776 To create a uniform buffer for a shader where the GLSL uniform block
3777 contains a single \c mat4 member, and update the contents:
3778
3779 \code
3780 QRhiBuffer *ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64);
3781 if (!ubuf->create()) { error(); }
3782 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3783 QMatrix4x4 mvp;
3784 // ... set up the modelview-projection matrix
3785 batch->updateDynamicBuffer(ubuf, 0, 64, mvp.constData());
3786 // ...
3787 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
3788 \endcode
3789
3790 An example of creating a buffer with vertex data:
3791
3792 \code
3793 const float vertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f };
3794 QRhiBuffer *vbuf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertices));
3795 if (!vbuf->create()) { error(); }
3796 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3797 batch->uploadStaticBuffer(vbuf, vertices);
3798 // ...
3799 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
3800 \endcode
3801
3802 An index buffer:
3803
3804 \code
3805 static const quint16 indices[] = { 0, 1, 2 };
3806 QRhiBuffer *ibuf = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indices));
3807 if (!ibuf->create()) { error(); }
3808 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3809 batch->uploadStaticBuffer(ibuf, indices);
3810 // ...
3811 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
3812 \endcode
3813
3814 \section2 Common patterns
3815
3816 A call to create() destroys any existing native resources if create() was
3817 successfully called before. If those native resources are still in use by
3818 an in-flight frame (i.e., there's a chance they are still read by the GPU),
3819 the destroying of those resources is deferred automatically. Thus a very
3820 common and convenient pattern to safely increase the size of an already
3821 initialized buffer is the following. In practice this drops and creates a
3822 whole new set of native resources underneath, so it is not necessarily a
3823 cheap operation, but is more convenient and still faster than the
3824 alternatives, because by not destroying the \c buf object itself, all
3825 references to it stay valid in other data structures (e.g., in any
3826 QRhiShaderResourceBinding the QRhiBuffer is referenced from).
3827
3828 \code
3829 if (buf->size() < newSize) {
3830 buf->setSize(newSize);
3831 if (!buf->create()) { error(); }
3832 }
3833 // continue using buf, fill it with new data
3834 \endcode
3835
3836 When working with uniform buffers, it will sometimes be necessary to
3837 combine data for multiple draw calls into a single buffer for efficiency
3838 reasons. Be aware of the aligment requirements: with some graphics APIs
3839 offsets for a uniform buffer must be aligned to 256 bytes. This applies
3840 both to QRhiShaderResourceBinding and to the dynamic offsets passed to
3841 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()}. Use the
3842 \l{QRhi::ubufAlignment()}{ubufAlignment()} and
3843 \l{QRhi::ubufAligned()}{ubufAligned()} functions to create portable code.
3844 As an example, the following is an outline for issuing multiple (\c N) draw
3845 calls with the same pipeline and geometry, but with a different data in the
3846 uniform buffers exposed at binding point 0. This assumes the buffer is
3847 exposed via
3848 \l{QRhiShaderResourceBinding::uniformBufferWithDynamicOffset()}{uniformBufferWithDynamicOffset()}
3849 which allows passing a QRhiCommandBuffer::DynamicOffset list to
3850 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()}.
3851
3852 \code
3853 const int N = 2;
3854 const int UB_SIZE = 64 + 4; // assuming a uniform block with { mat4 matrix; float opacity; }
3855 const int ONE_UBUF_SIZE = rhi->ubufAligned(UB_SIZE);
3856 const int TOTAL_UBUF_SIZE = N * ONE_UBUF_SIZE;
3857 QRhiBuffer *ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, TOTAL_UBUF_SIZE);
3858 if (!ubuf->create()) { error(); }
3859 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
3860 for (int i = 0; i < N; ++i) {
3861 batch->updateDynamicBuffer(ubuf, i * ONE_UBUF_SIZE, 64, matrix.constData());
3862 batch->updateDynamicBuffer(ubuf, i * ONE_UBUF_SIZE + 64, 4, &opacity);
3863 }
3864 // ...
3865 // beginPass(), set pipeline, etc., and then:
3866 for (int i = 0; i < N; ++i) {
3867 QRhiCommandBuffer::DynamicOffset dynOfs[] = { { 0, i * ONE_UBUF_SIZE } };
3868 cb->setShaderResources(srb, 1, dynOfs);
3869 cb->draw(36);
3870 }
3871 \endcode
3872
3873 \sa QRhiResourceUpdateBatch, QRhi, QRhiCommandBuffer
3874 */
3875
3876/*!
3877 \enum QRhiBuffer::Type
3878 Specifies storage type of buffer resource.
3879
3880 \value Immutable Indicates that the data is not expected to change ever
3881 after the initial upload. Under the hood such buffer resources are
3882 typically placed in device local (GPU) memory (on systems where
3883 applicable). Uploading new data is possible, but may be expensive. The
3884 upload typically happens by copying to a separate, host visible staging
3885 buffer from which a GPU buffer-to-buffer copy is issued into the actual
3886 GPU-only buffer.
3887
3888 \value Static Indicates that the data is expected to change only
3889 infrequently. Typically placed in device local (GPU) memory, where
3890 applicable. On backends where host visible staging buffers are used for
3891 uploading, the staging buffers are kept around for this type, unlike with
3892 Immutable, so subsequent uploads do not suffer in performance. Frequent
3893 updates, especially updates in consecutive frames, should be avoided.
3894
3895 \value Dynamic Indicates that the data is expected to change frequently.
3896 Not recommended for large buffers. Typically backed by host visible memory
3897 in 2 copies in order to allow for changing without stalling the graphics
3898 pipeline. The double buffering is managed transparently to the applications
3899 and is not exposed in the API here in any form. This is the recommended,
3900 and, with some backends, the only possible, type for buffers with
3901 UniformBuffer usage.
3902 */
3903
3904/*!
3905 \enum QRhiBuffer::UsageFlag
3906 Flag values to specify how the buffer is going to be used.
3907
3908 \value VertexBuffer Vertex buffer. This allows the QRhiBuffer to be used in
3909 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()}.
3910
3911 \value IndexBuffer Index buffer. This allows the QRhiBuffer to be used in
3912 \l{QRhiCommandBuffer::setVertexInput()}{setVertexInput()}.
3913
3914 \value UniformBuffer Uniform buffer (also called constant buffer). This
3915 allows the QRhiBuffer to be used in combination with
3916 \l{QRhiShaderResourceBinding::UniformBuffer}{UniformBuffer}. When
3917 \l{QRhi::NonDynamicUniformBuffers}{NonDynamicUniformBuffers} is reported as
3918 not supported, this usage can only be combined with the type Dynamic.
3919
3920 \value StorageBuffer Storage buffer. This allows the QRhiBuffer to be used
3921 in combination with \l{QRhiShaderResourceBinding::BufferLoad}{BufferLoad},
3922 \l{QRhiShaderResourceBinding::BufferStore}{BufferStore}, or
3923 \l{QRhiShaderResourceBinding::BufferLoadStore}{BufferLoadStore}. This usage
3924 can only be combined with the types Immutable or Static, and is only
3925 available when the \l{QRhi::Compute}{Compute feature} is reported as
3926 supported.
3927 */
3928
3929/*!
3930 \class QRhiBuffer::NativeBuffer
3931 \inmodule QtGuiPrivate
3932 \inheaderfile rhi/qrhi.h
3933 \brief Contains information about the underlying native resources of a buffer.
3934 */
3935
3936/*!
3937 \variable QRhiBuffer::NativeBuffer::objects
3938 \brief an array with pointers to the native object handles.
3939
3940 With OpenGL, the native handle is a GLuint value, so the elements in the \c
3941 objects array are pointers to a GLuint. With Vulkan, the native handle is a
3942 VkBuffer, so the elements of the array are pointers to a VkBuffer. With
3943 Direct3D 11 and Metal the elements are pointers to a ID3D11Buffer or
3944 MTLBuffer pointer, respectively. With Direct3D 12, the elements are
3945 pointers to a ID3D12Resource.
3946
3947 \note Pay attention to the fact that the elements are always pointers to
3948 the native buffer handle type, even if the native type itself is a pointer.
3949 (so the elements are \c{VkBuffer *} on Vulkan, even though VkBuffer itself
3950 is a pointer on 64-bit architectures).
3951 */
3952
3953/*!
3954 \variable QRhiBuffer::NativeBuffer::slotCount
3955 \brief Specifies the number of valid elements in the objects array.
3956
3957 The value can be 0, 1, 2, or 3 in practice. 0 indicates that the QRhiBuffer
3958 is not backed by any native buffer objects. This can happen with
3959 QRhiBuffers with the usage UniformBuffer when the underlying API does not
3960 support (or the backend chooses not to use) native uniform buffers. 1 is
3961 commonly used for Immutable and Static types (but some backends may
3962 differ). 2 or 3 is typical when the type is Dynamic (but some backends may
3963 differ).
3964
3965 \sa QRhi::currentFrameSlot(), QRhi::FramesInFlight
3966 */
3967
3968/*!
3969 \internal
3970 */
3971QRhiBuffer::QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, quint32 size_)
3972 : QRhiResource(rhi),
3973 m_type(type_), m_usage(usage_), m_size(size_)
3974{
3975}
3976
3977/*!
3978 \return the resource type.
3979 */
3980QRhiResource::Type QRhiBuffer::resourceType() const
3981{
3982 return Buffer;
3983}
3984
3985/*!
3986 \fn virtual bool QRhiBuffer::create() = 0
3987
3988 Creates the corresponding native graphics resources. If there are already
3989 resources present due to an earlier create() with no corresponding
3990 destroy(), then destroy() is called implicitly first.
3991
3992 \return \c true when successful, \c false when a graphics operation failed.
3993 Regardless of the return value, calling destroy() is always safe.
3994 */
3995
3996/*!
3997 \fn QRhiBuffer::Type QRhiBuffer::type() const
3998 \return the buffer type.
3999 */
4000
4001/*!
4002 \fn void QRhiBuffer::setType(Type t)
4003 Sets the buffer's type to \a t.
4004 */
4005
4006/*!
4007 \fn QRhiBuffer::UsageFlags QRhiBuffer::usage() const
4008 \return the buffer's usage flags.
4009 */
4010
4011/*!
4012 \fn void QRhiBuffer::setUsage(UsageFlags u)
4013 Sets the buffer's usage flags to \a u.
4014 */
4015
4016/*!
4017 \fn quint32 QRhiBuffer::size() const
4018
4019 \return the buffer's size in bytes.
4020
4021 This is always the value that was passed to setSize() or QRhi::newBuffer().
4022 Internally, the native buffers may be bigger if that is required by the
4023 underlying graphics API.
4024 */
4025
4026/*!
4027 \fn void QRhiBuffer::setSize(quint32 sz)
4028
4029 Sets the size of the buffer in bytes. The size is normally specified in
4030 QRhi::newBuffer() so this function is only used when the size has to be
4031 changed. As with other setters, the size only takes effect when calling
4032 create(), and for already created buffers this involves releasing the previous
4033 native resource and creating new ones under the hood.
4034
4035 Backends may choose to allocate buffers bigger than \a sz in order to
4036 fulfill alignment requirements. This is hidden from the applications and
4037 size() will always report the size requested in \a sz.
4038 */
4039
4040/*!
4041 \return the underlying native resources for this buffer. The returned value
4042 will be empty if exposing the underlying native resources is not supported by
4043 the backend.
4044
4045 A QRhiBuffer may be backed by multiple native buffer objects, depending on
4046 the type() and the QRhi backend in use. When this is the case, all of them
4047 are returned in the objects array in the returned struct, with slotCount
4048 specifying the number of native buffer objects. While
4049 \l{QRhi::beginFrame()}{recording a frame}, QRhi::currentFrameSlot() can be
4050 used to determine which of the native buffers QRhi is using for operations
4051 that read or write from this QRhiBuffer within the frame being recorded.
4052
4053 In some cases a QRhiBuffer will not be backed by a native buffer object at
4054 all. In this case slotCount will be set to 0 and no valid native objects
4055 are returned. This is not an error, and is perfectly valid when a given
4056 backend does not use native buffers for QRhiBuffers with certain types or
4057 usages.
4058
4059 \note Be aware that QRhi backends may employ various buffer update
4060 strategies. Unlike textures, where uploading image data always means
4061 recording a buffer-to-image (or similar) copy command on the command
4062 buffer, buffers, in particular Dynamic and UniformBuffer ones, can operate
4063 in many different ways. For example, a QRhiBuffer with usage type
4064 UniformBuffer may not even be backed by a native buffer object at all if
4065 uniform buffers are not used or supported by a given backend and graphics
4066 API. There are also differences to how data is written to the buffer and
4067 the type of backing memory used. For buffers backed by host visible memory,
4068 calling this function guarantees that pending host writes are executed for
4069 all the returned native buffers.
4070
4071 \sa QRhi::currentFrameSlot(), QRhi::FramesInFlight
4072 */
4073QRhiBuffer::NativeBuffer QRhiBuffer::nativeBuffer()
4074{
4075 return { {}, 0 };
4076}
4077
4078/*!
4079 \return a pointer to a memory block with the host visible buffer data.
4080
4081 This is a shortcut for medium-to-large dynamic uniform buffers that have
4082 their \b entire contents (or at least all regions that are read by the
4083 shaders in the current frame) changed \b{in every frame} and the
4084 QRhiResourceUpdateBatch-based update mechanism is seen too heavy due to the
4085 amount of data copying involved.
4086
4087 The call to this function must be eventually followed by a call to
4088 endFullDynamicUniformBufferUpdateForCurrentFrame(), before recording any
4089 render or compute pass that relies on this buffer.
4090
4091 \warning Updating data via this method is not compatible with
4092 QRhiResourceUpdateBatch-based updates and readbacks. Unexpected behavior
4093 may occur when attempting to combine the two update models for the same
4094 buffer. Similarly, the data updated this direct way may not be visible to
4095 \l{QRhiResourceUpdateBatch::readBackBuffer()}{readBackBuffer operations},
4096 depending on the backend.
4097
4098 \warning When updating buffer data via this method, the update must be done
4099 in every frame, otherwise backends that perform double or triple buffering
4100 of resources may end up in unexpected behavior.
4101
4102 \warning Partial updates are not possible with this approach since some
4103 backends may choose a strategy where the previous contents of the buffer is
4104 lost upon calling this function. Data must be written to all regions that
4105 are read by shaders in the frame currently being prepared.
4106
4107 \warning This function can only be called when recording a frame, so
4108 between QRhi::beginFrame() and QRhi::endFrame().
4109
4110 \warning This function can only be called on Dynamic buffers.
4111 */
4112char *QRhiBuffer::beginFullDynamicBufferUpdateForCurrentFrame()
4113{
4114 return nullptr;
4115}
4116
4117/*!
4118 To be called when the entire contents of the buffer data has been updated
4119 in the memory block returned from
4120 beginFullDynamicBufferUpdateForCurrentFrame().
4121 */
4122void QRhiBuffer::endFullDynamicBufferUpdateForCurrentFrame()
4123{
4124}
4125
4126/*!
4127 \internal
4128 */
4129void QRhiBuffer::fullDynamicBufferUpdateForCurrentFrame(const void *data, quint32 size)
4130{
4131 char *p = beginFullDynamicBufferUpdateForCurrentFrame();
4132 if (p) {
4133 memcpy(p, data, size > 0 ? size : m_size);
4134 endFullDynamicBufferUpdateForCurrentFrame();
4135 }
4136}
4137
4138/*!
4139 \class QRhiRenderBuffer
4140 \inmodule QtGuiPrivate
4141 \inheaderfile rhi/qrhi.h
4142 \since 6.6
4143 \brief Renderbuffer resource.
4144
4145 Renderbuffers cannot be sampled or read but have some benefits over
4146 textures in some cases:
4147
4148 A \l DepthStencil renderbuffer may be lazily allocated and be backed by
4149 transient memory with some APIs. On some platforms this may mean the
4150 depth/stencil buffer uses no physical backing at all.
4151
4152 \l Color renderbuffers are useful since QRhi::MultisampleRenderBuffer may be
4153 supported even when QRhi::MultisampleTexture is not.
4154
4155 How the renderbuffer is implemented by a backend is not exposed to the
4156 applications. In some cases it may be backed by ordinary textures, while in
4157 others there may be a different kind of native resource used.
4158
4159 Renderbuffers that are used as (and are only used as) depth-stencil buffers
4160 in combination with a QRhiSwapChain's color buffers should have the
4161 UsedWithSwapChainOnly flag set. This serves a double purpose: such buffers,
4162 depending on the backend and the underlying APIs, be more efficient, and
4163 QRhi provides automatic sizing behavior to match the color buffers, which
4164 means calling setPixelSize() and create() are not necessary for such
4165 renderbuffers.
4166
4167 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4168 for details.
4169 */
4170
4171/*!
4172 \enum QRhiRenderBuffer::Type
4173 Specifies the type of the renderbuffer
4174
4175 \value DepthStencil Combined depth/stencil
4176 \value Color Color
4177 */
4178
4179/*!
4180 \struct QRhiRenderBuffer::NativeRenderBuffer
4181 \inmodule QtGuiPrivate
4182 \inheaderfile rhi/qrhi.h
4183 \brief Wraps a native renderbuffer object.
4184 */
4185
4186/*!
4187 \variable QRhiRenderBuffer::NativeRenderBuffer::object
4188 \brief 64-bit integer containing the native object handle.
4189
4190 Used with QRhiRenderBuffer::createFrom().
4191
4192 With OpenGL the native handle is a GLuint value. \c object is expected to
4193 be a valid OpenGL renderbuffer object ID.
4194 */
4195
4196/*!
4197 \enum QRhiRenderBuffer::Flag
4198 Flag values for flags() and setFlags()
4199
4200 \value UsedWithSwapChainOnly For DepthStencil renderbuffers this indicates
4201 that the renderbuffer is only used in combination with a QRhiSwapChain, and
4202 never in any other way. This provides automatic sizing and resource
4203 rebuilding, so calling setPixelSize() or create() is not needed whenever
4204 this flag is set. This flag value may also trigger backend-specific
4205 behavior, for example with OpenGL, where a separate windowing system
4206 interface API is in use (EGL, GLX, etc.), the flag is especially important
4207 as it avoids creating any actual renderbuffer resource as there is already
4208 a windowing system provided depth/stencil buffer as requested by
4209 QSurfaceFormat.
4210 */
4211
4212/*!
4213 \internal
4214 */
4215QRhiRenderBuffer::QRhiRenderBuffer(QRhiImplementation *rhi, Type type_, const QSize &pixelSize_,
4216 int sampleCount_, Flags flags_,
4217 QRhiTexture::Format backingFormatHint_)
4218 : QRhiResource(rhi),
4219 m_type(type_), m_pixelSize(pixelSize_), m_sampleCount(sampleCount_), m_flags(flags_),
4220 m_backingFormatHint(backingFormatHint_)
4221{
4222}
4223
4224/*!
4225 \return the resource type.
4226 */
4227QRhiResource::Type QRhiRenderBuffer::resourceType() const
4228{
4229 return RenderBuffer;
4230}
4231
4232/*!
4233 \fn virtual bool QRhiRenderBuffer::create() = 0
4234
4235 Creates the corresponding native graphics resources. If there are already
4236 resources present due to an earlier create() with no corresponding
4237 destroy(), then destroy() is called implicitly first.
4238
4239 \return \c true when successful, \c false when a graphics operation failed.
4240 Regardless of the return value, calling destroy() is always safe.
4241 */
4242
4243/*!
4244 Similar to create() except that no new native renderbuffer objects are
4245 created. Instead, the native renderbuffer object specified by \a src is
4246 used.
4247
4248 This allows importing an existing renderbuffer object (which must belong to
4249 the same device or sharing context, depending on the graphics API) from an
4250 external graphics engine.
4251
4252 \note This is currently applicable to OpenGL only. This function exists
4253 solely to allow importing a renderbuffer object that is bound to some
4254 special, external object, such as an EGLImageKHR. Once the application
4255 performed the glEGLImageTargetRenderbufferStorageOES call, the renderbuffer
4256 object can be passed to this function to create a wrapping
4257 QRhiRenderBuffer, which in turn can be passed in as a color attachment to
4258 a QRhiTextureRenderTarget to enable rendering to the EGLImage.
4259
4260 \note pixelSize(), sampleCount(), and flags() must still be set correctly.
4261 Passing incorrect sizes and other values to QRhi::newRenderBuffer() and
4262 then following it with a createFrom() expecting that the native
4263 renderbuffer object alone is sufficient to deduce such values is \b wrong
4264 and will lead to problems.
4265
4266 \note QRhiRenderBuffer does not take ownership of the native object, and
4267 destroy() will not release that object.
4268
4269 \note This function is only implemented when the QRhi::RenderBufferImport
4270 feature is reported as \l{QRhi::isFeatureSupported()}{supported}. Otherwise,
4271 the function does nothing and the return value is \c false.
4272
4273 \return \c true when successful, \c false when not supported.
4274 */
4275bool QRhiRenderBuffer::createFrom(NativeRenderBuffer src)
4276{
4277 Q_UNUSED(src);
4278 return false;
4279}
4280
4281/*!
4282 \fn QRhiRenderBuffer::Type QRhiRenderBuffer::type() const
4283 \return the renderbuffer type.
4284 */
4285
4286/*!
4287 \fn void QRhiRenderBuffer::setType(Type t)
4288 Sets the type to \a t.
4289 */
4290
4291/*!
4292 \fn QSize QRhiRenderBuffer::pixelSize() const
4293 \return the pixel size.
4294 */
4295
4296/*!
4297 \fn void QRhiRenderBuffer::setPixelSize(const QSize &sz)
4298 Sets the size (in pixels) to \a sz.
4299 */
4300
4301/*!
4302 \fn int QRhiRenderBuffer::sampleCount() const
4303 \return the sample count. 1 means no multisample antialiasing.
4304 */
4305
4306/*!
4307 \fn void QRhiRenderBuffer::setSampleCount(int s)
4308 Sets the sample count to \a s.
4309 */
4310
4311/*!
4312 \fn QRhiRenderBuffer::Flags QRhiRenderBuffer::flags() const
4313 \return the flags.
4314 */
4315
4316/*!
4317 \fn void QRhiRenderBuffer::setFlags(Flags f)
4318 Sets the flags to \a f.
4319 */
4320
4321/*!
4322 \fn virtual QRhiTexture::Format QRhiRenderBuffer::backingFormat() const = 0
4323
4324 \internal
4325 */
4326
4327/*!
4328 \class QRhiTexture
4329 \inmodule QtGuiPrivate
4330 \inheaderfile rhi/qrhi.h
4331 \since 6.6
4332 \brief Texture resource.
4333
4334 A QRhiTexture encapsulates a native texture object, such as a \c VkImage or
4335 \c MTLTexture.
4336
4337 A QRhiTexture instance is always created by calling
4338 \l{QRhi::newTexture()}{the QRhi's newTexture() function}. This creates no
4339 native graphics resources. To do that, call create() after setting the
4340 appropriate options, such as the format and size, although in most cases
4341 these are already set based on the arguments passed to
4342 \l{QRhi::newTexture()}{newTexture()}.
4343
4344 Setting the \l{QRhiTexture::Flags}{flags} correctly is essential, otherwise
4345 various errors can occur depending on the underlying QRhi backend and
4346 graphics API. For example, when a texture will be rendered into from a
4347 render pass via QRhiTextureRenderTarget, the texture must be created with
4348 the \l RenderTarget flag set. Similarly, when the texture is going to be
4349 \l{QRhiResourceUpdateBatch::readBackTexture()}{read back}, the \l
4350 UsedAsTransferSource flag must be set upfront. Mipmapped textures must have
4351 the MipMapped flag set. And so on. It is not possible to change the flags
4352 once create() has succeeded. To release the existing and create a new
4353 native texture object with the changed settings, call the setters and call
4354 create() again. This then might be a potentially expensive operation.
4355
4356 \section2 Example usage
4357
4358 To create a 2D texture with a size of 512x512 pixels and set its contents to all green:
4359
4360 \code
4361 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, QSize(512, 512));
4362 if (!texture->create()) { error(); }
4363 QRhiResourceUpdateBatch *batch = rhi->nextResourceUpdateBatch();
4364 QImage image(512, 512, QImage::Format_RGBA8888);
4365 image.fill(Qt::green);
4366 batch->uploadTexture(texture, image);
4367 // ...
4368 commandBuffer->resourceUpdate(batch); // or, alternatively, pass 'batch' to a beginPass() call
4369 \endcode
4370
4371 \section2 Common patterns
4372
4373 A call to create() destroys any existing native resources if create() was
4374 successfully called before. If those native resources are still in use by
4375 an in-flight frame (i.e., there's a chance they are still read by the GPU),
4376 the destroying of those resources is deferred automatically. Thus a very
4377 common and convenient pattern to safely change the size of an already
4378 existing texture is the following. In practice this drops and creates a
4379 whole new native texture resource underneath, so it is not necessarily a
4380 cheap operation, but is more convenient and still faster than the
4381 alternatives, because by not destroying the \c texture object itself, all
4382 references to it stay valid in other data structures (e.g., in any
4383 QShaderResourceBinding the QRhiTexture is referenced from).
4384
4385 \code
4386 // determine newSize, e.g. based on the swapchain's output size or other factors
4387 if (texture->pixelSize() != newSize) {
4388 texture->setPixelSize(newSize);
4389 if (!texture->create()) { error(); }
4390 }
4391 // continue using texture, fill it with new data
4392 \endcode
4393
4394 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4395 for details.
4396
4397 \sa QRhiResourceUpdateBatch, QRhi, QRhiTextureRenderTarget
4398 */
4399
4400/*!
4401 \enum QRhiTexture::Flag
4402
4403 Flag values to specify how the texture is going to be used. Not honoring
4404 the flags set before create() and attempting to use the texture in ways that
4405 was not declared upfront can lead to unspecified behavior or decreased
4406 performance depending on the backend and the underlying graphics API.
4407
4408 \value RenderTarget The texture going to be used in combination with
4409 QRhiTextureRenderTarget.
4410
4411 \value CubeMap The texture is a cubemap. Such textures have 6 layers, one
4412 for each face in the order of +X, -X, +Y, -Y, +Z, -Z. Cubemap textures
4413 cannot be multisample.
4414
4415 \value MipMapped The texture has mipmaps. The appropriate mip count is
4416 calculated automatically and can also be retrieved via
4417 QRhi::mipLevelsForSize(). The images for the mip levels have to be
4418 provided in the texture uploaded or generated via
4419 QRhiResourceUpdateBatch::generateMips(). Multisample textures cannot have
4420 mipmaps.
4421
4422 \value sRGB Use an sRGB format.
4423
4424 \value UsedAsTransferSource The texture is used as the source of a texture
4425 copy or readback, meaning the texture is given as the source in
4426 QRhiResourceUpdateBatch::copyTexture() or
4427 QRhiResourceUpdateBatch::readBackTexture().
4428
4429 \value UsedWithGenerateMips The texture is going to be used with
4430 QRhiResourceUpdateBatch::generateMips().
4431
4432 \value UsedWithLoadStore The texture is going to be used with image
4433 load/store operations, for example, in a compute shader.
4434
4435 \value UsedAsCompressedAtlas The texture has a compressed format and the
4436 dimensions of subresource uploads may not match the texture size.
4437
4438 \value ExternalOES The texture should use the GL_TEXTURE_EXTERNAL_OES
4439 target with OpenGL. This flag is ignored with other graphics APIs.
4440
4441 \value ThreeDimensional The texture is a 3D texture. Such textures should
4442 be created with the QRhi::newTexture() overload taking a depth in addition
4443 to width and height. A 3D texture can have mipmaps but cannot be
4444 multisample. When rendering into, or uploading data to a 3D texture, the \c
4445 layer specified in the render target's color attachment or the upload
4446 description refers to a single slice in range [0..depth-1]. The underlying
4447 graphics API may not support 3D textures at run time. Support is indicated
4448 by the QRhi::ThreeDimensionalTextures feature.
4449
4450 \value TextureRectangleGL The texture should use the GL_TEXTURE_RECTANGLE
4451 target with OpenGL. This flag is ignored with other graphics APIs. Just
4452 like ExternalOES, this flag is useful when working with platform APIs where
4453 native OpenGL texture objects received from the platform are wrapped in a
4454 QRhiTexture, and the platform can only provide textures for a non-2D
4455 texture target.
4456
4457 \value TextureArray The texture is a texture array, i.e. a single texture
4458 object that is a homogeneous array of 2D textures. Texture arrays are
4459 created with QRhi::newTextureArray(). The underlying graphics API may not
4460 support texture array objects at run time. Support is indicated by the
4461 QRhi::TextureArrays feature. When rendering into, or uploading data to a
4462 texture array, the \c layer specified in the render target's color
4463 attachment or the upload description selects a single element in the array.
4464
4465 \value OneDimensional The texture is a 1D texture. Such textures can be
4466 created by passing a 0 height and depth to QRhi::newTexture(). Note that
4467 there can be limitations on one dimensional textures depending on the
4468 underlying graphics API. For example, rendering to them or using them with
4469 mipmap-based filtering may be unsupported. This is indicated by the
4470 QRhi::OneDimensionalTextures and QRhi::OneDimensionalTextureMipmaps
4471 feature flags.
4472
4473 \value UsedAsShadingRateMap
4474 */
4475
4476/*!
4477 \enum QRhiTexture::Format
4478
4479 Specifies the texture format. See also QRhi::isTextureFormatSupported() and
4480 note that flags() can modify the format when QRhiTexture::sRGB is set.
4481
4482 \value UnknownFormat Not a valid format. This cannot be passed to setFormat().
4483
4484 \value RGBA8 Four components, unsigned normalized 8-bit per component. Always supported. (32 bits total)
4485
4486 \value BGRA8 Four components, unsigned normalized 8-bit per component. (32 bits total)
4487
4488 \value R8 One component, unsigned normalized 8-bit. (8 bits total)
4489
4490 \value RG8 Two components, unsigned normalized 8-bit. (16 bits total)
4491
4492 \value R16 One component, unsigned normalized 16-bit. (16 bits total)
4493
4494 \value RG16 Two components, unsigned normalized 16-bit. (32 bits total)
4495
4496 \value RED_OR_ALPHA8 Either same as R8, or is a similar format with the component swizzled to alpha,
4497 depending on \l{QRhi::RedOrAlpha8IsRed}{RedOrAlpha8IsRed}. (8 bits total)
4498
4499 \value RGBA16F Four components, 16-bit float. (64 bits total)
4500
4501 \value RGBA32F Four components, 32-bit float. (128 bits total)
4502
4503 \value R16F One component, 16-bit float. (16 bits total)
4504
4505 \value R32F One component, 32-bit float. (32 bits total)
4506
4507 \value RGB10A2 Four components, unsigned normalized 10 bit R, G, and B,
4508 2-bit alpha. This is a packed format so native endianness applies. Note
4509 that there is no BGR10A2. This is because RGB10A2 maps to
4510 DXGI_FORMAT_R10G10B10A2_UNORM with D3D, MTLPixelFormatRGB10A2Unorm with
4511 Metal, VK_FORMAT_A2B10G10R10_UNORM_PACK32 with Vulkan, and
4512 GL_RGB10_A2/GL_RGB/GL_UNSIGNED_INT_2_10_10_10_REV on OpenGL (ES). This is
4513 the only universally supported RGB30 option. The corresponding QImage
4514 formats are QImage::Format_BGR30 and QImage::Format_A2BGR30_Premultiplied.
4515 (32 bits total)
4516
4517 \value D16 16-bit depth (normalized unsigned integer)
4518
4519 \value D24 24-bit depth (normalized unsigned integer)
4520
4521 \value D24S8 24-bit depth (normalized unsigned integer), 8 bit stencil
4522
4523 \value D32F 32-bit depth (32-bit float)
4524
4525 \value [since 6.9] D32FS8 32-bit depth (32-bit float), 8 bits of stencil, 24 bits unused
4526 (64 bits total)
4527
4528 \value BC1
4529 \value BC2
4530 \value BC3
4531 \value BC4
4532 \value BC5
4533 \value BC6H
4534 \value BC7
4535
4536 \value ETC2_RGB8
4537 \value ETC2_RGB8A1
4538 \value ETC2_RGBA8
4539
4540 \value ASTC_4x4
4541 \value ASTC_5x4
4542 \value ASTC_5x5
4543 \value ASTC_6x5
4544 \value ASTC_6x6
4545 \value ASTC_8x5
4546 \value ASTC_8x6
4547 \value ASTC_8x8
4548 \value ASTC_10x5
4549 \value ASTC_10x6
4550 \value ASTC_10x8
4551 \value ASTC_10x10
4552 \value ASTC_12x10
4553 \value ASTC_12x12
4554
4555 \value [since 6.9] R8UI One component, unsigned 8-bit. (8 bits total)
4556 \value [since 6.9] R32UI One component, unsigned 32-bit. (32 bits total)
4557 \value [since 6.9] RG32UI Two components, unsigned 32-bit. (64 bits total)
4558 \value [since 6.9] RGBA32UI Four components, unsigned 32-bit. (128 bits total)
4559
4560 \value [since 6.10] R8SI One component, signed 8-bit. (8 bits total)
4561 \value [since 6.10] R32SI One component, signed 32-bit. (32 bits total)
4562 \value [since 6.10] RG32SI Two components, signed 32-bit. (64 bits total)
4563 \value [since 6.10] RGBA32SI Four components, signed 32-bit. (128 bits total)
4564 */
4565
4566// When adding new texture formats, update void tst_QRhi::textureFormats_data().
4567
4568/*!
4569 \struct QRhiTexture::NativeTexture
4570 \inmodule QtGuiPrivate
4571 \inheaderfile rhi/qrhi.h
4572 \brief Contains information about the underlying native resources of a texture.
4573 */
4574
4575/*!
4576 \variable QRhiTexture::NativeTexture::object
4577 \brief 64-bit integer containing the native object handle.
4578
4579 With OpenGL, the native handle is a GLuint value, so \c object can then be
4580 cast to a GLuint. With Vulkan, the native handle is a VkImage, so \c object
4581 can be cast to a VkImage. With Direct3D 11 and Metal \c object contains a
4582 ID3D11Texture2D or MTLTexture pointer, respectively. With Direct3D 12
4583 \c object contains a ID3D12Resource pointer.
4584 */
4585
4586/*!
4587 \variable QRhiTexture::NativeTexture::layout
4588 \brief Specifies the current image layout for APIs like Vulkan.
4589
4590 For Vulkan, \c layout contains a \c VkImageLayout value.
4591 */
4592
4593/*!
4594 \internal
4595 */
4596QRhiTexture::QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_, int depth_,
4597 int arraySize_, int sampleCount_, Flags flags_)
4598 : QRhiResource(rhi),
4599 m_format(format_), m_pixelSize(pixelSize_), m_depth(depth_),
4600 m_arraySize(arraySize_), m_sampleCount(sampleCount_), m_flags(flags_)
4601{
4602}
4603
4604/*!
4605 \return the resource type.
4606 */
4607QRhiResource::Type QRhiTexture::resourceType() const
4608{
4609 return Texture;
4610}
4611
4612/*!
4613 \fn virtual bool QRhiTexture::create() = 0
4614
4615 Creates the corresponding native graphics resources. If there are already
4616 resources present due to an earlier create() with no corresponding
4617 destroy(), then destroy() is called implicitly first.
4618
4619 \return \c true when successful, \c false when a graphics operation failed.
4620 Regardless of the return value, calling destroy() is always safe.
4621 */
4622
4623/*!
4624 \return the underlying native resources for this texture. The returned value
4625 will be empty if exposing the underlying native resources is not supported by
4626 the backend.
4627
4628 \sa createFrom()
4629 */
4630QRhiTexture::NativeTexture QRhiTexture::nativeTexture()
4631{
4632 return {};
4633}
4634
4635/*!
4636 Similar to create(), except that no new native textures are created.
4637 Instead, the native texture resources specified by \a src is used.
4638
4639 This allows importing an existing native texture object (which must belong
4640 to the same device or sharing context, depending on the graphics API) from
4641 an external graphics engine.
4642
4643 \return true if the specified existing native texture object has been
4644 successfully wrapped as a non-owning QRhiTexture.
4645
4646 \note format(), pixelSize(), sampleCount(), and flags() must still be set
4647 correctly. Passing incorrect sizes and other values to QRhi::newTexture()
4648 and then following it with a createFrom() expecting that the native texture
4649 object alone is sufficient to deduce such values is \b wrong and will lead
4650 to problems.
4651
4652 \note QRhiTexture does not take ownership of the texture object. destroy()
4653 does not free the object or any associated memory.
4654
4655 The opposite of this operation, exposing a QRhiTexture-created native
4656 texture object to a foreign engine, is possible via nativeTexture().
4657
4658 \note When importing a 3D texture, or a texture array object, or, with
4659 OpenGL ES, an external texture, it is then especially important to set the
4660 corresponding flags (ThreeDimensional, TextureArray, ExternalOES) via
4661 setFlags() before calling this function.
4662*/
4663bool QRhiTexture::createFrom(QRhiTexture::NativeTexture src)
4664{
4665 Q_UNUSED(src);
4666 return false;
4667}
4668
4669/*!
4670 With some graphics APIs, such as Vulkan, integrating custom rendering code
4671 that uses the graphics API directly needs special care when it comes to
4672 image layouts. This function allows communicating the expected \a layout the
4673 image backing the QRhiTexture is in after the native rendering commands.
4674
4675 For example, consider rendering into a QRhiTexture's VkImage directly with
4676 Vulkan in a code block enclosed by QRhiCommandBuffer::beginExternal() and
4677 QRhiCommandBuffer::endExternal(), followed by using the image for texture
4678 sampling in a QRhi-based render pass. To avoid potentially incorrect image
4679 layout transitions, this function can be used to indicate what the image
4680 layout will be once the commands recorded in said code block complete.
4681
4682 Calling this function makes sense only after
4683 QRhiCommandBuffer::endExternal() and before a subsequent
4684 QRhiCommandBuffer::beginPass().
4685
4686 This function has no effect with QRhi backends where the underlying
4687 graphics API does not expose a concept of image layouts.
4688
4689 \note With Vulkan \a layout is a \c VkImageLayout. With Direct 3D 12 \a
4690 layout is a value composed of the bits from \c D3D12_RESOURCE_STATES.
4691 */
4692void QRhiTexture::setNativeLayout(int layout)
4693{
4694 Q_UNUSED(layout);
4695}
4696
4697/*!
4698 \fn QRhiTexture::Format QRhiTexture::format() const
4699 \return the texture format.
4700 */
4701
4702/*!
4703 \fn void QRhiTexture::setFormat(QRhiTexture::Format fmt)
4704
4705 Sets the requested texture format to \a fmt.
4706
4707 \note The value set is only taken into account upon the next call to
4708 create(), i.e. when the underlying graphics resource are (re)created.
4709 Setting a new value is futile otherwise and must be avoided since it can
4710 lead to inconsistent state.
4711 */
4712
4713/*!
4714 \fn QSize QRhiTexture::pixelSize() const
4715 \return the size in pixels.
4716 */
4717
4718/*!
4719 \fn void QRhiTexture::setPixelSize(const QSize &sz)
4720
4721 Sets the texture size, specified in pixels, to \a sz.
4722
4723 \note The value set is only taken into account upon the next call to
4724 create(), i.e. when the underlying graphics resource are (re)created.
4725 Setting a new value is futile otherwise and must be avoided since it can
4726 lead to inconsistent state. The same applies to all other setters as well.
4727 */
4728
4729/*!
4730 \fn int QRhiTexture::depth() const
4731 \return the depth for 3D textures.
4732 */
4733
4734/*!
4735 \fn void QRhiTexture::setDepth(int depth)
4736 Sets the \a depth for a 3D texture.
4737 */
4738
4739/*!
4740 \fn int QRhiTexture::arraySize() const
4741 \return the texture array size.
4742 */
4743
4744/*!
4745 \fn void QRhiTexture::setArraySize(int arraySize)
4746 Sets the texture \a arraySize.
4747 */
4748
4749/*!
4750 \fn int QRhiTexture::arrayRangeStart() const
4751
4752 \return the first array layer when setArrayRange() was called.
4753
4754 \sa setArrayRange()
4755 */
4756
4757/*!
4758 \fn int QRhiTexture::arrayRangeLength() const
4759
4760 \return the exposed array range size when setArrayRange() was called.
4761
4762 \sa setArrayRange()
4763*/
4764
4765/*!
4766 \fn void QRhiTexture::setArrayRange(int startIndex, int count)
4767
4768 Normally all array layers are exposed and it is up to the shader to select
4769 the layer via the third coordinate passed to the \c{texture()} GLSL
4770 function when sampling the \c sampler2DArray. When QRhi::TextureArrayRange
4771 is reported as supported, calling setArrayRange() before create() or
4772 createFrom() requests selecting only the specified range, \a count elements
4773 starting from \a startIndex. The shader logic can then be written with this
4774 in mind.
4775
4776 \sa QRhi::TextureArrayRange
4777 */
4778
4779/*!
4780 \fn Flags QRhiTexture::flags() const
4781 \return the texture flags.
4782 */
4783
4784/*!
4785 \fn void QRhiTexture::setFlags(Flags f)
4786 Sets the texture flags to \a f.
4787 */
4788
4789/*!
4790 \fn int QRhiTexture::sampleCount() const
4791 \return the sample count. 1 means no multisample antialiasing.
4792 */
4793
4794/*!
4795 \fn void QRhiTexture::setSampleCount(int s)
4796 Sets the sample count to \a s.
4797 */
4798
4799/*!
4800 \struct QRhiTexture::ViewFormat
4801 \inmodule QtGuiPrivate
4802 \inheaderfile rhi/qrhi.h
4803 \since 6.8
4804 \brief Specifies the view format for reading or writing from or to the texture.
4805
4806 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4807 for details.
4808 */
4809
4810/*!
4811 \variable QRhiTexture::ViewFormat::format
4812 */
4813
4814/*!
4815 \variable QRhiTexture::ViewFormat::srgb
4816 */
4817
4818/*!
4819 \fn QRhiTexture::ViewFormat QRhiTexture::readViewFormat() const
4820 \since 6.8
4821 \return the view format used when sampling the texture. When not called, the view
4822 format is assumed to be the same as format().
4823 */
4824
4825/*!
4826 \fn void QRhiTexture::setReadViewFormat(const ViewFormat &fmt)
4827 \since 6.8
4828
4829 Sets the shader resource view format (or the format of the view used for
4830 sampling the texture) to \a fmt. By default the same format (and sRGB-ness)
4831 is used as the texture itself, and in most cases this function does not need
4832 to be called.
4833
4834 This setting is only taken into account when the \l QRhi::TextureViewFormat
4835 feature is reported as supported.
4836
4837 \note This functionality is provided to allow "casting" between
4838 non-sRGB and sRGB in order to get the shader reads perform, or not perform,
4839 the implicit sRGB conversions. Other types of casting may or may not be
4840 functional.
4841 */
4842
4843/*!
4844 \fn QRhiTexture::ViewFormat QRhiTexture::writeViewFormat() const
4845 \since 6.8
4846 \return the view format used when writing to the texture and when using it
4847 with image load/store. When not called, the view format is assumed to be the
4848 same as format().
4849 */
4850
4851/*!
4852 \fn void QRhiTexture::setWriteViewFormat(const ViewFormat &fmt)
4853 \since 6.8
4854
4855 Sets the render target view format to \a fmt. By default the same format
4856 (and sRGB-ness) is used as the texture itself, and in most cases this
4857 function does not need to be called.
4858
4859 One common use case for providing a write view format is working with
4860 externally provided textures that, outside of our control, use an sRGB
4861 format with 3D APIs such as Vulkan or Direct 3D, but the rendering engine is
4862 already prepared to handle linearization and conversion to sRGB at the end
4863 of its shading pipeline. In this case what is wanted when rendering into
4864 such a texture is a render target view (e.g. VkImageView) that has the same,
4865 but non-sRGB format. (if e.g. from an OpenXR implementation one gets a
4866 VK_FORMAT_R8G8B8A8_SRGB texture, it is likely that rendering into it should
4867 be done using a VK_FORMAT_R8G8B8A8_UNORM view, if that is what the rendering
4868 engine's pipeline requires; in this example one would call this function
4869 with a ViewFormat that has a format of QRhiTexture::RGBA8 and \c srgb set to
4870 \c false).
4871
4872 This setting is only taken into account when the \l QRhi::TextureViewFormat
4873 feature is reported as supported.
4874
4875 \note This functionality is provided to allow "casting" between
4876 non-sRGB and sRGB in order to get the shader write not perform, or perform,
4877 the implicit sRGB conversions. Other types of casting may or may not be
4878 functional.
4879 */
4880
4881/*!
4882 \class QRhiSampler
4883 \inmodule QtGuiPrivate
4884 \inheaderfile rhi/qrhi.h
4885 \since 6.6
4886 \brief Sampler resource.
4887
4888 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
4889 for details.
4890 */
4891
4892/*!
4893 \enum QRhiSampler::Filter
4894 Specifies the minification, magnification, or mipmap filtering
4895
4896 \value None Applicable only for mipmapMode(), indicates no mipmaps to be used
4897 \value Nearest
4898 \value Linear
4899 */
4900
4901/*!
4902 \enum QRhiSampler::AddressMode
4903 Specifies the addressing mode
4904
4905 \value Repeat
4906 \value ClampToEdge
4907 \value Mirror
4908 */
4909
4910/*!
4911 \enum QRhiSampler::CompareOp
4912 Specifies the texture comparison function.
4913
4914 \value Never (default)
4915 \value Less
4916 \value Equal
4917 \value LessOrEqual
4918 \value Greater
4919 \value NotEqual
4920 \value GreaterOrEqual
4921 \value Always
4922 */
4923
4924/*!
4925 \internal
4926 */
4927QRhiSampler::QRhiSampler(QRhiImplementation *rhi,
4928 Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
4929 AddressMode u_, AddressMode v_, AddressMode w_)
4930 : QRhiResource(rhi),
4931 m_magFilter(magFilter_), m_minFilter(minFilter_), m_mipmapMode(mipmapMode_),
4932 m_addressU(u_), m_addressV(v_), m_addressW(w_),
4933 m_compareOp(QRhiSampler::Never)
4934{
4935}
4936
4937/*!
4938 \return the resource type.
4939 */
4940QRhiResource::Type QRhiSampler::resourceType() const
4941{
4942 return Sampler;
4943}
4944
4945/*!
4946 \fn QRhiSampler::Filter QRhiSampler::magFilter() const
4947 \return the magnification filter mode.
4948 */
4949
4950/*!
4951 \fn void QRhiSampler::setMagFilter(Filter f)
4952 Sets the magnification filter mode to \a f.
4953 */
4954
4955/*!
4956 \fn QRhiSampler::Filter QRhiSampler::minFilter() const
4957 \return the minification filter mode.
4958 */
4959
4960/*!
4961 \fn void QRhiSampler::setMinFilter(Filter f)
4962 Sets the minification filter mode to \a f.
4963 */
4964
4965/*!
4966 \fn QRhiSampler::Filter QRhiSampler::mipmapMode() const
4967 \return the mipmap filter mode.
4968 */
4969
4970/*!
4971 \fn void QRhiSampler::setMipmapMode(Filter f)
4972
4973 Sets the mipmap filter mode to \a f.
4974
4975 Leave this set to None when the texture has no mip levels, or when the mip
4976 levels are not to be taken into account.
4977 */
4978
4979/*!
4980 \fn QRhiSampler::AddressMode QRhiSampler::addressU() const
4981 \return the horizontal wrap mode.
4982 */
4983
4984/*!
4985 \fn void QRhiSampler::setAddressU(AddressMode mode)
4986 Sets the horizontal wrap \a mode.
4987 */
4988
4989/*!
4990 \fn QRhiSampler::AddressMode QRhiSampler::addressV() const
4991 \return the vertical wrap mode.
4992 */
4993
4994/*!
4995 \fn void QRhiSampler::setAddressV(AddressMode mode)
4996 Sets the vertical wrap \a mode.
4997 */
4998
4999/*!
5000 \fn QRhiSampler::AddressMode QRhiSampler::addressW() const
5001 \return the depth wrap mode.
5002 */
5003
5004/*!
5005 \fn void QRhiSampler::setAddressW(AddressMode mode)
5006 Sets the depth wrap \a mode.
5007 */
5008
5009/*!
5010 \fn QRhiSampler::CompareOp QRhiSampler::textureCompareOp() const
5011 \return the texture comparison function.
5012 */
5013
5014/*!
5015 \fn void QRhiSampler::setTextureCompareOp(CompareOp op)
5016 Sets the texture comparison function \a op.
5017 */
5018
5019/*!
5020 \class QRhiShadingRateMap
5021 \inmodule QtGuiPrivate
5022 \inheaderfile rhi/qrhi.h
5023 \since 6.9
5024 \brief An object that wraps a texture or another kind of native 3D API object.
5025
5026 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5027 for details.
5028
5029 For an introduction to Variable Rate Shading (VRS), see
5030 \l{https://learn.microsoft.com/en-us/windows/win32/direct3d12/vrs}. Qt
5031 supports a subset of the VRS features offered by Direct 3D 12 and Vulkan. In
5032 addition, Metal's somewhat different mechanism is supported by making it
5033 possible to set up a QRhiShadingRateMap with an existing
5034 MTLRasterizationRateMap object.
5035 */
5036
5037/*!
5038 \struct QRhiShadingRateMap::NativeShadingRateMap
5039 \inmodule QtGuiPrivate
5040 \inheaderfile rhi/qrhi.h
5041 \since 6.9
5042 \brief Wraps a native shading rate map.
5043
5044 An example is MTLRasterizationRateMap with Metal. Other 3D APIs that use
5045 textures for image-based VRS do not use this struct since those can function
5046 via the QRhiTexture-based overload of QRhiShadingRate::createFrom().
5047 */
5048
5049/*!
5050 \variable QRhiShadingRateMap::NativeShadingRateMap::object
5051 \brief 64-bit integer containing the native object handle.
5052
5053 Used with QRhiShadingRateMap::createFrom(). For example, with Metal,
5054 \c object is expected to be an id<MTLRasterizationRateMap>.
5055 */
5056
5057/*!
5058 \internal
5059 */
5060QRhiShadingRateMap::QRhiShadingRateMap(QRhiImplementation *rhi)
5061 : QRhiResource(rhi)
5062{
5063}
5064
5065/*!
5066 \return the resource type.
5067 */
5068QRhiResource::Type QRhiShadingRateMap::resourceType() const
5069{
5070 return ShadingRateMap;
5071}
5072
5073/*!
5074 Sets up the shading rate map to use a native 3D API shading rate object
5075 \a src.
5076
5077 \return \c true when successful, \c false when not supported.
5078
5079 \note This is functional only when the QRhi::VariableRateShadingMap feature
5080 is reported as supported, while QRhi::VariableShadingRateMapWithTexture
5081 feature is not. Currently this is true for Metal, assuming variable rate
5082 shading is supported by the GPU.
5083
5084 \note With Metal, the \c object field of \a src is expected to contain an
5085 id<MTLRasterizationRateMap>. Note that Qt does not perform anything else
5086 apart from passing the MTLRasterizationRateMap on to the
5087 MTLRenderPassDescriptor. If any special scaling is required, it is up to the
5088 application (or the XR compositor) to perform that.
5089 */
5090bool QRhiShadingRateMap::createFrom(NativeShadingRateMap src)
5091{
5092 Q_UNUSED(src);
5093 return false;
5094}
5095
5096/*!
5097 Sets up the shading rate map to use the texture \a src as the
5098 image containing the per-tile shading rates.
5099
5100 \return \c true when successful, \c false when not supported.
5101
5102 The QRhiShadingRateMap does not take ownership of \a src.
5103
5104 \note This is functional only when the
5105 QRhi::VariableRateShadingMapWithTexture feature is reported as supported. In
5106 practice may be supported on Vulkan and Direct 3D 12 when using modern
5107 graphics cards. It will never be supported on OpenGL or Metal, for example.
5108
5109 \note \a src must have a format of QRhiTexture::R8UI.
5110
5111 \note \a src must have a width of \c{ceil(render_target_pixel_width /
5112 (float)tile_width)} and a height of \c{ceil(render_target_pixel_height /
5113 (float)tile_height)}. It is up to the application to ensure the size of the
5114 texture is as expected, using the above formula, at all times. The tile size
5115 can be queried via \l QRhi::resourceLimit() and
5116 QRhi::ShadingRateImageTileSize.
5117
5118 Each byte (texel) in the texture corresponds to the shading rate value for
5119 one tile. 0 indicates 1x1, while a value of 10 indicates 4x4. See
5120 \l{https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_shading_rate}{D3D12_SHADING_RATE}
5121 for other possible values.
5122 */
5123bool QRhiShadingRateMap::createFrom(QRhiTexture *src)
5124{
5125 Q_UNUSED(src);
5126 return false;
5127}
5128
5129/*!
5130 \class QRhiRenderPassDescriptor
5131 \inmodule QtGuiPrivate
5132 \inheaderfile rhi/qrhi.h
5133 \since 6.6
5134 \brief Render pass resource.
5135
5136 A render pass, if such a concept exists in the underlying graphics API, is
5137 a collection of attachments (color, depth, stencil) and describes how those
5138 attachments are used.
5139
5140 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5141 for details.
5142 */
5143
5144/*!
5145 \internal
5146 */
5147QRhiRenderPassDescriptor::QRhiRenderPassDescriptor(QRhiImplementation *rhi)
5148 : QRhiResource(rhi)
5149{
5150}
5151
5152/*!
5153 \return the resource type.
5154 */
5155QRhiResource::Type QRhiRenderPassDescriptor::resourceType() const
5156{
5157 return RenderPassDescriptor;
5158}
5159
5160/*!
5161 \fn virtual bool QRhiRenderPassDescriptor::isCompatible(const QRhiRenderPassDescriptor *other) const = 0
5162
5163 \return true if the \a other QRhiRenderPassDescriptor is compatible with
5164 this one, meaning \c this and \a other can be used interchangebly in
5165 QRhiGraphicsPipeline::setRenderPassDescriptor().
5166
5167 The concept of the compatibility of renderpass descriptors is similar to
5168 the \l{QRhiShaderResourceBindings::isLayoutCompatible}{layout
5169 compatibility} of QRhiShaderResourceBindings instances. They allow better
5170 reuse of QRhiGraphicsPipeline instances: for example, a
5171 QRhiGraphicsPipeline instance cache is expected to use these functions to
5172 look for a matching pipeline, instead of just comparing pointers, thus
5173 allowing a different QRhiRenderPassDescriptor and
5174 QRhiShaderResourceBindings to be used in combination with the pipeline, as
5175 long as they are compatible.
5176
5177 The exact details of compatibility depend on the underlying graphics API.
5178 Two renderpass descriptors
5179 \l{QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor()}{created}
5180 from the same QRhiTextureRenderTarget are always compatible.
5181
5182 Similarly to QRhiShaderResourceBindings, compatibility can also be tested
5183 without having two existing objects available. Extracting the opaque blob by
5184 calling serializedFormat() allows testing for compatibility by comparing the
5185 returned vector to another QRhiRenderPassDescriptor's
5186 serializedFormat(). This has benefits in certain situations, because it
5187 allows testing the compatibility of a QRhiRenderPassDescriptor with a
5188 QRhiGraphicsPipeline even when the QRhiRenderPassDescriptor the pipeline was
5189 originally built was is no longer available (but the data returned from its
5190 serializedFormat() still is).
5191
5192 \sa newCompatibleRenderPassDescriptor(), serializedFormat()
5193 */
5194
5195/*!
5196 \fn virtual QRhiRenderPassDescriptor *QRhiRenderPassDescriptor::newCompatibleRenderPassDescriptor() const = 0
5197
5198 \return a new QRhiRenderPassDescriptor that is
5199 \l{isCompatible()}{compatible} with this one.
5200
5201 This function allows cloning a QRhiRenderPassDescriptor. The returned
5202 object is ready to be used, and the ownership is transferred to the caller.
5203 Cloning a QRhiRenderPassDescriptor object can become useful in situations
5204 where the object is stored in data structures related to graphics pipelines
5205 (in order to allow creating new pipelines which in turn requires a
5206 renderpass descriptor object), and the lifetime of the renderpass
5207 descriptor created from a render target may be shorter than the pipelines.
5208 (for example, because the engine manages and destroys renderpasses together
5209 with the textures and render targets it was created from) In such a
5210 situation, it can be beneficial to store a cloned version in the data
5211 structures, and thus transferring ownership as well.
5212
5213 \sa isCompatible()
5214 */
5215
5216/*!
5217 \fn virtual QVector<quint32> QRhiRenderPassDescriptor::serializedFormat() const = 0
5218
5219 \return a vector of integers containing an opaque blob describing the data
5220 relevant for \l{isCompatible()}{compatibility}.
5221
5222 Given two QRhiRenderPassDescriptor objects \c rp1 and \c rp2, if the data
5223 returned from this function is identical, then \c{rp1->isCompatible(rp2)},
5224 and vice versa hold true as well.
5225
5226 \note The returned data is meant to be used for storing in memory and
5227 comparisons during the lifetime of the QRhi the object belongs to. It is not
5228 meant for storing on disk, reusing between processes, or using with multiple
5229 QRhi instances with potentially different backends.
5230
5231 \note Calling this function is expected to be a cheap operation since the
5232 backends are not supposed to calculate the data in this function, but rather
5233 return an already calculated series of data.
5234
5235 When creating reusable components as part of a library, where graphics
5236 pipelines are created and maintained while targeting a QRhiRenderTarget (be
5237 it a swapchain or a texture) managed by the client of the library, the
5238 components must be able to deal with a changing QRhiRenderPassDescriptor.
5239 For example, because the render target changes and so invalidates the
5240 previously QRhiRenderPassDescriptor (with regards to the new render target
5241 at least) due to having a potentially different color format and attachments
5242 now. Or because \l{QRhiShadingRateMap}{variable rate shading} is taken into
5243 use dynamically. A simple pattern that helps dealing with this is performing
5244 the following check on every frame, to recognize the case when the pipeline
5245 needs to be associated with a new QRhiRenderPassDescriptor, because
5246 something is different about the render target now, compared to earlier
5247 frames:
5248
5249 \code
5250 QRhiRenderPassDescriptor *rp = m_renderTarget->renderPassDescriptor();
5251 if (m_pipeline && rp->serializedFormat() != m_renderPassFormat) {
5252 m_pipeline->setRenderPassDescriptor(rp);
5253 m_renderPassFormat = rp->serializedFormat();
5254 m_pipeline->create();
5255 }
5256 // remember to store m_renderPassFormat also when creating m_pipeline the first time
5257 \endcode
5258
5259 \sa isCompatible()
5260 */
5261
5262/*!
5263 \return a pointer to a backend-specific QRhiNativeHandles subclass, such as
5264 QRhiVulkanRenderPassNativeHandles. The returned value is \nullptr when exposing
5265 the underlying native resources is not supported by the backend.
5266
5267 \sa QRhiVulkanRenderPassNativeHandles
5268 */
5269const QRhiNativeHandles *QRhiRenderPassDescriptor::nativeHandles()
5270{
5271 return nullptr;
5272}
5273
5274/*!
5275 \class QRhiRenderTarget
5276 \inmodule QtGuiPrivate
5277 \inheaderfile rhi/qrhi.h
5278 \since 6.6
5279 \brief Represents an onscreen (swapchain) or offscreen (texture) render target.
5280
5281 Applications do not create an instance of this class directly. Rather, it
5282 is the subclass QRhiTextureRenderTarget that is instantiable by clients of
5283 the API via \l{QRhi::newTextureRenderTarget()}{newTextureRenderTarget()}.
5284 The other subclass is QRhiSwapChainRenderTarget, which is the type
5285 QRhiSwapChain returns when calling
5286 \l{QRhiSwapChain::currentFrameRenderTarget()}{currentFrameRenderTarget()}.
5287
5288 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5289 for details.
5290
5291 \sa QRhiSwapChainRenderTarget, QRhiTextureRenderTarget
5292 */
5293
5294/*!
5295 \internal
5296 */
5297QRhiRenderTarget::QRhiRenderTarget(QRhiImplementation *rhi)
5298 : QRhiResource(rhi)
5299{
5300}
5301
5302/*!
5303 \fn virtual QSize QRhiRenderTarget::pixelSize() const = 0
5304
5305 \return the size in pixels.
5306
5307 Valid only after create() has been called successfully. Until then the
5308 result is a default-constructed QSize.
5309
5310 With QRhiTextureRenderTarget the returned size is the size of the
5311 associated attachments at the time of create(), in practice the size of the
5312 first color attachment, or the depth/stencil buffer if there are no color
5313 attachments. If the associated textures or renderbuffers are resized and
5314 rebuilt afterwards, then pixelSize() performs an implicit call to create()
5315 in order to rebuild the underlying data structures. This implicit check is
5316 similar to what QRhiCommandBuffer::beginPass() does, and ensures that the
5317 returned size is always up-to-date.
5318 */
5319
5320/*!
5321 \fn virtual float QRhiRenderTarget::devicePixelRatio() const = 0
5322
5323 \return the device pixel ratio. For QRhiTextureRenderTarget this is always
5324 1. For targets retrieved from a QRhiSwapChain the value reflects the
5325 \l{QWindow::devicePixelRatio()}{device pixel ratio} of the targeted
5326 QWindow.
5327 */
5328
5329/*!
5330 \fn virtual int QRhiRenderTarget::sampleCount() const = 0
5331
5332 \return the sample count or 1 if multisample antialiasing is not relevant for
5333 this render target.
5334 */
5335
5336/*!
5337 \fn QRhiRenderPassDescriptor *QRhiRenderTarget::renderPassDescriptor() const
5338
5339 \return the associated QRhiRenderPassDescriptor.
5340 */
5341
5342/*!
5343 \fn void QRhiRenderTarget::setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
5344
5345 Sets the QRhiRenderPassDescriptor \a desc for use with this render target.
5346 */
5347
5348/*!
5349 \internal
5350 */
5351QRhiSwapChainRenderTarget::QRhiSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain_)
5352 : QRhiRenderTarget(rhi),
5353 m_swapchain(swapchain_)
5354{
5355}
5356
5357/*!
5358 \class QRhiSwapChainRenderTarget
5359 \inmodule QtGuiPrivate
5360 \inheaderfile rhi/qrhi.h
5361 \since 6.6
5362 \brief Swapchain render target resource.
5363
5364 When targeting the color buffers of a swapchain, active render target is a
5365 QRhiSwapChainRenderTarget. This is what
5366 QRhiSwapChain::currentFrameRenderTarget() returns.
5367
5368 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5369 for details.
5370
5371 \sa QRhiSwapChain
5372 */
5373
5374/*!
5375 \return the resource type.
5376 */
5377QRhiResource::Type QRhiSwapChainRenderTarget::resourceType() const
5378{
5379 return SwapChainRenderTarget;
5380}
5381
5382/*!
5383 \fn QRhiSwapChain *QRhiSwapChainRenderTarget::swapChain() const
5384
5385 \return the swapchain object.
5386 */
5387
5388/*!
5389 \class QRhiTextureRenderTarget
5390 \inmodule QtGuiPrivate
5391 \inheaderfile rhi/qrhi.h
5392 \since 6.6
5393 \brief Texture render target resource.
5394
5395 A texture render target allows rendering into one or more textures,
5396 optionally with a depth texture or depth/stencil renderbuffer.
5397
5398 For multisample rendering the common approach is to use a renderbuffer as
5399 the color attachment and set the non-multisample destination texture as the
5400 \c{resolve texture}. For more information, read the detailed description of
5401 the \l QRhiColorAttachment class.
5402
5403 \note Textures used in combination with QRhiTextureRenderTarget must be
5404 created with the QRhiTexture::RenderTarget flag.
5405
5406 The simplest example of creating a render target with a texture as its
5407 single color attachment:
5408
5409 \code
5410 QRhiTexture *texture = rhi->newTexture(QRhiTexture::RGBA8, size, 1, QRhiTexture::RenderTarget);
5411 texture->create();
5412 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ texture });
5413 rp = rt->newCompatibleRenderPassDescriptor();
5414 rt->setRenderPassDescriptor(rp);
5415 rt->create();
5416 // rt can now be used with beginPass()
5417 \endcode
5418
5419 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5420 for details.
5421 */
5422
5423/*!
5424 \enum QRhiTextureRenderTarget::Flag
5425
5426 Flag values describing the load/store behavior for the render target. The
5427 load/store behavior may be baked into native resources under the hood,
5428 depending on the backend, and therefore it needs to be known upfront and
5429 cannot be changed without rebuilding (and so releasing and creating new
5430 native resources).
5431
5432 \value PreserveColorContents Indicates that the contents of the color
5433 attachments is to be loaded when starting a render pass, instead of
5434 clearing. This is potentially more expensive, especially on mobile (tiled)
5435 GPUs, but allows preserving the existing contents between passes. When doing
5436 multisample rendering with a resolve texture set, setting this flag also
5437 requests the multisample color data to be stored (written out) to the
5438 multisample texture or render buffer. (for non-multisample rendering the
5439 color data is always stored, but for MSAA storing the multisample data
5440 decreases efficiency for certain GPU architectures, hence defaulting to not
5441 writing it out) Note however that this is non-portable: in some cases there
5442 is no intermediate multisample texture on the graphics API level, e.g. when
5443 using OpenGL ES's \c{GL_EXT_multisampled_render_to_texture} as it is all
5444 implicit, handled by the OpenGL ES implementation. In that case,
5445 PreserveColorContents will likely have no effect. Therefore, avoid relying
5446 on this flag when using multisample rendering and the color attachment is
5447 using a multisample QRhiTexture (not QRhiRenderBuffer).
5448
5449 \value PreserveDepthStencilContents Indicates that the contents of the
5450 depth texture is to be loaded when starting a render pass, instead
5451 clearing. Only applicable when a texture is used as the depth buffer
5452 (QRhiTextureRenderTargetDescription::depthTexture() is set) because
5453 depth/stencil renderbuffers may not have any physical backing and data may
5454 not be written out in the first place.
5455
5456 \value DoNotStoreDepthStencilContents Indicates that the contents of the
5457 depth texture does not need to be written out. Relevant only when a
5458 QRhiTexture, not QRhiRenderBuffer, is used as the depth-stencil buffer,
5459 because for QRhiRenderBuffer this is implicit. When a depthResolveTexture is
5460 set, the flag is not relevant, because the behavior is then as if the flag
5461 was set. This enum value is introduced in Qt 6.8.
5462 */
5463
5464/*!
5465 \internal
5466 */
5467QRhiTextureRenderTarget::QRhiTextureRenderTarget(QRhiImplementation *rhi,
5468 const QRhiTextureRenderTargetDescription &desc_,
5469 Flags flags_)
5470 : QRhiRenderTarget(rhi),
5471 m_desc(desc_),
5472 m_flags(flags_)
5473{
5474}
5475
5476/*!
5477 \return the resource type.
5478 */
5479QRhiResource::Type QRhiTextureRenderTarget::resourceType() const
5480{
5481 return TextureRenderTarget;
5482}
5483
5484/*!
5485 \fn virtual QRhiRenderPassDescriptor *QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor() = 0
5486
5487 \return a new QRhiRenderPassDescriptor that is compatible with this render
5488 target.
5489
5490 The returned value is used in two ways: it can be passed to
5491 setRenderPassDescriptor() and
5492 QRhiGraphicsPipeline::setRenderPassDescriptor(). A render pass descriptor
5493 describes the attachments (color, depth/stencil) and the load/store
5494 behavior that can be affected by flags(). A QRhiGraphicsPipeline can only
5495 be used in combination with a render target that has a
5496 \l{QRhiRenderPassDescriptor::isCompatible()}{compatible}
5497 QRhiRenderPassDescriptor set.
5498
5499 Two QRhiTextureRenderTarget instances can share the same render pass
5500 descriptor as long as they have the same number and type of attachments.
5501 The associated QRhiTexture or QRhiRenderBuffer instances are not part of
5502 the render pass descriptor so those can differ in the two
5503 QRhiTextureRenderTarget instances.
5504
5505 \note resources, such as QRhiTexture instances, referenced in description()
5506 must already have create() called on them.
5507
5508 \sa create()
5509 */
5510
5511/*!
5512 \fn virtual bool QRhiTextureRenderTarget::create() = 0
5513
5514 Creates the corresponding native graphics resources. If there are already
5515 resources present due to an earlier create() with no corresponding
5516 destroy(), then destroy() is called implicitly first.
5517
5518 \note renderPassDescriptor() must be set before calling create(). To obtain
5519 a QRhiRenderPassDescriptor compatible with the render target, call
5520 newCompatibleRenderPassDescriptor() before create() but after setting all
5521 other parameters, such as description() and flags(). To save resources,
5522 reuse the same QRhiRenderPassDescriptor with multiple
5523 QRhiTextureRenderTarget instances, whenever possible. Sharing the same
5524 render pass descriptor is only possible when the render targets have the
5525 same number and type of attachments (the actual textures can differ) and
5526 the same flags.
5527
5528 \note resources, such as QRhiTexture instances, referenced in description()
5529 must already have create() called on them.
5530
5531 \return \c true when successful, \c false when a graphics operation failed.
5532 Regardless of the return value, calling destroy() is always safe.
5533 */
5534
5535/*!
5536 \fn QRhiTextureRenderTargetDescription QRhiTextureRenderTarget::description() const
5537 \return the render target description.
5538 */
5539
5540/*!
5541 \fn void QRhiTextureRenderTarget::setDescription(const QRhiTextureRenderTargetDescription &desc)
5542 Sets the render target description \a desc.
5543 */
5544
5545/*!
5546 \fn QRhiTextureRenderTarget::Flags QRhiTextureRenderTarget::flags() const
5547 \return the currently set flags.
5548 */
5549
5550/*!
5551 \fn void QRhiTextureRenderTarget::setFlags(Flags f)
5552 Sets the flags to \a f.
5553 */
5554
5555/*!
5556 \class QRhiShaderResourceBindings
5557 \inmodule QtGuiPrivate
5558 \inheaderfile rhi/qrhi.h
5559 \since 6.6
5560 \brief Encapsulates resources for making buffer, texture, sampler resources visible to shaders.
5561
5562 A QRhiShaderResourceBindings is a collection of QRhiShaderResourceBinding
5563 objects, each of which describe a single binding.
5564
5565 Take a fragment shader with the following interface:
5566
5567 \badcode
5568 layout(std140, binding = 0) uniform buf {
5569 mat4 mvp;
5570 int flip;
5571 } ubuf;
5572
5573 layout(binding = 1) uniform sampler2D tex;
5574 \endcode
5575
5576 To make resources visible to the shader, the following
5577 QRhiShaderResourceBindings could be created and then passed to
5578 QRhiGraphicsPipeline::setShaderResourceBindings():
5579
5580 \code
5581 QRhiShaderResourceBindings *srb = rhi->newShaderResourceBindings();
5582 srb->setBindings({
5583 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, ubuf),
5584 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, texture, sampler)
5585 });
5586 srb->create();
5587 // ...
5588 QRhiGraphicsPipeline *ps = rhi->newGraphicsPipeline();
5589 // ...
5590 ps->setShaderResourceBindings(srb);
5591 ps->create();
5592 // ...
5593 cb->setGraphicsPipeline(ps);
5594 cb->setShaderResources(); // binds srb
5595 \endcode
5596
5597 This assumes that \c ubuf is a QRhiBuffer, \c texture is a QRhiTexture,
5598 while \a sampler is a QRhiSampler. The example also assumes that the
5599 uniform block is present in the vertex shader as well so the same buffer is
5600 made visible to the vertex stage too.
5601
5602 \section3 Advanced usage
5603
5604 Building on the above example, let's assume that a pass now needs to use
5605 the exact same pipeline and shaders with a different texture. Creating a
5606 whole separate QRhiGraphicsPipeline just for this would be an overkill.
5607 This is why QRhiCommandBuffer::setShaderResources() allows specifying a \a
5608 srb argument. As long as the layouts (so the number of bindings and the
5609 binding points) match between two QRhiShaderResourceBindings, they can both
5610 be used with the same pipeline, assuming the pipeline was created with one of
5611 them in the first place. See isLayoutCompatible() for more details.
5612
5613 \code
5614 QRhiShaderResourceBindings *srb2 = rhi->newShaderResourceBindings();
5615 // ...
5616 cb->setGraphicsPipeline(ps);
5617 cb->setShaderResources(srb2); // binds srb2
5618 \endcode
5619
5620 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5621 for details.
5622 */
5623
5624/*!
5625 \typedef QRhiShaderResourceBindingSet
5626 \relates QRhi
5627 \since 6.7
5628
5629 Synonym for QRhiShaderResourceBindings.
5630*/
5631
5632/*!
5633 \internal
5634 */
5635QRhiShaderResourceBindings::QRhiShaderResourceBindings(QRhiImplementation *rhi)
5636 : QRhiResource(rhi)
5637{
5638 m_layoutDesc.reserve(BINDING_PREALLOC * QRhiShaderResourceBinding::LAYOUT_DESC_ENTRIES_PER_BINDING);
5639}
5640
5641/*!
5642 \return the resource type.
5643 */
5644QRhiResource::Type QRhiShaderResourceBindings::resourceType() const
5645{
5646 return ShaderResourceBindings;
5647}
5648
5649/*!
5650 \return \c true if the layout is compatible with \a other. The layout does
5651 not include the actual resource (such as, buffer or texture) and related
5652 parameters (such as, offset or size). It does include the binding point,
5653 pipeline stage, and resource type, however. The number and order of the
5654 bindings must also match in order to be compatible.
5655
5656 When there is a QRhiGraphicsPipeline created with this
5657 QRhiShaderResourceBindings, and the function returns \c true, \a other can
5658 then safely be passed to QRhiCommandBuffer::setShaderResources(), and so
5659 be used with the pipeline in place of this QRhiShaderResourceBindings.
5660
5661 \note This function must only be called after a successful create(), because
5662 it relies on data generated during the baking of the underlying data
5663 structures. This way the function can implement a comparison approach that
5664 is more efficient than iterating through two binding lists and calling
5665 QRhiShaderResourceBinding::isLayoutCompatible() on each pair. This becomes
5666 relevant especially when this function is called at a high frequency.
5667
5668 \sa serializedLayoutDescription()
5669 */
5670bool QRhiShaderResourceBindings::isLayoutCompatible(const QRhiShaderResourceBindings *other) const
5671{
5672 if (other == this)
5673 return true;
5674
5675 if (!other)
5676 return false;
5677
5678 // This can become a hot code path. Therefore we do not iterate and call
5679 // isLayoutCompatible() on m_bindings, but rather check a pre-calculated
5680 // hash code and then, if the hash matched, do a uint array comparison
5681 // (that's still more cache friendly).
5682
5683 return m_layoutDescHash == other->m_layoutDescHash
5684 && m_layoutDesc == other->m_layoutDesc;
5685}
5686
5687/*!
5688 \fn QVector<quint32> QRhiShaderResourceBindings::serializedLayoutDescription() const
5689
5690 \return a vector of integers containing an opaque blob describing the layout
5691 of the binding list, i.e. the data relevant for
5692 \l{isLayoutCompatible()}{layout compatibility tests}.
5693
5694 Given two objects \c srb1 and \c srb2, if the data returned from this
5695 function is identical, then \c{srb1->isLayoutCompatible(srb2)}, and vice
5696 versa hold true as well.
5697
5698 \note The returned data is meant to be used for storing in memory and
5699 comparisons during the lifetime of the QRhi the object belongs to. It is not
5700 meant for storing on disk, reusing between processes, or using with multiple
5701 QRhi instances with potentially different backends.
5702
5703 \sa isLayoutCompatible()
5704 */
5705
5706void QRhiImplementation::updateLayoutDesc(QRhiShaderResourceBindings *srb)
5707{
5708 srb->m_layoutDescHash = 0;
5709 srb->m_layoutDesc.clear();
5710 auto layoutDescAppender = std::back_inserter(srb->m_layoutDesc);
5711 for (const QRhiShaderResourceBinding &b : std::as_const(srb->m_bindings)) {
5712 const QRhiShaderResourceBinding::Data *d = &b.d;
5713 srb->m_layoutDescHash ^= uint(d->binding) ^ uint(d->stage) ^ uint(d->type)
5714 ^ uint(d->arraySize());
5715 layoutDescAppender = d->serialize(layoutDescAppender);
5716 }
5717}
5718
5719/*!
5720 \fn virtual bool QRhiShaderResourceBindings::create() = 0
5721
5722 Creates the corresponding resource binding set. Depending on the underlying
5723 graphics API, this may involve creating native graphics resources, and
5724 therefore it should not be assumed that this is a cheap operation.
5725
5726 If create() has been called before with no corresponding destroy(), then
5727 destroy() is called implicitly first.
5728
5729 \return \c true when successful, \c false when failed.
5730 Regardless of the return value, calling destroy() is always safe.
5731 */
5732
5733/*!
5734 \fn void QRhiShaderResourceBindings::setBindings(std::initializer_list<QRhiShaderResourceBinding> list)
5735 Sets the \a list of bindings.
5736 */
5737
5738/*!
5739 \fn template<typename InputIterator> void QRhiShaderResourceBindings::setBindings(InputIterator first, InputIterator last)
5740 Sets the list of bindings from the iterators \a first and \a last.
5741 */
5742
5743/*!
5744 \fn const QRhiShaderResourceBinding *QRhiShaderResourceBindings::cbeginBindings() const
5745 \return a const iterator pointing to the first item in the binding list.
5746 */
5747
5748/*!
5749 \fn const QRhiShaderResourceBinding *QRhiShaderResourceBindings::cendBindings() const
5750 \return a const iterator pointing just after the last item in the binding list.
5751 */
5752
5753/*!
5754 \fn const QRhiShaderResourceBinding *QRhiShaderResourceBindings::bindingAt(qsizetype index) const
5755 \return the binding at the specified \a index.
5756 */
5757
5758/*!
5759 \fn qsizetype QRhiShaderResourceBindings::bindingCount() const
5760 \return the number of bindings.
5761 */
5762
5763/*!
5764 \class QRhiShaderResourceBinding
5765 \inmodule QtGuiPrivate
5766 \inheaderfile rhi/qrhi.h
5767 \since 6.6
5768 \brief Describes the shader resource for a single binding point.
5769
5770 A QRhiShaderResourceBinding cannot be constructed directly. Instead, use the
5771 static functions such as uniformBuffer() or sampledTexture() to get an
5772 instance.
5773
5774 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
5775 for details.
5776 */
5777
5778/*!
5779 \enum QRhiShaderResourceBinding::Type
5780 Specifies type of the shader resource bound to a binding point
5781
5782 \value UniformBuffer Uniform buffer
5783
5784 \value SampledTexture Combined image sampler (a texture and sampler pair).
5785 Even when the shading language associated with the underlying 3D API has no
5786 support for this concept (e.g. D3D and HLSL), this is still supported
5787 because the shader translation layer takes care of the appropriate
5788 translation and remapping of binding points or shader registers.
5789
5790 \value Texture Texture (separate)
5791
5792 \value Sampler Sampler (separate)
5793
5794 \value ImageLoad Image load (with GLSL this maps to doing imageLoad() on a
5795 single level - and either one or all layers - of a texture exposed to the
5796 shader as an image object)
5797
5798 \value ImageStore Image store (with GLSL this maps to doing imageStore() or
5799 imageAtomic*() on a single level - and either one or all layers - of a
5800 texture exposed to the shader as an image object)
5801
5802 \value ImageLoadStore Image load and store
5803
5804 \value BufferLoad Storage buffer load (with GLSL this maps to reading from
5805 a shader storage buffer)
5806
5807 \value BufferStore Storage buffer store (with GLSL this maps to writing to
5808 a shader storage buffer)
5809
5810 \value BufferLoadStore Storage buffer load and store
5811 */
5812
5813/*!
5814 \enum QRhiShaderResourceBinding::StageFlag
5815 Flag values to indicate which stages the shader resource is visible in
5816
5817 \value VertexStage Vertex stage
5818 \value TessellationControlStage Tessellation control (hull shader) stage
5819 \value TessellationEvaluationStage Tessellation evaluation (domain shader) stage
5820 \value FragmentStage Fragment (pixel shader) stage
5821 \value ComputeStage Compute stage
5822 \value GeometryStage Geometry stage
5823 */
5824
5825/*!
5826 \return \c true if the layout is compatible with \a other. The layout does not
5827 include the actual resource (such as, buffer or texture) and related
5828 parameters (such as, offset or size).
5829
5830 For example, \c a and \c b below are not equal, but are compatible layout-wise:
5831
5832 \code
5833 auto a = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, buffer);
5834 auto b = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage, someOtherBuffer, 256);
5835 \endcode
5836 */
5837bool QRhiShaderResourceBinding::isLayoutCompatible(const QRhiShaderResourceBinding &other) const
5838{
5839 // everything that goes into a VkDescriptorSetLayoutBinding must match
5840 return d.binding == other.d.binding
5841 && d.stage == other.d.stage
5842 && d.type == other.d.type
5843 && d.arraySize() == other.d.arraySize();
5844}
5845
5846/*!
5847 \return a shader resource binding for the given binding number, pipeline
5848 stages, and buffer specified by \a binding, \a stage, and \a buf.
5849
5850 \note When \a buf is not null, it must have been created with
5851 QRhiBuffer::UniformBuffer.
5852
5853 \note \a buf can be null. It is valid to create a
5854 QRhiShaderResourceBindings with unspecified resources, but such an object
5855 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5856 suitable for creating pipelines. Such a pipeline must then always be used
5857 together with another, layout compatible QRhiShaderResourceBindings with
5858 resources present passed to QRhiCommandBuffer::setShaderResources().
5859
5860 \note If the size of \a buf exceeds the limit reported for
5861 QRhi::MaxUniformBufferRange, unexpected errors may occur.
5862 */
5863QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBuffer(
5864 int binding, StageFlags stage, QRhiBuffer *buf)
5865{
5866 QRhiShaderResourceBinding b;
5867 b.d.binding = binding;
5868 b.d.stage = stage;
5869 b.d.type = UniformBuffer;
5870 b.d.u.ubuf.buf = buf;
5871 b.d.u.ubuf.offset = 0;
5872 b.d.u.ubuf.maybeSize = 0; // entire buffer
5873 b.d.u.ubuf.hasDynamicOffset = false;
5874 return b;
5875}
5876
5877/*!
5878 \return a shader resource binding for the given binding number, pipeline
5879 stages, and buffer specified by \a binding, \a stage, and \a buf. This
5880 overload binds a region only, as specified by \a offset and \a size.
5881
5882 \note It is up to the user to ensure the offset is aligned to
5883 QRhi::ubufAlignment().
5884
5885 \note \a size must be greater than 0.
5886
5887 \note When \a buf is not null, it must have been created with
5888 QRhiBuffer::UniformBuffer.
5889
5890 \note \a buf can be null. It is valid to create a
5891 QRhiShaderResourceBindings with unspecified resources, but such an object
5892 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5893 suitable for creating pipelines. Such a pipeline must then always be used
5894 together with another, layout compatible QRhiShaderResourceBindings with
5895 resources present passed to QRhiCommandBuffer::setShaderResources().
5896
5897 \note If \a size exceeds the limit reported for QRhi::MaxUniformBufferRange,
5898 unexpected errors may occur.
5899 */
5900QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBuffer(
5901 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
5902{
5903 Q_ASSERT(size > 0);
5904 QRhiShaderResourceBinding b;
5905 b.d.binding = binding;
5906 b.d.stage = stage;
5907 b.d.type = UniformBuffer;
5908 b.d.u.ubuf.buf = buf;
5909 b.d.u.ubuf.offset = offset;
5910 b.d.u.ubuf.maybeSize = size;
5911 b.d.u.ubuf.hasDynamicOffset = false;
5912 return b;
5913}
5914
5915/*!
5916 \return a shader resource binding for the given binding number, pipeline
5917 stages, and buffer specified by \a binding, \a stage, and \a buf. The
5918 uniform buffer is assumed to have dynamic offset. The dynamic offset can be
5919 specified in QRhiCommandBuffer::setShaderResources(), thus allowing using
5920 varying offset values without creating new bindings for the buffer. The
5921 size of the bound region is specified by \a size. Like with non-dynamic
5922 offsets, \c{offset + size} cannot exceed the size of \a buf.
5923
5924 \note When \a buf is not null, it must have been created with
5925 QRhiBuffer::UniformBuffer.
5926
5927 \note \a buf can be null. It is valid to create a
5928 QRhiShaderResourceBindings with unspecified resources, but such an object
5929 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5930 suitable for creating pipelines. Such a pipeline must then always be used
5931 together with another, layout compatible QRhiShaderResourceBindings with
5932 resources present passed to QRhiCommandBuffer::setShaderResources().
5933
5934 \note If \a size exceeds the limit reported for QRhi::MaxUniformBufferRange,
5935 unexpected errors may occur.
5936 */
5937QRhiShaderResourceBinding QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(
5938 int binding, StageFlags stage, QRhiBuffer *buf, quint32 size)
5939{
5940 Q_ASSERT(size > 0);
5941 QRhiShaderResourceBinding b;
5942 b.d.binding = binding;
5943 b.d.stage = stage;
5944 b.d.type = UniformBuffer;
5945 b.d.u.ubuf.buf = buf;
5946 b.d.u.ubuf.offset = 0;
5947 b.d.u.ubuf.maybeSize = size;
5948 b.d.u.ubuf.hasDynamicOffset = true;
5949 return b;
5950}
5951
5952/*!
5953 \return a shader resource binding for the given binding number, pipeline
5954 stages, texture, and sampler specified by \a binding, \a stage, \a tex,
5955 \a sampler.
5956
5957 \note This function is equivalent to calling sampledTextures() with a
5958 \c count of 1.
5959
5960 \note \a tex and \a sampler can be null. It is valid to create a
5961 QRhiShaderResourceBindings with unspecified resources, but such an object
5962 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
5963 suitable for creating pipelines. Such a pipeline must then always be used
5964 together with another, layout compatible QRhiShaderResourceBindings with
5965 resources present passed to QRhiCommandBuffer::setShaderResources().
5966
5967 \note A shader may not be able to consume more than 16 textures/samplers,
5968 depending on the underlying graphics API. This hard limit must be kept in
5969 mind in renderer design. This does not apply to texture arrays which
5970 consume a single binding point (shader register) and can contain 256-2048
5971 textures, depending on the underlying graphics API. Arrays of textures (see
5972 sampledTextures()) are however no different in this regard than using the
5973 same number of individual textures.
5974
5975 \sa sampledTextures()
5976 */
5977QRhiShaderResourceBinding QRhiShaderResourceBinding::sampledTexture(
5978 int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler)
5979{
5980 QRhiShaderResourceBinding b;
5981 b.d.binding = binding;
5982 b.d.stage = stage;
5983 b.d.type = SampledTexture;
5984 b.d.u.stex.count = 1;
5985 b.d.u.stex.texSamplers[0] = { tex, sampler };
5986 return b;
5987}
5988
5989/*!
5990 \return a shader resource binding for the given binding number, pipeline
5991 stages, and the array of texture-sampler pairs specified by \a binding, \a
5992 stage, \a count, and \a texSamplers.
5993
5994 \note \a count must be at least 1, and not larger than 16.
5995
5996 \note When \a count is 1, this function is equivalent to sampledTexture().
5997
5998 This function is relevant when arrays of combined image samplers are
5999 involved. For example, in GLSL \c{layout(binding = 5) uniform sampler2D
6000 shadowMaps[8];} declares an array of combined image samplers. The
6001 application is then expected provide a QRhiShaderResourceBinding for
6002 binding point 5, set up by calling this function with \a count set to 8 and
6003 a valid texture and sampler for each element of the array.
6004
6005 \warning All elements of the array must be specified. With the above
6006 example, the only valid, portable approach is calling this function with a
6007 \a count of 8. Additionally, all QRhiTexture and QRhiSampler instances must
6008 be valid, meaning nullptr is not an accepted value. This is due to some of
6009 the underlying APIs, such as, Vulkan, that require a valid image and
6010 sampler object for each element in descriptor arrays. Applications are
6011 advised to provide "dummy" samplers and textures if some array elements are
6012 not relevant (due to not being accessed in the shader).
6013
6014 \note \a texSamplers can be null. It is valid to create a
6015 QRhiShaderResourceBindings with unspecified resources, but such an object
6016 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6017 suitable for creating pipelines. Such a pipeline must then always be used
6018 together with another, layout compatible QRhiShaderResourceBindings with
6019 resources present passed to QRhiCommandBuffer::setShaderResources().
6020
6021 \sa sampledTexture()
6022 */
6023QRhiShaderResourceBinding QRhiShaderResourceBinding::sampledTextures(
6024 int binding, StageFlags stage, int count, const TextureAndSampler *texSamplers)
6025{
6026 Q_ASSERT(count >= 1 && count <= Data::MAX_TEX_SAMPLER_ARRAY_SIZE);
6027 QRhiShaderResourceBinding b;
6028 b.d.binding = binding;
6029 b.d.stage = stage;
6030 b.d.type = SampledTexture;
6031 b.d.u.stex.count = count;
6032 for (int i = 0; i < count; ++i) {
6033 if (texSamplers)
6034 b.d.u.stex.texSamplers[i] = texSamplers[i];
6035 else
6036 b.d.u.stex.texSamplers[i] = { nullptr, nullptr };
6037 }
6038 return b;
6039}
6040
6041/*!
6042 \return a shader resource binding for the given binding number, pipeline
6043 stages, and texture specified by \a binding, \a stage, \a tex.
6044
6045 \note This function is equivalent to calling textures() with a
6046 \c count of 1.
6047
6048 \note \a tex can be null. It is valid to create a
6049 QRhiShaderResourceBindings with unspecified resources, but such an object
6050 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6051 suitable for creating pipelines. Such a pipeline must then always be used
6052 together with another, layout compatible QRhiShaderResourceBindings with
6053 resources present passed to QRhiCommandBuffer::setShaderResources().
6054
6055 This creates a binding for a separate texture (image) object, whereas
6056 sampledTexture() is suitable for combined image samplers. In
6057 Vulkan-compatible GLSL code separate textures are declared as \c texture2D
6058 as opposed to \c sampler2D: \c{layout(binding = 1) uniform texture2D tex;}
6059
6060 \note A shader may not be able to consume more than 16 textures, depending
6061 on the underlying graphics API. This hard limit must be kept in mind in
6062 renderer design. This does not apply to texture arrays which consume a
6063 single binding point (shader register) and can contain 256-2048 textures,
6064 depending on the underlying graphics API. Arrays of textures (see
6065 sampledTextures()) are however no different in this regard than using the
6066 same number of individual textures.
6067
6068 \sa textures(), sampler()
6069 */
6070QRhiShaderResourceBinding QRhiShaderResourceBinding::texture(int binding, StageFlags stage, QRhiTexture *tex)
6071{
6072 QRhiShaderResourceBinding b;
6073 b.d.binding = binding;
6074 b.d.stage = stage;
6075 b.d.type = Texture;
6076 b.d.u.stex.count = 1;
6077 b.d.u.stex.texSamplers[0] = { tex, nullptr };
6078 return b;
6079}
6080
6081/*!
6082 \return a shader resource binding for the given binding number, pipeline
6083 stages, and the array of (separate) textures specified by \a binding, \a
6084 stage, \a count, and \a tex.
6085
6086 \note \a count must be at least 1, and not larger than 16.
6087
6088 \note When \a count is 1, this function is equivalent to texture().
6089
6090 \warning All elements of the array must be specified.
6091
6092 \note \a tex can be null. It is valid to create a
6093 QRhiShaderResourceBindings with unspecified resources, but such an object
6094 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6095 suitable for creating pipelines. Such a pipeline must then always be used
6096 together with another, layout compatible QRhiShaderResourceBindings with
6097 resources present passed to QRhiCommandBuffer::setShaderResources().
6098
6099 \sa texture(), sampler()
6100 */
6101QRhiShaderResourceBinding QRhiShaderResourceBinding::textures(int binding, StageFlags stage, int count, QRhiTexture **tex)
6102{
6103 Q_ASSERT(count >= 1 && count <= Data::MAX_TEX_SAMPLER_ARRAY_SIZE);
6104 QRhiShaderResourceBinding b;
6105 b.d.binding = binding;
6106 b.d.stage = stage;
6107 b.d.type = Texture;
6108 b.d.u.stex.count = count;
6109 for (int i = 0; i < count; ++i) {
6110 if (tex)
6111 b.d.u.stex.texSamplers[i] = { tex[i], nullptr };
6112 else
6113 b.d.u.stex.texSamplers[i] = { nullptr, nullptr };
6114 }
6115 return b;
6116}
6117
6118/*!
6119 \return a shader resource binding for the given binding number, pipeline
6120 stages, and sampler specified by \a binding, \a stage, \a sampler.
6121
6122 \note \a sampler can be null. It is valid to create a
6123 QRhiShaderResourceBindings with unspecified resources, but such an object
6124 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6125 suitable for creating pipelines. Such a pipeline must then always be used
6126 together with another, layout compatible QRhiShaderResourceBindings with
6127 resources present passed to QRhiCommandBuffer::setShaderResources().
6128
6129 Arrays of separate samplers are not supported.
6130
6131 This creates a binding for a separate sampler object, whereas
6132 sampledTexture() is suitable for combined image samplers. In
6133 Vulkan-compatible GLSL code separate samplers are declared as \c sampler
6134 as opposed to \c sampler2D: \c{layout(binding = 2) uniform sampler samp;}
6135
6136 With both a \c texture2D and \c sampler present, they can be used together
6137 to sample the texture: \c{fragColor = texture(sampler2D(tex, samp),
6138 texcoord);}.
6139
6140 \note A shader may not be able to consume more than 16 samplers, depending
6141 on the underlying graphics API. This hard limit must be kept in mind in
6142 renderer design.
6143
6144 \sa texture()
6145 */
6146QRhiShaderResourceBinding QRhiShaderResourceBinding::sampler(int binding, StageFlags stage, QRhiSampler *sampler)
6147{
6148 QRhiShaderResourceBinding b;
6149 b.d.binding = binding;
6150 b.d.stage = stage;
6151 b.d.type = Sampler;
6152 b.d.u.stex.count = 1;
6153 b.d.u.stex.texSamplers[0] = { nullptr, sampler };
6154 return b;
6155}
6156
6157/*!
6158 \return a shader resource binding for a read-only storage image with the
6159 given \a binding number and pipeline \a stage. The image load operations
6160 will have access to all layers of the specified \a level. (so if the texture
6161 is a cubemap, the shader must use imageCube instead of image2D)
6162
6163 \note When \a tex is not null, it must have been created with
6164 QRhiTexture::UsedWithLoadStore.
6165
6166 \note \a tex can be null. It is valid to create a QRhiShaderResourceBindings
6167 with unspecified resources, but such an object cannot be used with
6168 QRhiCommandBuffer::setShaderResources(). It is however suitable for creating
6169 pipelines. Such a pipeline must then always be used together with another,
6170 layout compatible QRhiShaderResourceBindings with resources present passed
6171 to QRhiCommandBuffer::setShaderResources().
6172
6173 \note Image load/store is only available within the compute and fragment stages.
6174 */
6175QRhiShaderResourceBinding QRhiShaderResourceBinding::imageLoad(
6176 int binding, StageFlags stage, QRhiTexture *tex, int level)
6177{
6178 QRhiShaderResourceBinding b;
6179 b.d.binding = binding;
6180 b.d.stage = stage;
6181 b.d.type = ImageLoad;
6182 b.d.u.simage.tex = tex;
6183 b.d.u.simage.level = level;
6184 return b;
6185}
6186
6187/*!
6188 \return a shader resource binding for a write-only storage image with the
6189 given \a binding number and pipeline \a stage. The image store operations
6190 will have access to all layers of the specified \a level. (so if the texture
6191 is a cubemap, the shader must use imageCube instead of image2D)
6192
6193 \note When \a tex is not null, it must have been created with
6194 QRhiTexture::UsedWithLoadStore.
6195
6196 \note \a tex can be null. It is valid to create a QRhiShaderResourceBindings
6197 with unspecified resources, but such an object cannot be used with
6198 QRhiCommandBuffer::setShaderResources(). It is however suitable for creating
6199 pipelines. Such a pipeline must then always be used together with another,
6200 layout compatible QRhiShaderResourceBindings with resources present passed
6201 to QRhiCommandBuffer::setShaderResources().
6202
6203 \note Image load/store is only available within the compute and fragment stages.
6204 */
6205QRhiShaderResourceBinding QRhiShaderResourceBinding::imageStore(
6206 int binding, StageFlags stage, QRhiTexture *tex, int level)
6207{
6208 QRhiShaderResourceBinding b;
6209 b.d.binding = binding;
6210 b.d.stage = stage;
6211 b.d.type = ImageStore;
6212 b.d.u.simage.tex = tex;
6213 b.d.u.simage.level = level;
6214 return b;
6215}
6216
6217/*!
6218 \return a shader resource binding for a read/write storage image with the
6219 given \a binding number and pipeline \a stage. The image load/store operations
6220 will have access to all layers of the specified \a level. (so if the texture
6221 is a cubemap, the shader must use imageCube instead of image2D)
6222
6223 \note When \a tex is not null, it must have been created with
6224 QRhiTexture::UsedWithLoadStore.
6225
6226 \note \a tex can be null. It is valid to create a QRhiShaderResourceBindings
6227 with unspecified resources, but such an object cannot be used with
6228 QRhiCommandBuffer::setShaderResources(). It is however suitable for creating
6229 pipelines. Such a pipeline must then always be used together with another,
6230 layout compatible QRhiShaderResourceBindings with resources present passed
6231 to QRhiCommandBuffer::setShaderResources().
6232
6233 \note Image load/store is only available within the compute and fragment stages.
6234 */
6235QRhiShaderResourceBinding QRhiShaderResourceBinding::imageLoadStore(
6236 int binding, StageFlags stage, QRhiTexture *tex, int level)
6237{
6238 QRhiShaderResourceBinding b;
6239 b.d.binding = binding;
6240 b.d.stage = stage;
6241 b.d.type = ImageLoadStore;
6242 b.d.u.simage.tex = tex;
6243 b.d.u.simage.level = level;
6244 return b;
6245}
6246
6247/*!
6248 \return a shader resource binding for a read-only storage buffer with the
6249 given \a binding number and pipeline \a stage.
6250
6251 \note When \a buf is not null, must have been created with
6252 QRhiBuffer::StorageBuffer.
6253
6254 \note \a buf can be null. It is valid to create a
6255 QRhiShaderResourceBindings with unspecified resources, but such an object
6256 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6257 suitable for creating pipelines. Such a pipeline must then always be used
6258 together with another, layout compatible QRhiShaderResourceBindings with
6259 resources present passed to QRhiCommandBuffer::setShaderResources().
6260
6261 \note Buffer load/store is only guaranteed to be available within a compute
6262 pipeline. While some backends may support using these resources in a
6263 graphics pipeline as well, this is not universally supported, and even when
6264 it is, unexpected problems may arise when it comes to barriers and
6265 synchronization. Therefore, avoid using such resources with shaders other
6266 than compute.
6267 */
6268QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoad(
6269 int binding, StageFlags stage, QRhiBuffer *buf)
6270{
6271 QRhiShaderResourceBinding b;
6272 b.d.binding = binding;
6273 b.d.stage = stage;
6274 b.d.type = BufferLoad;
6275 b.d.u.sbuf.buf = buf;
6276 b.d.u.sbuf.offset = 0;
6277 b.d.u.sbuf.maybeSize = 0; // entire buffer
6278 return b;
6279}
6280
6281/*!
6282 \return a shader resource binding for a read-only storage buffer with the
6283 given \a binding number and pipeline \a stage. This overload binds a region
6284 only, as specified by \a offset and \a size.
6285
6286 \note When \a buf is not null, must have been created with
6287 QRhiBuffer::StorageBuffer.
6288
6289 \note \a buf can be null. It is valid to create a
6290 QRhiShaderResourceBindings with unspecified resources, but such an object
6291 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6292 suitable for creating pipelines. Such a pipeline must then always be used
6293 together with another, layout compatible QRhiShaderResourceBindings with
6294 resources present passed to QRhiCommandBuffer::setShaderResources().
6295
6296 \note Buffer load/store is only guaranteed to be available within a compute
6297 pipeline. While some backends may support using these resources in a
6298 graphics pipeline as well, this is not universally supported, and even when
6299 it is, unexpected problems may arise when it comes to barriers and
6300 synchronization. Therefore, avoid using such resources with shaders other
6301 than compute.
6302 */
6303QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoad(
6304 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
6305{
6306 Q_ASSERT(size > 0);
6307 QRhiShaderResourceBinding b;
6308 b.d.binding = binding;
6309 b.d.stage = stage;
6310 b.d.type = BufferLoad;
6311 b.d.u.sbuf.buf = buf;
6312 b.d.u.sbuf.offset = offset;
6313 b.d.u.sbuf.maybeSize = size;
6314 return b;
6315}
6316
6317/*!
6318 \return a shader resource binding for a write-only storage buffer with the
6319 given \a binding number and pipeline \a stage.
6320
6321 \note When \a buf is not null, must have been created with
6322 QRhiBuffer::StorageBuffer.
6323
6324 \note \a buf can be null. It is valid to create a
6325 QRhiShaderResourceBindings with unspecified resources, but such an object
6326 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6327 suitable for creating pipelines. Such a pipeline must then always be used
6328 together with another, layout compatible QRhiShaderResourceBindings with
6329 resources present passed to QRhiCommandBuffer::setShaderResources().
6330
6331 \note Buffer load/store is only guaranteed to be available within a compute
6332 pipeline. While some backends may support using these resources in a
6333 graphics pipeline as well, this is not universally supported, and even when
6334 it is, unexpected problems may arise when it comes to barriers and
6335 synchronization. Therefore, avoid using such resources with shaders other
6336 than compute.
6337 */
6338QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferStore(
6339 int binding, StageFlags stage, QRhiBuffer *buf)
6340{
6341 QRhiShaderResourceBinding b;
6342 b.d.binding = binding;
6343 b.d.stage = stage;
6344 b.d.type = BufferStore;
6345 b.d.u.sbuf.buf = buf;
6346 b.d.u.sbuf.offset = 0;
6347 b.d.u.sbuf.maybeSize = 0; // entire buffer
6348 return b;
6349}
6350
6351/*!
6352 \return a shader resource binding for a write-only storage buffer with the
6353 given \a binding number and pipeline \a stage. This overload binds a region
6354 only, as specified by \a offset and \a size.
6355
6356 \note When \a buf is not null, must have been created with
6357 QRhiBuffer::StorageBuffer.
6358
6359 \note \a buf can be null. It is valid to create a
6360 QRhiShaderResourceBindings with unspecified resources, but such an object
6361 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6362 suitable for creating pipelines. Such a pipeline must then always be used
6363 together with another, layout compatible QRhiShaderResourceBindings with
6364 resources present passed to QRhiCommandBuffer::setShaderResources().
6365
6366 \note Buffer load/store is only guaranteed to be available within a compute
6367 pipeline. While some backends may support using these resources in a
6368 graphics pipeline as well, this is not universally supported, and even when
6369 it is, unexpected problems may arise when it comes to barriers and
6370 synchronization. Therefore, avoid using such resources with shaders other
6371 than compute.
6372 */
6373QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferStore(
6374 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
6375{
6376 Q_ASSERT(size > 0);
6377 QRhiShaderResourceBinding b;
6378 b.d.binding = binding;
6379 b.d.stage = stage;
6380 b.d.type = BufferStore;
6381 b.d.u.sbuf.buf = buf;
6382 b.d.u.sbuf.offset = offset;
6383 b.d.u.sbuf.maybeSize = size;
6384 return b;
6385}
6386
6387/*!
6388 \return a shader resource binding for a read-write storage buffer with the
6389 given \a binding number and pipeline \a stage.
6390
6391 \note When \a buf is not null, must have been created with
6392 QRhiBuffer::StorageBuffer.
6393
6394 \note \a buf can be null. It is valid to create a
6395 QRhiShaderResourceBindings with unspecified resources, but such an object
6396 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6397 suitable for creating pipelines. Such a pipeline must then always be used
6398 together with another, layout compatible QRhiShaderResourceBindings with
6399 resources present passed to QRhiCommandBuffer::setShaderResources().
6400
6401 \note Buffer load/store is only guaranteed to be available within a compute
6402 pipeline. While some backends may support using these resources in a
6403 graphics pipeline as well, this is not universally supported, and even when
6404 it is, unexpected problems may arise when it comes to barriers and
6405 synchronization. Therefore, avoid using such resources with shaders other
6406 than compute.
6407 */
6408QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoadStore(
6409 int binding, StageFlags stage, QRhiBuffer *buf)
6410{
6411 QRhiShaderResourceBinding b;
6412 b.d.binding = binding;
6413 b.d.stage = stage;
6414 b.d.type = BufferLoadStore;
6415 b.d.u.sbuf.buf = buf;
6416 b.d.u.sbuf.offset = 0;
6417 b.d.u.sbuf.maybeSize = 0; // entire buffer
6418 return b;
6419}
6420
6421/*!
6422 \return a shader resource binding for a read-write storage buffer with the
6423 given \a binding number and pipeline \a stage. This overload binds a region
6424 only, as specified by \a offset and \a size.
6425
6426 \note When \a buf is not null, must have been created with
6427 QRhiBuffer::StorageBuffer.
6428
6429 \note \a buf can be null. It is valid to create a
6430 QRhiShaderResourceBindings with unspecified resources, but such an object
6431 cannot be used with QRhiCommandBuffer::setShaderResources(). It is however
6432 suitable for creating pipelines. Such a pipeline must then always be used
6433 together with another, layout compatible QRhiShaderResourceBindings with
6434 resources present passed to QRhiCommandBuffer::setShaderResources().
6435
6436 \note Buffer load/store is only guaranteed to be available within a compute
6437 pipeline. While some backends may support using these resources in a
6438 graphics pipeline as well, this is not universally supported, and even when
6439 it is, unexpected problems may arise when it comes to barriers and
6440 synchronization. Therefore, avoid using such resources with shaders other
6441 than compute.
6442 */
6443QRhiShaderResourceBinding QRhiShaderResourceBinding::bufferLoadStore(
6444 int binding, StageFlags stage, QRhiBuffer *buf, quint32 offset, quint32 size)
6445{
6446 Q_ASSERT(size > 0);
6447 QRhiShaderResourceBinding b;
6448 b.d.binding = binding;
6449 b.d.stage = stage;
6450 b.d.type = BufferLoadStore;
6451 b.d.u.sbuf.buf = buf;
6452 b.d.u.sbuf.offset = offset;
6453 b.d.u.sbuf.maybeSize = size;
6454 return b;
6455}
6456
6457/*!
6458 \return \c true if the contents of the two QRhiShaderResourceBinding
6459 objects \a a and \a b are equal. This includes the resources (buffer,
6460 texture) and related parameters (offset, size) as well. To only compare
6461 layouts (binding point, pipeline stage, resource type), use
6462 \l{QRhiShaderResourceBinding::isLayoutCompatible()}{isLayoutCompatible()}
6463 instead.
6464
6465 \relates QRhiShaderResourceBinding
6466 */
6467bool operator==(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) noexcept
6468{
6469 const QRhiShaderResourceBinding::Data *da = QRhiImplementation::shaderResourceBindingData(a);
6470 const QRhiShaderResourceBinding::Data *db = QRhiImplementation::shaderResourceBindingData(b);
6471
6472 if (da == db)
6473 return true;
6474
6475
6476 if (da->binding != db->binding
6477 || da->stage != db->stage
6478 || da->type != db->type)
6479 {
6480 return false;
6481 }
6482
6483 switch (da->type) {
6484 case QRhiShaderResourceBinding::UniformBuffer:
6485 if (da->u.ubuf.buf != db->u.ubuf.buf
6486 || da->u.ubuf.offset != db->u.ubuf.offset
6487 || da->u.ubuf.maybeSize != db->u.ubuf.maybeSize)
6488 {
6489 return false;
6490 }
6491 break;
6492 case QRhiShaderResourceBinding::SampledTexture:
6493 if (da->u.stex.count != db->u.stex.count)
6494 return false;
6495 for (int i = 0; i < da->u.stex.count; ++i) {
6496 if (da->u.stex.texSamplers[i].tex != db->u.stex.texSamplers[i].tex
6497 || da->u.stex.texSamplers[i].sampler != db->u.stex.texSamplers[i].sampler)
6498 {
6499 return false;
6500 }
6501 }
6502 break;
6503 case QRhiShaderResourceBinding::Texture:
6504 if (da->u.stex.count != db->u.stex.count)
6505 return false;
6506 for (int i = 0; i < da->u.stex.count; ++i) {
6507 if (da->u.stex.texSamplers[i].tex != db->u.stex.texSamplers[i].tex)
6508 return false;
6509 }
6510 break;
6511 case QRhiShaderResourceBinding::Sampler:
6512 if (da->u.stex.texSamplers[0].sampler != db->u.stex.texSamplers[0].sampler)
6513 return false;
6514 break;
6515 case QRhiShaderResourceBinding::ImageLoad:
6516 case QRhiShaderResourceBinding::ImageStore:
6517 case QRhiShaderResourceBinding::ImageLoadStore:
6518 if (da->u.simage.tex != db->u.simage.tex
6519 || da->u.simage.level != db->u.simage.level)
6520 {
6521 return false;
6522 }
6523 break;
6524 case QRhiShaderResourceBinding::BufferLoad:
6525 case QRhiShaderResourceBinding::BufferStore:
6526 case QRhiShaderResourceBinding::BufferLoadStore:
6527 if (da->u.sbuf.buf != db->u.sbuf.buf
6528 || da->u.sbuf.offset != db->u.sbuf.offset
6529 || da->u.sbuf.maybeSize != db->u.sbuf.maybeSize)
6530 {
6531 return false;
6532 }
6533 break;
6534 default:
6535 Q_UNREACHABLE_RETURN(false);
6536 }
6537
6538 return true;
6539}
6540
6541/*!
6542 \return \c false if all the bindings in the two QRhiShaderResourceBinding
6543 objects \a a and \a b are equal; otherwise returns \c true.
6544
6545 \relates QRhiShaderResourceBinding
6546 */
6547bool operator!=(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) noexcept
6548{
6549 return !(a == b);
6550}
6551
6552/*!
6553 \fn size_t qHash(const QRhiShaderResourceBinding &key, size_t seed)
6554 \qhashold{QRhiShaderResourceBinding}
6555 */
6556size_t qHash(const QRhiShaderResourceBinding &b, size_t seed) noexcept
6557{
6558 const QRhiShaderResourceBinding::Data *d = QRhiImplementation::shaderResourceBindingData(b);
6559 QtPrivate::QHashCombineWithSeed hash(seed);
6560 seed = hash(seed, d->binding);
6561 seed = hash(seed, d->stage);
6562 seed = hash(seed, d->type);
6563 switch (d->type) {
6564 case QRhiShaderResourceBinding::UniformBuffer:
6565 seed = hash(seed, reinterpret_cast<quintptr>(d->u.ubuf.buf));
6566 break;
6567 case QRhiShaderResourceBinding::SampledTexture:
6568 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].tex));
6569 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].sampler));
6570 break;
6571 case QRhiShaderResourceBinding::Texture:
6572 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].tex));
6573 break;
6574 case QRhiShaderResourceBinding::Sampler:
6575 seed = hash(seed, reinterpret_cast<quintptr>(d->u.stex.texSamplers[0].sampler));
6576 break;
6577 case QRhiShaderResourceBinding::ImageLoad:
6578 case QRhiShaderResourceBinding::ImageStore:
6579 case QRhiShaderResourceBinding::ImageLoadStore:
6580 seed = hash(seed, reinterpret_cast<quintptr>(d->u.simage.tex));
6581 break;
6582 case QRhiShaderResourceBinding::BufferLoad:
6583 case QRhiShaderResourceBinding::BufferStore:
6584 case QRhiShaderResourceBinding::BufferLoadStore:
6585 seed = hash(seed, reinterpret_cast<quintptr>(d->u.sbuf.buf));
6586 break;
6587 }
6588 return seed;
6589}
6590
6591#ifndef QT_NO_DEBUG_STREAM
6592QDebug operator<<(QDebug dbg, const QRhiShaderResourceBinding &b)
6593{
6594 QDebugStateSaver saver(dbg);
6595 const QRhiShaderResourceBinding::Data *d = QRhiImplementation::shaderResourceBindingData(b);
6596 dbg.nospace() << "QRhiShaderResourceBinding("
6597 << "binding=" << d->binding
6598 << " stage=" << d->stage
6599 << " type=" << d->type;
6600 switch (d->type) {
6601 case QRhiShaderResourceBinding::UniformBuffer:
6602 dbg.nospace() << " UniformBuffer("
6603 << "buffer=" << d->u.ubuf.buf
6604 << " offset=" << d->u.ubuf.offset
6605 << " maybeSize=" << d->u.ubuf.maybeSize
6606 << ')';
6607 break;
6608 case QRhiShaderResourceBinding::SampledTexture:
6609 dbg.nospace() << " SampledTextures("
6610 << "count=" << d->u.stex.count;
6611 for (int i = 0; i < d->u.stex.count; ++i) {
6612 dbg.nospace() << " texture=" << d->u.stex.texSamplers[i].tex
6613 << " sampler=" << d->u.stex.texSamplers[i].sampler;
6614 }
6615 dbg.nospace() << ')';
6616 break;
6617 case QRhiShaderResourceBinding::Texture:
6618 dbg.nospace() << " Textures("
6619 << "count=" << d->u.stex.count;
6620 for (int i = 0; i < d->u.stex.count; ++i)
6621 dbg.nospace() << " texture=" << d->u.stex.texSamplers[i].tex;
6622 dbg.nospace() << ')';
6623 break;
6624 case QRhiShaderResourceBinding::Sampler:
6625 dbg.nospace() << " Sampler("
6626 << " sampler=" << d->u.stex.texSamplers[0].sampler
6627 << ')';
6628 break;
6629 case QRhiShaderResourceBinding::ImageLoad:
6630 dbg.nospace() << " ImageLoad("
6631 << "texture=" << d->u.simage.tex
6632 << " level=" << d->u.simage.level
6633 << ')';
6634 break;
6635 case QRhiShaderResourceBinding::ImageStore:
6636 dbg.nospace() << " ImageStore("
6637 << "texture=" << d->u.simage.tex
6638 << " level=" << d->u.simage.level
6639 << ')';
6640 break;
6641 case QRhiShaderResourceBinding::ImageLoadStore:
6642 dbg.nospace() << " ImageLoadStore("
6643 << "texture=" << d->u.simage.tex
6644 << " level=" << d->u.simage.level
6645 << ')';
6646 break;
6647 case QRhiShaderResourceBinding::BufferLoad:
6648 dbg.nospace() << " BufferLoad("
6649 << "buffer=" << d->u.sbuf.buf
6650 << " offset=" << d->u.sbuf.offset
6651 << " maybeSize=" << d->u.sbuf.maybeSize
6652 << ')';
6653 break;
6654 case QRhiShaderResourceBinding::BufferStore:
6655 dbg.nospace() << " BufferStore("
6656 << "buffer=" << d->u.sbuf.buf
6657 << " offset=" << d->u.sbuf.offset
6658 << " maybeSize=" << d->u.sbuf.maybeSize
6659 << ')';
6660 break;
6661 case QRhiShaderResourceBinding::BufferLoadStore:
6662 dbg.nospace() << " BufferLoadStore("
6663 << "buffer=" << d->u.sbuf.buf
6664 << " offset=" << d->u.sbuf.offset
6665 << " maybeSize=" << d->u.sbuf.maybeSize
6666 << ')';
6667 break;
6668 default:
6669 dbg.nospace() << " UNKNOWN()";
6670 break;
6671 }
6672 dbg.nospace() << ')';
6673 return dbg;
6674}
6675#endif
6676
6677#ifndef QT_NO_DEBUG_STREAM
6678QDebug operator<<(QDebug dbg, const QRhiShaderResourceBindings &srb)
6679{
6680 QDebugStateSaver saver(dbg);
6681 dbg.nospace() << "QRhiShaderResourceBindings("
6682 << srb.m_bindings
6683 << ')';
6684 return dbg;
6685}
6686#endif
6687
6688/*!
6689 \class QRhiGraphicsPipeline
6690 \inmodule QtGuiPrivate
6691 \inheaderfile rhi/qrhi.h
6692 \since 6.6
6693 \brief Graphics pipeline state resource.
6694
6695 Represents a graphics pipeline. What exactly this map to in the underlying
6696 native graphics API, varies. Where there is a concept of pipeline objects,
6697 for example with Vulkan, the QRhi backend will create such an object upon
6698 calling create(). Elsewhere, for example with OpenGL, the
6699 QRhiGraphicsPipeline may merely collect the various state, and create()'s
6700 main task is to set up the corresponding shader program, but deferring
6701 looking at any of the requested state to a later point.
6702
6703 As with all QRhiResource subclasses, the two-phased initialization pattern
6704 applies: setting any values via the setters, for example setDepthTest(), is
6705 only effective after calling create(). Avoid changing any values once the
6706 QRhiGraphicsPipeline has been initialized via create(). To change some
6707 state, set the new value and call create() again. However, that will
6708 effectively release all underlying native resources and create new ones. As
6709 a result, it may be a heavy, expensive operation. Rather, prefer creating
6710 multiple pipelines with the different states, and
6711 \l{QRhiCommandBuffer::setGraphicsPipeline()}{switch between them} when
6712 recording the render pass.
6713
6714 \note Setting the shader stages is mandatory. There must be at least one
6715 stage, and there must be a vertex stage.
6716
6717 \note Setting the shader resource bindings is mandatory. The referenced
6718 QRhiShaderResourceBindings must already have create() called on it by the
6719 time create() is called. Associating with a QRhiShaderResourceBindings that
6720 has no bindings is also valid, as long as no shader in any stage expects any
6721 resources. Using a QRhiShaderResourceBindings object that does not specify
6722 any actual resources (i.e., the buffers, textures, etc. for the binding
6723 points are set to \nullptr) is valid as well, as long as a
6724 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible}
6725 QRhiShaderResourceBindings, that specifies resources for all the bindings,
6726 is going to be set via
6727 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()} when
6728 recording the render pass.
6729
6730 \note Setting the render pass descriptor is mandatory. To obtain a
6731 QRhiRenderPassDescriptor that can be passed to setRenderPassDescriptor(),
6732 use either QRhiTextureRenderTarget::newCompatibleRenderPassDescriptor() or
6733 QRhiSwapChain::newCompatibleRenderPassDescriptor().
6734
6735 \note Setting the vertex input layout is mandatory.
6736
6737 \note sampleCount() defaults to 1 and must match the sample count of the
6738 render target's color and depth stencil attachments.
6739
6740 \note The depth test, depth write, and stencil test are disabled by
6741 default. The face culling mode defaults to no culling.
6742
6743 \note stencilReadMask() and stencilWriteMask() apply to both faces. They
6744 both default to 0xFF.
6745
6746 \section2 Example usage
6747
6748 All settings of a graphics pipeline have defaults which might be suitable
6749 to many applications. Therefore a minimal example of creating a graphics
6750 pipeline could be the following. This assumes that the vertex shader takes
6751 a single \c{vec3 position} input at the input location 0. With the
6752 QRhiShaderResourceBindings and QRhiRenderPassDescriptor objects, plus the
6753 QShader collections for the vertex and fragment stages, a pipeline could be
6754 created like this:
6755
6756 \code
6757 QRhiShaderResourceBindings *srb;
6758 QRhiRenderPassDescriptor *rpDesc;
6759 QShader vs, fs;
6760 // ...
6761
6762 QRhiVertexInputLayout inputLayout;
6763 inputLayout.setBindings({ { 3 * sizeof(float) } });
6764 inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 } });
6765
6766 QRhiGraphicsPipeline *ps = rhi->newGraphicsPipeline();
6767 ps->setShaderStages({ { QRhiShaderStage::Vertex, vs }, { QRhiShaderStage::Fragment, fs } });
6768 ps->setVertexInputLayout(inputLayout);
6769 ps->setShaderResourceBindings(srb);
6770 ps->setRenderPassDescriptor(rpDesc);
6771 if (!ps->create()) { error(); }
6772 \endcode
6773
6774 The above code creates a pipeline object that uses the defaults for many
6775 settings and states. For example, it will use a \l Triangles topology, no
6776 backface culling, blending is disabled but color write is enabled for all
6777 four channels, depth test/write are disabled, stencil operations are
6778 disabled.
6779
6780 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
6781 for details.
6782
6783 \sa QRhiCommandBuffer, QRhi
6784 */
6785
6786/*!
6787 \enum QRhiGraphicsPipeline::Flag
6788
6789 Flag values for describing the dynamic state of the pipeline, and other
6790 options. The viewport is always dynamic.
6791
6792 \value UsesBlendConstants Indicates that a blend color constant will be set
6793 via QRhiCommandBuffer::setBlendConstants()
6794
6795 \value UsesStencilRef Indicates that a stencil reference value will be set
6796 via QRhiCommandBuffer::setStencilRef()
6797
6798 \value UsesScissor Indicates that a scissor rectangle will be set via
6799 QRhiCommandBuffer::setScissor()
6800
6801 \value CompileShadersWithDebugInfo Requests compiling shaders with debug
6802 information enabled. This is relevant only when runtime shader compilation
6803 from source code is involved, and only when the underlying infrastructure
6804 supports this. With concrete examples, this is not relevant with Vulkan and
6805 SPIR-V, because the GLSL-to-SPIR-V compilation does not happen at run
6806 time. On the other hand, consider Direct3D and HLSL, where there are
6807 multiple options: when the QShader packages ship with pre-compiled bytecode
6808 (\c DXBC), debug information is to be requested through the tool that
6809 generates the \c{.qsb} file, similarly to the case of Vulkan and
6810 SPIR-V. However, when having HLSL source code in the pre- or
6811 runtime-generated QShader packages, the first phase of compilation (HLSL
6812 source to intermediate format) happens at run time too, with this flag taken
6813 into account. Debug information is relevant in particular with tools like
6814 RenderDoc since it allows seeing the original source code when investigating
6815 the pipeline and when performing vertex or fragment shader debugging.
6816
6817 \value UsesShadingRate Indicates that a per-draw (per-pipeline) shading rate
6818 value will be set via QRhiCommandBuffer::setShadingRate(). Not specifying
6819 this flag and still calling setShadingRate() may lead to varying, unexpected
6820 results depending on the underlying graphics API.
6821 */
6822
6823/*!
6824 \enum QRhiGraphicsPipeline::Topology
6825 Specifies the primitive topology
6826
6827 \value Triangles (default)
6828 \value TriangleStrip
6829 \value TriangleFan (only available if QRhi::TriangleFanTopology is supported)
6830 \value Lines
6831 \value LineStrip
6832 \value Points
6833
6834 \value Patches (only available if QRhi::Tessellation is supported, and
6835 requires the tessellation stages to be present in the pipeline)
6836 */
6837
6838/*!
6839 \enum QRhiGraphicsPipeline::CullMode
6840 Specifies the culling mode
6841
6842 \value None No culling (default)
6843 \value Front Cull front faces
6844 \value Back Cull back faces
6845 */
6846
6847/*!
6848 \enum QRhiGraphicsPipeline::FrontFace
6849 Specifies the front face winding order
6850
6851 \value CCW Counter clockwise (default)
6852 \value CW Clockwise
6853 */
6854
6855/*!
6856 \enum QRhiGraphicsPipeline::ColorMaskComponent
6857 Flag values for specifying the color write mask
6858
6859 \value R
6860 \value G
6861 \value B
6862 \value A
6863 */
6864
6865/*!
6866 \enum QRhiGraphicsPipeline::BlendFactor
6867 Specifies the blend factor
6868
6869 \value Zero
6870 \value One
6871 \value SrcColor
6872 \value OneMinusSrcColor
6873 \value DstColor
6874 \value OneMinusDstColor
6875 \value SrcAlpha
6876 \value OneMinusSrcAlpha
6877 \value DstAlpha
6878 \value OneMinusDstAlpha
6879 \value ConstantColor
6880 \value OneMinusConstantColor
6881 \value ConstantAlpha
6882 \value OneMinusConstantAlpha
6883 \value SrcAlphaSaturate
6884 \value Src1Color
6885 \value OneMinusSrc1Color
6886 \value Src1Alpha
6887 \value OneMinusSrc1Alpha
6888 */
6889
6890/*!
6891 \enum QRhiGraphicsPipeline::BlendOp
6892 Specifies the blend operation
6893
6894 \value Add
6895 \value Subtract
6896 \value ReverseSubtract
6897 \value Min
6898 \value Max
6899 */
6900
6901/*!
6902 \enum QRhiGraphicsPipeline::CompareOp
6903 Specifies the depth or stencil comparison function
6904
6905 \value Never
6906 \value Less (default for depth)
6907 \value Equal
6908 \value LessOrEqual
6909 \value Greater
6910 \value NotEqual
6911 \value GreaterOrEqual
6912 \value Always (default for stencil)
6913 */
6914
6915/*!
6916 \enum QRhiGraphicsPipeline::StencilOp
6917 Specifies the stencil operation
6918
6919 \value StencilZero
6920 \value Keep (default)
6921 \value Replace
6922 \value IncrementAndClamp
6923 \value DecrementAndClamp
6924 \value Invert
6925 \value IncrementAndWrap
6926 \value DecrementAndWrap
6927 */
6928
6929/*!
6930 \enum QRhiGraphicsPipeline::PolygonMode
6931 \brief Specifies the polygon rasterization mode
6932
6933 Polygon Mode (Triangle Fill Mode in Metal, Fill Mode in D3D) specifies
6934 the fill mode used when rasterizing polygons. Polygons may be drawn as
6935 solids (Fill), or as a wire mesh (Line).
6936
6937 Support for non-fill polygon modes is optional and is indicated by the
6938 QRhi::NonFillPolygonMode feature. With OpenGL ES and some Vulkan
6939 implementations the feature will likely be reported as unsupported, which
6940 then means values other than Fill cannot be used.
6941
6942 \value Fill The interior of the polygon is filled (default)
6943 \value Line Boundary edges of the polygon are drawn as line segments.
6944 */
6945
6946/*!
6947 \struct QRhiGraphicsPipeline::TargetBlend
6948 \inmodule QtGuiPrivate
6949 \inheaderfile rhi/qrhi.h
6950 \since 6.6
6951 \brief Describes the blend state for one color attachment.
6952
6953 Defaults to color write enabled, blending disabled. The blend values are
6954 set up for pre-multiplied alpha (One, OneMinusSrcAlpha, One,
6955 OneMinusSrcAlpha) by default. This means that to get the alpha blending
6956 mode Qt Quick uses, it is enough to set the \c enable flag to true while
6957 leaving other values at their defaults.
6958
6959 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
6960 for details.
6961 */
6962
6963/*!
6964 \variable QRhiGraphicsPipeline::TargetBlend::colorWrite
6965 */
6966
6967/*!
6968 \variable QRhiGraphicsPipeline::TargetBlend::enable
6969 */
6970
6971/*!
6972 \variable QRhiGraphicsPipeline::TargetBlend::srcColor
6973 */
6974
6975/*!
6976 \variable QRhiGraphicsPipeline::TargetBlend::dstColor
6977 */
6978
6979/*!
6980 \variable QRhiGraphicsPipeline::TargetBlend::opColor
6981 */
6982
6983/*!
6984 \variable QRhiGraphicsPipeline::TargetBlend::srcAlpha
6985 */
6986
6987/*!
6988 \variable QRhiGraphicsPipeline::TargetBlend::dstAlpha
6989 */
6990
6991/*!
6992 \variable QRhiGraphicsPipeline::TargetBlend::opAlpha
6993 */
6994
6995/*!
6996 \struct QRhiGraphicsPipeline::StencilOpState
6997 \inmodule QtGuiPrivate
6998 \inheaderfile rhi/qrhi.h
6999 \since 6.6
7000 \brief Describes the stencil operation state.
7001
7002 The default-constructed StencilOpState has the following set:
7003 \list
7004 \li failOp - \l Keep
7005 \li depthFailOp - \l Keep
7006 \li passOp - \l Keep
7007 \li compareOp \l Always
7008 \endlist
7009
7010 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
7011 for details.
7012 */
7013
7014/*!
7015 \variable QRhiGraphicsPipeline::StencilOpState::failOp
7016 */
7017
7018/*!
7019 \variable QRhiGraphicsPipeline::StencilOpState::depthFailOp
7020 */
7021
7022/*!
7023 \variable QRhiGraphicsPipeline::StencilOpState::passOp
7024 */
7025
7026/*!
7027 \variable QRhiGraphicsPipeline::StencilOpState::compareOp
7028 */
7029
7030/*!
7031 \internal
7032 */
7033QRhiGraphicsPipeline::QRhiGraphicsPipeline(QRhiImplementation *rhi)
7034 : QRhiResource(rhi)
7035{
7036}
7037
7038/*!
7039 \return the resource type.
7040 */
7041QRhiResource::Type QRhiGraphicsPipeline::resourceType() const
7042{
7043 return GraphicsPipeline;
7044}
7045
7046/*!
7047 \fn virtual bool QRhiGraphicsPipeline::create() = 0
7048
7049 Creates the corresponding native graphics resources. If there are already
7050 resources present due to an earlier create() with no corresponding
7051 destroy(), then destroy() is called implicitly first.
7052
7053 \return \c true when successful, \c false when a graphics operation failed.
7054 Regardless of the return value, calling destroy() is always safe.
7055
7056 \note This may be, depending on the underlying graphics API, an expensive
7057 operation, especially when shaders get compiled/optimized from source or
7058 from an intermediate bytecode format to the GPU's own instruction set.
7059 Where applicable, the QRhi backend automatically sets up the relevant
7060 non-persistent facilities to accelerate this, for example the Vulkan
7061 backend automatically creates a \c VkPipelineCache to improve data reuse
7062 during the lifetime of the application.
7063
7064 \note Drivers may also employ various persistent (disk-based) caching
7065 strategies for shader and pipeline data, which is hidden to and is outside
7066 of Qt's control. In some cases, depending on the graphics API and the QRhi
7067 backend, there are facilities within QRhi for manually managing such a
7068 cache, allowing the retrieval of a serializable blob that can then be
7069 reloaded in the future runs of the application to ensure faster pipeline
7070 creation times. See QRhi::pipelineCacheData() and
7071 QRhi::setPipelineCacheData() for details. Note also that when working with
7072 a QRhi instance managed by a higher level Qt framework, such as Qt Quick,
7073 it is possible that such disk-based caching is taken care of automatically,
7074 for example QQuickWindow uses a disk-based pipeline cache by default (which
7075 comes in addition to any driver-level caching).
7076 */
7077
7078/*!
7079 \fn QRhiGraphicsPipeline::Flags QRhiGraphicsPipeline::flags() const
7080 \return the currently set flags.
7081 */
7082
7083/*!
7084 \fn void QRhiGraphicsPipeline::setFlags(Flags f)
7085 Sets the flags \a f.
7086 */
7087
7088/*!
7089 \fn QRhiGraphicsPipeline::Topology QRhiGraphicsPipeline::topology() const
7090 \return the currently set primitive topology.
7091 */
7092
7093/*!
7094 \fn void QRhiGraphicsPipeline::setTopology(Topology t)
7095 Sets the primitive topology \a t.
7096 */
7097
7098/*!
7099 \fn QRhiGraphicsPipeline::CullMode QRhiGraphicsPipeline::cullMode() const
7100 \return the currently set face culling mode.
7101 */
7102
7103/*!
7104 \fn void QRhiGraphicsPipeline::setCullMode(CullMode mode)
7105 Sets the specified face culling \a mode.
7106 */
7107
7108/*!
7109 \fn QRhiGraphicsPipeline::FrontFace QRhiGraphicsPipeline::frontFace() const
7110 \return the currently set front face mode.
7111 */
7112
7113/*!
7114 \fn void QRhiGraphicsPipeline::setFrontFace(FrontFace f)
7115 Sets the front face mode \a f.
7116 */
7117
7118/*!
7119 \fn void QRhiGraphicsPipeline::setTargetBlends(std::initializer_list<TargetBlend> list)
7120
7121 Sets the \a list of render target blend settings. This is a list because
7122 when multiple render targets are used (i.e., a QRhiTextureRenderTarget with
7123 more than one QRhiColorAttachment), there needs to be a TargetBlend
7124 structure per render target (color attachment).
7125
7126 By default there is one default-constructed TargetBlend set.
7127
7128 \sa QRhi::MaxColorAttachments
7129 */
7130
7131/*!
7132 \fn template<typename InputIterator> void QRhiGraphicsPipeline::setTargetBlends(InputIterator first, InputIterator last)
7133 Sets the list of render target blend settings from the iterators \a first and \a last.
7134 */
7135
7136/*!
7137 \fn const QRhiGraphicsPipeline::TargetBlend *QRhiGraphicsPipeline::cbeginTargetBlends() const
7138 \return a const iterator pointing to the first item in the render target blend setting list.
7139 */
7140
7141/*!
7142 \fn const QRhiGraphicsPipeline::TargetBlend *QRhiGraphicsPipeline::cendTargetBlends() const
7143 \return a const iterator pointing just after the last item in the render target blend setting list.
7144 */
7145
7146/*!
7147 \fn const QRhiGraphicsPipeline::TargetBlend *QRhiGraphicsPipeline::targetBlendAt(qsizetype index) const
7148 \return the render target blend setting at the specified \a index.
7149 */
7150
7151/*!
7152 \fn qsizetype QRhiGraphicsPipeline::targetBlendCount() const
7153 \return the number of render target blend settings.
7154 */
7155
7156/*!
7157 \fn bool QRhiGraphicsPipeline::hasDepthTest() const
7158 \return true if depth testing is enabled.
7159 */
7160
7161/*!
7162 \fn void QRhiGraphicsPipeline::setDepthTest(bool enable)
7163
7164 Enables or disables depth testing based on \a enable. Both depth test and
7165 the writing out of depth data are disabled by default.
7166
7167 \sa setDepthWrite()
7168 */
7169
7170/*!
7171 \fn bool QRhiGraphicsPipeline::hasDepthWrite() const
7172 \return true if depth write is enabled.
7173 */
7174
7175/*!
7176 \fn void QRhiGraphicsPipeline::setDepthWrite(bool enable)
7177
7178 Controls the writing out of depth data into the depth buffer based on
7179 \a enable. By default this is disabled. Depth write is typically enabled
7180 together with the depth test.
7181
7182 \note Enabling depth write without having depth testing enabled may not
7183 lead to the desired result, and should be avoided.
7184
7185 \sa setDepthTest()
7186 */
7187
7188/*!
7189 \fn bool QRhiGraphicsPipeline::hasDepthClamp() const
7190 \return true if depth clamp is enabled.
7191
7192 \since 6.11
7193 */
7194
7195/*!
7196 \fn void QRhiGraphicsPipeline::setDepthClamp(bool enable)
7197
7198 Enables depth clamping when \a enable is true. When depth clamping is
7199 enabled, primitives that would otherwise be clipped by the near or far
7200 clip plane are rasterized and their depth values are clamped to the
7201 depth range. When disabled (the default), such primitives are clipped.
7202
7203 \note This setting is ignored on OpenGL ES.
7204
7205 \since 6.11
7206 */
7207
7208/*!
7209 \fn QRhiGraphicsPipeline::CompareOp QRhiGraphicsPipeline::depthOp() const
7210 \return the depth comparison function.
7211 */
7212
7213/*!
7214 \fn void QRhiGraphicsPipeline::setDepthOp(CompareOp op)
7215 Sets the depth comparison function \a op.
7216 */
7217
7218/*!
7219 \fn bool QRhiGraphicsPipeline::hasStencilTest() const
7220 \return true if stencil testing is enabled.
7221 */
7222
7223/*!
7224 \fn void QRhiGraphicsPipeline::setStencilTest(bool enable)
7225 Enables or disables stencil tests based on \a enable.
7226 By default this is disabled.
7227 */
7228
7229/*!
7230 \fn QRhiGraphicsPipeline::StencilOpState QRhiGraphicsPipeline::stencilFront() const
7231 \return the current stencil test state for front faces.
7232 */
7233
7234/*!
7235 \fn void QRhiGraphicsPipeline::setStencilFront(const StencilOpState &state)
7236 Sets the stencil test \a state for front faces.
7237 */
7238
7239/*!
7240 \fn QRhiGraphicsPipeline::StencilOpState QRhiGraphicsPipeline::stencilBack() const
7241 \return the current stencil test state for back faces.
7242 */
7243
7244/*!
7245 \fn void QRhiGraphicsPipeline::setStencilBack(const StencilOpState &state)
7246 Sets the stencil test \a state for back faces.
7247 */
7248
7249/*!
7250 \fn quint32 QRhiGraphicsPipeline::stencilReadMask() const
7251 \return the currrent stencil read mask.
7252 */
7253
7254/*!
7255 \fn void QRhiGraphicsPipeline::setStencilReadMask(quint32 mask)
7256 Sets the stencil read \a mask. The default value is 0xFF.
7257 */
7258
7259/*!
7260 \fn quint32 QRhiGraphicsPipeline::stencilWriteMask() const
7261 \return the current stencil write mask.
7262 */
7263
7264/*!
7265 \fn void QRhiGraphicsPipeline::setStencilWriteMask(quint32 mask)
7266 Sets the stencil write \a mask. The default value is 0xFF.
7267 */
7268
7269/*!
7270 \fn int QRhiGraphicsPipeline::sampleCount() const
7271 \return the currently set sample count. 1 means no multisample antialiasing.
7272 */
7273
7274/*!
7275 \fn void QRhiGraphicsPipeline::setSampleCount(int s)
7276
7277 Sets the sample count. Typical values for \a s are 1, 4, or 8. The pipeline
7278 must always be compatible with the render target, i.e. the sample counts
7279 must match.
7280
7281 \sa QRhi::supportedSampleCounts()
7282 */
7283
7284/*!
7285 \fn float QRhiGraphicsPipeline::lineWidth() const
7286 \return the currently set line width. The default is 1.0f.
7287 */
7288
7289/*!
7290 \fn void QRhiGraphicsPipeline::setLineWidth(float width)
7291
7292 Sets the line \a width. If the QRhi::WideLines feature is reported as
7293 unsupported at runtime, values other than 1.0f are ignored.
7294 */
7295
7296/*!
7297 \fn int QRhiGraphicsPipeline::depthBias() const
7298 \return the currently set depth bias.
7299 */
7300
7301/*!
7302 \fn void QRhiGraphicsPipeline::setDepthBias(int bias)
7303 Sets the depth \a bias. The default value is 0.
7304 */
7305
7306/*!
7307 \fn float QRhiGraphicsPipeline::slopeScaledDepthBias() const
7308 \return the currently set slope scaled depth bias.
7309 */
7310
7311/*!
7312 \fn void QRhiGraphicsPipeline::setSlopeScaledDepthBias(float bias)
7313 Sets the slope scaled depth \a bias. The default value is 0.
7314 */
7315
7316/*!
7317 \fn void QRhiGraphicsPipeline::setShaderStages(std::initializer_list<QRhiShaderStage> list)
7318 Sets the \a list of shader stages.
7319 */
7320
7321/*!
7322 \fn template<typename InputIterator> void QRhiGraphicsPipeline::setShaderStages(InputIterator first, InputIterator last)
7323 Sets the list of shader stages from the iterators \a first and \a last.
7324 */
7325
7326/*!
7327 \fn const QRhiShaderStage *QRhiGraphicsPipeline::cbeginShaderStages() const
7328 \return a const iterator pointing to the first item in the shader stage list.
7329 */
7330
7331/*!
7332 \fn const QRhiShaderStage *QRhiGraphicsPipeline::cendShaderStages() const
7333 \return a const iterator pointing just after the last item in the shader stage list.
7334 */
7335
7336/*!
7337 \fn const QRhiShaderStage *QRhiGraphicsPipeline::shaderStageAt(qsizetype index) const
7338 \return the shader stage at the specified \a index.
7339 */
7340
7341/*!
7342 \fn qsizetype QRhiGraphicsPipeline::shaderStageCount() const
7343 \return the number of shader stages in this pipeline.
7344 */
7345
7346/*!
7347 \fn QRhiVertexInputLayout QRhiGraphicsPipeline::vertexInputLayout() const
7348 \return the currently set vertex input layout specification.
7349 */
7350
7351/*!
7352 \fn void QRhiGraphicsPipeline::setVertexInputLayout(const QRhiVertexInputLayout &layout)
7353 Specifies the vertex input \a layout.
7354 */
7355
7356/*!
7357 \fn QRhiShaderResourceBindings *QRhiGraphicsPipeline::shaderResourceBindings() const
7358 \return the currently associated QRhiShaderResourceBindings object.
7359 */
7360
7361/*!
7362 \fn void QRhiGraphicsPipeline::setShaderResourceBindings(QRhiShaderResourceBindings *srb)
7363
7364 Associates with \a srb describing the resource binding layout and the
7365 resources (QRhiBuffer, QRhiTexture) themselves. The latter is optional,
7366 because only the layout matters during pipeline creation. Therefore, the \a
7367 srb passed in here can leave the actual buffer or texture objects
7368 unspecified (\nullptr) as long as there is another,
7369 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible}
7370 QRhiShaderResourceBindings bound via
7371 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()} before
7372 recording the draw calls.
7373 */
7374
7375/*!
7376 \fn QRhiRenderPassDescriptor *QRhiGraphicsPipeline::renderPassDescriptor() const
7377 \return the currently set QRhiRenderPassDescriptor.
7378 */
7379
7380/*!
7381 \fn void QRhiGraphicsPipeline::setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
7382 Associates with the specified QRhiRenderPassDescriptor \a desc.
7383 */
7384
7385/*!
7386 \fn int QRhiGraphicsPipeline::patchControlPointCount() const
7387 \return the currently set patch control point count.
7388 */
7389
7390/*!
7391 \fn void QRhiGraphicsPipeline::setPatchControlPointCount(int count)
7392
7393 Sets the number of patch control points to \a count. The default value is
7394 3. This is used only when the topology is set to \l Patches.
7395 */
7396
7397/*!
7398 \fn QRhiGraphicsPipeline::PolygonMode QRhiGraphicsPipeline::polygonMode() const
7399 \return the polygon mode.
7400 */
7401
7402/*!
7403 \fn void QRhiGraphicsPipeline::setPolygonMode(PolygonMode mode)
7404 Sets the polygon \a mode. The default is Fill.
7405
7406 \sa QRhi::NonFillPolygonMode
7407 */
7408
7409/*!
7410 \fn int QRhiGraphicsPipeline::multiViewCount() const
7411 \return the view count. The default is 0, indicating no multiview rendering.
7412 \since 6.7
7413 */
7414
7415/*!
7416 \fn void QRhiGraphicsPipeline::setMultiViewCount(int count)
7417 Sets the view \a count for multiview rendering. The default is 0,
7418 indicating no multiview rendering.
7419 \a count must be 2 or larger to trigger multiview rendering.
7420
7421 Multiview is only available when the \l{QRhi::MultiView}{MultiView feature}
7422 is reported as supported. The render target must be a 2D texture array, and
7423 the color attachment for the render target must have the same \a count set.
7424
7425 See QRhiColorAttachment::setMultiViewCount() for further details on
7426 multiview rendering.
7427
7428 \since 6.7
7429 \sa QRhi::MultiView, QRhiColorAttachment::setMultiViewCount()
7430 */
7431
7432/*!
7433 \class QRhiSwapChain
7434 \inmodule QtGuiPrivate
7435 \inheaderfile rhi/qrhi.h
7436 \since 6.6
7437 \brief Swapchain resource.
7438
7439 A swapchain enables presenting rendering results to a surface. A swapchain
7440 is typically backed by a set of color buffers. Of these, one is displayed
7441 at a time.
7442
7443 Below is a typical pattern for creating and managing a swapchain and some
7444 associated resources in order to render onto a QWindow:
7445
7446 \code
7447 void init()
7448 {
7449 sc = rhi->newSwapChain();
7450 ds = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
7451 QSize(), // no need to set the size here due to UsedWithSwapChainOnly
7452 1,
7453 QRhiRenderBuffer::UsedWithSwapChainOnly);
7454 sc->setWindow(window);
7455 sc->setDepthStencil(ds);
7456 rp = sc->newCompatibleRenderPassDescriptor();
7457 sc->setRenderPassDescriptor(rp);
7458 resizeSwapChain();
7459 }
7460
7461 void resizeSwapChain()
7462 {
7463 hasSwapChain = sc->createOrResize();
7464 }
7465
7466 void render()
7467 {
7468 if (!hasSwapChain || notExposed)
7469 return;
7470
7471 if (sc->currentPixelSize() != sc->surfacePixelSize() || newlyExposed) {
7472 resizeSwapChain();
7473 if (!hasSwapChain)
7474 return;
7475 newlyExposed = false;
7476 }
7477
7478 rhi->beginFrame(sc);
7479 // ...
7480 rhi->endFrame(sc);
7481 }
7482 \endcode
7483
7484 Avoid relying on QWindow resize events to resize swapchains, especially
7485 considering that surface sizes may not always fully match the QWindow
7486 reported dimensions. The safe, cross-platform approach is to do the check
7487 via surfacePixelSize() whenever starting a new frame.
7488
7489 Releasing the swapchain must happen while the QWindow and the underlying
7490 native window is fully up and running. Building on the previous example:
7491
7492 \code
7493 void releaseSwapChain()
7494 {
7495 if (hasSwapChain) {
7496 sc->destroy();
7497 hasSwapChain = false;
7498 }
7499 }
7500
7501 // assuming Window is our QWindow subclass
7502 bool Window::event(QEvent *e)
7503 {
7504 switch (e->type()) {
7505 case QEvent::UpdateRequest: // for QWindow::requestUpdate()
7506 render();
7507 break;
7508 case QEvent::PlatformSurface:
7509 if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed)
7510 releaseSwapChain();
7511 break;
7512 default:
7513 break;
7514 }
7515 return QWindow::event(e);
7516 }
7517 \endcode
7518
7519 Initializing the swapchain and starting to render the first frame cannot
7520 start at any time. The safe, cross-platform approach is to rely on expose
7521 events. QExposeEvent is a loosely specified event that is sent whenever a
7522 window gets mapped, obscured, and resized, depending on the platform.
7523
7524 \code
7525 void Window::exposeEvent(QExposeEvent *)
7526 {
7527 // initialize and start rendering when the window becomes usable for graphics purposes
7528 if (isExposed() && !running) {
7529 running = true;
7530 init();
7531 }
7532
7533 // stop pushing frames when not exposed or size becomes 0
7534 if ((!isExposed() || (hasSwapChain && sc->surfacePixelSize().isEmpty())) && running)
7535 notExposed = true;
7536
7537 // continue when exposed again and the surface has a valid size
7538 if (isExposed() && running && notExposed && !sc->surfacePixelSize().isEmpty()) {
7539 notExposed = false;
7540 newlyExposed = true;
7541 }
7542
7543 if (isExposed() && !sc->surfacePixelSize().isEmpty())
7544 render();
7545 }
7546 \endcode
7547
7548 Once the rendering has started, a simple way to request a new frame is
7549 QWindow::requestUpdate(). While on some platforms this is merely a small
7550 timer, on others it has a specific implementation: for instance on macOS or
7551 iOS it may be backed by
7552 \l{https://developer.apple.com/documentation/corevideo/cvdisplaylink?language=objc}{CVDisplayLink}.
7553 The example above is already prepared for update requests by handling
7554 QEvent::UpdateRequest.
7555
7556 While acting as a QRhiRenderTarget, QRhiSwapChain also manages a
7557 QRhiCommandBuffer. Calling QRhi::endFrame() submits the recorded commands
7558 and also enqueues a \c present request. The default behavior is to do this
7559 with a swap interval of 1, meaning synchronizing to the display's vertical
7560 refresh is enabled. Thus the rendering thread calling beginFrame() and
7561 endFrame() will get throttled to vsync. On some backends this can be
7562 disabled by passing QRhiSwapChain:NoVSync in flags().
7563
7564 Multisampling (MSAA) is handled transparently to the applications when
7565 requested via setSampleCount(). Where applicable, QRhiSwapChain will take
7566 care of creating additional color buffers and issuing a multisample resolve
7567 command at the end of a frame. For OpenGL, it is necessary to request the
7568 appropriate sample count also via QSurfaceFormat, by calling
7569 QSurfaceFormat::setDefaultFormat() before initializing the QRhi.
7570
7571 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
7572 for details.
7573 */
7574
7575/*!
7576 \enum QRhiSwapChain::Flag
7577 Flag values to describe swapchain properties
7578
7579 \value SurfaceHasPreMulAlpha Indicates that the target surface has
7580 transparency with premultiplied alpha. For example, this is what Qt Quick
7581 uses when the alpha channel is enabled on the target QWindow, because the
7582 scenegraph rendrerer always outputs fragments with alpha multiplied into
7583 the red, green, and blue values. To ensure identical behavior across
7584 platforms, always set QSurfaceFormat::alphaBufferSize() to a non-zero value
7585 on the target QWindow whenever this flag is set on the swapchain.
7586
7587 \value SurfaceHasNonPreMulAlpha Indicates the target surface has
7588 transparency with non-premultiplied alpha. Be aware that this may not be
7589 supported on some systems, if the system compositor always expects content
7590 with premultiplied alpha. In that case the behavior with this flag set is
7591 expected to be equivalent to SurfaceHasPreMulAlpha.
7592
7593 \value sRGB Requests to pick an sRGB format for the swapchain's color
7594 buffers and/or render target views, where applicable. Note that this
7595 implies that sRGB framebuffer update and blending will get enabled for all
7596 content targeting this swapchain, and opting out is not possible. For
7597 OpenGL, set \l{QSurfaceFormat::sRGBColorSpace}{sRGBColorSpace} on the
7598 QSurfaceFormat of the QWindow in addition. Applicable only when the
7599 swapchain format is set to QRhiSwapChain::SDR.
7600
7601 \value UsedAsTransferSource Indicates the swapchain will be used as the
7602 source of a readback in QRhiResourceUpdateBatch::readBackTexture().
7603
7604 \value NoVSync Requests disabling waiting for vertical sync, also avoiding
7605 throttling the rendering thread. The behavior is backend specific and
7606 applicable only where it is possible to control this. Some may ignore the
7607 request altogether. For OpenGL, try instead setting the swap interval to 0
7608 on the QWindow via QSurfaceFormat::setSwapInterval().
7609
7610 \value MinimalBufferCount Requests creating the swapchain with the minimum
7611 number of buffers, which is in practice 2, unless the graphics
7612 implementation has a higher minimum number than that. Only applicable with
7613 backends where such control is available via the graphics API, for example,
7614 Vulkan. By default it is up to the backend to decide what number of buffers
7615 it requests (in practice this is almost always either 2 or 3), and it is
7616 not the applications' concern. However, on Vulkan for instance the backend
7617 will likely prefer the higher number (3), for example to avoid odd
7618 performance issues with some Vulkan implementations on mobile devices. It
7619 could be that on some platforms it can prove to be beneficial to force the
7620 lower buffer count (2), so this flag allows forcing that. Note that all
7621 this has no effect on the number of frames kept in flight, so the CPU
7622 (QRhi) will still prepare frames at most \c{N - 1} frames ahead of the GPU,
7623 even when the swapchain image buffer count larger than \c N. (\c{N} =
7624 QRhi::FramesInFlight and typically 2).
7625 */
7626
7627/*!
7628 \enum QRhiSwapChain::Format
7629 Describes the swapchain format. The default format is SDR.
7630
7631 This enum is used with
7632 \l{QRhiSwapChain::isFormatSupported()}{isFormatSupported()} to check
7633 upfront if creating the swapchain with the given format is supported by the
7634 platform and the window's associated screen, and with
7635 \l{QRhiSwapChain::setFormat()}{setFormat()}
7636 to set the requested format in the swapchain before calling
7637 \l{QRhiSwapChain::createOrResize()}{createOrResize()} for the first time.
7638
7639 \value SDR 8-bit RGBA or BGRA, depending on the backend and platform. With
7640 OpenGL ES in particular, it could happen that the platform provides less
7641 than 8 bits (e.g. due to EGL and the QSurfaceFormat choosing a 565 or 444
7642 format - this is outside the control of QRhi). Standard dynamic range. May
7643 be combined with setting the QRhiSwapChain::sRGB flag.
7644
7645 \value HDRExtendedSrgbLinear 16-bit float RGBA, high dynamic range,
7646 extended linear sRGB (scRGB) color space. This involves Rec. 709 primaries
7647 (same as SDR/sRGB) and linear colors. Conversion to the display's native
7648 color space (such as, HDR10) is performed by the windowing system. On
7649 Windows this is the canonical color space of the system compositor, and is
7650 the recommended format for HDR swapchains in general on desktop platforms.
7651
7652 \value HDR10 10-bit unsigned int RGB or BGR with 2 bit alpha, high dynamic
7653 range, HDR10 (Rec. 2020) color space with an ST2084 PQ transfer function.
7654
7655 \value HDRExtendedDisplayP3Linear 16-bit float RGBA, high dynamic range,
7656 extended linear Display P3 color space. The primary choice for HDR on
7657 platforms such as iOS and VisionOS.
7658 */
7659
7660/*!
7661 \internal
7662 */
7663QRhiSwapChain::QRhiSwapChain(QRhiImplementation *rhi)
7664 : QRhiResource(rhi)
7665{
7666}
7667
7668/*!
7669 \return the resource type.
7670 */
7671QRhiResource::Type QRhiSwapChain::resourceType() const
7672{
7673 return SwapChain;
7674}
7675
7676/*!
7677 \fn QSize QRhiSwapChain::currentPixelSize() const
7678
7679 \return the size with which the swapchain was last successfully built. Use
7680 this to decide if createOrResize() needs to be called again: if
7681 \c{currentPixelSize() != surfacePixelSize()} then the swapchain needs to be
7682 resized.
7683
7684 \note Typical rendering logic will call this function to get the output
7685 size when starting to prepare a new frame, and base dependent calculations
7686 (such as, the viewport) on the size returned from this function.
7687
7688 While in many cases the value is the same as \c{QWindow::size() *
7689 QWindow::devicePixelRatio()}, relying on the QWindow-reported size is not
7690 guaranteed to be correct on all platforms and graphics API implementations.
7691 Using this function is therefore strongly recommended whenever there is a
7692 need to identify the dimensions, in pixels, of the output layer or surface.
7693
7694 This also has the added benefit of avoiding potential data races when QRhi
7695 is used on a dedicated rendering thread, because the need to call QWindow
7696 functions, that may then access data updated on the main thread, is
7697 avoided.
7698
7699 \sa surfacePixelSize()
7700 */
7701
7702/*!
7703 \fn virtual QSize QRhiSwapChain::surfacePixelSize() = 0
7704
7705 \return The size of the window's associated surface or layer.
7706
7707 \warning Do not assume this is the same as \c{QWindow::size() *
7708 QWindow::devicePixelRatio()}. With some graphics APIs and windowing system
7709 interfaces (for example, Vulkan) there is a theoretical possibility for a
7710 surface to assume a size different from the associated window. To support
7711 these cases, \b{rendering logic must always base size-derived calculations
7712 (such as, viewports) on the size reported from QRhiSwapChain, and never on
7713 the size queried from QWindow}.
7714
7715 \note \b{Can also be called before createOrResize(), if at least window() is
7716 already set. This in combination with currentPixelSize() allows to detect
7717 when a swapchain needs to be resized.} However, watch out for the fact that
7718 the size of the underlying native object (surface, layer, or similar) is
7719 "live", so whenever this function is called, it returns the latest value
7720 reported by the underlying implementation, without any atomicity guarantee.
7721 Therefore, using this function to determine pixel sizes for graphics
7722 resources that are used in a frame is strongly discouraged. Rely on
7723 currentPixelSize() instead which returns a size that is atomic and will not
7724 change between createOrResize() invocations.
7725
7726 \note For depth-stencil buffers used in combination with the swapchain's
7727 color buffers, it is strongly recommended to rely on the automatic sizing
7728 and rebuilding behavior provided by the
7729 QRhiRenderBuffer:UsedWithSwapChainOnly flag. Avoid querying the surface
7730 size via this function just to get a size that can be passed to
7731 QRhiRenderBuffer::setPixelSize() as that would suffer from the lack of
7732 atomicity as described above.
7733
7734 \sa currentPixelSize()
7735 */
7736
7737/*!
7738 \fn virtual bool QRhiSwapChain::isFormatSupported(Format f) = 0
7739
7740 \return true if the given swapchain format \a f is supported. SDR is always
7741 supported.
7742
7743 \note Can be called independently of createOrResize(), but window() must
7744 already be set. Calling without the window set may lead to unexpected
7745 results depending on the backend and platform (most likely false for any
7746 HDR format), because HDR format support is usually tied to the output
7747 (screen) to which the swapchain's associated window belongs at any given
7748 time. If the result is true for a HDR format, then creating the swapchain
7749 with that format is expected to succeed as long as the window is not moved
7750 to another screen in the meantime.
7751
7752 The main use of this function is to call it before the first
7753 createOrResize() after the window is already set. This allow the QRhi
7754 backends to perform platform or windowing system specific queries to
7755 determine if the window (and the screen it is on) is capable of true HDR
7756 output with the specified format.
7757
7758 When the format is reported as supported, call setFormat() to set the
7759 requested format and call createOrResize(). Be aware of the consequences
7760 however: successfully requesting a HDR format will involve having to deal
7761 with a different color space, possibly doing white level correction for
7762 non-HDR-aware content, adjusting tonemapping methods, adjusting offscreen
7763 render target settings, etc.
7764
7765 \sa setFormat()
7766 */
7767
7768/*!
7769 \fn virtual QRhiCommandBuffer *QRhiSwapChain::currentFrameCommandBuffer() = 0
7770
7771 \return a command buffer on which rendering commands and resource updates
7772 can be recorded within a \l{QRhi::beginFrame()}{beginFrame} -
7773 \l{QRhi::endFrame()}{endFrame} block, assuming beginFrame() was called with
7774 this swapchain.
7775
7776 \note The returned object is valid also after endFrame(), up until the next
7777 beginFrame(), but the returned command buffer should not be used to record
7778 any commands then. Rather, it can be used to query data collected during
7779 the frame (or previous frames), for example by calling
7780 \l{QRhiCommandBuffer::lastCompletedGpuTime()}{lastCompletedGpuTime()}.
7781
7782 \note The value must not be cached and reused between frames. The caller
7783 should not hold on to the returned object once
7784 \l{QRhi::beginFrame()}{beginFrame()} is called again. Instead, the command
7785 buffer object should be queried again by calling this function.
7786*/
7787
7788/*!
7789 \fn virtual QRhiRenderTarget *QRhiSwapChain::currentFrameRenderTarget() = 0
7790
7791 \return a render target that can used with beginPass() in order to render
7792 the swapchain's current backbuffer. Only valid within a
7793 QRhi::beginFrame() - QRhi::endFrame() block where beginFrame() was called
7794 with this swapchain.
7795
7796 \note the value must not be cached and reused between frames
7797 */
7798
7799/*!
7800 \enum QRhiSwapChain::StereoTargetBuffer
7801 Selects the backbuffer to use with a stereoscopic swapchain.
7802
7803 \value LeftBuffer
7804 \value RightBuffer
7805 */
7806
7807/*!
7808 \return a render target that can be used with beginPass() in order to
7809 render to the swapchain's left or right backbuffer. This overload should be
7810 used only with stereoscopic rendering, that is, when the associated QWindow
7811 is backed by two color buffers, one for each eye, instead of just one.
7812
7813 When stereoscopic rendering is not supported, the return value will be
7814 the default target. It is supported by all hardware backends except for Metal, in
7815 combination with \l QSurfaceFormat::StereoBuffers, assuming it is supported
7816 by the graphics and display driver stack at run time. Metal and Null backends
7817 are going to return the default render target from this overload.
7818
7819 \note the value must not be cached and reused between frames
7820 */
7821QRhiRenderTarget *QRhiSwapChain::currentFrameRenderTarget(StereoTargetBuffer targetBuffer)
7822{
7823 Q_UNUSED(targetBuffer);
7824 return currentFrameRenderTarget();
7825}
7826
7827/*!
7828 \fn virtual bool QRhiSwapChain::createOrResize() = 0
7829
7830 Creates the swapchain if not already done and resizes the swapchain buffers
7831 to match the current size of the targeted surface. Call this whenever the
7832 size of the target surface is different than before.
7833
7834 \note call destroy() only when the swapchain needs to be released
7835 completely, typically upon
7836 QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed. To perform resizing, just
7837 call createOrResize().
7838
7839 \return \c true when successful, \c false when a graphics operation failed.
7840 Regardless of the return value, calling destroy() is always safe.
7841 */
7842
7843/*!
7844 \fn QWindow *QRhiSwapChain::window() const
7845 \return the currently set window.
7846 */
7847
7848/*!
7849 \fn void QRhiSwapChain::setWindow(QWindow *window)
7850 Sets the \a window.
7851 */
7852
7853/*!
7854 \fn QRhiSwapChainProxyData QRhiSwapChain::proxyData() const
7855 \return the currently set proxy data.
7856 */
7857
7858/*!
7859 \fn void QRhiSwapChain::setProxyData(const QRhiSwapChainProxyData &d)
7860 Sets the proxy data \a d.
7861
7862 \sa QRhi::updateSwapChainProxyData()
7863 */
7864
7865/*!
7866 \fn QRhiSwapChain::Flags QRhiSwapChain::flags() const
7867 \return the currently set flags.
7868 */
7869
7870/*!
7871 \fn void QRhiSwapChain::setFlags(Flags f)
7872 Sets the flags \a f.
7873 */
7874
7875/*!
7876 \fn QRhiSwapChain::Format QRhiSwapChain::format() const
7877 \return the currently set format.
7878 */
7879
7880/*!
7881 \fn void QRhiSwapChain::setFormat(Format f)
7882 Sets the format \a f.
7883
7884 Avoid setting formats that are reported as unsupported from
7885 isFormatSupported(). Note that support for a given format may depend on the
7886 screen the swapchain's associated window is opened on. On some platforms,
7887 such as Windows and macOS, for HDR output to work it is necessary to have
7888 HDR output enabled in the display settings.
7889
7890 See isFormatSupported(), \l QRhiSwapChainHdrInfo, and \l Format for more
7891 information on high dynamic range output.
7892 */
7893
7894/*!
7895 \fn QRhiRenderBuffer *QRhiSwapChain::depthStencil() const
7896 \return the currently associated renderbuffer for depth-stencil.
7897 */
7898
7899/*!
7900 \fn void QRhiSwapChain::setDepthStencil(QRhiRenderBuffer *ds)
7901 Sets the renderbuffer \a ds for use as a depth-stencil buffer.
7902 */
7903
7904/*!
7905 \fn int QRhiSwapChain::sampleCount() const
7906 \return the currently set sample count. 1 means no multisample antialiasing.
7907 */
7908
7909/*!
7910 \fn void QRhiSwapChain::setSampleCount(int samples)
7911
7912 Sets the sample count. Common values for \a samples are 1 (no MSAA), 4 (4x
7913 MSAA), or 8 (8x MSAA).
7914
7915 \sa QRhi::supportedSampleCounts()
7916 */
7917
7918/*!
7919 \fn QRhiRenderPassDescriptor *QRhiSwapChain::renderPassDescriptor() const
7920 \return the currently associated QRhiRenderPassDescriptor object.
7921 */
7922
7923/*!
7924 \fn void QRhiSwapChain::setRenderPassDescriptor(QRhiRenderPassDescriptor *desc)
7925 Associates with the QRhiRenderPassDescriptor \a desc.
7926 */
7927
7928/*!
7929 \fn virtual QRhiRenderPassDescriptor *QRhiSwapChain::newCompatibleRenderPassDescriptor() = 0;
7930
7931 \return a new QRhiRenderPassDescriptor that is compatible with this swapchain.
7932
7933 The returned value is used in two ways: it can be passed to
7934 setRenderPassDescriptor() and
7935 QRhiGraphicsPipeline::setRenderPassDescriptor(). A render pass descriptor
7936 describes the attachments (color, depth/stencil) and the load/store
7937 behavior that can be affected by flags(). A QRhiGraphicsPipeline can only
7938 be used in combination with a swapchain that has a
7939 \l{QRhiRenderPassDescriptor::isCompatible()}{compatible}
7940 QRhiRenderPassDescriptor set.
7941
7942 \sa createOrResize()
7943 */
7944
7945/*!
7946 \fn QRhiShadingRateMap *QRhiSwapChain::shadingRateMap() const
7947 \return the currently set QRhiShadingRateMap. By default this is \nullptr.
7948 \since 6.9
7949 */
7950
7951/*!
7952 \fn void QRhiSwapChain::setShadingRateMap(QRhiShadingRateMap *map)
7953
7954 Associates with the specified QRhiShadingRateMap \a map. This is functional
7955 only when the \l QRhi::VariableRateShadingMap feature is reported as
7956 supported.
7957
7958 When QRhiCommandBuffer::setShadingRate() is also called, the higher of two
7959 the shading rates are used for each tile. There is currently no control
7960 offered over the combiner behavior.
7961
7962 \note Setting a shading rate map implies that a different, new
7963 QRhiRenderPassDescriptor is needed and some of the native swapchain objects
7964 must be rebuilt. Therefore, if the swapchain is already set up, call
7965 newCompatibleRenderPassDescriptor() and setRenderPassDescriptor() right
7966 after setShadingRateMap(). Then, createOrResize() must also be called again.
7967 This has rolling consequences, for example for graphics pipelines: those
7968 also need to be associated with the new QRhiRenderPassDescriptor and then
7969 rebuilt. See \l QRhiRenderPassDescriptor::serializedFormat() for some
7970 suggestions on how to deal with this. Remember to set the
7971 QRhiGraphicsPipeline::UsesShadingRate flag for them as well.
7972
7973 \since 6.9
7974 */
7975
7976/*!
7977 \struct QRhiSwapChainHdrInfo
7978 \inmodule QtGuiPrivate
7979 \inheaderfile rhi/qrhi.h
7980 \since 6.6
7981
7982 \brief Describes the high dynamic range related information of the
7983 swapchain's associated output.
7984
7985 To perform HDR-compatible tonemapping, where the target range is not [0,1],
7986 one often needs to know the maximum luminance of the display the
7987 swapchain's window is associated with. While this is often made
7988 user-configurable (think brightness, gamma and similar settings in games),
7989 it can be highly useful to set defaults based on the values reported by the
7990 display itself, thus providing a decent starting point.
7991
7992 There are some problems however: the information is exposed in different
7993 forms on different platforms, whereas with cross-platform graphics APIs
7994 there is often no associated solution at all, because managing such
7995 information is not in the scope of the API (and may rather be retrievable
7996 via other platform-specific means, if any).
7997
7998 With Metal on macOS/iOS, there is no luminance values exposed in the
7999 platform APIs. Instead, the maximum color component value, that would be
8000 1.0 in a non-HDR setup, is provided. The \c limitsType field indicates what
8001 kind of information is available. It is then up to the clients of QRhi to
8002 access the correct data from the \c limits union and use it as they see
8003 fit.
8004
8005 With an API like Vulkan, where there is no way to get such information, the
8006 values are always the built-in defaults.
8007
8008 Therefore, the struct returned from QRhiSwapChain::hdrInfo() contains
8009 either some hard-coded defaults or real values received from an API such as
8010 DXGI (IDXGIOutput6) or Cocoa (NSScreen). When no platform queries are
8011 available (or needs using platform facilities out of scope for QRhi), the
8012 hard-coded defaults are a maximum luminance of 1000 nits and an SDR white
8013 level of 200.
8014
8015 The struct also exposes the presumed luminance behavior of the platform and
8016 its compositor, to indicate what a color component value of 1.0 is treated
8017 as in a HDR color buffer. In some cases it will be necessary to perform
8018 color correction of non-HDR content composited with HDR content. To enable
8019 this, the SDR white level is queried from the system on some platforms
8020 (Windows) and exposed here.
8021
8022 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8023 for details.
8024
8025 \sa QRhiSwapChain::hdrInfo()
8026 */
8027
8028/*!
8029 \enum QRhiSwapChainHdrInfo::LimitsType
8030
8031 \value LuminanceInNits Indicates that the \l limits union has its
8032 \c luminanceInNits struct set
8033
8034 \value ColorComponentValue Indicates that the \l limits union has its
8035 \c colorComponentValue struct set
8036*/
8037
8038/*!
8039 \enum QRhiSwapChainHdrInfo::LuminanceBehavior
8040
8041 \value SceneReferred Indicates that the color value of 1.0 is interpreted
8042 as 80 nits. This is the behavior of HDR-enabled windows with the Windows
8043 compositor. See
8044 \l{https://learn.microsoft.com/en-us/windows/win32/direct3darticles/high-dynamic-range}{this
8045 page} for more information on HDR on Windows.
8046
8047 \value DisplayReferred Indicates that the color value of 1.0 is interpreted
8048 as the value of the SDR white. (which can be e.g. 200 nits, but will vary
8049 depending on screen brightness) This is the behavior of HDR-enabled windows
8050 on Apple platforms. See
8051 \l{https://developer.apple.com/documentation/metal/hdr_content/displaying_hdr_content_in_a_metal_layer}{this
8052 page} for more information on Apple's EDR system.
8053*/
8054
8055/*!
8056 \variable QRhiSwapChainHdrInfo::limitsType
8057
8058 With Metal on macOS/iOS, there is no luminance values exposed in the
8059 platform APIs. Instead, the maximum color component value, that would be
8060 1.0 in a non-HDR setup, is provided. This value indicates what kind of
8061 information is available in \l limits.
8062
8063 \sa QRhiSwapChain::hdrInfo()
8064*/
8065
8066/*!
8067 \variable QRhiSwapChainHdrInfo::limits
8068
8069 Contains the actual values queried from the graphics API or the platform.
8070 The type of data is indicated by \l limitsType. This is therefore a union.
8071 There are currently two options:
8072
8073 Luminance values in nits:
8074
8075 \code
8076 struct {
8077 float minLuminance;
8078 float maxLuminance;
8079 } luminanceInNits;
8080 \endcode
8081
8082 On Windows the minimum and maximum luminance depends on the screen
8083 brightness. While not relevant for desktops, on laptops the screen
8084 brightness may change at any time. Increasing brightness implies decreased
8085 maximum luminance. In addition, the results may also be dependent on the
8086 HDR Content Brightness set in Windows Settings' System/Display/HDR view,
8087 if there is such a setting.
8088
8089 Note however that the changes made to the laptop screen's brightness or in
8090 the system settings while the application is running are not necessarily
8091 reflected in the returned values, meaning calling hdrInfo() again may still
8092 return the same luminance range as before for the rest of the process'
8093 lifetime. The exact behavior is up to DXGI and Qt has no control over it.
8094
8095 \note The Windows compositor works in scene-referred mode for HDR content.
8096 A color component value of 1.0 corresponds to a luminance of 80 nits. When
8097 rendering non-HDR content (e.g. 2D UI elements), the correction of the
8098 white level is often necessary. (e.g., outputting the fragment color (1, 1,
8099 1) will likely lead to showing a shade of white that is too dim on-screen)
8100 See \l sdrWhiteLevel.
8101
8102 For macOS/iOS, the current maximum and potential maximum color
8103 component values are provided:
8104
8105 \code
8106 struct {
8107 float maxColorComponentValue;
8108 float maxPotentialColorComponentValue;
8109 } colorComponentValue;
8110 \endcode
8111
8112 The value may depend on the screen brightness, which on laptops means that
8113 the result may change in the next call to hdrInfo() if the brightness was
8114 changed in the meantime. The maximum screen brightness implies a maximum
8115 color value of 1.0.
8116
8117 \note Apple's EDR is display-referred. 1.0 corresponds to a luminance level
8118 of SDR white (e.g. 200 nits), the value of which varies based on the screen
8119 brightness and possibly other settings. The exact luminance value for that,
8120 or the maximum luminance of the display, are not exposed to the
8121 applications.
8122
8123 \note It has been observed that the color component values are not set to
8124 the correct larger-than-1 value right away on startup on some macOS
8125 systems, but the values tend to change during or after the first frame.
8126
8127 \sa QRhiSwapChain::hdrInfo()
8128*/
8129
8130/*!
8131 \variable QRhiSwapChainHdrInfo::luminanceBehavior
8132
8133 Describes the platform's presumed behavior with regards to color values.
8134
8135 \sa sdrWhiteLevel
8136 */
8137
8138/*!
8139 \variable QRhiSwapChainHdrInfo::sdrWhiteLevel
8140
8141 On Windows this is the dynamic SDR white level in nits. The value is
8142 dependent on the screen brightness (on laptops), and the SDR or HDR Content
8143 Brightness settings in the Windows settings' System/Display/HDR view.
8144
8145 To perform white level correction for non-HDR (SDR) content, such as 2D UI
8146 elemenents, multiply the final color with sdrWhiteLevel / 80.0 whenever
8147 \l luminanceBehavior is SceneReferred. (assuming Windows and a linear
8148 extended sRGB (scRGB) color space)
8149
8150 On other platforms the value is always a pre-defined value, 200. This may
8151 not match the system's actual SDR white level, but the value of this
8152 variable is not relevant in practice when the \l luminanceBehavior is
8153 DisplayReferred, because then the color component value of 1.0 refers to
8154 the SDR white by default.
8155
8156 \sa luminanceBehavior
8157*/
8158
8159/*!
8160 \return the HDR information for the associated display.
8161
8162 Do not assume that this is a cheap operation. Depending on the platform,
8163 this function makes various platform queries which may have a performance
8164 impact.
8165
8166 \note Can be called before createOrResize() as long as the window is
8167 \l{setWindow()}{set}.
8168
8169 \note What happens when moving a window with an initialized swapchain
8170 between displays (HDR to HDR with different characteristics, HDR to SDR,
8171 etc.) is not currently well-defined and depends heavily on the windowing
8172 system and compositor, with potentially varying behavior between platforms.
8173 Currently QRhi only guarantees that hdrInfo() returns valid data, if
8174 available, for the display to which the swapchain's associated window
8175 belonged at the time of createOrResize().
8176
8177 \sa QRhiSwapChainHdrInfo
8178 */
8179QRhiSwapChainHdrInfo QRhiSwapChain::hdrInfo()
8180{
8181 QRhiSwapChainHdrInfo info;
8182 info.limitsType = QRhiSwapChainHdrInfo::LuminanceInNits;
8183 info.limits.luminanceInNits.minLuminance = 0.0f;
8184 info.limits.luminanceInNits.maxLuminance = 1000.0f;
8185 info.luminanceBehavior = QRhiSwapChainHdrInfo::SceneReferred;
8186 info.sdrWhiteLevel = 200.0f;
8187 return info;
8188}
8189
8190#ifndef QT_NO_DEBUG_STREAM
8191QDebug operator<<(QDebug dbg, const QRhiSwapChainHdrInfo &info)
8192{
8193 QDebugStateSaver saver(dbg);
8194 dbg.nospace() << "QRhiSwapChainHdrInfo(";
8195 switch (info.limitsType) {
8197 dbg.nospace() << " minLuminance=" << info.limits.luminanceInNits.minLuminance
8198 << " maxLuminance=" << info.limits.luminanceInNits.maxLuminance;
8199 break;
8201 dbg.nospace() << " maxColorComponentValue=" << info.limits.colorComponentValue.maxColorComponentValue;
8202 dbg.nospace() << " maxPotentialColorComponentValue=" << info.limits.colorComponentValue.maxPotentialColorComponentValue;
8203 break;
8204 }
8205 switch (info.luminanceBehavior) {
8207 dbg.nospace() << " scene-referred, SDR white level=" << info.sdrWhiteLevel;
8208 break;
8210 dbg.nospace() << " display-referred";
8211 break;
8212 }
8213 dbg.nospace() << ')';
8214 return dbg;
8215}
8216#endif
8217
8218/*!
8219 \class QRhiComputePipeline
8220 \inmodule QtGuiPrivate
8221 \inheaderfile rhi/qrhi.h
8222 \since 6.6
8223 \brief Compute pipeline state resource.
8224
8225 \note Setting the shader resource bindings is mandatory. The referenced
8226 QRhiShaderResourceBindings must already have created() called on it by the
8227 time create() is called.
8228
8229 \note Setting the shader is mandatory.
8230
8231 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8232 for details.
8233 */
8234
8235/*!
8236 \enum QRhiComputePipeline::Flag
8237
8238 Flag values for describing pipeline options.
8239
8240 \value CompileShadersWithDebugInfo Requests compiling shaders with debug
8241 information enabled, when applicable. See
8242 QRhiGraphicsPipeline::CompileShadersWithDebugInfo for more information.
8243 */
8244
8245/*!
8246 \return the resource type.
8247 */
8248QRhiResource::Type QRhiComputePipeline::resourceType() const
8249{
8250 return ComputePipeline;
8251}
8252
8253/*!
8254 \internal
8255 */
8256QRhiComputePipeline::QRhiComputePipeline(QRhiImplementation *rhi)
8257 : QRhiResource(rhi)
8258{
8259}
8260
8261/*!
8262 \fn QRhiComputePipeline::Flags QRhiComputePipeline::flags() const
8263 \return the currently set flags.
8264 */
8265
8266/*!
8267 \fn void QRhiComputePipeline::setFlags(Flags f)
8268 Sets the flags \a f.
8269 */
8270
8271/*!
8272 \fn QRhiShaderStage QRhiComputePipeline::shaderStage() const
8273 \return the currently set shader.
8274 */
8275
8276/*!
8277 \fn void QRhiComputePipeline::setShaderStage(const QRhiShaderStage &stage)
8278
8279 Sets the shader to use. \a stage can only refer to the
8280 \l{QRhiShaderStage::Compute}{compute stage}.
8281 */
8282
8283/*!
8284 \fn QRhiShaderResourceBindings *QRhiComputePipeline::shaderResourceBindings() const
8285 \return the currently associated QRhiShaderResourceBindings object.
8286 */
8287
8288/*!
8289 \fn void QRhiComputePipeline::setShaderResourceBindings(QRhiShaderResourceBindings *srb)
8290
8291 Associates with \a srb describing the resource binding layout and the
8292 resources (QRhiBuffer, QRhiTexture) themselves. The latter is optional. As
8293 with graphics pipelines, the \a srb passed in here can leave the actual
8294 buffer or texture objects unspecified (\nullptr) as long as there is
8295 another,
8296 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible}
8297 QRhiShaderResourceBindings bound via
8298 \l{QRhiCommandBuffer::setShaderResources()}{setShaderResources()} before
8299 recording the dispatch call.
8300 */
8301
8302/*!
8303 \class QRhiCommandBuffer
8304 \inmodule QtGuiPrivate
8305 \inheaderfile rhi/qrhi.h
8306 \since 6.6
8307 \brief Command buffer resource.
8308
8309 Not creatable by applications at the moment. The only ways to obtain a
8310 valid QRhiCommandBuffer are to get it from the targeted swapchain via
8311 QRhiSwapChain::currentFrameCommandBuffer(), or, in case of rendering
8312 completely offscreen, initializing one via QRhi::beginOffscreenFrame().
8313
8314 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
8315 for details.
8316 */
8317
8318/*!
8319 \enum QRhiCommandBuffer::IndexFormat
8320 Specifies the index data type
8321
8322 \value IndexUInt16 Unsigned 16-bit (quint16)
8323 \value IndexUInt32 Unsigned 32-bit (quint32)
8324 */
8325
8326/*!
8327 \enum QRhiCommandBuffer::BeginPassFlag
8328 Flag values for QRhi::beginPass()
8329
8330 \value ExternalContent Specifies that there will be a call to
8331 QRhiCommandBuffer::beginExternal() in this pass. Some backends, Vulkan in
8332 particular, will fail if this flag is not set and beginExternal() is still
8333 called.
8334
8335 \value DoNotTrackResourcesForCompute Specifies that there is no need to
8336 track resources used in this pass if the only purpose of such tracking is
8337 to generate barriers for compute. Implies that there are no compute passes
8338 in the frame. This is an optimization hint that may be taken into account
8339 by certain backends, OpenGL in particular, allowing them to skip certain
8340 operations. When this flag is set for a render pass in a frame, calling
8341 \l{QRhiCommandBuffer::beginComputePass()}{beginComputePass()} in that frame
8342 may lead to unexpected behavior, depending on the resource dependencies
8343 between the render and compute passes.
8344 */
8345
8346/*!
8347 \typedef QRhiCommandBuffer::DynamicOffset
8348
8349 Synonym for std::pair<int, quint32>. The first entry is the binding, the second
8350 is the offset in the buffer.
8351*/
8352
8353/*!
8354 \typedef QRhiCommandBuffer::VertexInput
8355
8356 Synonym for std::pair<QRhiBuffer *, quint32>. The second entry is an offset in
8357 the buffer specified by the first.
8358*/
8359
8360/*!
8361 \internal
8362 */
8363QRhiCommandBuffer::QRhiCommandBuffer(QRhiImplementation *rhi)
8364 : QRhiResource(rhi)
8365{
8366}
8367
8368/*!
8369 \return the resource type.
8370 */
8371QRhiResource::Type QRhiCommandBuffer::resourceType() const
8372{
8373 return CommandBuffer;
8374}
8375
8376static const char *resourceTypeStr(const QRhiResource *res)
8377{
8378 switch (res->resourceType()) {
8379 case QRhiResource::Buffer:
8380 return "Buffer";
8381 case QRhiResource::Texture:
8382 return "Texture";
8383 case QRhiResource::Sampler:
8384 return "Sampler";
8385 case QRhiResource::RenderBuffer:
8386 return "RenderBuffer";
8387 case QRhiResource::RenderPassDescriptor:
8388 return "RenderPassDescriptor";
8389 case QRhiResource::SwapChainRenderTarget:
8390 return "SwapChainRenderTarget";
8391 case QRhiResource::TextureRenderTarget:
8392 return "TextureRenderTarget";
8393 case QRhiResource::ShaderResourceBindings:
8394 return "ShaderResourceBindings";
8395 case QRhiResource::GraphicsPipeline:
8396 return "GraphicsPipeline";
8397 case QRhiResource::SwapChain:
8398 return "SwapChain";
8399 case QRhiResource::ComputePipeline:
8400 return "ComputePipeline";
8401 case QRhiResource::CommandBuffer:
8402 return "CommandBuffer";
8403 case QRhiResource::ShadingRateMap:
8404 return "ShadingRateMap";
8405 }
8406
8407 Q_UNREACHABLE_RETURN("");
8408}
8409
8410QRhiImplementation::~QRhiImplementation()
8411{
8412 qDeleteAll(resUpdPool);
8413
8414 // Be nice and show something about leaked stuff. Though we may not get
8415 // this far with some backends where the allocator or the api may check
8416 // and freak out for unfreed graphics objects in the derived dtor already.
8417#ifndef QT_NO_DEBUG
8418 // debug builds: just do it always
8419 static bool leakCheck = true;
8420#else
8421 // release builds: opt-in
8422 static bool leakCheck = qEnvironmentVariableIntValue("QT_RHI_LEAK_CHECK");
8423#endif
8424 if (!resources.isEmpty()) {
8425 if (leakCheck) {
8426 qWarning("QRhi %p going down with %d unreleased resources that own native graphics objects. This is not nice.",
8427 q, int(resources.size()));
8428 }
8429 for (auto it = resources.cbegin(), end = resources.cend(); it != end; ++it) {
8430 QRhiResource *res = it.key();
8431 const bool ownsNativeResources = it.value();
8432 if (leakCheck && ownsNativeResources)
8433 qWarning(" %s resource %p (%s)", resourceTypeStr(res), res, res->m_objectName.constData());
8434
8435 // Null out the resource's rhi pointer. This is why it makes sense to do null
8436 // checks in the destroy() implementations of the various resource types. It
8437 // allows to survive in bad applications that somehow manage to destroy a
8438 // resource of a QRhi after the QRhi itself.
8439 res->m_rhi = nullptr;
8440 }
8441 }
8442}
8443
8444bool QRhiImplementation::isCompressedFormat(QRhiTexture::Format format) const
8445{
8446 return (format >= QRhiTexture::BC1 && format <= QRhiTexture::BC7)
8447 || (format >= QRhiTexture::ETC2_RGB8 && format <= QRhiTexture::ETC2_RGBA8)
8448 || (format >= QRhiTexture::ASTC_4x4 && format <= QRhiTexture::ASTC_12x12);
8449}
8450
8451void QRhiImplementation::compressedFormatInfo(QRhiTexture::Format format, const QSize &size,
8452 quint32 *bpl, quint32 *byteSize,
8453 QSize *blockDim) const
8454{
8455 int xdim = 4;
8456 int ydim = 4;
8457 quint32 blockSize = 0;
8458
8459 switch (format) {
8460 case QRhiTexture::BC1:
8461 blockSize = 8;
8462 break;
8463 case QRhiTexture::BC2:
8464 blockSize = 16;
8465 break;
8466 case QRhiTexture::BC3:
8467 blockSize = 16;
8468 break;
8469 case QRhiTexture::BC4:
8470 blockSize = 8;
8471 break;
8472 case QRhiTexture::BC5:
8473 blockSize = 16;
8474 break;
8475 case QRhiTexture::BC6H:
8476 blockSize = 16;
8477 break;
8478 case QRhiTexture::BC7:
8479 blockSize = 16;
8480 break;
8481
8482 case QRhiTexture::ETC2_RGB8:
8483 blockSize = 8;
8484 break;
8485 case QRhiTexture::ETC2_RGB8A1:
8486 blockSize = 8;
8487 break;
8488 case QRhiTexture::ETC2_RGBA8:
8489 blockSize = 16;
8490 break;
8491
8492 case QRhiTexture::ASTC_4x4:
8493 blockSize = 16;
8494 break;
8495 case QRhiTexture::ASTC_5x4:
8496 blockSize = 16;
8497 xdim = 5;
8498 break;
8499 case QRhiTexture::ASTC_5x5:
8500 blockSize = 16;
8501 xdim = ydim = 5;
8502 break;
8503 case QRhiTexture::ASTC_6x5:
8504 blockSize = 16;
8505 xdim = 6;
8506 ydim = 5;
8507 break;
8508 case QRhiTexture::ASTC_6x6:
8509 blockSize = 16;
8510 xdim = ydim = 6;
8511 break;
8512 case QRhiTexture::ASTC_8x5:
8513 blockSize = 16;
8514 xdim = 8;
8515 ydim = 5;
8516 break;
8517 case QRhiTexture::ASTC_8x6:
8518 blockSize = 16;
8519 xdim = 8;
8520 ydim = 6;
8521 break;
8522 case QRhiTexture::ASTC_8x8:
8523 blockSize = 16;
8524 xdim = ydim = 8;
8525 break;
8526 case QRhiTexture::ASTC_10x5:
8527 blockSize = 16;
8528 xdim = 10;
8529 ydim = 5;
8530 break;
8531 case QRhiTexture::ASTC_10x6:
8532 blockSize = 16;
8533 xdim = 10;
8534 ydim = 6;
8535 break;
8536 case QRhiTexture::ASTC_10x8:
8537 blockSize = 16;
8538 xdim = 10;
8539 ydim = 8;
8540 break;
8541 case QRhiTexture::ASTC_10x10:
8542 blockSize = 16;
8543 xdim = ydim = 10;
8544 break;
8545 case QRhiTexture::ASTC_12x10:
8546 blockSize = 16;
8547 xdim = 12;
8548 ydim = 10;
8549 break;
8550 case QRhiTexture::ASTC_12x12:
8551 blockSize = 16;
8552 xdim = ydim = 12;
8553 break;
8554
8555 default:
8556 Q_UNREACHABLE();
8557 break;
8558 }
8559
8560 const quint32 wblocks = uint((size.width() + xdim - 1) / xdim);
8561 const quint32 hblocks = uint((size.height() + ydim - 1) / ydim);
8562
8563 if (bpl)
8564 *bpl = wblocks * blockSize;
8565 if (byteSize)
8566 *byteSize = wblocks * hblocks * blockSize;
8567 if (blockDim)
8568 *blockDim = QSize(xdim, ydim);
8569}
8570
8571void QRhiImplementation::textureFormatInfo(QRhiTexture::Format format, const QSize &size,
8572 quint32 *bpl, quint32 *byteSize, quint32 *bytesPerPixel) const
8573{
8574 if (isCompressedFormat(format)) {
8575 compressedFormatInfo(format, size, bpl, byteSize, nullptr);
8576 return;
8577 }
8578
8579 quint32 bpc = 0;
8580 switch (format) {
8581 case QRhiTexture::RGBA8:
8582 bpc = 4;
8583 break;
8584 case QRhiTexture::BGRA8:
8585 bpc = 4;
8586 break;
8587 case QRhiTexture::R8:
8588 bpc = 1;
8589 break;
8590 case QRhiTexture::RG8:
8591 bpc = 2;
8592 break;
8593 case QRhiTexture::R16:
8594 bpc = 2;
8595 break;
8596 case QRhiTexture::RG16:
8597 bpc = 4;
8598 break;
8599 case QRhiTexture::RED_OR_ALPHA8:
8600 bpc = 1;
8601 break;
8602
8603 case QRhiTexture::RGBA16F:
8604 bpc = 8;
8605 break;
8606 case QRhiTexture::RGBA32F:
8607 bpc = 16;
8608 break;
8609 case QRhiTexture::R16F:
8610 bpc = 2;
8611 break;
8612 case QRhiTexture::R32F:
8613 bpc = 4;
8614 break;
8615
8616 case QRhiTexture::RGB10A2:
8617 bpc = 4;
8618 break;
8619
8620 case QRhiTexture::D16:
8621 bpc = 2;
8622 break;
8623 case QRhiTexture::D24:
8624 case QRhiTexture::D24S8:
8625 case QRhiTexture::D32F:
8626 bpc = 4;
8627 break;
8628
8629 case QRhiTexture::D32FS8:
8630 bpc = 8;
8631 break;
8632
8633 case QRhiTexture::R8SI:
8634 case QRhiTexture::R8UI:
8635 bpc = 1;
8636 break;
8637 case QRhiTexture::R32SI:
8638 case QRhiTexture::R32UI:
8639 bpc = 4;
8640 break;
8641 case QRhiTexture::RG32SI:
8642 case QRhiTexture::RG32UI:
8643 bpc = 8;
8644 break;
8645 case QRhiTexture::RGBA32SI:
8646 case QRhiTexture::RGBA32UI:
8647 bpc = 16;
8648 break;
8649
8650 default:
8651 Q_UNREACHABLE();
8652 break;
8653 }
8654
8655 if (bpl)
8656 *bpl = uint(size.width()) * bpc;
8657 if (byteSize)
8658 *byteSize = uint(size.width() * size.height()) * bpc;
8659 if (bytesPerPixel)
8660 *bytesPerPixel = bpc;
8661}
8662
8663bool QRhiImplementation::isStencilSupportingFormat(QRhiTexture::Format format) const
8664{
8665 switch (format) {
8666 case QRhiTexture::D24S8:
8667 case QRhiTexture::D32FS8:
8668 return true;
8669 default:
8670 break;
8671 }
8672 return false;
8673}
8674
8675bool QRhiImplementation::sanityCheckGraphicsPipeline(QRhiGraphicsPipeline *ps)
8676{
8677 if (ps->cbeginShaderStages() == ps->cendShaderStages()) {
8678 qWarning("Cannot build a graphics pipeline without any stages");
8679 return false;
8680 }
8681
8682 bool hasVertexStage = false;
8683 for (auto it = ps->cbeginShaderStages(), itEnd = ps->cendShaderStages(); it != itEnd; ++it) {
8684 if (!it->shader().isValid()) {
8685 qWarning("Empty shader passed to graphics pipeline");
8686 return false;
8687 }
8688 if (it->type() == QRhiShaderStage::Vertex)
8689 hasVertexStage = true;
8690 }
8691 if (!hasVertexStage) {
8692 qWarning("Cannot build a graphics pipeline without a vertex stage");
8693 return false;
8694 }
8695
8696 if (!ps->renderPassDescriptor()) {
8697 qWarning("Cannot build a graphics pipeline without a QRhiRenderPassDescriptor");
8698 return false;
8699 }
8700
8701 if (!ps->shaderResourceBindings()) {
8702 qWarning("Cannot build a graphics pipeline without QRhiShaderResourceBindings");
8703 return false;
8704 }
8705
8706 return true;
8707}
8708
8709bool QRhiImplementation::sanityCheckShaderResourceBindings(QRhiShaderResourceBindings *srb)
8710{
8711#ifndef QT_NO_DEBUG
8712 bool bindingsOk = true;
8713 const int CHECKED_BINDINGS_COUNT = 64;
8714 bool bindingSeen[CHECKED_BINDINGS_COUNT] = {};
8715 for (auto it = srb->cbeginBindings(), end = srb->cendBindings(); it != end; ++it) {
8716 const int binding = shaderResourceBindingData(*it)->binding;
8717 if (binding >= CHECKED_BINDINGS_COUNT)
8718 continue;
8719 if (binding < 0) {
8720 qWarning("Invalid binding number %d", binding);
8721 bindingsOk = false;
8722 continue;
8723 }
8724 switch (shaderResourceBindingData(*it)->type) {
8725 case QRhiShaderResourceBinding::UniformBuffer:
8726 if (!bindingSeen[binding]) {
8727 bindingSeen[binding] = true;
8728 } else {
8729 qWarning("Uniform buffer duplicates an existing binding number %d", binding);
8730 bindingsOk = false;
8731 }
8732 break;
8733 case QRhiShaderResourceBinding::SampledTexture:
8734 if (!bindingSeen[binding]) {
8735 bindingSeen[binding] = true;
8736 } else {
8737 qWarning("Combined image sampler duplicates an existing binding number %d", binding);
8738 bindingsOk = false;
8739 }
8740 break;
8741 case QRhiShaderResourceBinding::Texture:
8742 if (!bindingSeen[binding]) {
8743 bindingSeen[binding] = true;
8744 } else {
8745 qWarning("Texture duplicates an existing binding number %d", binding);
8746 bindingsOk = false;
8747 }
8748 break;
8749 case QRhiShaderResourceBinding::Sampler:
8750 if (!bindingSeen[binding]) {
8751 bindingSeen[binding] = true;
8752 } else {
8753 qWarning("Sampler duplicates an existing binding number %d", binding);
8754 bindingsOk = false;
8755 }
8756 break;
8757 case QRhiShaderResourceBinding::ImageLoad:
8758 case QRhiShaderResourceBinding::ImageStore:
8759 case QRhiShaderResourceBinding::ImageLoadStore:
8760 if (!bindingSeen[binding]) {
8761 bindingSeen[binding] = true;
8762 } else {
8763 qWarning("Image duplicates an existing binding number %d", binding);
8764 bindingsOk = false;
8765 }
8766 break;
8767 case QRhiShaderResourceBinding::BufferLoad:
8768 case QRhiShaderResourceBinding::BufferStore:
8769 case QRhiShaderResourceBinding::BufferLoadStore:
8770 if (!bindingSeen[binding]) {
8771 bindingSeen[binding] = true;
8772 } else {
8773 qWarning("Buffer duplicates an existing binding number %d", binding);
8774 bindingsOk = false;
8775 }
8776 break;
8777 default:
8778 qWarning("Unknown binding type %d", int(shaderResourceBindingData(*it)->type));
8779 bindingsOk = false;
8780 break;
8781 }
8782 }
8783
8784 if (!bindingsOk) {
8785 qWarning() << *srb;
8786 return false;
8787 }
8788#else
8789 Q_UNUSED(srb);
8790#endif
8791 return true;
8792}
8793
8794int QRhiImplementation::effectiveSampleCount(int sampleCount) const
8795{
8796 // Stay compatible with QSurfaceFormat and friends where samples == 0 means the same as 1.
8797 const int s = qBound(1, sampleCount, 64);
8798 const QList<int> supported = supportedSampleCounts();
8799 int result = 1;
8800
8801 // Stay compatible with Qt 5 in that requesting an unsupported sample count
8802 // is not an error (although we still do a categorized debug print about
8803 // this), and rather a supported value, preferably a close one, not just 1,
8804 // is used instead. This is actually deviating from Qt 5 as that performs a
8805 // clamping only and does not handle cases such as when sample count 2 is
8806 // not supported but 4 is. (OpenGL handles things like that gracefully,
8807 // other APIs may not, so improve this by picking the next largest, or in
8808 // absence of that, the largest value; this with the goal to not reduce
8809 // quality by rather picking a larger-than-requested value than a smaller one)
8810
8811 for (int i = 0, ie = supported.count(); i != ie; ++i) {
8812 // assumes the 'supported' list is sorted
8813 if (supported[i] >= s) {
8814 result = supported[i];
8815 break;
8816 }
8817 }
8818
8819 if (result != s) {
8820 if (result == 1 && !supported.isEmpty())
8821 result = supported.last();
8822 qCDebug(QRHI_LOG_INFO, "Attempted to set unsupported sample count %d, using %d instead",
8823 sampleCount, result);
8824 }
8825
8826 return result;
8827}
8828
8829/*!
8830 \internal
8831 */
8832QRhi::QRhi()
8833{
8834}
8835
8836/*!
8837 Destructor. Destroys the backend and releases resources.
8838 */
8839QRhi::~QRhi()
8840{
8841 if (!d)
8842 return;
8843
8844 d->runCleanup();
8845
8846 qDeleteAll(d->pendingDeleteResources);
8847 d->pendingDeleteResources.clear();
8848
8849 d->destroy();
8850 delete d;
8851}
8852
8853QRhiImplementation *QRhiImplementation::newInstance(QRhi::Implementation impl, QRhiInitParams *params, QRhiNativeHandles *importDevice)
8854{
8855 QRhiImplementation *d = nullptr;
8856
8857 switch (impl) {
8858 case QRhi::Null:
8859 d = new QRhiNull(static_cast<QRhiNullInitParams *>(params));
8860 break;
8861 case QRhi::Vulkan:
8862#if QT_CONFIG(vulkan)
8863 d = new QRhiVulkan(static_cast<QRhiVulkanInitParams *>(params),
8864 static_cast<QRhiVulkanNativeHandles *>(importDevice));
8865 break;
8866#else
8867 Q_UNUSED(importDevice);
8868 qWarning("This build of Qt has no Vulkan support");
8869 break;
8870#endif
8871 case QRhi::OpenGLES2:
8872#ifndef QT_NO_OPENGL
8873 d = new QRhiGles2(static_cast<QRhiGles2InitParams *>(params),
8874 static_cast<QRhiGles2NativeHandles *>(importDevice));
8875 break;
8876#else
8877 qWarning("This build of Qt has no OpenGL support");
8878 break;
8879#endif
8880 case QRhi::D3D11:
8881#ifdef Q_OS_WIN
8882 d = new QRhiD3D11(static_cast<QRhiD3D11InitParams *>(params),
8883 static_cast<QRhiD3D11NativeHandles *>(importDevice));
8884 break;
8885#else
8886 qWarning("This platform has no Direct3D 11 support");
8887 break;
8888#endif
8889 case QRhi::Metal:
8890#if QT_CONFIG(metal)
8891 d = new QRhiMetal(static_cast<QRhiMetalInitParams *>(params),
8892 static_cast<QRhiMetalNativeHandles *>(importDevice));
8893 break;
8894#else
8895 qWarning("This platform has no Metal support");
8896 break;
8897#endif
8898 case QRhi::D3D12:
8899#ifdef Q_OS_WIN
8900#ifdef QRHI_D3D12_AVAILABLE
8901 d = new QRhiD3D12(static_cast<QRhiD3D12InitParams *>(params),
8902 static_cast<QRhiD3D12NativeHandles *>(importDevice));
8903 break;
8904#else
8905 qWarning("Qt was built without Direct3D 12 support. "
8906 "This is likely due to having ancient SDK headers (such as d3d12.h) in the Qt build environment. "
8907 "Rebuild Qt with an SDK supporting D3D12 features introduced in Windows 10 version 1703, "
8908 "or use an MSVC build as those typically are built with more up-to-date SDKs.");
8909 break;
8910#endif
8911#else
8912 qWarning("This platform has no Direct3D 12 support");
8913 break;
8914#endif
8915 }
8916
8917 return d;
8918}
8919
8920void QRhiImplementation::prepareForCreate(QRhi *rhi, QRhi::Implementation impl, QRhi::Flags flags, QRhiAdapter *adapter)
8921{
8922 q = rhi;
8923
8924 debugMarkers = flags.testFlag(QRhi::EnableDebugMarkers);
8925
8926 implType = impl;
8927 implThread = QThread::currentThread();
8928
8929 requestedRhiAdapter = adapter;
8930}
8931
8932QRhi::AdapterList QRhiImplementation::enumerateAdaptersBeforeCreate(QRhiNativeHandles *) const
8933{
8934 return {};
8935}
8936
8937/*!
8938 \overload
8939
8940 Equivalent to create(\a impl, \a params, \a flags, \a importDevice, \c nullptr).
8941 */
8942QRhi *QRhi::create(Implementation impl, QRhiInitParams *params, Flags flags, QRhiNativeHandles *importDevice)
8943{
8944 return create(impl, params, flags, importDevice, nullptr);
8945}
8946
8947/*!
8948 \return a new QRhi instance with a backend for the graphics API specified
8949 by \a impl with the specified \a flags. \return \c nullptr if the
8950 function fails.
8951
8952 \a params must point to an instance of one of the backend-specific
8953 subclasses of QRhiInitParams, such as, QRhiVulkanInitParams,
8954 QRhiMetalInitParams, QRhiD3D11InitParams, QRhiD3D12InitParams,
8955 QRhiGles2InitParams. See these classes for examples on creating a QRhi.
8956
8957 QRhi by design does not implement any fallback logic: if the specified API
8958 cannot be initialized, create() will fail, with warnings printed on the
8959 debug output by the backends. The clients of QRhi, for example Qt Quick,
8960 may however provide additional logic that allow falling back to an API
8961 different than what was requested, depending on the platform. If the
8962 intention is just to test if initialization would succeed when calling
8963 create() at later point, it is preferable to use probe() instead of
8964 create(), because with some backends probing can be implemented in a more
8965 lightweight manner as opposed to create(), which performs full
8966 initialization of the infrastructure and is wasteful if that QRhi instance
8967 is then thrown immediately away.
8968
8969 \a importDevice allows using an already existing graphics device, without
8970 QRhi creating its own. When not null, this parameter must point to an
8971 instance of one of the subclasses of QRhiNativeHandles:
8972 QRhiVulkanNativeHandles, QRhiD3D11NativeHandles, QRhiD3D12NativeHandles,
8973 QRhiMetalNativeHandles, QRhiGles2NativeHandles. The exact details and
8974 semantics depend on the backand and the underlying graphics API.
8975
8976 Specifying a QRhiAdapter in \a adapter offers a transparent, cross-API
8977 alternative to passing in a \c VkPhysicalDevice via QRhiVulkanNativeHandles,
8978 or an adapter LUID via QRhiD3D12NativeHandles. The ownership of \a adapter
8979 is not taken. See enumerateAdapters() for more information on this approach.
8980
8981 \note \a importDevice and \a adapter cannot be both specified.
8982
8983 \sa probe()
8984 */
8985QRhi *QRhi::create(Implementation impl, QRhiInitParams *params, Flags flags, QRhiNativeHandles *importDevice, QRhiAdapter *adapter)
8986{
8987 if (adapter && importDevice)
8988 qWarning("adapter and importDevice should not both be non-null in QRhi::create()");
8989
8990 std::unique_ptr<QRhiImplementation> rd(QRhiImplementation::newInstance(impl, params, importDevice));
8991 if (!rd)
8992 return nullptr;
8993
8994 std::unique_ptr<QRhi> r(new QRhi);
8995 r->d = rd.release();
8996 r->d->prepareForCreate(r.get(), impl, flags, adapter);
8997 if (!r->d->create(flags))
8998 return nullptr;
8999
9000 return r.release();
9001}
9002
9003/*!
9004 \return true if create() can be expected to succeed when called the given
9005 \a impl and \a params.
9006
9007 For some backends this is equivalent to calling create(), checking its
9008 return value, and then destroying the resulting QRhi.
9009
9010 For others, in particular with Metal, there may be a specific probing
9011 implementation, which allows testing in a more lightweight manner without
9012 polluting the debug output with warnings upon failures.
9013
9014 \sa create()
9015 */
9016bool QRhi::probe(QRhi::Implementation impl, QRhiInitParams *params)
9017{
9018 bool ok = false;
9019
9020 // The only place currently where this makes sense is Metal, where the API
9021 // is simple enough so that a special probing function - doing nothing but
9022 // a MTLCreateSystemDefaultDevice - is reasonable. Elsewhere, just call
9023 // create() and then drop the result.
9024
9025 if (impl == Metal) {
9026#if QT_CONFIG(metal)
9027 ok = QRhiMetal::probe(static_cast<QRhiMetalInitParams *>(params));
9028#endif
9029 } else {
9030 QRhi *rhi = create(impl, params);
9031 ok = rhi != nullptr;
9032 delete rhi;
9033 }
9034 return ok;
9035}
9036
9037/*!
9038 \typedef QRhi::AdapterList
9039 \relates QRhi
9040 \since 6.10
9041
9042 Synonym for QVector<QRhiAdapter *>.
9043*/
9044
9045/*!
9046 \return the list of adapters (physical devices) present, or an empty list
9047 when such control is not available with a given graphics API.
9048
9049 Backends where such level of control is not available, the returned list is
9050 always empty. Thus an empty list does not indicate there are no graphics
9051 devices in the system, but that fine-grained control over selecting which
9052 one to use is not available.
9053
9054 Backends for Direct 3D 11, Direct 3D 12, and Vulkan can be expected to fully
9055 support enumerating adapters. Others may not. The backend is specified by \a
9056 impl. A QRhiAdapter returned from this function must only be used in a
9057 create() call with the same \a impl. Some underlying APIs may present
9058 further limitations, with Vulkan in particular the QRhiAdapter is specified
9059 to the QVulkanInstance (\c VkInstance).
9060
9061 The caller is expected to destroy the QRhiAdapter objects in the list. Apart
9062 from querying \l{QRhiAdapter::}{info()}, the only purpose of these objects is
9063 to be passed on to create(), or the corresponding functions in higher layers
9064 such as Qt Quick.
9065
9066 The following snippet, written specifically for Vulkan, shows how to
9067 enumerate the available physical devices and request to create a QRhi for
9068 the chosen one. This in practice is equivalent to passing in a \c
9069 VkPhysicalDevice via a QRhiVulkanNativeHandles to create(), but it involves
9070 less API-specific code on the application side:
9071
9072 \code
9073 QRhiVulkanInitParams initParams;
9074 initParams.inst = &vulkanInstance;
9075 QRhi::AdapterList adapters = QRhi::enumerateAdapters(QRhi::Vulkan, &initParams);
9076 QRhiAdapter *chosenAdapter = nullptr;
9077 for (QRhiAdapter *adapter : adapters) {
9078 if (looksGood(adapter->info())) {
9079 chosenAdapter = adapter;
9080 break;
9081 }
9082 }
9083 QRhi *rhi = QRhi::create(QRhi::Vulkan, &initParams, {}, nullptr, chosenAdapter);
9084 qDeleteAll(adapters);
9085 \endcode
9086
9087 Passing in \a params is required due to some of the underlying graphics
9088 APIs' design. With Vulkan in particular, the QVulkanInstance must be
9089 provided, since enumerating is not possible without it. Other fields in the
9090 backend-specific \a params will not actually be used by this function.
9091
9092 \a nativeHandles is optional. When specified, it must be a valid
9093 QRhiD3D11NativeHandles, QRhiD3D12NativeHandles, or QRhiVulkanNativeHandles,
9094 similarly to create(). However, unlike create(), only the physical device
9095 (in case of Vulkan) or the adapter LUID (in case of D3D) fields are used,
9096 all other fields are ignored. This can be used the restrict the results to a
9097 given adapter. The returned list will contain 1 or 0 elements in this case.
9098
9099 Note how in the previous code snippet the looksGood() function
9100 implementation cannot perform any platform-specific filtering based on the
9101 true adapter / physical device identity, such as the adapter LUID on Windows
9102 or the VkPhysicalDevice with Vulkan. This is because QRhiDriverInfo does not
9103 contain platform-specific data. Instead, use \a nativeHandles to get the
9104 results filtered already inside enumerateAdapters().
9105
9106 The following two snippets, using Direct 3D 12 as an example, are equivalent
9107 in practice:
9108
9109 \code
9110 // enumerateAdapters-based approach from Qt 6.10 on
9111 QRhiD3D12InitParams initParams;
9112 QRhiD3D12NativeHandles nativeHandles;
9113 nativeHandles.adapterLuidLow = luid.LowPart; // retrieved a LUID from somewhere, now pass it on to Qt
9114 nativeHandles.adapterLuidHigh = luid.HighPart;
9115 QRhi::AdapterList adapters = QRhi::enumerateAdapters(QRhi::D3D12, &initParams, &nativeHandles);
9116 if (adapters.isEmpty()) { qWarning("Requested adapter was not found"); }
9117 QRhi *rhi = QRhi::create(QRhi::D3D12, &initParams, {}, nullptr, adapters[0]);
9118 qDeleteAll(adapters);
9119 \endcode
9120
9121 \code
9122 // traditional approach, more lightweight
9123 QRhiD3D12InitParams initParams;
9124 QRhiD3D12NativeHandles nativeHandles;
9125 nativeHandles.adapterLuidLow = luid.LowPart; // retrieved a LUID from somewhere, now pass it on to Qt
9126 nativeHandles.adapterLuidHigh = luid.HighPart;
9127 QRhi *rhi = QRhi::create(QRhi::D3D12, &initParams, {}, &nativeHandles, nullptr);
9128 \endcode
9129
9130 \since 6.10
9131 \sa create()
9132 */
9133QRhi::AdapterList QRhi::enumerateAdapters(Implementation impl, QRhiInitParams *params, QRhiNativeHandles *nativeHandles)
9134{
9135 std::unique_ptr<QRhiImplementation> rd(QRhiImplementation::newInstance(impl, params, nullptr));
9136 if (!rd)
9137 return {};
9138
9139 return rd->enumerateAdaptersBeforeCreate(nativeHandles);
9140}
9141
9142/*!
9143 \struct QRhiSwapChainProxyData
9144 \inmodule QtGuiPrivate
9145 \inheaderfile rhi/qrhi.h
9146 \since 6.6
9147
9148 \brief Opaque data describing native objects needed to set up a swapchain.
9149
9150 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9151 for details.
9152
9153 \sa QRhi::updateSwapChainProxyData()
9154 */
9155
9156/*!
9157 Generates and returns a QRhiSwapChainProxyData struct containing opaque
9158 data specific to the backend and graphics API specified by \a impl. \a
9159 window is the QWindow a swapchain is targeting.
9160
9161 The returned struct can be passed to QRhiSwapChain::setProxyData(). This
9162 makes sense in threaded rendering systems: this static function is expected
9163 to be called on the \b{main (gui) thread}, unlike all QRhi operations, then
9164 transferred to the thread working with the QRhi and QRhiSwapChain and passed
9165 on to the swapchain. This allows doing native platform queries that are
9166 only safe to be called on the main thread, for example to query the
9167 CAMetalLayer from a NSView, and then passing on the data to the
9168 QRhiSwapChain living on the rendering thread. With the Metal example, doing
9169 the view.layer access on a dedicated rendering thread causes a warning in
9170 the Xcode Thread Checker. With the data proxy mechanism, this is avoided.
9171
9172 When threads are not involved, generating and passing on the
9173 QRhiSwapChainProxyData is not required: backends are guaranteed to be able
9174 to query whatever is needed on their own, and if everything lives on the
9175 main (gui) thread, that should be sufficient.
9176
9177 \note \a impl should match what the QRhi is created with. For example,
9178 calling with QRhi::Metal on a non-Apple platform will not generate any
9179 useful data.
9180 */
9181QRhiSwapChainProxyData QRhi::updateSwapChainProxyData(QRhi::Implementation impl, QWindow *window)
9182{
9183#if QT_CONFIG(metal)
9184 if (impl == Metal)
9185 return QRhiMetal::updateSwapChainProxyData(window);
9186#else
9187 Q_UNUSED(impl);
9188 Q_UNUSED(window);
9189#endif
9190 return {};
9191}
9192
9193/*!
9194 \return the backend type for this QRhi.
9195 */
9196QRhi::Implementation QRhi::backend() const
9197{
9198 return d->implType;
9199}
9200
9201/*!
9202 \return a friendly name for the backend \a impl, usually the name of the 3D
9203 API in use.
9204 */
9205const char *QRhi::backendName(Implementation impl)
9206{
9207 switch (impl) {
9208 case QRhi::Null:
9209 return "Null";
9210 case QRhi::Vulkan:
9211 return "Vulkan";
9212 case QRhi::OpenGLES2:
9213 return "OpenGL";
9214 case QRhi::D3D11:
9215 return "D3D11";
9216 case QRhi::Metal:
9217 return "Metal";
9218 case QRhi::D3D12:
9219 return "D3D12";
9220 }
9221
9222 Q_UNREACHABLE_RETURN("Unknown");
9223}
9224
9225/*!
9226 \return the backend type as string for this QRhi.
9227 */
9228const char *QRhi::backendName() const
9229{
9230 return backendName(d->implType);
9231}
9232
9233/*!
9234 \enum QRhiDriverInfo::DeviceType
9235 Specifies the graphics device's type, when the information is available.
9236
9237 In practice this is only applicable with Vulkan and Metal. With Direct 3D
9238 11 and 12, using an adapter with the software flag set leads to the value
9239 \c CpuDevice. Otherwise, and with OpenGL, the value is always UnknownDevice.
9240
9241 \value UnknownDevice
9242 \value IntegratedDevice
9243 \value DiscreteDevice
9244 \value ExternalDevice
9245 \value VirtualDevice
9246 \value CpuDevice
9247*/
9248
9249/*!
9250 \struct QRhiDriverInfo
9251 \inmodule QtGuiPrivate
9252 \inheaderfile rhi/qrhi.h
9253 \since 6.6
9254
9255 \brief Describes the physical device, adapter, or graphics API
9256 implementation that is used by an initialized QRhi.
9257
9258 Graphics APIs offer different levels and kinds of information. The only
9259 value that is available across all APIs is the deviceName, which is a
9260 freetext description of the physical device, adapter, or is a combination
9261 of the strings reported for \c{GL_VENDOR} + \c{GL_RENDERER} +
9262 \c{GL_VERSION}. The deviceId is always 0 for OpenGL. vendorId is always 0
9263 for OpenGL and Metal. deviceType is always UnknownDevice for OpenGL and
9264 Direct 3D.
9265
9266 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9267 for details.
9268 */
9269
9270/*!
9271 \variable QRhiDriverInfo::deviceName
9272
9273 \sa QRhi::driverInfo()
9274*/
9275
9276/*!
9277 \variable QRhiDriverInfo::deviceId
9278
9279 \sa QRhi::driverInfo()
9280*/
9281
9282/*!
9283 \variable QRhiDriverInfo::vendorId
9284
9285 \sa QRhi::driverInfo()
9286*/
9287
9288/*!
9289 \variable QRhiDriverInfo::deviceType
9290
9291 \sa QRhi::driverInfo(), QRhiDriverInfo::DeviceType
9292*/
9293
9294#ifndef QT_NO_DEBUG_STREAM
9295static inline const char *deviceTypeStr(QRhiDriverInfo::DeviceType type)
9296{
9297 switch (type) {
9298 case QRhiDriverInfo::UnknownDevice:
9299 return "Unknown";
9300 case QRhiDriverInfo::IntegratedDevice:
9301 return "Integrated";
9302 case QRhiDriverInfo::DiscreteDevice:
9303 return "Discrete";
9304 case QRhiDriverInfo::ExternalDevice:
9305 return "External";
9306 case QRhiDriverInfo::VirtualDevice:
9307 return "Virtual";
9308 case QRhiDriverInfo::CpuDevice:
9309 return "Cpu";
9310 }
9311
9312 Q_UNREACHABLE_RETURN(nullptr);
9313}
9314QDebug operator<<(QDebug dbg, const QRhiDriverInfo &info)
9315{
9316 QDebugStateSaver saver(dbg);
9317 dbg.nospace() << "QRhiDriverInfo(deviceName=" << info.deviceName
9318 << " deviceId=0x" << Qt::hex << info.deviceId
9319 << " vendorId=0x" << info.vendorId
9320 << " deviceType=" << deviceTypeStr(info.deviceType)
9321 << ')';
9322 return dbg;
9323}
9324#endif
9325
9326/*!
9327 \return metadata for the graphics device used by this successfully
9328 initialized QRhi instance.
9329 */
9330QRhiDriverInfo QRhi::driverInfo() const
9331{
9332 return d->driverInfo();
9333}
9334
9335/*!
9336 \class QRhiAdapter
9337 \inmodule QtGuiPrivate
9338 \inheaderfile rhi/qrhi.h
9339 \since 6.10
9340
9341 \brief Represents a physical graphics device.
9342
9343 Some QRhi backends target graphics APIs that expose the concept of \c
9344 adapters or \c{physical devices}. Call the static \l
9345 {QRhi::}{enumerateAdapters()} function to retrieve a list of the adapters
9346 present in the system. Pass one of the returned QRhiAdapter objects to \l
9347 {QRhi::}{create()} in order to request using the adapter or physical device
9348 the QRhiAdapter corresponds to. Other than exposing the QRhiDriverInfo,
9349 QRhiAdapter is to be treated as an opaque handle.
9350
9351 \note With Vulkan, the QRhiAdapter is valid only as long as the
9352 QVulkanInstance that was used for \l{QRhi::}{enumerateAdapters()} is valid.
9353 This also means that a QRhiAdapter is tied to the Vulkan instance
9354 (QVulkanInstance, \c VkInstance) and cannot be used in the context of
9355 another Vulkan instance.
9356
9357 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9358 for details.
9359 */
9360
9361/*!
9362 \fn virtual QRhiDriverInfo QRhiAdapter::info() const = 0
9363
9364 \return the corresponding QRhiDriverInfo.
9365 */
9366
9367/*!
9368 \internal
9369 */
9370QRhiAdapter::~QRhiAdapter()
9371{
9372}
9373
9374/*!
9375 \return the thread on which the QRhi was \l{QRhi::create()}{initialized}.
9376 */
9377QThread *QRhi::thread() const
9378{
9379 return d->implThread;
9380}
9381
9382/*!
9383 Registers a \a callback that is invoked when the QRhi is destroyed.
9384
9385 The callback will run with the graphics resource still available, so this
9386 provides an opportunity for the application to cleanly release QRhiResource
9387 instances belonging to the QRhi. This is particularly useful for managing
9388 the lifetime of resources stored in \c cache type of objects, where the
9389 cache holds QRhiResources or objects containing QRhiResources.
9390
9391 \sa ~QRhi()
9392 */
9393void QRhi::addCleanupCallback(const CleanupCallback &callback)
9394{
9395 d->addCleanupCallback(callback);
9396}
9397
9398/*!
9399 \overload
9400
9401 Registers \a callback to be invoked when the QRhi is destroyed. This
9402 overload takes an opaque pointer, \a key, that is used to ensure that a
9403 given callback is registered (and so called) only once.
9404
9405 \sa removeCleanupCallback()
9406 */
9407void QRhi::addCleanupCallback(const void *key, const CleanupCallback &callback)
9408{
9409 d->addCleanupCallback(key, callback);
9410}
9411
9412/*!
9413 Deregisters the callback with \a key. If no cleanup callback was registered
9414 with \a key, the function does nothing. Callbacks registered without a key
9415 cannot be removed.
9416
9417 \sa addCleanupCallback()
9418 */
9419void QRhi::removeCleanupCallback(const void *key)
9420{
9421 d->removeCleanupCallback(key);
9422}
9423
9424void QRhiImplementation::runCleanup()
9425{
9426 for (const QRhi::CleanupCallback &f : std::as_const(cleanupCallbacks))
9427 f(q);
9428
9429 cleanupCallbacks.clear();
9430
9431 for (auto it = keyedCleanupCallbacks.cbegin(), end = keyedCleanupCallbacks.cend(); it != end; ++it)
9432 it.value()(q);
9433
9434 keyedCleanupCallbacks.clear();
9435}
9436
9437/*!
9438 \class QRhiResourceUpdateBatch
9439 \inmodule QtGuiPrivate
9440 \inheaderfile rhi/qrhi.h
9441 \since 6.6
9442 \brief Records upload and copy type of operations.
9443
9444 With QRhi it is no longer possible to perform copy type of operations at
9445 arbitrary times. Instead, all such operations are recorded into batches
9446 that are then passed, most commonly, to QRhiCommandBuffer::beginPass().
9447 What then happens under the hood is hidden from the application: the
9448 underlying implementations can defer and implement these operations in
9449 various different ways.
9450
9451 A resource update batch owns no graphics resources and does not perform any
9452 actual operations on its own. It should rather be viewed as a command
9453 buffer for update, upload, and copy type of commands.
9454
9455 To get an available, empty batch from the pool, call
9456 QRhi::nextResourceUpdateBatch().
9457
9458 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
9459 for details.
9460 */
9461
9462/*!
9463 \internal
9464 */
9465QRhiResourceUpdateBatch::QRhiResourceUpdateBatch(QRhiImplementation *rhi)
9466 : d(new QRhiResourceUpdateBatchPrivate)
9467{
9468 d->q = this;
9469 d->rhi = rhi;
9470}
9471
9472QRhiResourceUpdateBatch::~QRhiResourceUpdateBatch()
9473{
9474 delete d;
9475}
9476
9477/*!
9478 \return the batch to the pool. This should only be used when the batch is
9479 not passed to one of QRhiCommandBuffer::beginPass(),
9480 QRhiCommandBuffer::endPass(), or QRhiCommandBuffer::resourceUpdate()
9481 because these implicitly call destroy().
9482
9483 \note QRhiResourceUpdateBatch instances must never by \c deleted by
9484 applications.
9485 */
9486void QRhiResourceUpdateBatch::release()
9487{
9488 d->free();
9489}
9490
9491/*!
9492 Copies all queued operations from the \a other batch into this one.
9493
9494 \note \a other may no longer contain valid data after the merge operation,
9495 and must not be submitted, but it will still need to be released by calling
9496 release().
9497
9498 This allows for a convenient pattern where resource updates that are
9499 already known during the initialization step are collected into a batch
9500 that is then merged into another when starting to first render pass later
9501 on:
9502
9503 \code
9504 void init()
9505 {
9506 initialUpdates = rhi->nextResourceUpdateBatch();
9507 initialUpdates->uploadStaticBuffer(vbuf, vertexData);
9508 initialUpdates->uploadStaticBuffer(ibuf, indexData);
9509 // ...
9510 }
9511
9512 void render()
9513 {
9514 QRhiResourceUpdateBatch *resUpdates = rhi->nextResourceUpdateBatch();
9515 if (initialUpdates) {
9516 resUpdates->merge(initialUpdates);
9517 initialUpdates->release();
9518 initialUpdates = nullptr;
9519 }
9520 // resUpdates->updateDynamicBuffer(...);
9521 cb->beginPass(rt, clearCol, clearDs, resUpdates);
9522 }
9523 \endcode
9524 */
9525void QRhiResourceUpdateBatch::merge(QRhiResourceUpdateBatch *other)
9526{
9527 d->merge(other->d);
9528}
9529
9530/*!
9531 \return true until the number of buffer and texture operations enqueued
9532 onto this batch is below a reasonable limit.
9533
9534 The return value is false when the number of buffer and/or texture
9535 operations added to this batch have reached, or are about to reach, a
9536 certain limit. The batch is fully functional afterwards as well, but may
9537 need to allocate additional memory. Therefore, a renderer that collects
9538 lots of buffer and texture updates in a single batch when preparing a frame
9539 may want to consider \l{QRhiCommandBuffer::resourceUpdate()}{submitting the
9540 batch} and \l{QRhi::nextResourceUpdateBatch()}{starting a new one} when
9541 this function returns false.
9542 */
9543bool QRhiResourceUpdateBatch::hasOptimalCapacity() const
9544{
9545 return d->hasOptimalCapacity();
9546}
9547
9548/*!
9549 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9550 QRhiBuffer::Dynamic.
9551
9552 The region is specified \a offset and \a size. The actual bytes to write
9553 are specified by \a data which must have at least \a size bytes available.
9554
9555 \a data is copied and can safely be destroyed or changed once this function
9556 returns.
9557
9558 \note If host writes are involved, which is the case with
9559 updateDynamicBuffer() typically as such buffers are backed by host visible
9560 memory with most backends, they may accumulate within a frame. Thus pass 1
9561 reading a region changed by a batch passed to pass 2 may see the changes
9562 specified in pass 2's update batch.
9563
9564 \note QRhi transparently manages double buffering in order to prevent
9565 stalling the graphics pipeline. The fact that a QRhiBuffer may have
9566 multiple native buffer objects underneath can be safely ignored when using
9567 the QRhi and QRhiResourceUpdateBatch.
9568 */
9569void QRhiResourceUpdateBatch::updateDynamicBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
9570{
9571 if (size > 0) {
9572 const int idx = d->activeBufferOpCount++;
9573 const int opListSize = d->bufferOps.size();
9574 if (idx < opListSize)
9575 QRhiResourceUpdateBatchPrivate::BufferOp::changeToDynamicUpdate(&d->bufferOps[idx], buf, offset, size, data);
9576 else
9577 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::dynamicUpdate(buf, offset, size, data));
9578 }
9579}
9580
9581/*!
9582 \overload
9583 \since 6.10
9584
9585 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9586 QRhiBuffer::Dynamic.
9587
9588 \a data is moved into the batch instead of copied with this overload.
9589 */
9590void QRhiResourceUpdateBatch::updateDynamicBuffer(QRhiBuffer *buf, quint32 offset, QByteArray data)
9591{
9592 if (!data.isEmpty()) {
9593 const int idx = d->activeBufferOpCount++;
9594 const int opListSize = d->bufferOps.size();
9595 if (idx < opListSize)
9596 QRhiResourceUpdateBatchPrivate::BufferOp::changeToDynamicUpdate(&d->bufferOps[idx], buf, offset, std::move(data));
9597 else
9598 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::dynamicUpdate(buf, offset, std::move(data)));
9599 }
9600}
9601
9602/*!
9603 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9604 QRhiBuffer::Immutable or QRhiBuffer::Static.
9605
9606 The region is specified \a offset and \a size. The actual bytes to write
9607 are specified by \a data which must have at least \a size bytes available.
9608
9609 \a data is copied and can safely be destroyed or changed once this function
9610 returns.
9611 */
9612void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
9613{
9614 if (size > 0) {
9615 const int idx = d->activeBufferOpCount++;
9616 if (idx < d->bufferOps.size())
9617 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, offset, size, data);
9618 else
9619 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, offset, size, data));
9620 }
9621}
9622
9623/*!
9624 \overload
9625 \since 6.10
9626
9627 Enqueues updating a region of a QRhiBuffer \a buf created with the type
9628 QRhiBuffer::Immutable or QRhiBuffer::Static.
9629
9630 \a data is moved into the batch instead of copied with this overload.
9631 */
9632void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, QByteArray data)
9633{
9634 if (!data.isEmpty()) {
9635 const int idx = d->activeBufferOpCount++;
9636 if (idx < d->bufferOps.size())
9637 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, offset, std::move(data));
9638 else
9639 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, offset, std::move(data)));
9640 }
9641}
9642
9643/*!
9644 \overload
9645
9646 Enqueues updating the entire QRhiBuffer \a buf created with the type
9647 QRhiBuffer::Immutable or QRhiBuffer::Static.
9648 */
9649void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, const void *data)
9650{
9651 if (buf->size() > 0) {
9652 const int idx = d->activeBufferOpCount++;
9653 if (idx < d->bufferOps.size())
9654 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, 0, 0, data);
9655 else
9656 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, 0, 0, data));
9657 }
9658}
9659
9660/*!
9661 \overload
9662 \since 6.10
9663
9664 Enqueues updating the entire QRhiBuffer \a buf created with the type
9665 QRhiBuffer::Immutable or QRhiBuffer::Static.
9666
9667 \a data is moved into the batch instead of copied with this overload.
9668
9669 \a data size must equal the size of \a buf.
9670 */
9671void QRhiResourceUpdateBatch::uploadStaticBuffer(QRhiBuffer *buf, QByteArray data)
9672{
9673 if (buf->size() > 0 && quint32(data.size()) == buf->size()) {
9674 const int idx = d->activeBufferOpCount++;
9675 if (idx < d->bufferOps.size())
9676 QRhiResourceUpdateBatchPrivate::BufferOp::changeToStaticUpload(&d->bufferOps[idx], buf, 0, std::move(data));
9677 else
9678 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::staticUpload(buf, 0, std::move(data)));
9679 }
9680}
9681
9682/*!
9683 Enqueues reading back a region of the QRhiBuffer \a buf. The size of the
9684 region is specified by \a size in bytes, \a offset is the offset in bytes
9685 to start reading from.
9686
9687 A readback is asynchronous. \a result contains a callback that is invoked
9688 when the operation has completed. The data is provided in
9689 QRhiReadbackResult::data. Upon successful completion that QByteArray
9690 will have a size equal to \a size. On failure the QByteArray will be empty.
9691
9692 \note Reading buffers with a usage different than QRhiBuffer::UniformBuffer
9693 is supported only when the QRhi::ReadBackNonUniformBuffer feature is
9694 reported as supported.
9695
9696 \note The asynchronous readback is guaranteed to have completed when one of
9697 the following conditions is met: \l{QRhi::finish()}{finish()} has been
9698 called; or, at least \c N frames have been \l{QRhi::endFrame()}{submitted},
9699 including the frame that issued the readback operation, and the
9700 \l{QRhi::beginFrame()}{recording of a new frame} has been started, where \c
9701 N is the \l{QRhi::resourceLimit()}{resource limit value} returned for
9702 QRhi::MaxAsyncReadbackFrames.
9703
9704 \sa readBackTexture(), QRhi::isFeatureSupported(), QRhi::resourceLimit()
9705 */
9706void QRhiResourceUpdateBatch::readBackBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, QRhiReadbackResult *result)
9707{
9708 const int idx = d->activeBufferOpCount++;
9709 if (idx < d->bufferOps.size())
9710 d->bufferOps[idx] = QRhiResourceUpdateBatchPrivate::BufferOp::read(buf, offset, size, result);
9711 else
9712 d->bufferOps.append(QRhiResourceUpdateBatchPrivate::BufferOp::read(buf, offset, size, result));
9713}
9714
9715/*!
9716 Enqueues uploading the image data for one or more mip levels in one or more
9717 layers of the texture \a tex.
9718
9719 The details of the copy (source QImage or compressed texture data, regions,
9720 target layers and levels) are described in \a desc.
9721 */
9722void QRhiResourceUpdateBatch::uploadTexture(QRhiTexture *tex, const QRhiTextureUploadDescription &desc)
9723{
9724 if (desc.cbeginEntries() != desc.cendEntries()) {
9725 const int idx = d->activeTextureOpCount++;
9726 if (idx < d->textureOps.size())
9727 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::upload(tex, desc);
9728 else
9729 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::upload(tex, desc));
9730 }
9731}
9732
9733/*!
9734 Enqueues uploading the image data for mip level 0 of layer 0 of the texture
9735 \a tex.
9736
9737 \a tex must have an uncompressed format. Its format must also be compatible
9738 with the QImage::format() of \a image. The source data is given in \a
9739 image.
9740 */
9741void QRhiResourceUpdateBatch::uploadTexture(QRhiTexture *tex, const QImage &image)
9742{
9743 uploadTexture(tex,
9744 QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(image)));
9745}
9746
9747/*!
9748 Enqueues a texture-to-texture copy operation from \a src into \a dst as
9749 described by \a desc.
9750
9751 \note The source texture \a src must be created with
9752 QRhiTexture::UsedAsTransferSource.
9753
9754 \note The format of the textures must match. With most graphics
9755 APIs the data is copied as-is without any format conversions. If
9756 \a dst and \a src are created with different formats, unspecified
9757 issues may arise.
9758 */
9759void QRhiResourceUpdateBatch::copyTexture(QRhiTexture *dst, QRhiTexture *src, const QRhiTextureCopyDescription &desc)
9760{
9761 const int idx = d->activeTextureOpCount++;
9762 if (idx < d->textureOps.size())
9763 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::copy(dst, src, desc);
9764 else
9765 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::copy(dst, src, desc));
9766}
9767
9768/*!
9769 Enqueues a texture-to-host copy operation as described by \a rb.
9770
9771 Normally \a rb will specify a QRhiTexture as the source. However, when the
9772 swapchain in the current frame was created with
9773 QRhiSwapChain::UsedAsTransferSource, it can also be the source of the
9774 readback. For this, leave the texture set to null in \a rb.
9775
9776 Unlike other operations, the results here need to be processed by the
9777 application. Therefore, \a result provides not just the data but also a
9778 callback as operations on the batch are asynchronous by nature:
9779
9780 \code
9781 rhi->beginFrame(swapchain);
9782 cb->beginPass(swapchain->currentFrameRenderTarget(), colorClear, dsClear);
9783 // ...
9784 QRhiReadbackResult *rbResult = new QRhiReadbackResult;
9785 rbResult->completed = [rbResult] {
9786 {
9787 const QImage::Format fmt = QImage::Format_RGBA8888_Premultiplied; // fits QRhiTexture::RGBA8
9788 const uchar *p = reinterpret_cast<const uchar *>(rbResult->data.constData());
9789 QImage image(p, rbResult->pixelSize.width(), rbResult->pixelSize.height(), fmt);
9790 image.save("result.png");
9791 }
9792 delete rbResult;
9793 };
9794 QRhiResourceUpdateBatch *u = nextResourceUpdateBatch();
9795 QRhiReadbackDescription rb; // no texture -> uses the current backbuffer of sc
9796 u->readBackTexture(rb, rbResult);
9797 cb->endPass(u);
9798 rhi->endFrame(swapchain);
9799 \endcode
9800
9801 \note The texture must be created with QRhiTexture::UsedAsTransferSource.
9802
9803 \note Multisample textures cannot be read back.
9804
9805 \note The readback returns raw byte data, in order to allow the applications
9806 to interpret it in any way they see fit. Be aware of the blending settings
9807 of rendering code: if the blending is set up to rely on premultiplied alpha,
9808 the results of the readback must also be interpreted as Premultiplied.
9809
9810 \note When interpreting the resulting raw data, be aware that the readback
9811 happens with a byte ordered format. A \l{QRhiTexture::RGBA8}{RGBA8} texture
9812 maps therefore to byte ordered QImage formats, such as,
9813 QImage::Format_RGBA8888.
9814
9815 \note The asynchronous readback is guaranteed to have completed when one of
9816 the following conditions is met: \l{QRhi::finish()}{finish()} has been
9817 called; or, at least \c N frames have been \l{QRhi::endFrame()}{submitted},
9818 including the frame that issued the readback operation, and the
9819 \l{QRhi::beginFrame()}{recording of a new frame} has been started, where \c
9820 N is the \l{QRhi::resourceLimit()}{resource limit value} returned for
9821 QRhi::MaxAsyncReadbackFrames.
9822
9823 A single readback operation copies one mip level of one layer (cubemap face
9824 or 3D slice or texture array element) at a time. The level and layer are
9825 specified by the respective fields in \a rb.
9826
9827 \sa readBackBuffer(), QRhi::resourceLimit()
9828 */
9829void QRhiResourceUpdateBatch::readBackTexture(const QRhiReadbackDescription &rb, QRhiReadbackResult *result)
9830{
9831 const int idx = d->activeTextureOpCount++;
9832 if (idx < d->textureOps.size())
9833 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::read(rb, result);
9834 else
9835 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::read(rb, result));
9836}
9837
9838/*!
9839 Enqueues a mipmap generation operation for the specified texture \a tex.
9840
9841 Both 2D and cube textures are supported.
9842
9843 \note The texture must be created with QRhiTexture::MipMapped and
9844 QRhiTexture::UsedWithGenerateMips.
9845
9846 \warning QRhi cannot guarantee that mipmaps can be generated for all
9847 supported texture formats. For example, QRhiTexture::RGBA32F is not a \c
9848 filterable format in OpenGL ES 3.0 and Metal on iOS, and therefore the
9849 mipmap generation request may fail. RGBA8 and RGBA16F are typically
9850 filterable, so it is recommended to use these formats when mipmap generation
9851 is desired.
9852 */
9853void QRhiResourceUpdateBatch::generateMips(QRhiTexture *tex)
9854{
9855 const int idx = d->activeTextureOpCount++;
9856 if (idx < d->textureOps.size())
9857 d->textureOps[idx] = QRhiResourceUpdateBatchPrivate::TextureOp::genMips(tex);
9858 else
9859 d->textureOps.append(QRhiResourceUpdateBatchPrivate::TextureOp::genMips(tex));
9860}
9861
9862/*!
9863 \return an available, empty batch to which copy type of operations can be
9864 recorded.
9865
9866 \note the return value is not owned by the caller and must never be
9867 destroyed. Instead, the batch is returned the pool for reuse by passing
9868 it to QRhiCommandBuffer::beginPass(), QRhiCommandBuffer::endPass(), or
9869 QRhiCommandBuffer::resourceUpdate(), or by calling
9870 QRhiResourceUpdateBatch::release() on it.
9871
9872 \note Can be called outside beginFrame() - endFrame() as well since a batch
9873 instance just collects data on its own, it does not perform any operations.
9874
9875 Due to not being tied to a frame being recorded, the following sequence is
9876 valid for example:
9877
9878 \code
9879 rhi->beginFrame(swapchain);
9880 QRhiResourceUpdateBatch *u = rhi->nextResourceUpdateBatch();
9881 u->uploadStaticBuffer(buf, data);
9882 // ... do not commit the batch
9883 rhi->endFrame();
9884 // u stays valid (assuming buf stays valid as well)
9885 rhi->beginFrame(swapchain);
9886 swapchain->currentFrameCommandBuffer()->resourceUpdate(u);
9887 // ... draw with buf
9888 rhi->endFrame();
9889 \endcode
9890
9891 \warning The maximum number of batches per QRhi is 64. When this limit is
9892 reached, the function will return null until a batch is returned to the
9893 pool.
9894 */
9895QRhiResourceUpdateBatch *QRhi::nextResourceUpdateBatch()
9896{
9897 // By default we prefer spreading out the utilization of the worst case 64
9898 // (but typically 4) batches as much as possible, meaning we won't pick the
9899 // first one even if it's free, but prefer picking one after the last picked
9900 // one. Relevant due to implicit sharing (the backend may hold on to the
9901 // QRhiBufferData until frame no. current+FramesInFlight-1, but
9902 // implementations may vary), combined with the desire to reuse container
9903 // and QRhiBufferData allocations in bufferOps instead of flooding every
9904 // frame with allocs. See free(). In typical Qt Quick scenes this leads to
9905 // eventually seeding all 4 (or more) resource batches with buffer operation
9906 // data allocations which may (*) then be reused in subsequent frames. This
9907 // comes at the expense of using more memory, but has proven good results
9908 // when (CPU) profiling typical Quick/Quick3D apps.
9909 //
9910 // (*) Due to implicit sharing(ish), the exact behavior is unpredictable. If
9911 // a backend holds on to the QRhiBufferData for, e.g., a dynamic buffer
9912 // update, and then there is a new assign() for that same QRhiBufferData
9913 // while the refcount is still 2, it will "detach" (without contents) and
9914 // there is no reuse of the alloc. This is mitigated by the 'choose the one
9915 // afer the last picked one' logic when handing out batches.
9916
9917 auto nextFreeBatch = [this]() -> QRhiResourceUpdateBatch * {
9918 auto isFree = [this](int i) -> QRhiResourceUpdateBatch * {
9919 const quint64 mask = 1ULL << quint64(i);
9920 if (!(d->resUpdPoolMap & mask)) {
9921 d->resUpdPoolMap |= mask;
9922 QRhiResourceUpdateBatch *u = d->resUpdPool[i];
9923 QRhiResourceUpdateBatchPrivate::get(u)->poolIndex = i;
9924 d->lastResUpdIdx = i;
9925 return u;
9926 }
9927 return nullptr;
9928 };
9929 const int poolSize = d->resUpdPool.size();
9930 for (int i = d->lastResUpdIdx + 1; i < poolSize; ++i) {
9931 if (QRhiResourceUpdateBatch *u = isFree(i))
9932 return u;
9933 }
9934 for (int i = 0; i <= d->lastResUpdIdx; ++i) {
9935 if (QRhiResourceUpdateBatch *u = isFree(i))
9936 return u;
9937 }
9938 return nullptr;
9939 };
9940
9941 QRhiResourceUpdateBatch *u = nextFreeBatch();
9942 if (!u) {
9943 const int oldSize = d->resUpdPool.size();
9944 // 4, 8, 12, ..., up to 64
9945 const int newSize = oldSize + qMin(4, qMax(0, 64 - oldSize));
9946 d->resUpdPool.resize(newSize);
9947 for (int i = oldSize; i < newSize; ++i)
9948 d->resUpdPool[i] = new QRhiResourceUpdateBatch(d);
9949 u = nextFreeBatch();
9950 if (!u)
9951 qWarning("Resource update batch pool exhausted (max is 64)");
9952 }
9953
9954 return u;
9955}
9956
9958{
9959 Q_ASSERT(poolIndex >= 0 && rhi->resUpdPool[poolIndex] == q);
9960
9961 quint32 bufferDataTotal = 0;
9962 quint32 bufferLargeAllocTotal = 0;
9963 for (const BufferOp &op : std::as_const(bufferOps)) {
9964 bufferDataTotal += op.data.size();
9965 bufferLargeAllocTotal += op.data.largeAlloc(); // alloc when > 1 KB
9966 }
9967
9968 if (QRHI_LOG_RUB().isDebugEnabled()) {
9969 qDebug() << "[rub] release to pool upd.batch #" << poolIndex
9970 << "/ bufferOps active" << activeBufferOpCount
9971 << "of" << bufferOps.count()
9972 << "data" << bufferDataTotal
9973 << "largeAlloc" << bufferLargeAllocTotal
9974 << "textureOps active" << activeTextureOpCount
9975 << "of" << textureOps.count();
9976 }
9977
9980
9981 const quint64 mask = 1ULL << quint64(poolIndex);
9982 rhi->resUpdPoolMap &= ~mask;
9983 poolIndex = -1;
9984
9985 // textureOps is cleared, to not keep the potentially large image pixel
9986 // data alive, but it is expected that the container keeps the list alloc
9987 // at least. Only trimOpList() goes for the more aggressive route with squeeze.
9988 textureOps.clear();
9989
9990 // bufferOps is not touched in many cases, to allow reusing allocations
9991 // (incl. in the elements' QRhiBufferData) as much as possible when this
9992 // batch is used again in the future, which is important for performance, in
9993 // particular with Qt Quick where it is easy for scenes to produce lots of,
9994 // typically small buffer changes on every frame.
9995 //
9996 // However, ensure that even in the unlikely case of having the max number
9997 // of batches (64) created in resUpdPool, no more than 64 MB in total is
9998 // used up by buffer data just to help future reuse. For simplicity, if
9999 // there is more than 1 MB data -> clear. Applications with frequent, huge
10000 // buffer updates probably have other bottlenecks anyway.
10001 if (bufferLargeAllocTotal > 1024 * 1024)
10002 bufferOps.clear();
10003}
10004
10006{
10007 int combinedSize = activeBufferOpCount + other->activeBufferOpCount;
10008 if (bufferOps.size() < combinedSize)
10009 bufferOps.resize(combinedSize);
10010 for (int i = activeBufferOpCount; i < combinedSize; ++i)
10011 bufferOps[i] = std::move(other->bufferOps[i - activeBufferOpCount]);
10013
10014 combinedSize = activeTextureOpCount + other->activeTextureOpCount;
10015 if (textureOps.size() < combinedSize)
10016 textureOps.resize(combinedSize);
10017 for (int i = activeTextureOpCount; i < combinedSize; ++i)
10018 textureOps[i] = std::move(other->textureOps[i - activeTextureOpCount]);
10020}
10021
10027
10029{
10030 // Unlike free(), this is expected to aggressively deallocate all memory
10031 // used by both the buffer and texture operation lists. (i.e. using
10032 // squeeze() to only keep the stack prealloc of the QVLAs)
10033 //
10034 // This (e.g. just the destruction of bufferOps elements) may have a
10035 // non-negligible performance impact e.g. with Qt Quick with scenes where
10036 // there are lots of buffer operations per frame.
10037
10039 bufferOps.clear();
10040 bufferOps.squeeze();
10041
10043 textureOps.clear();
10044 textureOps.squeeze();
10045}
10046
10047/*!
10048 Sometimes committing resource updates is necessary or just more convenient
10049 without starting a render pass. Calling this function with \a
10050 resourceUpdates is an alternative to passing \a resourceUpdates to a
10051 beginPass() call (or endPass(), which would be typical in case of readbacks).
10052
10053 \note Cannot be called inside a pass.
10054 */
10055void QRhiCommandBuffer::resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates)
10056{
10057 if (resourceUpdates)
10058 m_rhi->resourceUpdate(this, resourceUpdates);
10059}
10060
10061/*!
10062 Records starting a new render pass targeting the render target \a rt.
10063
10064 \a resourceUpdates, when not null, specifies a resource update batch that
10065 is to be committed and then released.
10066
10067 The color and depth/stencil buffers of the render target are normally
10068 cleared. The clear values are specified in \a colorClearValue and \a
10069 depthStencilClearValue. The exception is when the render target was created
10070 with QRhiTextureRenderTarget::PreserveColorContents and/or
10071 QRhiTextureRenderTarget::PreserveDepthStencilContents. The clear values are
10072 ignored then.
10073
10074 \note Enabling preserved color or depth contents leads to decreased
10075 performance depending on the underlying hardware. Mobile GPUs with tiled
10076 architecture benefit from not having to reload the previous contents into
10077 the tile buffer. Similarly, a QRhiTextureRenderTarget with a QRhiTexture as
10078 the depth buffer is less efficient than a QRhiRenderBuffer since using a
10079 depth texture triggers requiring writing the data out to it, while with
10080 renderbuffers this is not needed (as the API does not allow sampling or
10081 reading from a renderbuffer).
10082
10083 \note Do not assume that any state or resource bindings persist between
10084 passes.
10085
10086 \note The QRhiCommandBuffer's \c set and \c draw functions can only be
10087 called inside a pass. Also, with the exception of setGraphicsPipeline(),
10088 they expect to have a pipeline set already on the command buffer.
10089 Unspecified issues may arise otherwise, depending on the backend.
10090
10091 If \a rt is a QRhiTextureRenderTarget, beginPass() performs a check to see
10092 if the texture and renderbuffer objects referenced from the render target
10093 are up-to-date. This is similar to what setShaderResources() does for
10094 QRhiShaderResourceBindings. If any of the attachments had been rebuilt
10095 since QRhiTextureRenderTarget::create(), an implicit call to create() is
10096 made on \a rt. Therefore, if \a rt has a QRhiTexture color attachment \c
10097 texture, and one needs to make the texture a different size, the following
10098 is then valid:
10099 \code
10100 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget({ { texture } });
10101 rt->create();
10102 // ...
10103 texture->setPixelSize(new_size);
10104 texture->create();
10105 cb->beginPass(rt, colorClear, dsClear); // this is ok, no explicit rt->create() is required before
10106 \endcode
10107
10108 \a flags allow controlling certain advanced functionality. One commonly used
10109 flag is \c ExternalContents. This should be specified whenever
10110 beginExternal() will be called within the pass started by this function.
10111
10112 \sa endPass(), BeginPassFlags
10113 */
10114void QRhiCommandBuffer::beginPass(QRhiRenderTarget *rt,
10115 const QColor &colorClearValue,
10116 const QRhiDepthStencilClearValue &depthStencilClearValue,
10117 QRhiResourceUpdateBatch *resourceUpdates,
10118 BeginPassFlags flags)
10119{
10120 m_rhi->beginPass(this, rt, colorClearValue, depthStencilClearValue, resourceUpdates, flags);
10121}
10122
10123/*!
10124 Records ending the current render pass.
10125
10126 \a resourceUpdates, when not null, specifies a resource update batch that
10127 is to be committed and then released.
10128
10129 \sa beginPass()
10130 */
10131void QRhiCommandBuffer::endPass(QRhiResourceUpdateBatch *resourceUpdates)
10132{
10133 m_rhi->endPass(this, resourceUpdates);
10134}
10135
10136/*!
10137 Records setting a new graphics pipeline \a ps.
10138
10139 \note This function must be called before recording other \c set or \c draw
10140 commands on the command buffer.
10141
10142 \note QRhi will optimize out unnecessary invocations within a pass, so
10143 therefore overoptimizing to avoid calls to this function is not necessary
10144 on the applications' side.
10145
10146 \note This function can only be called inside a render pass, meaning
10147 between a beginPass() and endPass() call.
10148
10149 \note The new graphics pipeline \a ps must be a valid pointer.
10150 */
10151void QRhiCommandBuffer::setGraphicsPipeline(QRhiGraphicsPipeline *ps)
10152{
10153 Q_ASSERT(ps != nullptr);
10154 m_rhi->setGraphicsPipeline(this, ps);
10155}
10156
10157/*!
10158 Records binding a set of shader resources, such as, uniform buffers or
10159 textures, that are made visible to one or more shader stages.
10160
10161 \a srb can be null in which case the current graphics or compute pipeline's
10162 associated QRhiShaderResourceBindings is used. When \a srb is non-null, it
10163 must be
10164 \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout-compatible},
10165 meaning the layout (number of bindings, the type and binding number of each
10166 binding) must fully match the QRhiShaderResourceBindings that was
10167 associated with the pipeline at the time of calling the pipeline's create().
10168
10169 There are cases when a seemingly unnecessary setShaderResources() call is
10170 mandatory: when rebuilding a resource referenced from \a srb, for example
10171 changing the size of a QRhiBuffer followed by a QRhiBuffer::create(), this
10172 is the place where associated native objects (such as descriptor sets in
10173 case of Vulkan) are updated to refer to the current native resources that
10174 back the QRhiBuffer, QRhiTexture, QRhiSampler objects referenced from \a
10175 srb. In this case setShaderResources() must be called even if \a srb is
10176 the same as in the last call.
10177
10178 When \a srb is not null, the QRhiShaderResourceBindings object the pipeline
10179 was built with in create() is guaranteed to be not accessed in any form. In
10180 fact, it does not need to be valid even at this point: destroying the
10181 pipeline's associated srb after create() and instead explicitly specifying
10182 another, \l{QRhiShaderResourceBindings::isLayoutCompatible()}{layout
10183 compatible} one in every setShaderResources() call is valid.
10184
10185 \a dynamicOffsets allows specifying buffer offsets for uniform buffers that
10186 were associated with \a srb via
10187 QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(). This is
10188 different from providing the offset in the \a srb itself: dynamic offsets
10189 do not require building a new QRhiShaderResourceBindings for every
10190 different offset, can avoid writing the underlying descriptors (with
10191 backends where applicable), and so they may be more efficient. Each element
10192 of \a dynamicOffsets is a \c binding - \c offset pair.
10193 \a dynamicOffsetCount specifies the number of elements in \a dynamicOffsets.
10194
10195 \note All offsets in \a dynamicOffsets must be byte aligned to the value
10196 returned from QRhi::ubufAlignment().
10197
10198 \note Some backends may limit the number of supported dynamic offsets.
10199 Avoid using a \a dynamicOffsetCount larger than 8.
10200
10201 \note QRhi will optimize out unnecessary invocations within a pass (taking
10202 the conditions described above into account), so therefore overoptimizing
10203 to avoid calls to this function is not necessary on the applications' side.
10204
10205 \note This function can only be called inside a render or compute pass,
10206 meaning between a beginPass() and endPass(), or beginComputePass() and
10207 endComputePass().
10208 */
10209void QRhiCommandBuffer::setShaderResources(QRhiShaderResourceBindings *srb,
10210 int dynamicOffsetCount,
10211 const DynamicOffset *dynamicOffsets)
10212{
10213 m_rhi->setShaderResources(this, srb, dynamicOffsetCount, dynamicOffsets);
10214}
10215
10216/*!
10217 Records vertex input bindings.
10218
10219 The index buffer used by subsequent drawIndexed() commands is specified by
10220 \a indexBuf, \a indexOffset, and \a indexFormat. \a indexBuf can be set to
10221 null when indexed drawing is not needed.
10222
10223 Vertex buffer bindings are batched. \a startBinding specifies the first
10224 binding number. The recorded command then binds each buffer from \a
10225 bindings to the binding point \c{startBinding + i} where \c i is the index
10226 in \a bindings. Each element in \a bindings specifies a QRhiBuffer and an
10227 offset.
10228
10229 \note Some backends may limit the number of vertex buffer bindings. Avoid
10230 using a \a bindingCount larger than 8.
10231
10232 Superfluous vertex input and index changes in the same pass are ignored
10233 automatically with most backends and therefore applications do not need to
10234 overoptimize to avoid calls to this function.
10235
10236 \note This function can only be called inside a render pass, meaning
10237 between a beginPass() and endPass() call.
10238
10239 As a simple example, take a vertex shader with two inputs:
10240
10241 \badcode
10242 layout(location = 0) in vec4 position;
10243 layout(location = 1) in vec3 color;
10244 \endcode
10245
10246 and assume we have the data available in interleaved format, using only 2
10247 floats for position (so 5 floats per vertex: x, y, r, g, b). A QRhiGraphicsPipeline for
10248 this shader can then be created using the input layout:
10249
10250 \code
10251 QRhiVertexInputLayout inputLayout;
10252 inputLayout.setBindings({
10253 { 5 * sizeof(float) }
10254 });
10255 inputLayout.setAttributes({
10256 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
10257 { 0, 1, QRhiVertexInputAttribute::Float3, 2 * sizeof(float) }
10258 });
10259 \endcode
10260
10261 Here there is one buffer binding (binding number 0), with two inputs
10262 referencing it. When recording the pass, once the pipeline is set, the
10263 vertex bindings can be specified simply like the following, assuming vbuf
10264 is the QRhiBuffer with all the interleaved position+color data:
10265
10266 \code
10267 const QRhiCommandBuffer::VertexInput vbufBinding(vbuf, 0);
10268 cb->setVertexInput(0, 1, &vbufBinding);
10269 \endcode
10270 */
10271void QRhiCommandBuffer::setVertexInput(int startBinding, int bindingCount, const VertexInput *bindings,
10272 QRhiBuffer *indexBuf, quint32 indexOffset,
10273 IndexFormat indexFormat)
10274{
10275 m_rhi->setVertexInput(this, startBinding, bindingCount, bindings, indexBuf, indexOffset, indexFormat);
10276}
10277
10278/*!
10279 Records setting the active viewport rectangle specified in \a viewport.
10280
10281 With backends where the underlying graphics API has scissoring always
10282 enabled, this function also sets the scissor to match the viewport whenever
10283 the active QRhiGraphicsPipeline does not have
10284 \l{QRhiGraphicsPipeline::UsesScissor}{UsesScissor} set.
10285
10286 \note QRhi assumes OpenGL-style viewport coordinates, meaning x and y are
10287 bottom-left.
10288
10289 \note This function can only be called inside a render pass, meaning
10290 between a beginPass() and endPass() call.
10291 */
10292void QRhiCommandBuffer::setViewport(const QRhiViewport &viewport)
10293{
10294 m_rhi->setViewport(this, viewport);
10295}
10296
10297/*!
10298 Records setting the active scissor rectangle specified in \a scissor.
10299
10300 This can only be called when the bound pipeline has
10301 \l{QRhiGraphicsPipeline::UsesScissor}{UsesScissor} set. When the flag is
10302 set on the active pipeline, this function must be called because scissor
10303 testing will get enabled and so a scissor rectangle must be provided.
10304
10305 \note QRhi assumes OpenGL-style viewport coordinates, meaning x and y are
10306 bottom-left.
10307
10308 \note This function can only be called inside a render pass, meaning
10309 between a beginPass() and endPass() call.
10310 */
10311void QRhiCommandBuffer::setScissor(const QRhiScissor &scissor)
10312{
10313 m_rhi->setScissor(this, scissor);
10314}
10315
10316/*!
10317 Records setting the active blend constants to \a c.
10318
10319 This can only be called when the bound pipeline has
10320 QRhiGraphicsPipeline::UsesBlendConstants set.
10321
10322 \note This function can only be called inside a render pass, meaning
10323 between a beginPass() and endPass() call.
10324 */
10325void QRhiCommandBuffer::setBlendConstants(const QColor &c)
10326{
10327 m_rhi->setBlendConstants(this, c);
10328}
10329
10330/*!
10331 Records setting the active stencil reference value to \a refValue.
10332
10333 This can only be called when the bound pipeline has
10334 QRhiGraphicsPipeline::UsesStencilRef set.
10335
10336 \note This function can only be called inside a render pass, meaning between
10337 a beginPass() and endPass() call.
10338 */
10339void QRhiCommandBuffer::setStencilRef(quint32 refValue)
10340{
10341 m_rhi->setStencilRef(this, refValue);
10342}
10343
10344/*!
10345 Sets the shading rate for the following draw calls to \a coarsePixelSize.
10346
10347 The default is 1x1.
10348
10349 Functional only when the \l QRhi::VariableRateShading feature is reported as
10350 supported and the QRhiGraphicsPipeline(s) bound on the command buffer were
10351 declaring \l QRhiGraphicsPipeline::UsesShadingRate when creating them.
10352
10353 Call \l QRhi::supportedShadingRates() to check what shading rates are
10354 supported for a given sample count.
10355
10356 When both a QRhiShadingRateMap and this function is in use, the higher of
10357 two the shading rates are used for each tile. There is currently no control
10358 offered over the combiner behavior.
10359
10360 \since 6.9
10361 */
10362void QRhiCommandBuffer::setShadingRate(const QSize &coarsePixelSize)
10363{
10364 m_rhi->setShadingRate(this, coarsePixelSize);
10365}
10366
10367/*!
10368 Records a non-indexed draw.
10369
10370 The number of vertices is specified in \a vertexCount. For instanced
10371 drawing set \a instanceCount to a value other than 1. \a firstVertex is the
10372 index of the first vertex to draw. When drawing multiple instances, the
10373 first instance ID is specified by \a firstInstance.
10374
10375 \note \a firstInstance may not be supported, and is ignored when the
10376 QRhi::BaseInstance feature is reported as not supported. The first instance
10377 ID is always 0 in that case. QRhi::BaseInstance is never supported with
10378 OpenGL at the moment, mainly due to OpenGL ES limitations, and therefore
10379 portable applications should not be designed to rely on this argument.
10380
10381 \note Shaders that need to access the index of the current vertex or
10382 instance must use \c gl_VertexIndex and \c gl_InstanceIndex, i.e., the
10383 Vulkan-compatible built-in variables, instead of \c gl_VertexID and \c
10384 gl_InstanceID.
10385
10386 \note When \a firstInstance is non-zero, \c gl_InstanceIndex will not
10387 include the base value with some of the underlying 3D APIs. This is
10388 indicated by the QRhi::InstanceIndexIncludesBaseInstance feature. If relying
10389 on a base instance value cannot be avoided, applications are advised to pass
10390 in the value as a uniform conditionally based on what that feature reports,
10391 and add it to \c gl_InstanceIndex in the shader.
10392
10393 \note This function can only be called inside a render pass, meaning
10394 between a beginPass() and endPass() call.
10395 */
10396void QRhiCommandBuffer::draw(quint32 vertexCount,
10397 quint32 instanceCount,
10398 quint32 firstVertex,
10399 quint32 firstInstance)
10400{
10401 m_rhi->draw(this, vertexCount, instanceCount, firstVertex, firstInstance);
10402}
10403
10404/*!
10405 Records an indexed draw.
10406
10407 The number of vertices is specified in \a indexCount. \a firstIndex is the
10408 base index. The effective offset in the index buffer is given by
10409 \c{indexOffset + firstIndex * n} where \c n is 2 or 4 depending on the
10410 index element type. \c indexOffset is specified in setVertexInput().
10411
10412 \note The effective offset in the index buffer must be 4 byte aligned with
10413 some backends (for example, Metal). With these backends the
10414 \l{QRhi::NonFourAlignedEffectiveIndexBufferOffset}{NonFourAlignedEffectiveIndexBufferOffset}
10415 feature will be reported as not-supported.
10416
10417 \a vertexOffset (also called \c{base vertex}) is a signed value that is
10418 added to the element index before indexing into the vertex buffer. Support
10419 for this is not always available, and the value is ignored when the feature
10420 QRhi::BaseVertex is reported as unsupported.
10421
10422 For instanced drawing set \a instanceCount to a value other than 1. When
10423 drawing multiple instances, the first instance ID is specified by \a
10424 firstInstance.
10425
10426 \note \a firstInstance may not be supported, and is ignored when the
10427 QRhi::BaseInstance feature is reported as not supported. The first instance
10428 ID is always 0 in that case. QRhi::BaseInstance is never supported with
10429 OpenGL at the moment, mainly due to OpenGL ES limitations, and therefore
10430 portable applications should not be designed to rely on this argument.
10431
10432 \note Shaders that need to access the index of the current vertex or
10433 instance must use \c gl_VertexIndex and \c gl_InstanceIndex, i.e., the
10434 Vulkan-compatible built-in variables, instead of \c gl_VertexID and \c
10435 gl_InstanceID.
10436
10437 \note When \a firstInstance is non-zero, \c gl_InstanceIndex will not
10438 include the base value with some of the underlying 3D APIs. This is
10439 indicated by the QRhi::InstanceIndexIncludesBaseInstance feature. If relying
10440 on a base instance value cannot be avoided, applications are advised to pass
10441 in the value as a uniform conditionally based on what that feature reports,
10442 and add it to \c gl_InstanceIndex in the shader.
10443
10444 \note This function can only be called inside a render pass, meaning
10445 between a beginPass() and endPass() call.
10446 */
10447void QRhiCommandBuffer::drawIndexed(quint32 indexCount,
10448 quint32 instanceCount,
10449 quint32 firstIndex,
10450 qint32 vertexOffset,
10451 quint32 firstInstance)
10452{
10453 m_rhi->drawIndexed(this, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
10454}
10455
10456/*!
10457 Records a named debug group on the command buffer with the specified \a
10458 name. This is shown in graphics debugging tools such as
10459 \l{https://renderdoc.org/}{RenderDoc} and
10460 \l{https://developer.apple.com/xcode/}{XCode}. The end of the grouping is
10461 indicated by debugMarkEnd().
10462
10463 \note Ignored when QRhi::DebugMarkers are not supported or
10464 QRhi::EnableDebugMarkers is not set.
10465
10466 \note Can be called anywhere within the frame, both inside and outside of passes.
10467 */
10468void QRhiCommandBuffer::debugMarkBegin(const QByteArray &name)
10469{
10470 m_rhi->debugMarkBegin(this, name);
10471}
10472
10473/*!
10474 Records the end of a debug group.
10475
10476 \note Ignored when QRhi::DebugMarkers are not supported or
10477 QRhi::EnableDebugMarkers is not set.
10478
10479 \note Can be called anywhere within the frame, both inside and outside of passes.
10480 */
10481void QRhiCommandBuffer::debugMarkEnd()
10482{
10483 m_rhi->debugMarkEnd(this);
10484}
10485
10486/*!
10487 Inserts a debug message \a msg into the command stream.
10488
10489 \note Ignored when QRhi::DebugMarkers are not supported or
10490 QRhi::EnableDebugMarkers is not set.
10491
10492 \note With some backends debugMarkMsg() is only supported inside a pass and
10493 is ignored when called outside a pass. With others it is recorded anywhere
10494 within the frame.
10495 */
10496void QRhiCommandBuffer::debugMarkMsg(const QByteArray &msg)
10497{
10498 m_rhi->debugMarkMsg(this, msg);
10499}
10500
10501/*!
10502 Records starting a new compute pass.
10503
10504 \a resourceUpdates, when not null, specifies a resource update batch that
10505 is to be committed and then released.
10506
10507 \note Do not assume that any state or resource bindings persist between
10508 passes.
10509
10510 \note A compute pass can record setComputePipeline(), setShaderResources(),
10511 and dispatch() calls, not graphics ones. General functionality, such as,
10512 debug markers and beginExternal() is available both in render and compute
10513 passes.
10514
10515 \note Compute is only available when the \l{QRhi::Compute}{Compute} feature
10516 is reported as supported.
10517
10518 \a flags is not currently used.
10519 */
10520void QRhiCommandBuffer::beginComputePass(QRhiResourceUpdateBatch *resourceUpdates, BeginPassFlags flags)
10521{
10522 m_rhi->beginComputePass(this, resourceUpdates, flags);
10523}
10524
10525/*!
10526 Records ending the current compute pass.
10527
10528 \a resourceUpdates, when not null, specifies a resource update batch that
10529 is to be committed and then released.
10530 */
10531void QRhiCommandBuffer::endComputePass(QRhiResourceUpdateBatch *resourceUpdates)
10532{
10533 m_rhi->endComputePass(this, resourceUpdates);
10534}
10535
10536/*!
10537 Records setting a new compute pipeline \a ps.
10538
10539 \note This function must be called before recording setShaderResources() or
10540 dispatch() commands on the command buffer.
10541
10542 \note QRhi will optimize out unnecessary invocations within a pass, so
10543 therefore overoptimizing to avoid calls to this function is not necessary
10544 on the applications' side.
10545
10546 \note This function can only be called inside a compute pass, meaning
10547 between a beginComputePass() and endComputePass() call.
10548 */
10549void QRhiCommandBuffer::setComputePipeline(QRhiComputePipeline *ps)
10550{
10551 m_rhi->setComputePipeline(this, ps);
10552}
10553
10554/*!
10555 Records dispatching compute work items, with \a x, \a y, and \a z
10556 specifying the number of local workgroups in the corresponding dimension.
10557
10558 \note This function can only be called inside a compute pass, meaning
10559 between a beginComputePass() and endComputePass() call.
10560
10561 \note \a x, \a y, and \a z must fit the limits from the underlying graphics
10562 API implementation at run time. The maximum values are typically 65535.
10563
10564 \note Watch out for possible limits on the local workgroup size as well.
10565 This is specified in the shader, for example: \c{layout(local_size_x = 16,
10566 local_size_y = 16) in;}. For example, with OpenGL the minimum value mandated
10567 by the specification for the number of invocations in a single local work
10568 group (the product of \c local_size_x, \c local_size_y, and \c local_size_z)
10569 is 1024, while with OpenGL ES (3.1) the value may be as low as 128. This
10570 means that the example given above may be rejected by some OpenGL ES
10571 implementations as the number of invocations is 256.
10572 */
10573void QRhiCommandBuffer::dispatch(int x, int y, int z)
10574{
10575 m_rhi->dispatch(this, x, y, z);
10576}
10577
10578/*!
10579 \return a pointer to a backend-specific QRhiNativeHandles subclass, such as
10580 QRhiVulkanCommandBufferNativeHandles. The returned value is \nullptr when
10581 exposing the underlying native resources is not supported by, or not
10582 applicable to, the backend.
10583
10584 \sa QRhiVulkanCommandBufferNativeHandles,
10585 QRhiMetalCommandBufferNativeHandles, beginExternal(), endExternal()
10586 */
10587const QRhiNativeHandles *QRhiCommandBuffer::nativeHandles()
10588{
10589 return m_rhi->nativeHandles(this);
10590}
10591
10592/*!
10593 To be called when the application before the application is about to
10594 enqueue commands to the current pass' command buffer by calling graphics
10595 API functions directly.
10596
10597 \note This is only available when the intent was declared upfront in
10598 beginPass() or beginComputePass(). Therefore this function must only be
10599 called when the pass recording was started with specifying
10600 QRhiCommandBuffer::ExternalContent.
10601
10602 With Vulkan, Metal, or Direct3D 12 one can query the native command buffer
10603 or encoder objects via nativeHandles() and enqueue commands to them. With
10604 OpenGL or Direct3D 11 the (device) context can be retrieved from
10605 QRhi::nativeHandles(). However, this must never be done without ensuring
10606 the QRhiCommandBuffer's state stays up-to-date. Hence the requirement for
10607 wrapping any externally added command recording between beginExternal() and
10608 endExternal(). Conceptually this is the same as QPainter's
10609 \l{QPainter::beginNativePainting()}{beginNativePainting()} and
10610 \l{QPainter::endNativePainting()}{endNativePainting()} functions.
10611
10612 For OpenGL in particular, this function has an additional task: it makes
10613 sure the context is made current on the current thread.
10614
10615 \note Once beginExternal() is called, no other render pass specific
10616 functions (\c set* or \c draw*) must be called on the
10617 QRhiCommandBuffer until endExternal().
10618
10619 \warning Some backends may return a native command buffer object from
10620 QRhiCommandBuffer::nativeHandles() that is different from the primary one
10621 when inside a beginExternal() - endExternal() block. Therefore it is
10622 important to (re)query the native command buffer object after calling
10623 beginExternal(). In practical terms this means that with Vulkan for example
10624 the externally recorded Vulkan commands are placed onto a secondary command
10625 buffer (with VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT).
10626 nativeHandles() returns this secondary command buffer when called between
10627 begin/endExternal.
10628
10629 \sa endExternal(), nativeHandles()
10630 */
10631void QRhiCommandBuffer::beginExternal()
10632{
10633 m_rhi->beginExternal(this);
10634}
10635
10636/*!
10637 To be called once the externally added commands are recorded to the command
10638 buffer or context.
10639
10640 \note All QRhiCommandBuffer state must be assumed as invalid after calling
10641 this function. Pipelines, vertex and index buffers, and other state must be
10642 set again if more draw calls are recorded after the external commands.
10643
10644 \sa beginExternal(), nativeHandles()
10645 */
10646void QRhiCommandBuffer::endExternal()
10647{
10648 m_rhi->endExternal(this);
10649}
10650
10651/*!
10652 \return the last available timestamp, in seconds, when
10653 \l QRhi::EnableTimestamps was enabled when creating the QRhi. The value
10654 indicates the elapsed time on the GPU during the last completed frame.
10655
10656 \note Do not expect results other than 0 when the QRhi::Timestamps feature
10657 is not reported as supported, or when QRhi::EnableTimestamps was not passed
10658 to QRhi::create(). There are exceptions to this, because with some graphics
10659 APIs (Metal) timings are available without having to perform extra
10660 operations (timestamp queries), but portable applications should always
10661 consciously opt-in to timestamp collection when they know it is needed, and
10662 call this function accordingly.
10663
10664 Care must be exercised with the interpretation of the value, as its
10665 precision and granularity is often not controlled by Qt, and depends on the
10666 underlying graphics API and its implementation. In particular, comparing
10667 the values between different graphics APIs and hardware is discouraged and
10668 may be meaningless.
10669
10670 When the frame was recorded with \l{QRhi::beginFrame()}{beginFrame()} and
10671 \l{QRhi::endFrame()}{endFrame()}, i.e., with a swapchain, the timing values
10672 will likely become available asynchronously. The returned value may
10673 therefore be 0 (e.g., for the first 1-2 frames) or the last known value
10674 referring to some previous frame. The value my also
10675 become 0 again under certain conditions, such as when resizing the window.
10676 It can be expected that the most up-to-date available value is retrieved in
10677 beginFrame() and becomes queriable via this function once beginFrame()
10678 returns.
10679
10680 \note Do not assume that the value refers to the previous
10681 (\c{currently_recorded - 1}) frame. It may refer to \c{currently_recorded -
10682 2} or \c{currently_recorded - 3} as well. The exact behavior may depend on
10683 the graphics API and its implementation.
10684
10685 On the other hand, with offscreen frames the returned value is up-to-date
10686 once \l{QRhi::endOffscreenFrame()}{endOffscreenFrame()} returns, because
10687 offscreen frames reduce GPU pipelining and wait the the commands to be
10688 complete.
10689
10690 \note This means that, unlike with swapchain frames, with offscreen frames
10691 the returned value is guaranteed to refer to the frame that has just been
10692 submitted and completed. (assuming this function is called after
10693 endOffscreenFrame() but before the next beginOffscreenFrame())
10694
10695 Watch out for the consequences of GPU frequency scaling and GPU clock
10696 changes, depending on the platform. For example, on Windows the returned
10697 timing may vary in a quite wide range between frames with modern graphics
10698 cards, even when submitting frames with a similar, or the same workload.
10699 This is out of scope for Qt to control and solve, generally speaking.
10700 However, the D3D12 backend automatically calls
10701 \l{https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device-setstablepowerstate}{ID3D12Device::SetStablePowerState()}
10702 whenever the environment variable \c QT_D3D_STABLE_POWER_STATE is set to a
10703 non-zero value. This can greatly stabilize the result. It can also have a
10704 non-insignificant effect on the CPU-side timings measured via QElapsedTimer
10705 for example, especially when offscreen frames are involved.
10706
10707 \note Do not and never ship applications to production with
10708 \c QT_D3D_STABLE_POWER_STATE set. See the Windows API documentation for details.
10709
10710 \sa QRhi::Timestamps, QRhi::EnableTimestamps
10711 */
10712double QRhiCommandBuffer::lastCompletedGpuTime()
10713{
10714 return m_rhi->lastCompletedGpuTime(this);
10715}
10716
10717/*!
10718 \return the value (typically an offset) \a v aligned to the uniform buffer
10719 alignment given by by ubufAlignment().
10720 */
10721int QRhi::ubufAligned(int v) const
10722{
10723 const int byteAlign = ubufAlignment();
10724 return (v + byteAlign - 1) & ~(byteAlign - 1);
10725}
10726
10727/*!
10728 \return the number of mip levels for a given \a size.
10729 */
10730int QRhi::mipLevelsForSize(const QSize &size)
10731{
10732 return qFloor(std::log2(qMax(size.width(), size.height()))) + 1;
10733}
10734
10735/*!
10736 \return the texture image size for a given \a mipLevel, calculated based on
10737 the level 0 size given in \a baseLevelSize.
10738 */
10739QSize QRhi::sizeForMipLevel(int mipLevel, const QSize &baseLevelSize)
10740{
10741 const int w = qMax(1, baseLevelSize.width() >> mipLevel);
10742 const int h = qMax(1, baseLevelSize.height() >> mipLevel);
10743 return QSize(w, h);
10744}
10745
10746/*!
10747 \return \c true if the underlying graphics API has the Y axis pointing up
10748 in framebuffers and images.
10749
10750 In practice this is \c true for OpenGL only.
10751 */
10752bool QRhi::isYUpInFramebuffer() const
10753{
10754 return d->isYUpInFramebuffer();
10755}
10756
10757/*!
10758 \return \c true if the underlying graphics API has the Y axis pointing up
10759 in its normalized device coordinate system.
10760
10761 In practice this is \c false for Vulkan only.
10762
10763 \note clipSpaceCorrMatrix() includes the corresponding adjustment (to make
10764 Y point up) in its returned matrix.
10765 */
10766bool QRhi::isYUpInNDC() const
10767{
10768 return d->isYUpInNDC();
10769}
10770
10771/*!
10772 \return \c true if the underlying graphics API uses depth range [0, 1] in
10773 clip space.
10774
10775 In practice this is \c false for OpenGL only, because OpenGL uses a
10776 post-projection depth range of [-1, 1]. (not to be confused with the
10777 NDC-to-window mapping controlled by glDepthRange(), which uses a range of
10778 [0, 1], unless overridden by the QRhiViewport) In some OpenGL versions
10779 glClipControl() could be used to change this, but the OpenGL backend of
10780 QRhi does not use that function as it is not available in OpenGL ES or
10781 OpenGL versions lower than 4.5.
10782
10783 \note clipSpaceCorrMatrix() includes the corresponding adjustment in its
10784 returned matrix. Therefore, many users of QRhi do not need to take any
10785 further measures apart from pre-multiplying their projection matrices with
10786 clipSpaceCorrMatrix(). However, some graphics techniques, such as, some
10787 types of shadow mapping, involve working with and outputting depth values
10788 in the shaders. These will need to query and take the value of this
10789 function into account as appropriate.
10790 */
10791bool QRhi::isClipDepthZeroToOne() const
10792{
10793 return d->isClipDepthZeroToOne();
10794}
10795
10796/*!
10797 \return a matrix that can be used to allow applications keep using
10798 OpenGL-targeted vertex data and perspective projection matrices (such as,
10799 the ones generated by QMatrix4x4::perspective()), regardless of the active
10800 QRhi backend.
10801
10802 In a typical renderer, once \c{this_matrix * mvp} is used instead of just
10803 \c mvp, vertex data with Y up and viewports with depth range 0 - 1 can be
10804 used without considering what backend (and so graphics API) is going to be
10805 used at run time. This way branching based on isYUpInNDC() and
10806 isClipDepthZeroToOne() can be avoided (although such logic may still become
10807 required when implementing certain advanced graphics techniques).
10808
10809 See
10810 \l{https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/}{this
10811 page} for a discussion of the topic from Vulkan perspective.
10812 */
10813QMatrix4x4 QRhi::clipSpaceCorrMatrix() const
10814{
10815 return d->clipSpaceCorrMatrix();
10816}
10817
10818/*!
10819 \return \c true if the specified texture \a format modified by \a flags is
10820 supported.
10821
10822 The query is supported both for uncompressed and compressed formats.
10823 */
10824bool QRhi::isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const
10825{
10826 return d->isTextureFormatSupported(format, flags);
10827}
10828
10829/*!
10830 \return \c true if the specified \a feature is supported
10831 */
10832bool QRhi::isFeatureSupported(QRhi::Feature feature) const
10833{
10834 return d->isFeatureSupported(feature);
10835}
10836
10837/*!
10838 \return the value for the specified resource \a limit.
10839
10840 The values are expected to be queried by the backends upon initialization,
10841 meaning calling this function is a light operation.
10842 */
10843int QRhi::resourceLimit(ResourceLimit limit) const
10844{
10845 return d->resourceLimit(limit);
10846}
10847
10848/*!
10849 \return a pointer to the backend-specific collection of native objects
10850 for the device, context, and similar concepts used by the backend.
10851
10852 Cast to QRhiVulkanNativeHandles, QRhiD3D11NativeHandles,
10853 QRhiD3D12NativeHandles, QRhiGles2NativeHandles, or QRhiMetalNativeHandles
10854 as appropriate.
10855
10856 \note No ownership is transferred, neither for the returned pointer nor for
10857 any native objects.
10858 */
10859const QRhiNativeHandles *QRhi::nativeHandles()
10860{
10861 return d->nativeHandles();
10862}
10863
10864/*!
10865 With OpenGL this makes the OpenGL context current on the current thread.
10866 The function has no effect with other backends.
10867
10868 Calling this function is relevant typically in Qt framework code, when one
10869 has to ensure external OpenGL code provided by the application can still
10870 run like it did before with direct usage of OpenGL, as long as the QRhi is
10871 using the OpenGL backend.
10872
10873 \return false when failed, similarly to QOpenGLContext::makeCurrent(). When
10874 the operation failed, isDeviceLost() can be called to determine if there
10875 was a loss of context situation. Such a check is equivalent to checking via
10876 QOpenGLContext::isValid().
10877
10878 \sa QOpenGLContext::makeCurrent(), QOpenGLContext::isValid()
10879 */
10880bool QRhi::makeThreadLocalNativeContextCurrent()
10881{
10882 return d->makeThreadLocalNativeContextCurrent();
10883}
10884
10885/*!
10886 With backends and graphics APIs where applicable, this function allows to
10887 provide additional arguments to the \b next submission of commands to the
10888 graphics command queue.
10889
10890 In particular, with Vulkan this allows passing in a list of Vulkan semaphore
10891 objects for \c vkQueueSubmit() to signal and wait on. \a params must then be
10892 a \l QRhiVulkanQueueSubmitParams. This becomes essential in certain advanced
10893 use cases, such as when performing native Vulkan calls that involve having
10894 to wait on and signal VkSemaphores that the application's custom Vulkan
10895 rendering or compute code manages. In addition, this also allows specifying
10896 additional semaphores to wait on in the next \c vkQueuePresentKHR().
10897
10898 \note This function affects the next queue submission only, which will
10899 happen in endFrame(), endOffscreenFrame(), or finish(). The enqueuing of
10900 present happens in endFrame().
10901
10902 With many other backends the implementation of this function is a no-op.
10903
10904 \since 6.9
10905 */
10906void QRhi::setQueueSubmitParams(QRhiNativeHandles *params)
10907{
10908 d->setQueueSubmitParams(params);
10909}
10910
10911/*!
10912 Attempts to release resources in the backend's caches. This can include both
10913 CPU and GPU resources. Only memory and resources that can be recreated
10914 automatically are in scope. As an example, if the backend's
10915 QRhiGraphicsPipeline implementation maintains a cache of shader compilation
10916 results, calling this function leads to emptying that cache, thus
10917 potentially freeing up memory and graphics resources.
10918
10919 Calling this function makes sense in resource constrained environments,
10920 where at a certain point there is a need to ensure minimal resource usage,
10921 at the expense of performance.
10922 */
10923void QRhi::releaseCachedResources()
10924{
10925 d->releaseCachedResources();
10926
10927 for (QRhiResourceUpdateBatch *u : d->resUpdPool) {
10928 if (u->d->poolIndex < 0)
10929 u->d->trimOpLists();
10930 }
10931}
10932
10933/*!
10934 \return true if the graphics device was lost.
10935
10936 The loss of the device is typically detected in beginFrame(), endFrame() or
10937 QRhiSwapChain::createOrResize(), depending on the backend and the underlying
10938 native APIs. The most common is endFrame() because that is where presenting
10939 happens. With some backends QRhiSwapChain::createOrResize() can also fail
10940 due to a device loss. Therefore this function is provided as a generic way
10941 to check if a device loss was detected by a previous operation.
10942
10943 When the device is lost, no further operations should be done via the QRhi.
10944 Rather, all QRhi resources should be released, followed by destroying the
10945 QRhi. A new QRhi can then be attempted to be created. If successful, all
10946 graphics resources must be reinitialized. If not, try again later,
10947 repeatedly.
10948
10949 While simple applications may decide to not care about device loss,
10950 on the commonly used desktop platforms a device loss can happen
10951 due to a variety of reasons, including physically disconnecting the
10952 graphics adapter, disabling the device or driver, uninstalling or upgrading
10953 the graphics driver, or due to errors that lead to a graphics device reset.
10954 Some of these can happen under perfectly normal circumstances as well, for
10955 example the upgrade of the graphics driver to a newer version is a common
10956 task that can happen at any time while a Qt application is running. Users
10957 may very well expect applications to be able to survive this, even when the
10958 application is actively using an API like OpenGL or Direct3D.
10959
10960 Qt's own frameworks built on top of QRhi, such as, Qt Quick, can be
10961 expected to handle and take appropriate measures when a device loss occurs.
10962 If the data for graphics resources, such as textures and buffers, are still
10963 available on the CPU side, such an event may not be noticeable on the
10964 application level at all since graphics resources can seamlessly be
10965 reinitialized then. However, applications and libraries working directly
10966 with QRhi are expected to be prepared to check and handle device loss
10967 situations themselves.
10968
10969 \note With OpenGL, applications may need to opt-in to context reset
10970 notifications by setting QSurfaceFormat::ResetNotification on the
10971 QOpenGLContext. This is typically done by enabling the flag in
10972 QRhiGles2InitParams::format. Keep in mind however that some systems may
10973 generate context resets situations even when this flag is not set.
10974 */
10975bool QRhi::isDeviceLost() const
10976{
10977 return d->isDeviceLost();
10978}
10979
10980/*!
10981 \return a binary data blob with data collected from the
10982 QRhiGraphicsPipeline and QRhiComputePipeline successfully created during
10983 the lifetime of this QRhi.
10984
10985 By saving and then, in subsequent runs of the same application, reloading
10986 the cache data, pipeline and shader creation times can potentially be
10987 reduced. What exactly the cache and its serialized version includes is not
10988 specified, is always specific to the backend used, and in some cases also
10989 dependent on the particular implementation of the graphics API.
10990
10991 When the PipelineCacheDataLoadSave is reported as unsupported, the returned
10992 QByteArray is empty.
10993
10994 When the EnablePipelineCacheDataSave flag was not specified when calling
10995 create(), the returned QByteArray may be empty, even when the
10996 PipelineCacheDataLoadSave feature is supported.
10997
10998 When the returned data is non-empty, it is always specific to the Qt
10999 version and QRhi backend. In addition, in some cases there is a strong
11000 dependency to the graphics device and the exact driver version used. QRhi
11001 takes care of adding the appropriate header and safeguards that ensure that
11002 the data can always be passed safely to setPipelineCacheData(), therefore
11003 attempting to load data from a run on another version of a driver will be
11004 handled safely and gracefully.
11005
11006 \note Calling releaseCachedResources() may, depending on the backend, clear
11007 the pipeline data collected. A subsequent call to this function may then
11008 not return any data.
11009
11010 See EnablePipelineCacheDataSave for further details about this feature.
11011
11012 \note Minimize the number of calls to this function. Retrieving the blob is
11013 not always a cheap operation, and therefore this function should only be
11014 called at a low frequency, ideally only once e.g. when closing the
11015 application.
11016
11017 \sa setPipelineCacheData(), create(), isFeatureSupported()
11018 */
11019QByteArray QRhi::pipelineCacheData()
11020{
11021 return d->pipelineCacheData();
11022}
11023
11024/*!
11025 Loads \a data into the pipeline cache, when applicable.
11026
11027 When the PipelineCacheDataLoadSave is reported as unsupported, the function
11028 is safe to call, but has no effect.
11029
11030 The blob returned by pipelineCacheData() is always specific to the Qt
11031 version, the QRhi backend, and, in some cases, also to the graphics device,
11032 and a given version of the graphics driver. QRhi takes care of adding the
11033 appropriate header and safeguards that ensure that the data can always be
11034 passed safely to this function. If there is a mismatch, e.g. because the
11035 driver has been upgraded to a newer version, or because the data was
11036 generated from a different QRhi backend, a warning is printed and \a data
11037 is safely ignored.
11038
11039 With Vulkan, this maps directly to VkPipelineCache. Calling this function
11040 creates a new Vulkan pipeline cache object, with its initial data sourced
11041 from \a data. The pipeline cache object is then used by all subsequently
11042 created QRhiGraphicsPipeline and QRhiComputePipeline objects, thus
11043 accelerating, potentially, the pipeline creation.
11044
11045 With other APIs there is no real pipeline cache, but they may provide a
11046 cache with bytecode from shader compilations (D3D) or program binaries
11047 (OpenGL). In applications that perform a lot of shader compilation from
11048 source at run time this can provide a significant boost in subsequent runs
11049 if the "pipeline cache" is pre-seeded from an earlier run using this
11050 function.
11051
11052 \note QRhi cannot give any guarantees that \a data has an effect on the
11053 pipeline and shader creation performance. With APIs like Vulkan, it is up
11054 to the driver to decide if \a data is used for some purpose, or if it is
11055 ignored.
11056
11057 See EnablePipelineCacheDataSave for further details about this feature.
11058
11059 \note This mechanism offered by QRhi is independent of the drivers' own
11060 internal caching mechanism, if any. This means that, depending on the
11061 graphics API and its implementation, the exact effects of retrieving and
11062 then reloading \a data are not predictable. Improved performance may not be
11063 visible at all in case other caching mechanisms outside of Qt's control are
11064 already active.
11065
11066 \note Minimize the number of calls to this function. Loading the blob is
11067 not always a cheap operation, and therefore this function should only be
11068 called at a low frequency, ideally only once e.g. when starting the
11069 application.
11070
11071 \warning Serialized pipeline cache data is assumed to be trusted content. Qt
11072 performs robust parsing of the header and metadata included in \a data,
11073 application developers are however advised to never pass in data from
11074 untrusted sources.
11075
11076 \sa pipelineCacheData(), isFeatureSupported()
11077 */
11078void QRhi::setPipelineCacheData(const QByteArray &data)
11079{
11080 d->setPipelineCacheData(data);
11081}
11082
11083/*!
11084 \struct QRhiStats
11085 \inmodule QtGuiPrivate
11086 \inheaderfile rhi/qrhi.h
11087 \since 6.6
11088
11089 \brief Statistics provided from the underlying memory allocator.
11090
11091 \note This is a RHI API with limited compatibility guarantees, see \l QRhi
11092 for details.
11093 */
11094
11095/*!
11096 \variable QRhiStats::totalPipelineCreationTime
11097
11098 The total time in milliseconds spent in graphics and compute pipeline
11099 creation, which usually involves shader compilation or cache lookups, and
11100 potentially expensive processing.
11101
11102 \note The value should not be compared between different backends since the
11103 concept of "pipelines" and what exactly happens under the hood during, for
11104 instance, a call to QRhiGraphicsPipeline::create(), differ greatly between
11105 graphics APIs and their implementations.
11106
11107 \sa QRhi::statistics()
11108*/
11109
11110/*!
11111 \variable QRhiStats::blockCount
11112
11113 Statistic reported from the Vulkan or D3D12 memory allocator.
11114
11115 \sa QRhi::statistics()
11116*/
11117
11118/*!
11119 \variable QRhiStats::allocCount
11120
11121 Statistic reported from the Vulkan or D3D12 memory allocator.
11122
11123 \sa QRhi::statistics()
11124*/
11125
11126/*!
11127 \variable QRhiStats::usedBytes
11128
11129 Statistic reported from the Vulkan or D3D12 memory allocator.
11130
11131 \sa QRhi::statistics()
11132*/
11133
11134/*!
11135 \variable QRhiStats::unusedBytes
11136
11137 Statistic reported from the Vulkan or D3D12 memory allocator.
11138
11139 \sa QRhi::statistics()
11140*/
11141
11142/*!
11143 \variable QRhiStats::totalUsageBytes
11144
11145 Valid only with D3D12 currently. Matches IDXGIAdapter3::QueryVideoMemoryInfo().
11146
11147 \sa QRhi::statistics()
11148*/
11149
11150#ifndef QT_NO_DEBUG_STREAM
11151QDebug operator<<(QDebug dbg, const QRhiStats &info)
11152{
11153 QDebugStateSaver saver(dbg);
11154 dbg.nospace() << "QRhiStats("
11155 << "totalPipelineCreationTime=" << info.totalPipelineCreationTime
11156 << " blockCount=" << info.blockCount
11157 << " allocCount=" << info.allocCount
11158 << " usedBytes=" << info.usedBytes
11159 << " unusedBytes=" << info.unusedBytes
11160 << " totalUsageBytes=" << info.totalUsageBytes
11161 << ')';
11162 return dbg;
11163}
11164#endif
11165
11166/*!
11167 Gathers and returns statistics about the timings and allocations of
11168 graphics resources.
11169
11170 Data about memory allocations is only available with some backends, where
11171 such operations are under Qt's control. With graphics APIs where there is
11172 no lower level control over resource memory allocations, this will never be
11173 supported and all relevant fields in the results are 0.
11174
11175 With Vulkan in particular, the values are valid always, and are queried
11176 from the underlying memory allocator library. This gives an insight into
11177 the memory requirements of the active buffers and textures.
11178
11179 The same is true for Direct 3D 12. In addition to the memory allocator
11180 library's statistics, here the result also includes a \c totalUsageBytes
11181 field which reports the total size including additional resources that are
11182 not under the memory allocator library's control (swapchain buffers,
11183 descriptor heaps, etc.), as reported by DXGI.
11184
11185 The values correspond to all types of memory used, combined. (i.e. video +
11186 system in case of a discreet GPU)
11187
11188 Additional data, such as the total time in milliseconds spent in graphics
11189 and compute pipeline creation (which usually involves shader compilation or
11190 cache lookups, and potentially expensive processing) is available with most
11191 backends.
11192
11193 \note The elapsed times for operations such as pipeline creation may be
11194 affected by various factors. The results should not be compared between
11195 different backends since the concept of "pipelines" and what exactly
11196 happens under the hood during, for instance, a call to
11197 QRhiGraphicsPipeline::create(), differ greatly between graphics APIs and
11198 their implementations.
11199
11200 \note Additionally, many drivers will likely employ various caching
11201 strategies for shaders, programs, pipelines. (independently of Qt's own
11202 similar facilities, such as setPipelineCacheData() or the OpenGL-specific
11203 program binary disk cache). Because such internal behavior is transparent
11204 to the API client, Qt and QRhi have no knowledge or control over the exact
11205 caching strategy, persistency, invalidation of the cached data, etc. When
11206 reading timings, such as the time spent on pipeline creation, the potential
11207 presence and unspecified behavior of driver-level caching mechanisms should
11208 be kept in mind.
11209 */
11210QRhiStats QRhi::statistics() const
11211{
11212 return d->statistics();
11213}
11214
11215/*!
11216 \return a new graphics pipeline resource.
11217
11218 \sa QRhiResource::destroy()
11219 */
11220QRhiGraphicsPipeline *QRhi::newGraphicsPipeline()
11221{
11222 return d->createGraphicsPipeline();
11223}
11224
11225/*!
11226 \return a new compute pipeline resource.
11227
11228 \note Compute is only available when the \l{QRhi::Compute}{Compute} feature
11229 is reported as supported.
11230
11231 \sa QRhiResource::destroy()
11232 */
11233QRhiComputePipeline *QRhi::newComputePipeline()
11234{
11235 return d->createComputePipeline();
11236}
11237
11238/*!
11239 \return a new shader resource binding collection resource.
11240
11241 \sa QRhiResource::destroy()
11242 */
11243QRhiShaderResourceBindings *QRhi::newShaderResourceBindings()
11244{
11245 return d->createShaderResourceBindings();
11246}
11247
11248/*!
11249 \return a new buffer with the specified \a type, \a usage, and \a size.
11250
11251 \note Some \a usage and \a type combinations may not be supported by all
11252 backends. See \l{QRhiBuffer::UsageFlag}{UsageFlags} and
11253 \l{QRhi::NonDynamicUniformBuffers}{the feature flags}.
11254
11255 \note Backends may choose to allocate buffers bigger than \a size. This is
11256 done transparently to applications, so there are no special restrictions on
11257 the value of \a size. QRhiBuffer::size() will always report back the value
11258 that was requested in \a size.
11259
11260 \sa QRhiResource::destroy()
11261 */
11262QRhiBuffer *QRhi::newBuffer(QRhiBuffer::Type type,
11263 QRhiBuffer::UsageFlags usage,
11264 quint32 size)
11265{
11266 return d->createBuffer(type, usage, size);
11267}
11268
11269/*!
11270 \return a new renderbuffer with the specified \a type, \a pixelSize, \a
11271 sampleCount, and \a flags.
11272
11273 When \a backingFormatHint is set to a texture format other than
11274 QRhiTexture::UnknownFormat, it may be used by the backend to decide what
11275 format to use for the storage backing the renderbuffer.
11276
11277 \note \a backingFormatHint becomes relevant typically when multisampling
11278 and floating point texture formats are involved: rendering into a
11279 multisample QRhiRenderBuffer and then resolving into a non-RGBA8
11280 QRhiTexture implies (with some graphics APIs) that the storage backing the
11281 QRhiRenderBuffer uses the matching non-RGBA8 format. That means that
11282 passing a format like QRhiTexture::RGBA32F is important, because backends
11283 will typically opt for QRhiTexture::RGBA8 by default, which would then
11284 break later on due to attempting to set up RGBA8->RGBA32F multisample
11285 resolve in the color attachment(s) of the QRhiTextureRenderTarget.
11286
11287 \sa QRhiResource::destroy()
11288 */
11289QRhiRenderBuffer *QRhi::newRenderBuffer(QRhiRenderBuffer::Type type,
11290 const QSize &pixelSize,
11291 int sampleCount,
11292 QRhiRenderBuffer::Flags flags,
11293 QRhiTexture::Format backingFormatHint)
11294{
11295 return d->createRenderBuffer(type, pixelSize, sampleCount, flags, backingFormatHint);
11296}
11297
11298/*!
11299 \return a new 1D or 2D texture with the specified \a format, \a pixelSize, \a
11300 sampleCount, and \a flags.
11301
11302 A 1D texture array must have QRhiTexture::OneDimensional set in \a flags. This
11303 function will implicitly set this flag if the \a pixelSize height is 0.
11304
11305 \note \a format specifies the requested internal and external format,
11306 meaning the data to be uploaded to the texture will need to be in a
11307 compatible format, while the native texture may (but is not guaranteed to,
11308 in case of OpenGL at least) use this format internally.
11309
11310 \note 1D textures are only functional when the OneDimensionalTextures feature is
11311 reported as supported at run time. Further, mipmaps on 1D textures are only
11312 functional when the OneDimensionalTextureMipmaps feature is reported at run time.
11313
11314 \sa QRhiResource::destroy()
11315 */
11316QRhiTexture *QRhi::newTexture(QRhiTexture::Format format,
11317 const QSize &pixelSize,
11318 int sampleCount,
11319 QRhiTexture::Flags flags)
11320{
11321 if (pixelSize.height() == 0)
11322 flags |= QRhiTexture::OneDimensional;
11323
11324 return d->createTexture(format, pixelSize, 1, 0, sampleCount, flags);
11325}
11326
11327/*!
11328 \return a new 1D, 2D or 3D texture with the specified \a format, \a width, \a
11329 height, \a depth, \a sampleCount, and \a flags.
11330
11331 This overload is suitable for 3D textures because it allows specifying \a
11332 depth. A 3D texture must have QRhiTexture::ThreeDimensional set in \a
11333 flags, but using this overload that can be omitted because the flag is set
11334 implicitly whenever \a depth is greater than 0. For 1D, 2D and cube textures \a
11335 depth should be set to 0.
11336
11337 A 1D texture must have QRhiTexture::OneDimensional set in \a flags. This overload
11338 will implicitly set this flag if both \a height and \a depth are 0.
11339
11340 \note 3D textures are only functional when the ThreeDimensionalTextures
11341 feature is reported as supported at run time.
11342
11343 \note 1D textures are only functional when the OneDimensionalTextures feature is
11344 reported as supported at run time. Further, mipmaps on 1D textures are only
11345 functional when the OneDimensionalTextureMipmaps feature is reported at run time.
11346
11347 \overload
11348 */
11349QRhiTexture *QRhi::newTexture(QRhiTexture::Format format,
11350 int width, int height, int depth,
11351 int sampleCount,
11352 QRhiTexture::Flags flags)
11353{
11354 if (depth > 0)
11355 flags |= QRhiTexture::ThreeDimensional;
11356
11357 if (height == 0 && depth == 0)
11358 flags |= QRhiTexture::OneDimensional;
11359
11360 return d->createTexture(format, QSize(width, height), depth, 0, sampleCount, flags);
11361}
11362
11363/*!
11364 \return a new 1D or 2D texture array with the specified \a format, \a arraySize,
11365 \a pixelSize, \a sampleCount, and \a flags.
11366
11367 This function implicitly sets QRhiTexture::TextureArray in \a flags.
11368
11369 A 1D texture array must have QRhiTexture::OneDimensional set in \a flags. This
11370 function will implicitly set this flag if the \a pixelSize height is 0.
11371
11372 \note Do not confuse texture arrays with arrays of textures. A QRhiTexture
11373 created by this function is usable with 1D or 2D array samplers in the shader, for
11374 example: \c{layout(binding = 1) uniform sampler2DArray texArr;}. Arrays of
11375 textures refers to a list of textures that are exposed to the shader via
11376 QRhiShaderResourceBinding::sampledTextures() and a count > 1, and declared
11377 in the shader for example like this: \c{layout(binding = 1) uniform
11378 sampler2D textures[4];}
11379
11380 \note This is only functional when the TextureArrays feature is reported as
11381 supported at run time.
11382
11383 \note 1D textures are only functional when the OneDimensionalTextures feature is
11384 reported as supported at run time. Further, mipmaps on 1D textures are only
11385 functional when the OneDimensionalTextureMipmaps feature is reported at run time.
11386
11387
11388 \sa newTexture()
11389 */
11390QRhiTexture *QRhi::newTextureArray(QRhiTexture::Format format,
11391 int arraySize,
11392 const QSize &pixelSize,
11393 int sampleCount,
11394 QRhiTexture::Flags flags)
11395{
11396 flags |= QRhiTexture::TextureArray;
11397
11398 if (pixelSize.height() == 0)
11399 flags |= QRhiTexture::OneDimensional;
11400
11401 return d->createTexture(format, pixelSize, 1, arraySize, sampleCount, flags);
11402}
11403
11404/*!
11405 \return a new sampler with the specified magnification filter \a magFilter,
11406 minification filter \a minFilter, mipmapping mode \a mipmapMode, and the
11407 addressing (wrap) modes \a addressU, \a addressV, and \a addressW.
11408
11409 \note Setting \a mipmapMode to a value other than \c None implies that
11410 images for all relevant mip levels will be provided either via
11411 \l{QRhiResourceUpdateBatch::uploadTexture()}{texture uploads} or by calling
11412 \l{QRhiResourceUpdateBatch::generateMips()}{generateMips()} on the texture
11413 that is used with this sampler. Attempting to use the sampler with a
11414 texture that has no data for all relevant mip levels will lead to rendering
11415 errors, with the exact behavior dependent on the underlying graphics API.
11416
11417 \sa QRhiResource::destroy()
11418 */
11419QRhiSampler *QRhi::newSampler(QRhiSampler::Filter magFilter,
11420 QRhiSampler::Filter minFilter,
11421 QRhiSampler::Filter mipmapMode,
11422 QRhiSampler::AddressMode addressU,
11423 QRhiSampler::AddressMode addressV,
11424 QRhiSampler::AddressMode addressW)
11425{
11426 return d->createSampler(magFilter, minFilter, mipmapMode, addressU, addressV, addressW);
11427}
11428
11429/*!
11430 \return a new shading rate map object.
11431
11432 \since 6.9
11433 */
11434QRhiShadingRateMap *QRhi::newShadingRateMap()
11435{
11436 return d->createShadingRateMap();
11437}
11438
11439/*!
11440 \return a new texture render target with color and depth/stencil
11441 attachments given in \a desc, and with the specified \a flags.
11442
11443 \sa QRhiResource::destroy()
11444 */
11445
11446QRhiTextureRenderTarget *QRhi::newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
11447 QRhiTextureRenderTarget::Flags flags)
11448{
11449 return d->createTextureRenderTarget(desc, flags);
11450}
11451
11452/*!
11453 \return a new swapchain.
11454
11455 \sa QRhiResource::destroy(), QRhiSwapChain::createOrResize()
11456 */
11457QRhiSwapChain *QRhi::newSwapChain()
11458{
11459 return d->createSwapChain();
11460}
11461
11462/*!
11463 Starts a new frame targeting the next available buffer of \a swapChain.
11464
11465 A frame consists of resource updates and one or more render and compute
11466 passes.
11467
11468 \a flags can indicate certain special cases.
11469
11470 The high level pattern of rendering into a QWindow using a swapchain:
11471
11472 \list
11473
11474 \li Create a swapchain.
11475
11476 \li Call QRhiSwapChain::createOrResize() whenever the surface size is
11477 different than before.
11478
11479 \li Call QRhiSwapChain::destroy() on
11480 QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed.
11481
11482 \li Then on every frame:
11483 \badcode
11484 beginFrame(sc);
11485 updates = nextResourceUpdateBatch();
11486 updates->...
11487 QRhiCommandBuffer *cb = sc->currentFrameCommandBuffer();
11488 cb->beginPass(sc->currentFrameRenderTarget(), colorClear, dsClear, updates);
11489 ...
11490 cb->endPass();
11491 ... // more passes as necessary
11492 endFrame(sc);
11493 \endcode
11494
11495 \endlist
11496
11497 \return QRhi::FrameOpSuccess on success, or another QRhi::FrameOpResult
11498 value on failure. Some of these should be treated as soft, "try again
11499 later" type of errors: When QRhi::FrameOpSwapChainOutOfDate is returned,
11500 the swapchain is to be resized or updated by calling
11501 QRhiSwapChain::createOrResize(). The application should then attempt to
11502 generate a new frame. QRhi::FrameOpDeviceLost means the graphics device is
11503 lost but this may also be recoverable by releasing all resources, including
11504 the QRhi itself, and then recreating all resources. See isDeviceLost() for
11505 further discussion.
11506
11507 \sa endFrame(), beginOffscreenFrame(), isDeviceLost()
11508 */
11509QRhi::FrameOpResult QRhi::beginFrame(QRhiSwapChain *swapChain, BeginFrameFlags flags)
11510{
11511 if (d->inFrame)
11512 qWarning("Attempted to call beginFrame() within a still active frame; ignored");
11513
11514 qCDebug(QRHI_LOG_RUB) << "[rub] new frame";
11515
11516 QRhi::FrameOpResult r = !d->inFrame ? d->beginFrame(swapChain, flags) : FrameOpSuccess;
11517 if (r == FrameOpSuccess)
11518 d->inFrame = true;
11519
11520 return r;
11521}
11522
11523/*!
11524 Ends, commits, and presents a frame that was started in the last
11525 beginFrame() on \a swapChain.
11526
11527 Double (or triple) buffering is managed internally by the QRhiSwapChain and
11528 QRhi.
11529
11530 \a flags can optionally be used to change the behavior in certain ways.
11531 Passing QRhi::SkipPresent skips queuing the Present command or calling
11532 swapBuffers.
11533
11534 \return QRhi::FrameOpSuccess on success, or another QRhi::FrameOpResult
11535 value on failure. Some of these should be treated as soft, "try again
11536 later" type of errors: When QRhi::FrameOpSwapChainOutOfDate is returned,
11537 the swapchain is to be resized or updated by calling
11538 QRhiSwapChain::createOrResize(). The application should then attempt to
11539 generate a new frame. QRhi::FrameOpDeviceLost means the graphics device is
11540 lost but this may also be recoverable by releasing all resources, including
11541 the QRhi itself, and then recreating all resources. See isDeviceLost() for
11542 further discussion.
11543
11544 \sa beginFrame(), isDeviceLost()
11545 */
11546QRhi::FrameOpResult QRhi::endFrame(QRhiSwapChain *swapChain, EndFrameFlags flags)
11547{
11548 if (!d->inFrame)
11549 qWarning("Attempted to call endFrame() without an active frame; ignored");
11550
11551 QRhi::FrameOpResult r = d->inFrame ? d->endFrame(swapChain, flags) : FrameOpSuccess;
11552 d->inFrame = false;
11553 // deleteLater is a high level QRhi concept the backends know
11554 // nothing about - handle it here.
11555 qDeleteAll(d->pendingDeleteResources);
11556 d->pendingDeleteResources.clear();
11557
11558 return r;
11559}
11560
11561/*!
11562 \return true when there is an active frame, meaning there was a
11563 beginFrame() (or beginOffscreenFrame()) with no corresponding endFrame()
11564 (or endOffscreenFrame()) yet.
11565
11566 \sa currentFrameSlot(), beginFrame(), endFrame()
11567 */
11568bool QRhi::isRecordingFrame() const
11569{
11570 return d->inFrame;
11571}
11572
11573/*!
11574 \return the current frame slot index while recording a frame. Unspecified
11575 when called outside an active frame (that is, when isRecordingFrame() is \c
11576 false).
11577
11578 With backends like Vulkan or Metal, it is the responsibility of the QRhi
11579 backend to block whenever starting a new frame and finding the CPU is
11580 already \c{FramesInFlight - 1} frames ahead of the GPU (because the command
11581 buffer submitted in frame no. \c{current} - \c{FramesInFlight} has not yet
11582 completed).
11583
11584 Resources that tend to change between frames (such as, the native buffer
11585 object backing a QRhiBuffer with type QRhiBuffer::Dynamic) exist in
11586 multiple versions, so that each frame, that can be submitted while a
11587 previous one is still being processed, works with its own copy, thus
11588 avoiding the need to stall the pipeline when preparing the frame. (The
11589 contents of a resource that may still be in use in the GPU should not be
11590 touched, but simply always waiting for the previous frame to finish would
11591 reduce GPU utilization and ultimately, performance and efficiency.)
11592
11593 Conceptually this is somewhat similar to copy-on-write schemes used by some
11594 C++ containers and other types. It may also be similar to what an OpenGL or
11595 Direct 3D 11 implementation performs internally for certain type of objects.
11596
11597 In practice, such double (or triple) buffering resources is realized in
11598 the Vulkan, Metal, and similar QRhi backends by having a fixed number of
11599 native resource (such as, VkBuffer) \c slots behind a QRhiResource. That
11600 can then be indexed by a frame slot index running 0, 1, ..,
11601 FramesInFlight-1, and then wrapping around.
11602
11603 All this is managed transparently to the users of QRhi. However,
11604 applications that integrate rendering done directly with the graphics API
11605 may want to perform a similar double or triple buffering of their own
11606 graphics resources. That is then most easily achieved by knowing the values
11607 of the maximum number of in-flight frames (retrievable via resourceLimit())
11608 and the current frame (slot) index (returned by this function).
11609
11610 \sa isRecordingFrame(), beginFrame(), endFrame()
11611 */
11612int QRhi::currentFrameSlot() const
11613{
11614 return d->currentFrameSlot;
11615}
11616
11617/*!
11618 Starts a new offscreen frame. Provides a command buffer suitable for
11619 recording rendering commands in \a cb. \a flags is used to indicate
11620 certain special cases, just like with beginFrame().
11621
11622 \note The QRhiCommandBuffer stored to *cb is not owned by the caller.
11623
11624 Rendering without a swapchain is possible as well. The typical use case is
11625 to use it in completely offscreen applications, e.g. to generate image
11626 sequences by rendering and reading back without ever showing a window.
11627
11628 Usage in on-screen applications (so beginFrame, endFrame,
11629 beginOffscreenFrame, endOffscreenFrame, beginFrame, ...) is possible too
11630 but it does reduce parallelism so it should be done only infrequently.
11631
11632 Offscreen frames do not let the CPU potentially generate another frame
11633 while the GPU is still processing the previous one. This has the side
11634 effect that if readbacks are scheduled, the results are guaranteed to be
11635 available once endOffscreenFrame() returns. That is not the case with
11636 frames targeting a swapchain: there the GPU is potentially better utilized,
11637 but working with readback operations needs more care from the application
11638 because endFrame(), unlike endOffscreenFrame(), does not guarantee that the
11639 results from the readback are available at that point.
11640
11641 The skeleton of rendering a frame without a swapchain and then reading the
11642 frame contents back could look like the following:
11643
11644 \code
11645 QRhiReadbackResult rbResult;
11646 QRhiCommandBuffer *cb;
11647 rhi->beginOffscreenFrame(&cb);
11648 cb->beginPass(rt, colorClear, dsClear);
11649 // ...
11650 u = nextResourceUpdateBatch();
11651 u->readBackTexture(rb, &rbResult);
11652 cb->endPass(u);
11653 rhi->endOffscreenFrame();
11654 // image data available in rbResult
11655 \endcode
11656
11657 \sa endOffscreenFrame(), beginFrame()
11658 */
11659QRhi::FrameOpResult QRhi::beginOffscreenFrame(QRhiCommandBuffer **cb, BeginFrameFlags flags)
11660{
11661 if (d->inFrame)
11662 qWarning("Attempted to call beginOffscreenFrame() within a still active frame; ignored");
11663
11664 qCDebug(QRHI_LOG_RUB) << "[rub] new offscreen frame";
11665
11666 QRhi::FrameOpResult r = !d->inFrame ? d->beginOffscreenFrame(cb, flags) : FrameOpSuccess;
11667 if (r == FrameOpSuccess)
11668 d->inFrame = true;
11669
11670 return r;
11671}
11672
11673/*!
11674 Ends, submits, and waits for the offscreen frame.
11675
11676 \a flags is not currently used.
11677
11678 \sa beginOffscreenFrame()
11679 */
11680QRhi::FrameOpResult QRhi::endOffscreenFrame(EndFrameFlags flags)
11681{
11682 if (!d->inFrame)
11683 qWarning("Attempted to call endOffscreenFrame() without an active frame; ignored");
11684
11685 QRhi::FrameOpResult r = d->inFrame ? d->endOffscreenFrame(flags) : FrameOpSuccess;
11686 d->inFrame = false;
11687 qDeleteAll(d->pendingDeleteResources);
11688 d->pendingDeleteResources.clear();
11689
11690 return r;
11691}
11692
11693/*!
11694 Waits for any work on the graphics queue (where applicable) to complete,
11695 then executes all deferred operations, like completing readbacks and
11696 resource releases. Can be called inside and outside of a frame, but not
11697 inside a pass. Inside a frame it implies submitting any work on the
11698 command buffer.
11699
11700 \note Avoid this function. One case where it may be needed is when the
11701 results of an enqueued readback in a swapchain-based frame are needed at a
11702 fixed given point and so waiting for the results is desired.
11703 */
11704QRhi::FrameOpResult QRhi::finish()
11705{
11706 return d->finish();
11707}
11708
11709/*!
11710 \return the list of supported sample counts.
11711
11712 A typical example would be (1, 2, 4, 8).
11713
11714 With some backend this list of supported values is fixed in advance, while
11715 with some others the (physical) device properties indicate what is
11716 supported at run time.
11717
11718 \sa QRhiRenderBuffer::setSampleCount(), QRhiTexture::setSampleCount(),
11719 QRhiGraphicsPipeline::setSampleCount(), QRhiSwapChain::setSampleCount()
11720 */
11721QList<int> QRhi::supportedSampleCounts() const
11722{
11723 return d->supportedSampleCounts();
11724}
11725
11726/*!
11727 \return the minimum uniform buffer offset alignment in bytes. This is
11728 typically 256.
11729
11730 Attempting to bind a uniform buffer region with an offset not aligned to
11731 this value will lead to failures depending on the backend and the
11732 underlying graphics API.
11733
11734 \sa ubufAligned()
11735 */
11736int QRhi::ubufAlignment() const
11737{
11738 return d->ubufAlignment();
11739}
11740
11741/*!
11742 \return The list of supported variable shading rates for the specified \a sampleCount.
11743
11744 1x1 is always supported.
11745
11746 \since 6.9
11747 */
11748QList<QSize> QRhi::supportedShadingRates(int sampleCount) const
11749{
11750 return d->supportedShadingRates(sampleCount);
11751}
11752
11753Q_CONSTINIT static QBasicAtomicInteger<QRhiGlobalObjectIdGenerator::Type> counter = Q_BASIC_ATOMIC_INITIALIZER(0);
11754
11755QRhiGlobalObjectIdGenerator::Type QRhiGlobalObjectIdGenerator::newId()
11756{
11757 return counter.fetchAndAddRelaxed(1) + 1;
11758}
11759
11761{
11762 return m_buffers.isEmpty() && m_textures.isEmpty();
11763}
11764
11766{
11767 m_buffers.clear();
11768 m_textures.clear();
11769}
11770
11776
11777void QRhiPassResourceTracker::registerBuffer(QRhiBuffer *buf, int slot, BufferAccess *access, BufferStage *stage,
11778 const UsageState &state)
11779{
11780 auto it = m_buffers.find(buf);
11781 if (it != m_buffers.end()) {
11782 Buffer &b = it->second;
11783 if (Q_UNLIKELY(b.access != *access)) {
11784 const QByteArray name = buf->name();
11785 qWarning("Buffer %p (%s) used with different accesses within the same pass, this is not allowed.",
11786 buf, name.constData());
11787 return;
11788 }
11789 if (b.stage != *stage) {
11790 b.stage = earlierStage(b.stage, *stage);
11791 *stage = b.stage;
11792 }
11793 return;
11794 }
11795
11796 Buffer b;
11797 b.slot = slot;
11798 b.access = *access;
11799 b.stage = *stage;
11800 b.stateAtPassBegin = state; // first use -> initial state
11801 m_buffers.insert(buf, b);
11802}
11803
11809
11816
11818 const UsageState &state)
11819{
11820 auto it = m_textures.find(tex);
11821 if (it != m_textures.end()) {
11822 Texture &t = it->second;
11823 if (t.access != *access) {
11824 // Different subresources of a texture may be used for both load
11825 // and store in the same pass. (think reading from one mip level
11826 // and writing to another one in a compute shader) This we can
11827 // handle by treating the entire resource as read-write.
11828 if (Q_LIKELY(isImageLoadStore(t.access) && isImageLoadStore(*access))) {
11830 *access = t.access;
11831 } else {
11832 const QByteArray name = tex->name();
11833 qWarning("Texture %p (%s) used with different accesses within the same pass, this is not allowed.",
11834 tex, name.constData());
11835 }
11836 }
11837 if (t.stage != *stage) {
11838 t.stage = earlierStage(t.stage, *stage);
11839 *stage = t.stage;
11840 }
11841 return;
11842 }
11843
11844 Texture t;
11845 t.access = *access;
11846 t.stage = *stage;
11847 t.stateAtPassBegin = state; // first use -> initial state
11848 m_textures.insert(tex, t);
11849}
11850
11851QRhiPassResourceTracker::BufferStage QRhiPassResourceTracker::toPassTrackerBufferStage(QRhiShaderResourceBinding::StageFlags stages)
11852{
11853 // pick the earlier stage (as this is going to be dstAccessMask)
11854 if (stages.testFlag(QRhiShaderResourceBinding::VertexStage))
11856 if (stages.testFlag(QRhiShaderResourceBinding::TessellationControlStage))
11858 if (stages.testFlag(QRhiShaderResourceBinding::TessellationEvaluationStage))
11860 if (stages.testFlag(QRhiShaderResourceBinding::FragmentStage))
11862 if (stages.testFlag(QRhiShaderResourceBinding::ComputeStage))
11864 if (stages.testFlag(QRhiShaderResourceBinding::GeometryStage))
11866
11867 Q_UNREACHABLE_RETURN(QRhiPassResourceTracker::BufVertexStage);
11868}
11869
11870QRhiPassResourceTracker::TextureStage QRhiPassResourceTracker::toPassTrackerTextureStage(QRhiShaderResourceBinding::StageFlags stages)
11871{
11872 // pick the earlier stage (as this is going to be dstAccessMask)
11873 if (stages.testFlag(QRhiShaderResourceBinding::VertexStage))
11875 if (stages.testFlag(QRhiShaderResourceBinding::TessellationControlStage))
11877 if (stages.testFlag(QRhiShaderResourceBinding::TessellationEvaluationStage))
11879 if (stages.testFlag(QRhiShaderResourceBinding::FragmentStage))
11881 if (stages.testFlag(QRhiShaderResourceBinding::ComputeStage))
11883 if (stages.testFlag(QRhiShaderResourceBinding::GeometryStage))
11885
11886 Q_UNREACHABLE_RETURN(QRhiPassResourceTracker::TexVertexStage);
11887}
11888
11889QT_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:807
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:818
bool isEmpty() const
Definition qrhi.cpp:11760
void registerBuffer(QRhiBuffer *buf, int slot, BufferAccess *access, BufferStage *stage, const UsageState &state)
Definition qrhi.cpp:11777
void registerTexture(QRhiTexture *tex, TextureAccess *access, TextureStage *stage, const UsageState &state)
Definition qrhi.cpp:11817
QRhiImplementation * rhi
Definition qrhi_p.h:582
static const int BUFFER_OPS_STATIC_ALLOC
Definition qrhi_p.h:574
void merge(QRhiResourceUpdateBatchPrivate *other)
Definition qrhi.cpp:10005
QRhiResourceUpdateBatch * q
Definition qrhi_p.h:581
static const int TEXTURE_OPS_STATIC_ALLOC
Definition qrhi_p.h:578
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
static const char * resourceTypeStr(const QRhiResource *res)
Definition qrhi.cpp:8376
static QRhiPassResourceTracker::BufferStage earlierStage(QRhiPassResourceTracker::BufferStage a, QRhiPassResourceTracker::BufferStage b)
Definition qrhi.cpp:11771
QDebug operator<<(QDebug dbg, const QRhiSwapChainHdrInfo &info)
Definition qrhi.cpp:8191
static bool isImageLoadStore(QRhiPassResourceTracker::TextureAccess access)
Definition qrhi.cpp:11810
static const char * deviceTypeStr(QRhiDriverInfo::DeviceType type)
\variable QRhiDriverInfo::deviceName
Definition qrhi.cpp:9295
static QRhiPassResourceTracker::TextureStage earlierStage(QRhiPassResourceTracker::TextureStage a, QRhiPassResourceTracker::TextureStage b)
Definition qrhi.cpp:11804
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:191
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1555
LimitsType limitsType
Definition qrhi.h:1566
float maxPotentialColorComponentValue
Definition qrhi.h:1574
LuminanceBehavior luminanceBehavior
Definition qrhi.h:1577
float maxColorComponentValue
Definition qrhi.h:1573