Qt 3D RHI へのレンダリングの移植
念のためにお知らせしますが、Qt 6 では、Qt 3D はデフォルトで RHI レンダリングバックエンドを使用するようになります。
Qt 5 シリーズの古い OpenGL バックエンドを引き続き使用することも可能です。 これを有効にするには、環境変数 QT3D_RENDERER を opengl に設定します。これは、アプリケーションを RHI に対応させるために移植したくない場合や、RHI バックエンドでは現在制限されている、あるいは利用できない機能が必要な場合に必要です。
現在、RHI に関する既知の制限事項は以下の通りです。
- 明示的にブリティッシュ(Blit)を行う方法がない(クワッドをフレームバッファにレンダリングして手動でブリティッシュを行う必要がある)
- MemoryBarrier を明示的に設定できない
- すべてのテクスチャ形式が利用可能ではない
- Draw Indirectは現在サポートされていません
- ジオメトリシェーダーは現在サポートされていません。
- RHI バックエンドによって、サポートされる機能セットが異なる場合があります。
また、Qt 3D の OpenGL レンダリングバックエンドと、OpenGL 上で動作するQt 3D の RHI レンダリングバックエンドを混同しないようご注意ください。
RHI は、さまざまなグラフィックス API を抽象化したものです。つまり、特定のプラットフォーム上で、複数の RHI が複数のバックエンドを使用する可能性があります。
RHI に特定のバックエンドを強制的に使用させるには、環境変数 QSG_RHI_BACKEND を opengl、vulkan、metal、directx のいずれかに設定する必要があります。
RHI 互換のテクニックの追加
Qt 3D のマテリアル/エフェクトにRHIサポートを追加するには、RHIを対象とした新しいテクニックが必要となります。本稿執筆時点では、有効なRHIバージョンは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);
}RHI互換シェーダーの作成
RHIがどのバックエンド上で動作するかにかかわらず、シェーダーはGLSL 450で記述されます。
以前のGLSLバージョンと比較して変更点はごくわずかですが、主な顕著な違いはユニフォームの宣言方法にあります。また、イン変数およびアウト変数にはロケーションを定義する必要があり、シェーダーステージ間で一貫性を保つ必要がある点にもご注意ください。
#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()
{
...
}シェーダーの変更に関する詳細については、以下を参照してください。Qt3DRender::QShaderProgram
Qt 3D Extras
Qt 3D のマテリアル「Extras」は、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.