class QQuickRhiItem

The QQuickRhiItem class is a portable alternative to QQuickFramebufferObject that is not tied to OpenGL, but rather allows integrating rendering with the QRhi APIs with Qt Quick. More

Inheritance diagram of PySide6.QtQuick.QQuickRhiItem

Added in version 6.7.

Synopsis

Properties

Methods

Virtual methods

Signals

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Note

QQuickRhiItem is in tech preview in Qt 6.7. The API is under development and subject to change.

QQuickRhiItem is effectively the counterpart of QRhiWidget in the world of Qt Quick. Both of these are meant to be subclassed, and they both enable recording QRhi-based rendering that targets an offscreen color buffer. The resulting 2D image is then composited with the rest of the Qt Quick scene.

Note

While QQuickRhiItem is a public Qt API, the QRhi family of classes in the Qt Gui module, including QShader and QShaderDescription, offer limited compatibility guarantees. There are no source or binary compatibility guarantees for these classes, meaning the API is only guaranteed to work with the Qt version the application was developed against. Source incompatible changes are however aimed to be kept at a minimum and will only be made in minor releases (6.7, 6.8, and so on). qquickrhiitem.h does not directly include any QRhi-related headers. To use those classes when implementing a QQuickRhiItem subclass, link to Qt::GuiPrivate (if using CMake), and include the appropriate headers with the rhi prefix, for example #include <rhi/qrhi.h>.

QQuickRhiItem is a replacement for the legacy QQuickFramebufferObject class. The latter is inherently tied to OpenGL / OpenGL ES, whereas QQuickRhiItem works with the QRhi classes, allowing to run the same rendering code with Vulkan, Metal, Direct 3D 11/12, and OpenGL / OpenGL ES. Conceptually and functionally they are very close, and migrating from QQuickFramebufferObject to QQuickRhiItem is straightforward. QQuickFramebufferObject continues to be available to ensure compatibility for existing application code that works directly with the OpenGL API.

Note

QQuickRhiItem will not be functional when using the software adaptation of the Qt Quick scene graph.

On most platforms, the scene graph rendering, and thus the rendering performed by the QQuickRhiItem will occur on a dedicated thread . For this reason, the QQuickRhiItem class enforces a strict separation between the item implementation (the QQuickItem subclass) and the actual rendering logic. All item logic, such as properties and UI-related helper functions exposed to QML must be located in the QQuickRhiItem subclass. Everything that relates to rendering must be located in the QQuickRhiItemRenderer class. To avoid race conditions and read/write issues from two threads it is important that the renderer and the item never read or write shared variables. Communication between the item and the renderer should primarily happen via the QQuickRhiItem::synchronize() function. This function will be called on the render thread while the GUI thread is blocked. Using queued connections or events for communication between item and renderer is also possible.

Applications must subclass both QQuickRhiItem and QQuickRhiItemRenderer . The pure virtual createRenderer() function must be reimplemented to return a new instance of the QQuickRhiItemRenderer subclass.

As with QRhiWidget, QQuickRhiItem automatically managed the color buffer, which is a 2D texture (QRhiTexture) normally, or a QRhiRenderBuffer when multisampling is in use. (some 3D APIs differentiate between textures and renderbuffers, while with some others the underlying native resource is the same; renderbuffers are used mainly to allow multisampling with OpenGL ES 3.0)

The size of the texture will by default adapt to the size of the item (with the device pixel ratio taken into account). If the item size changes, the texture is recreated with the correct size. If a fixed size is preferred, set fixedColorBufferWidth and fixedColorBufferHeight to non-zero values.

QQuickRhiItem is a texture provider and can be used directly in ShaderEffects and other classes that consume texture providers.

