Qt 3D Portar el renderizado a RHI
Como recordatorio, en Qt 6, Qt 3D usará por defecto su backend de renderizado RHI.
Usar el antiguo backend OpenGL de la serie Qt 5 sigue siendo posible. Esto puede habilitarse estableciendo la variable de entorno QT3D_RENDERER a opengl. Esto es necesario en caso de que no quieras portar tu aplicación para soportar RHI o en caso de que necesites características que actualmente están limitadas o no están disponibles en el backend RHI.
Actualmente, las limitaciones conocidas de RHI son:
- No hay manera de Blit explícitamente (tienes que blit manualmente mediante la representación de un quad en un framebuffer)
- MemoryBarrier No se puede configurar explícitamente
- No todos los formatos de texturas están disponibles
- Draw Indirect no está soportado actualmente
- Los Geometry Shaders no están soportados actualmente.
- Diferentes RHI backends pueden soportar diferentes características.
Tenga cuidado de no confundir el backend de renderizado OpenGL de Qt 3D con el backend de renderizado RHI de Qt 3D que se ejecuta sobre OpenGL.
RHI es una abstracción sobre diferentes API gráficas. Esto significa que en una plataforma determinada, varios RHI podrían utilizar varios backends.
Para forzar a RHI a usar un determinado backend, la variable de entorno QSG_RHI_BACKEND debe establecerse a uno de opengl, vulkan, metal, directx.
Añadir técnicas compatibles con RHI
Para añadir compatibilidad con RHI a un Material / Efecto de Qt 3D, se necesitará una nueva Técnica orientada a RHI. En el momento de escribir este artículo, la única versión válida de RHI es 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);
}Creación de un sombreador compatible con RHI
Independientemente del backend sobre el que se ejecute RHI, los sombreadores se escribirán en GLSL 450. Los cambios son mínimos en comparación con GLSL anteriores.
Los cambios son mínimos en comparación con versiones anteriores de GLSL, las principales diferencias notables están en la forma en que se declaran los uniformes. También hay que tener en cuenta que las variables de entrada y salida deben tener definidas sus ubicaciones y que éstas deben ser consistentes entre las distintas etapas del 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()
{
...
}Para más detalles sobre los cambios en los sombreadores, consulta Qt3DRender::QShaderProgram
Qt 3D Extras
Los materiales en Qt 3D Extras han sido portados a 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.