In questa pagina

Qt 3D Porting del rendering su RHI

Si ricorda che in Qt 6, Qt 3D utilizzerà per impostazione predefinita il backend di rendering RHI.

È comunque possibile continuare a utilizzare il backend Qt OpenGL della serie Qt 5. È possibile abilitarlo impostando la variabile d'ambiente QT3D_RENDERER su opengl. Ciò è necessario nel caso in cui non si desideri effettuare il porting dell’applicazione per supportare RHI o nel caso in cui siano necessarie funzionalità attualmente limitate o non disponibili sul backend RHI.

Attualmente, le limitazioni note di RHI sono:

  • Non è possibile eseguire il blit in modo esplicito (è necessario eseguire il blit manualmente renderizzando un quadrilatero in un framebuffer)
  • Non è possibile impostare esplicitamente MemoryBarrier
  • Non tutti i formati di texture sono disponibili
  • Il Draw Indirect non è attualmente supportato
  • I Geometry Shader al momento non sono supportati.
  • Backend RHI diversi potrebbero supportare insiemi di funzionalità differenti.

Si prega inoltre di fare attenzione a non confondere il backend di rendering OpenGL Qt 3D con il backend di rendering RHI di Qt 3D che gira su OpenGL.

RHI è un'astrazione su diverse API grafiche. Ciò significa che su una data piattaforma, diversi RHI potrebbero utilizzare diversi backend.

Per forzare RHI a utilizzare un determinato backend, la variabile d’ambiente QSG_RHI_BACKEND deve essere impostata su uno dei seguenti valori: opengl, vulkan, metal, directx.

Aggiunta di tecniche compatibili con RHI

Per aggiungere il supporto RHI a un materiale o effetto Qt 3D, sarà necessaria una nuova tecnica destinata a RHI. Al momento della stesura di questo documento, l’unica versione RHI valida è la 1.0.

Material {
    Effect {
        techniques: [
            Technique {
                id: gl3Technique
                graphicsApiFilter {
                    api: GraphicsApiFilter.OpenGL
                    profile: GraphicsApiFilter.CoreProfile
                    majorVersion: 3
                    minorVersion: 1
                }
                renderPasses: RenderPass {
                    id: gl3Pass
                    shaderProgram: ShaderProgram {
                        ...
                    }
                }
            },
            Technique {
                id: rhiTechnique
                graphicsApiFilter {
                    api: GraphicsApiFilter.RHI
                    profile: GraphicsApiFilter.NoProfile
                    majorVersion: 1
                    minorVersion: 0
                }
                renderPasses: RenderPass {
                    id: rhiPass
                    shaderProgram: ShaderProgram {
                        ...
                    }
                }
            }
        ]
    }
}
QMaterial *material = new QMaterial();
QEffect *effect = new QEffect();

// Set the effect on the material
material->setEffect(effect);

{
    QTechnique *gl3Technique = new QTechnique();
    QRenderPass *gl3Pass = new QRenderPass();
    QShaderProgram *glShader = new QShaderProgram();

    // Set the shader on the render pass
    gl3Pass->setShaderProgram(glShader);

    // Add the pass to the technique
    gl3Technique->addRenderPass(gl3Pass);

    // Set the targeted GL version for the technique
    gl3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL);
    gl3Technique->graphicsApiFilter()->setMajorVersion(3);
    gl3Technique->graphicsApiFilter()->setMinorVersion(1);
    gl3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile);

    // Add the technique to the effect
    effect->addTechnique(gl3Technique);
}

{
    QTechnique *rhiTechnique = new QTechnique();
    QRenderPass *rhiPass = new QRenderPass();
    QShaderProgram *rhiShader = new QShaderProgram();

    // Set the shader on the render pass
    rhiPass->setShaderProgram(glShader);

    // Add the pass to the technique
    rhiTechnique->addRenderPass(rhiPass);

    // Set the targeted RHI version for the technique
    rhiTechnique->graphicsApiFilter()->setApi(QGraphicsApiFilter::RHI);
    rhiTechnique->graphicsApiFilter()->setMajorVersion(1);
    rhiTechnique->graphicsApiFilter()->setMinorVersion(0);
    rhiTechnique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile);

    // Add the technique to the effect
    effect->addTechnique(rhiTechnique);
}

Creazione di uno shader compatibile con RHI

Indipendentemente dal backend su cui verrà eseguito RHI, gli shader saranno scritti in GLSL 450.

Le modifiche sono minime rispetto alle versioni precedenti di GLSL; le principali differenze evidenti riguardano il modo in cui vengono dichiarate le uniform. Si noti inoltre che le variabili di ingresso e di uscita devono avere una posizione definita e che devono essere coerenti tra le diverse fasi dello shader.

#version 450 core

layout(location = 0) in vec3 vertexPosition;
layout(location = 0) out vec3 worldPosition;

layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
  mat4 viewMatrix;
  mat4 projectionMatrix;
  mat4 uncorrectedProjectionMatrix;
  mat4 clipCorrectionMatrix;
  mat4 viewProjectionMatrix;
  mat4 inverseViewMatrix;
  mat4 inverseProjectionMatrix;
  mat4 inverseViewProjectionMatrix;
  mat4 viewportMatrix;
  mat4 inverseViewportMatrix;
  vec4 textureTransformMatrix;
  vec3 eyePosition;
  float aspectRatio;
  float gamma;
  float exposure;
  float time;
  float yUpInNDC;
  float yUpInFBO;
};

layout(std140, binding = 1) uniform qt3d_command_uniforms {
  mat4 modelMatrix;
  mat4 inverseModelMatrix;
  mat4 modelViewMatrix;
  mat3 modelNormalMatrix;
  mat4 inverseModelViewMatrix;
  mat4 modelViewProjection;
  mat4 inverseModelViewProjectionMatrix;
};

void main()
{
    ...
}

Per ulteriori dettagli sulle modifiche agli shader, consultare Qt3DRender::QShaderProgram

Qt 3D Extras

I materiali presenti in Qt 3D Extras sono stati portati su RHI.

© 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.