While not a primary use case, QQuickRhiItem also allows incorporating rendering code that directly uses a 3D graphics API such as Vulkan, Metal, Direct 3D, or OpenGL. See QRhiCommandBuffer::beginExternal() for details on recording native commands within a QRhi render pass, as well as QRhiTexture::createFrom() for a way to wrap an existing native texture and then use it with QRhi in a subsequent render pass. See also QQuickGraphicsConfiguration regarding configuring the native 3D API environment (e.g. device extensions) and note that the QQuickWindow can be associated with a custom QVulkanInstance by calling QWindow::setVulkanInstance() early enough.

Note

QQuickRhiItem always uses the same QRhi instance the QQuickWindow uses (and by extension, the same OpenGL context, Vulkan device, etc.). To choose which underlying 3D graphics API is used, call setGraphicsApi() on the QQuickWindow early enough. Changing it is not possible once the scene graph has initialized, and all QQuickRhiItem instances in the scene will render using the same 3D API.

A simple example

Take the following subclass of QQuickRhiItem . It is shown here in complete form. It renders a single triangle with a perspective projection, where the triangle is rotated based on the angle property of the custom item. (meaning it can be driven for example with animations such as NumberAnimation from QML)

class ExampleRhiItemRenderer(QQuickRhiItemRenderer):

# public
    def initialize(cb):
    def synchronize(item):
    def render(cb):
# private
    m_rhi = None
    std.unique_ptr<QRhiBuffer> m_vbuf
    std.unique_ptr<QRhiBuffer> m_ubuf
    std.unique_ptr<QRhiShaderResourceBindings> m_srb
    std.unique_ptr<QRhiGraphicsPipeline> m_pipeline
    m_viewProjection = QMatrix4x4()
    m_angle = 0.0f

class ExampleRhiItem(QQuickRhiItem):

    Q_OBJECT
    QML_NAMED_ELEMENT(ExampleRhiItem)
    Q_PROPERTY(float angle READ angle WRITE setAngle NOTIFY angleChanged)
# public
    QQuickRhiItemRenderer createRenderer() override
    float angle() { return m_angle; }
    def setAngle(a):
# signals
    def angleChanged():
# private
    m_angle = 0.0f

QQuickRhiItemRenderer ExampleRhiItem.createRenderer()

    return ExampleRhiItemRenderer()

def setAngle(self, a):

    if m_angle == a:
        return
    m_angle = a
    angleChanged.emit()
    update()

def synchronize(self, rhiItem):

    item = ExampleRhiItem(rhiItem)
    if item.angle() != m_angle:
        m_angle = item.angle()

def getShader(name):

    f = QFile(name)
    return f.open(QIODevice.ReadOnly) if QShader.fromSerialized(f.readAll()) else QShader()

vertexData = {
    0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
    -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
    0.5f, -0.5f, 0.0f, 0.0f, 1.0f,

def initialize(self, cb):

    if m_rhi != rhi():
        m_pipeline.reset()
        m_rhi = rhi()

    if not m_pipeline:
        m_vbuf.reset(m_rhi.newBuffer(QRhiBuffer.Immutable, QRhiBuffer.VertexBuffer, sizeof(vertexData)))
        m_vbuf.create()
        m_ubuf.reset(m_rhi.newBuffer(QRhiBuffer.Dynamic, QRhiBuffer.UniformBuffer, 64))
        m_ubuf.create()
        m_srb.reset(m_rhi.newShaderResourceBindings())
        m_srb.setBindings({
            QRhiShaderResourceBinding.uniformBuffer(0, QRhiShaderResourceBinding.VertexStage, m_ubuf.get()),
        })
        m_srb.create()
        m_pipeline.reset(m_rhi.newGraphicsPipeline())
        m_pipeline.setShaderStages({
            { QRhiShaderStage.Vertex, getShader(":/shaders/color.vert.qsb") },
            { QRhiShaderStage.Fragment, getShader(":/shaders/color.frag.qsb") }
        })
        inputLayout = QRhiVertexInputLayout()
        inputLayout.setBindings({
            { 5 * sizeof(float) }
        })
        inputLayout.setAttributes({
            { 0, 0, QRhiVertexInputAttribute.Float2, 0 },
            { 0, 1, QRhiVertexInputAttribute.Float3, 2 * sizeof(float) }
        })
        m_pipeline.setVertexInputLayout(inputLayout)
        m_pipeline.setShaderResourceBindings(m_srb.get())
        m_pipeline.setRenderPassDescriptor(renderTarget().renderPassDescriptor())
        m_pipeline.create()
        resourceUpdates = m_rhi.nextResourceUpdateBatch()
        resourceUpdates.uploadStaticBuffer(m_vbuf.get(), vertexData)
        cb.resourceUpdate(resourceUpdates)

    outputSize = renderTarget().pixelSize()
    m_viewProjection = m_rhi.clipSpaceCorrMatrix()
    m_viewProjection.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f)
    m_viewProjection.translate(0, 0, -4)

