QShaderProgram Class

class Qt3DRender ::QShaderProgram

Encapsulates a Shader Program. 更多...

头: #include <QShaderProgram>
CMake: find_package(Qt6 COMPONENTS 3drender REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::3drender)
qmake: QT += 3drender
Since: Qt 5.5
实例化: ShaderProgram
继承: Qt3DCore::QNode

公共类型

enum Format { GLSL, SPIRV }
enum ShaderType { Vertex, Fragment, TessellationControl, TessellationEvaluation, Geometry, Compute }
enum Status { NotReady, Ready, Error }

特性

公共函数

QByteArray computeShaderCode () const
Qt3DRender::QShaderProgram::Format format () const
QByteArray fragmentShaderCode () const
QByteArray geometryShaderCode () const
QString log () const
void setFormat (Qt3DRender::QShaderProgram::Format format )
void setShaderCode (Qt3DRender::QShaderProgram::ShaderType type , const QByteArray & shaderCode )
QByteArray shaderCode (Qt3DRender::QShaderProgram::ShaderType type ) const
Qt3DRender::QShaderProgram::Status status () const
QByteArray tessellationControlShaderCode () const
QByteArray tessellationEvaluationShaderCode () const
QByteArray vertexShaderCode () const

公共槽

void setComputeShaderCode (const QByteArray & computeShaderCode )
void setFragmentShaderCode (const QByteArray & fragmentShaderCode )
void setGeometryShaderCode (const QByteArray & geometryShaderCode )
void setTessellationControlShaderCode (const QByteArray & tessellationControlShaderCode )
void setTessellationEvaluationShaderCode (const QByteArray & tessellationEvaluationShaderCode )
void setVertexShaderCode (const QByteArray & vertexShaderCode )

信号

void computeShaderCodeChanged (const QByteArray & computeShaderCode )
void formatChanged (Qt3DRender::QShaderProgram::Format format )
void fragmentShaderCodeChanged (const QByteArray & fragmentShaderCode )
void geometryShaderCodeChanged (const QByteArray & geometryShaderCode )
void logChanged (const QString & log )
void statusChanged (Qt3DRender::QShaderProgram::Status status )
void tessellationControlShaderCodeChanged (const QByteArray & tessellationControlShaderCode )
void tessellationEvaluationShaderCodeChanged (const QByteArray & tessellationEvaluationShaderCode )
void vertexShaderCodeChanged (const QByteArray & vertexShaderCode )

静态公共成员

QByteArray loadSource (const QUrl & sourceUrl )

