ShaderProgram QML Type

Encapsulates a Shader Program. More...

Import Statement: import Qt3D.Render 2.0
Since: Qt 5.5
Instantiates: QShaderProgram



Detailed Description

ShaderProgram class encapsulates a shader program. A shader program consists of several different shaders, such as vertex and fragment shaders.

Qt3D will automatically populate a set of default uniforms if they are encountered during the shader instrospection phase.

Default UniformAssociated Qt3D Parameter nameGLSL declaration
ModelMatrixmodelMatrixuniform mat4 modelMatrix;
ViewMatrixviewMatrixuniform mat4 viewMatrix;
ProjectionMatrixprojectionMatrixuniform mat4 projectionMatrix;
ModelViewMatrixmodelViewuniform mat4 modelView;
ViewProjectionMatrixviewProjectionMatrixuniform mat4 viewProjectionMatrix;
uniform mat4 modelViewProjection;
uniform mat4 mvp;
InverseModelMatrixinverseModelMatrixuniform mat4 inverseModelMatrix;
InverseViewMatrixinverseViewMatrixuniform mat4 inverseViewMatrix;
InverseProjectionMatrixinverseProjectionMatrixuniform mat4 inverseProjectionMatrix;
InverseModelViewMatrixinverseModelViewuniform mat4 inverseModelView;
InverseViewProjectionMatrixinverseViewProjectionMatrixuniform mat4 inverseViewProjectionMatrix;
InverseModelViewProjectionMatrixinverseModelViewProjectionuniform mat4 inverseModelViewProjection;
ModelNormalMatrixmodelNormalMatrixuniform mat3 modelNormalMatrix;
ModelViewNormalMatrixmodelViewNormaluniform mat3 modelViewNormal;
ViewportMatrixviewportMatrixuniform mat4 viewportMatrix;
InverseViewportMatrixinverseViewportMatrixuniform mat4 inverseViewportMatrix;
(surface width / surface height)
aspectRatiouniform float aspectRatio;
Exposureexposureuniform float exposure;
Gammagammauniform float gamma;
(in nano seconds)
timeuniform float time;
EyePositioneyePositionuniform vec3 eyePosition;
SkinningPaletteskinningPalette[0]const int maxJoints = 100;
uniform mat4 skinningPalette[maxJoints];

RHI Support

When writing GLSL 450 shader code to use with Qt 3D's RHI backend, the default uniforms will be provided as 2 uniform buffer objects.

The binding locations for these is set to bindings 0 for RenderView uniforms and 1 for Command uniforms.

#version 450 core

layout(location = 0) in vec3 vertexPosition;

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()
    gl_Position = (projectionMatrix * viewMatrix * modelMatrix * vertexPosition);

For user defined uniform buffer object, use binding starting at 2 or auto to let Qt 3D work out the binding automatically. Make sure to remain consistent between the different shader stages.

#version 450 core

layout(std140, binding = auto) uniform my_uniforms {
  vec4 myColor;

layout(location=0) out vec4 fragColor;

void main()
    fragColor = myColor;

There is no change involved when it comes to feeding values to uniforms.

For the above example, setting myColor could be done with:

Parameter { name: "myColor"; value: "blue" }

Textures still have to be defined as standalone uniforms.

#version 450 core

layout(binding=0) uniform sampler2D source;

layout(location=0) out vec4 fragColor;

void main()
    fragColor = texture(source, vec2(0.5, 0.5));

Property Documentation

computeShaderCode : string

Holds the compute shader code used by this shader program.

computeShaderGraph : string

Holds the URL to the compute shader graph used by this shader program builder.

[since 5.15] format : enumeration

Holds the format of the code provided on the ShaderProgram. The default is ShaderProgram.GLSL

This property was introduced in Qt 5.15.

fragmentShaderCode : string

Holds the fragment shader code used by this shader program.

fragmentShaderGraph : string

Holds the URL to the fragment shader graph used by this shader program builder.

geometryShaderCode : string

Holds the geometry shader code used by this shader program.

geometryShaderGraph : string

Holds the URL to the geometry shader graph used by this shader program builder.

log : string

Holds the log of the current shader program. This is useful to diagnose a compilation failure of the shader program.

status : enumeration

Holds the status of the current shader program.

tessellationControlShaderCode : string

Holds the tesselation control shader code used by this shader program.

tessellationControlShaderGraph : string

Holds the URL to the tesselation control shader graph used by this shader program builder.

tessellationEvaluationShaderCode : string

Holds the tesselation evaluation shader code used by this shader program.

tessellationEvaluationShaderGraph : string

Holds the URL to the tesselation evaluation shader graph used by this shader program builder.

vertexShaderCode : string

Holds the vertex shader code used by this shader program.

vertexShaderGraph : string

Holds the URL to the vertex shader graph used by this shader program builder.

Method Documentation

string loadSource(url sourceUrl)

Returns the shader code loaded from sourceUrl.

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