def render(self, cb):

    resourceUpdates = m_rhi.nextResourceUpdateBatch()
    modelViewProjection = m_viewProjection
    modelViewProjection.rotate(m_angle, 0, 1, 0)
    resourceUpdates.updateDynamicBuffer(m_ubuf.get(), 0, 64, modelViewProjection.constData())
    clearColor = QColor.fromRgbF(0.4f, 0.7f, 0.0f, 1.0f)
    cb.beginPass(renderTarget(), clearColor, { 1.0f, 0 }, resourceUpdates)
    cb.setGraphicsPipeline(m_pipeline.get())
    outputSize = renderTarget().pixelSize()
    cb.setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height()))
    cb.setShaderResources()
    QRhiCommandBuffer.VertexInput vbufBinding(m_vbuf.get(), 0)
    cb.setVertexInput(0, 1, vbufBinding)
    cb.draw(3)
    cb.endPass()

It is notable that this simple class is almost exactly the same as the code shown in the QRhiWidget introduction. The vertex and fragment shaders are the same as well. These are provided as Vulkan-style GLSL source code and must be processed first by the Qt shader infrastructure first. This is achieved either by running the qsb command-line tool manually, or by using the qt_add_shaders() function in CMake. The QQuickRhiItem loads these pre-processed .qsb files that are shipped with the application. See Qt Shader Tools for more information about Qt’s shader translation infrastructure.

color.vert

#version 440
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 color;
layout(location = 0) out vec3 v_color;
layout(std140, binding = 0) uniform buf {
    mat4 mvp;
};

void main()
{
    v_color = color;
    gl_Position = mvp * position;
}

color.frag

#version 440
layout(location = 0) in vec3 v_color;
layout(location = 0) out vec4 fragColor;

void main()
{
    fragColor = vec4(v_color, 1.0);
}

Once exposed to QML (note the QML_NAMED_ELEMENT), our custom item can be instantiated in any scene. (after importing the appropriate URI specified for qt_add_qml_module in the CMake project)

ExampleRhiItem {
    anchors.fill: parent
    anchors.margins: 10
    NumberAnimation on angle { from: 0; to: 360; duration: 5000; loops: Animation.Infinite }
}

See Scene Graph - RHI Texture Item for a more complex example.

See also

QQuickRhiItemRenderer Scene Graph - RHI Texture Item Scene Graph and Rendering

class TextureFormat

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property alphaBlendingᅟ: bool

Controls if blending is always enabled when drawing the quad textured with the content generated by the QQuickRhiItem and its renderer.

The default value is false. This is for performance reasons: if semi-transparency is not involved, because the QQuickRhiItemRenderer clears to an opaque color and never renders fragments with alpha smaller than 1, then there is no point in enabling blending.

If the QQuickRhiItemRenderer subclass renders with semi-transparency involved, set this property to true.

Note

Under certain conditions blending is still going to happen regardless of the value of this property. For example, if the item’s opacity (more precisely, the combined opacity inherited from the parent chain) is smaller than 1, blending will be automatically enabled even when this property is set to false.