详细描述

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 Uniform Associated Qt3D Parameter name GLSL declaration
ModelMatrix modelMatrix uniform mat4 modelMatrix;
ViewMatrix viewMatrix uniform mat4 viewMatrix;
ProjectionMatrix projectionMatrix uniform mat4 projectionMatrix;
ModelViewMatrix modelView uniform mat4 modelView;
ViewProjectionMatrix viewProjectionMatrix uniform mat4 viewProjectionMatrix;
ModelViewProjectionMatrix modelViewProjection
mvp
uniform mat4 modelViewProjection;
uniform mat4 mvp;
InverseModelMatrix inverseModelMatrix uniform mat4 inverseModelMatrix;
InverseViewMatrix inverseViewMatrix uniform mat4 inverseViewMatrix;
InverseProjectionMatrix inverseProjectionMatrix uniform mat4 inverseProjectionMatrix;
InverseModelViewMatrix inverseModelView uniform mat4 inverseModelView;
InverseViewProjectionMatrix inverseViewProjectionMatrix uniform mat4 inverseViewProjectionMatrix;
InverseModelViewProjectionMatrix inverseModelViewProjection uniform mat4 inverseModelViewProjection;
ModelNormalMatrix modelNormalMatrix uniform mat3 modelNormalMatrix;
ModelViewNormalMatrix modelViewNormal uniform mat3 modelViewNormal;
ViewportMatrix viewportMatrix uniform mat4 viewportMatrix;
InverseViewportMatrix inverseViewportMatrix uniform mat4 inverseViewportMatrix;
AspectRatio
(surface width / surface height)
aspectRatio uniform float aspectRatio;
Exposure exposure uniform float exposure;
Gamma gamma uniform float gamma;
时间
(in nano seconds)
time uniform float time;
EyePosition eyePosition uniform vec3 eyePosition;
SkinningPalette skinningPalette[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:

QParameter *parameter = new QParameter();
parameter->setName("myColor");
parameter->setValue(QVariant::fromValue(QColor(Qt::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));
}
					

成员类型文档编制

[since 5.15] enum QShaderProgram:: Format

This enum identifies the format of the shader code used.

常量 描述
Qt3DRender::QShaderProgram::GLSL 0 OpenGL
Qt3DRender::QShaderProgram::SPIRV 1 Vulkan, OpenGL 5

该枚举在 Qt 5.15 引入或被修改。

enum QShaderProgram:: ShaderType

This enum identifies the type of shader used.

常量 描述
Qt3DRender::QShaderProgram::Vertex 0 Vertex shader
Qt3DRender::QShaderProgram::Fragment 1 Fragment shader
Qt3DRender::QShaderProgram::TessellationControl 2 Tesselation control shader
Qt3DRender::QShaderProgram::TessellationEvaluation 3 Tesselation evaluation shader
Qt3DRender::QShaderProgram::Geometry 4 Geometry shader
Qt3DRender::QShaderProgram::Compute 5 Compute shader

enum QShaderProgram:: Status

This enum identifies the status of shader used.

常量 描述
Qt3DRender::QShaderProgram::NotReady 0 The shader hasn't been compiled and linked yet
Qt3DRender::QShaderProgram::Ready 1 The shader was successfully compiled
Qt3DRender::QShaderProgram::Error 2 An error occurred while compiling the shader

特性文档编制

computeShaderCode : QByteArray

Holds the compute shader code used by this shader program.

访问函数:

QByteArray computeShaderCode () const
void setComputeShaderCode (const QByteArray & computeShaderCode )

通知程序信号:

void computeShaderCodeChanged (const QByteArray & computeShaderCode )

[since 5.15] format : Format

Holds the format of the code provided on the ShaderProgram 。默认为 ShaderProgram .GLSL

该特性在 Qt 5.15 引入。

访问函数:

Qt3DRender::QShaderProgram::Format format () const
void setFormat (Qt3DRender::QShaderProgram::Format format )

通知程序信号:

void formatChanged (Qt3DRender::QShaderProgram::Format format )

fragmentShaderCode : QByteArray

Holds the fragment shader code used by this shader program.

访问函数:

QByteArray fragmentShaderCode () const
void setFragmentShaderCode (const QByteArray & fragmentShaderCode )

通知程序信号:

void fragmentShaderCodeChanged (const QByteArray & fragmentShaderCode )

geometryShaderCode : QByteArray

Holds the geometry shader code used by this shader program.

访问函数:

QByteArray geometryShaderCode () const
void setGeometryShaderCode (const QByteArray & geometryShaderCode )

通知程序信号:

void geometryShaderCodeChanged (const QByteArray & geometryShaderCode )

[read-only] log : const QString

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

访问函数:

QString log () const

通知程序信号:

void logChanged (const QString & log )

[read-only] status : const Status

Holds the status of the current shader program.

访问函数:

Qt3DRender::QShaderProgram::Status status () const

通知程序信号:

void statusChanged (Qt3DRender::QShaderProgram::Status status )

tessellationControlShaderCode : QByteArray

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

访问函数:

QByteArray tessellationControlShaderCode () const
void setTessellationControlShaderCode (const QByteArray & tessellationControlShaderCode )

通知程序信号:

void tessellationControlShaderCodeChanged (const QByteArray & tessellationControlShaderCode )

tessellationEvaluationShaderCode : QByteArray

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

访问函数:

QByteArray tessellationEvaluationShaderCode () const
void setTessellationEvaluationShaderCode (const QByteArray & tessellationEvaluationShaderCode )

通知程序信号:

void tessellationEvaluationShaderCodeChanged (const QByteArray & tessellationEvaluationShaderCode )

vertexShaderCode : QByteArray

Holds the vertex shader code used by this shader program.

访问函数:

QByteArray vertexShaderCode () const
void setVertexShaderCode (const QByteArray & vertexShaderCode )

通知程序信号:

void vertexShaderCodeChanged (const QByteArray & vertexShaderCode )

成员函数文档编制

[static invokable] QByteArray QShaderProgram:: loadSource (const QUrl & sourceUrl )

Returns the shader code loaded from sourceUrl .

注意: 此函数可以被援引,通过元对象系统和从 QML。见 Q_INVOKABLE .

void QShaderProgram:: setShaderCode ( Qt3DRender::QShaderProgram::ShaderType type , const QByteArray & shaderCode )

Sets the shader code for type of shader to the shaderCode .

另请参阅 shaderCode ().

QByteArray QShaderProgram:: shaderCode ( Qt3DRender::QShaderProgram::ShaderType type ) const

Returns the shader code for type .

另请参阅 setShaderCode ().

Qt3DRender::QShaderProgram::Status QShaderProgram:: status () const

Returns the status of the current shader program.

注意: getter 函数对于特性 status。