On this page

Scene Graph - Direct3D 11 Under QML

Shows how to render directly with Direct3D 11 under a Qt Quick scene.

The Direct3D 11 Under QML example draws custom Direct3D 11 content under a Qt Quick scene. A custom QQuickItem creates a renderer. The renderer gets the Direct3D 11 device from QSGRendererInterface and records its draw call before the scene graph draws the Qt Quick items. A QML animation changes the size of the squircle. Read this example to learn how to mix native Direct3D 11 rendering with Qt Quick.

Animated squircle rendered with Direct3D 11 under Qt Quick scene with text overlay

Running the Example

You can run the example from:

What to look for

Connecting to the render signals

The D3D11Squircle::sync() function runs on the render thread while the GUI thread is blocked. With the basic render loop, there is no separate render thread, and the function runs on the GUI thread. The function creates the renderer, connects the renderer to the render signals, and copies the QML property values to the renderer:

void D3D11Squircle::sync()
{
    if (!m_renderer) {
        m_renderer = new SquircleRenderer;
        connect(window(), &QQuickWindow::beforeRendering, m_renderer, &SquircleRenderer::frameStart, Qt::DirectConnection);
        connect(window(), &QQuickWindow::beforeRenderPassRecording, m_renderer, &SquircleRenderer::mainPassRecordingStart, Qt::DirectConnection);
    }
    m_renderer->setViewportSize(window()->size() * window()->devicePixelRatio());
    m_renderer->setT(m_t);
    m_renderer->setWindow(window());
}

Both connections use Qt::DirectConnection, so the slots run on the render thread that emits the signals.

Getting the native Direct3D 11 objects

The scene graph emits QQuickWindow::beforeRendering() at the start of each frame, before the render pass starts. In response, the renderer queries the Direct3D 11 device and device context that the scene graph uses:

void SquircleRenderer::frameStart()
{
    QSGRendererInterface *rif = m_window->rendererInterface();

    // We are not prepared for anything other than running with the RHI and its D3D11 backend.
    Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::Direct3D11);

    m_device = reinterpret_cast<ID3D11Device *>(rif->getResource(m_window, QSGRendererInterface::DeviceResource));
    Q_ASSERT(m_device);
    m_context = reinterpret_cast<ID3D11DeviceContext *>(rif->getResource(m_window, QSGRendererInterface::DeviceContextResource));
    Q_ASSERT(m_context);

    if (m_vert.isEmpty())
        prepareShader(VertexStage);
    if (m_frag.isEmpty())
        prepareShader(FragmentStage);

    if (!m_initialized)
        init();
}

The renderer creates its resources on the device of the scene graph, so the resources work in the render pass of the scene graph.

Recording the draw call

The scene graph emits QQuickWindow::beforeRenderPassRecording() after the main render pass starts and before the scene graph records the commands for the Qt Quick items:

void SquircleRenderer::mainPassRecordingStart()
{
    m_window->beginExternalCommands();

    D3D11_MAPPED_SUBRESOURCE mp;
    // will copy the entire constant buffer every time -> pass WRITE_DISCARD -> prevent pipeline stalls
    HRESULT hr = m_context->Map(m_cbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &mp);
    if (SUCCEEDED(hr)) {
        float t = m_t;
        memcpy(mp.pData, &t, 4);
        m_context->Unmap(m_cbuf, 0);
    } else {
        qFatal("Failed to map constant buffer: 0x%x", uint(hr));
    }

    D3D11_VIEWPORT v;
    v.TopLeftX = 0;
    v.TopLeftY = 0;
    v.Width = m_viewportSize.width();
    v.Height = m_viewportSize.height();
    v.MinDepth = 0;
    v.MaxDepth = 1;
    m_context->RSSetViewports(1, &v);

    m_context->VSSetShader(m_vs, nullptr, 0);
    m_context->PSSetShader(m_ps, nullptr, 0);
    m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
    m_context->IASetInputLayout(m_inputLayout);
    m_context->OMSetDepthStencilState(m_dsState, 0);
    float blendConstants[] = { 1, 1, 1, 1 };
    m_context->OMSetBlendState(m_blendState, blendConstants, 0xFFFFFFFF);
    m_context->RSSetState(m_rastState);

    const UINT stride = 2 * sizeof(float); // vec2
    const UINT offset = 0;
    m_context->IASetVertexBuffers(0, 1, &m_vbuf, &stride, &offset);
    m_context->PSSetConstantBuffers(0, 1, &m_cbuf);

    m_context->Draw(4, 0);

    m_window->endExternalCommands();
}

To record native commands, call QQuickWindow::beginExternalCommands() before the commands and QQuickWindow::endExternalCommands() after the commands. Always call both functions as a pair. To draw on top of the Qt Quick scene instead, connect to QQuickWindow::afterRenderPassRecording().

Animating the shader input

The D3D11Squircle.t property controls the squircle size. The QML code animates the property between 0 and 1. In each frame, the renderer writes the value into the constant buffer of the shader:

import QtQuick
import D3D11UnderQML

Item {

    width: 320
    height: 480

    D3D11Squircle {
        SequentialAnimation on t {
            NumberAnimation { to: 1; duration: 2500; easing.type: Easing.InQuad }
            NumberAnimation { to: 0; duration: 2500; easing.type: Easing.OutQuad }
            loops: Animation.Infinite
            running: true
        }
    }
Releasing the resources

The renderer owns the Direct3D 11 resources, and its destructor releases them. The example deletes the renderer on the render thread. Two cases need handling:

  • When the scene graph releases its graphics resources, it emits QQuickWindow::sceneGraphInvalidated(), which calls D3D11Squircle::cleanup().
  • When the item leaves the window, the scene graph calls QQuickItem::releaseResources(). The D3D11Squircle::releaseResources() function schedules a job that deletes the renderer on the render thread.

Connecting to QQuickWindow::sceneGraphInvalidated() alone does not cover both cases. The GUI thread can delete the item before the job runs, so the job stores only the renderer:

void D3D11Squircle::cleanup()
{
    delete m_renderer;
    m_renderer = nullptr;
}

class CleanupJob : public QRunnable
{
public:
    CleanupJob(SquircleRenderer *renderer) : m_renderer(renderer) { }
    void run() override { delete m_renderer; }
private:
    SquircleRenderer *m_renderer;
};

void D3D11Squircle::releaseResources()
{
    window()->scheduleRenderJob(new CleanupJob(m_renderer), QQuickWindow::BeforeSynchronizingStage);
    m_renderer = nullptr;
}

Platform notes

The example runs on Windows only. The example requires the Direct3D 11 backend of the scene graph. The main.cpp file selects the backend before it creates the window:

    QQuickWindow::setGraphicsApi(QSGRendererInterface::Direct3D11);

The example compiles the HLSL shaders at runtime with D3DCompile(), so the example links to d3dcompiler and d3d11.

The following examples render the same content with other graphics APIs:

Example project @ code.qt.io

See also Qt Quick Scene Graph, QQuickWindow, and QSGRendererInterface.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.