Note

The Qt Quick scene graph relies on and expect pre-multiplied alpha. For example, if the intention is to clear the background in the renderer to an alpha value of 0.5, then make sure to multiply the red, green, and blue clear color values with 0.5 as well. Otherwise the blending results will be incorrect.

Access functions:
property colorBufferFormatᅟ: QQuickRhiItem.TextureFormat

This property controls the texture format for the texture used as the color buffer. The default value is TextureFormat::RGBA8. QQuickRhiItem supports rendering to a subset of the formats supported by QRhiTexture. Only formats that are reported as supported from QRhi::isTextureFormatSupported() should be specified, rendering will not be functional otherwise.

Note

Setting a new format when the item and its renderer are already initialized and have rendered implies that all QRhiGraphicsPipeline objects created by the renderer may become unusable, if the associated QRhiRenderPassDescriptor is now incompatible due to the different texture format. Similarly to changing sampleCount dynamically, this means that initialize() or render() implementations must then take care of releasing the existing pipelines and creating new ones.

Access functions:
property effectiveColorBufferSizeᅟ: QSize

This property exposes the size, in pixels, of the underlying color buffer (the QRhiTexture or QRhiRenderBuffer). It is provided for use on the GUI (main) thread, in QML bindings or JavaScript.

Note

QQuickRhiItemRenderer implementations, operating on the scene graph render thread, should not use this property. Those should rather query the size from the render target .

Note

The value becomes available asynchronously from the main thread’s perspective in the sense that the value changes when rendering happens on the render thread. This means that this property is useful mainly in QML bindings. Application code must not assume that the value is up to date already when the QQuickRhiItem object is constructed.

This is a read-only property.

Access functions:
property fixedColorBufferHeightᅟ: int

The fixed height, in pixels, of the item’s associated texture. Relevant when a fixed texture size is desired that does not depend on the item’s size. This size has no effect on the geometry of the item (its size and placement within the scene), which means the texture’s content will appear stretched (scaled up) or scaled down onto the item’s area.

For example, setting a size that is exactly twice the item’s (pixel) size effectively performs 2x supersampling (rendering at twice the resolution and then implicitly scaling down when texturing the quad corresponding to the item in the scene).

By default the value is 0. A value of 0 means that texture’s size follows the item’s size. (texture size = item size * device pixel ratio).

Access functions:
property fixedColorBufferWidthᅟ: int

The fixed width, in pixels, of the item’s associated texture or renderbuffer. Relevant when a fixed color buffer size is desired that does not depend on the item’s size. This size has no effect on the geometry of the item (its size and placement within the scene), which means the texture’s content will appear stretched (scaled up) or scaled down onto the item’s area.

For example, setting a size that is exactly twice the item’s (pixel) size effectively performs 2x supersampling (rendering at twice the resolution and then implicitly scaling down when texturing the quad corresponding to the item in the scene).

By default the value is 0. A value of 0 means that texture’s size follows the item’s size. (texture size = item size * device pixel ratio).

Access functions:
property mirrorVerticallyᅟ: bool

This property controls if texture UVs are flipped when drawing the textured quad. It has no effect on the contents of the offscreen color buffer and the rendering implemented by the QQuickRhiItemRenderer .

The default value is false.

Access functions:
property sampleCountᅟ: int

This property controls for sample count for multisample antialiasing. By default the value is 1 which means MSAA is disabled.

Valid values are 1, 4, 8, and sometimes 16 and 32. QRhi::supportedSampleCounts() can be used to query the supported sample counts at run time, but typically applications should request 1 (no MSAA), 4x (normal MSAA) or 8x (high MSAA).

Note

Setting a new value implies that all QRhiGraphicsPipeline objects created by the renderer must use the same sample count from then on. Existing QRhiGraphicsPipeline objects created with a different sample count must not be used anymore. When the value changes, all color and depth-stencil buffers are destroyed and recreated automatically, and initialize() is invoked again. However, when isAutoRenderTargetEnabled() is false, it will be up to the application to manage this with regards to the depth-stencil buffer or additional color buffers.

Changing the sample count from the default 1 to a higher value implies that colorTexture() becomes None and msaaColorBuffer() starts returning a valid object. Switching back to 1 (or 0), implies the opposite: in the next call to initialize() msaaColorBuffer() is going to return None, whereas colorTexture() becomes once again valid. In addition, resolveTexture() returns a valid (non-multisample) QRhiTexture whenever the sample count is greater than 1 (i.e., MSAA is in use).

Access functions:
__init__([parent=None])
Parameters:

parentQQuickItem

Constructs a new QQuickRhiItem with the given parent.

alphaBlending()
Return type:

bool

Getter of property alphaBlendingᅟ .

alphaBlendingChanged()

Notification signal of property alphaBlendingᅟ .

autoRenderTargetChanged()
colorBufferFormat()
Return type:

TextureFormat

Getter of property colorBufferFormatᅟ .

colorBufferFormatChanged()

Notification signal of property colorBufferFormatᅟ .

abstract createRenderer()
Return type:

QQuickRhiItemRenderer

Reimplement this function to create and return a new instance of a QQuickRhiItemRenderer subclass.

This function will be called on the rendering thread while the GUI thread is blocked.

effectiveColorBufferSize()
Return type:

QSize

Getter of property effectiveColorBufferSizeᅟ .

effectiveColorBufferSizeChanged()

Notification signal of property effectiveColorBufferSizeᅟ .

fixedColorBufferHeight()
Return type:

int

Getter of property fixedColorBufferHeightᅟ .

fixedColorBufferHeightChanged()

Notification signal of property fixedColorBufferHeightᅟ .

fixedColorBufferWidth()
Return type:

int

Getter of property fixedColorBufferWidthᅟ .

fixedColorBufferWidthChanged()

Notification signal of property fixedColorBufferWidthᅟ .

isAutoRenderTargetEnabled()
Return type:

bool

Returns the current automatic depth-stencil buffer and render target management setting.

By default this value is true.

isMirrorVerticallyEnabled()
Return type:

bool

Getter of property mirrorVerticallyᅟ .

mirrorVerticallyChanged()

Notification signal of property mirrorVerticallyᅟ .

sampleCount()
Return type:

int

See also

setSampleCount()

Getter of property sampleCountᅟ .

sampleCountChanged()

Notification signal of property sampleCountᅟ .

setAlphaBlending(enable)
Parameters:

enable – bool

See also

alphaBlending()

Setter of property alphaBlendingᅟ .

setAutoRenderTarget(enabled)
Parameters:

enabled – bool

Controls if a depth-stencil QRhiRenderBuffer and a QRhiTextureRenderTarget is created and maintained automatically by the item. The default value is true. Call this function early on, for example from the derived class’ constructor, with enabled set to false to disable this.

In automatic mode, the size and sample count of the depth-stencil buffer follows the color buffer texture’s settings. In non-automatic mode, renderTarget() and depthStencilBuffer() always return None and it is then up to the application’s implementation of initialize() to take care of setting up and managing these objects.

setColorBufferFormat(format)
Parameters:

formatTextureFormat

Setter of property colorBufferFormatᅟ .

setFixedColorBufferHeight(height)
Parameters:

height – int

Setter of property fixedColorBufferHeightᅟ .

setFixedColorBufferWidth(width)
Parameters:

width – int

Setter of property fixedColorBufferWidthᅟ .

setMirrorVertically(enable)
Parameters:

enable – bool

Setter of property mirrorVerticallyᅟ .

setSampleCount(samples)
Parameters:

samples – int

See also

sampleCount()

Setter of property sampleCountᅟ .