En esta página

Qt3DRender::QShaderProgram Class

class Qt3DRender::QShaderProgram

Encapsula un programa de sombreado. Más...

Cabecera: #include <QShaderProgram>
CMake: find_package(Qt6 REQUIRED COMPONENTS 3drender)
target_link_libraries(mytarget PRIVATE Qt6::3drender)
qmake: QT += 3drender
En QML: ShaderProgram
Hereda: Qt3DCore::QNode
Status: Obsoleto

Tipos públicos

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

Propiedades

Funciones públicas

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

Ranuras públicas

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)

Señales

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)

Miembros públicos estáticos

QByteArray loadSource(const QUrl &sourceUrl)

Descripción Detallada

Un programa de sombreado se compone de varios sombreadores diferentes, como sombreadores de vértices y de fragmentos.

Qt3D rellenará automáticamente un conjunto de uniformes por defecto si se encuentran durante la fase de instrospección del shader.

Uniforme por defectoAsociado Qt3D Nombre del parámetroDeclaración GLSL
ModelMatrixmodelMatrixmat4 uniforme modelMatrix;
ViewMatrixviewMatrixmat4 uniforme viewMatrix;
Matriz de proyecciónmatrizdeproyecciónmat4 uniforme projectionMatrix;
ModelViewMatrixmodelViewmat4 uniforme modelView;
ViewProjectionMatrixviewProjectionMatrixmat4 uniforme viewProjectionMatrix;
ModelViewProjectionMatrixmodelViewProjection
mvp
uniform mat4 modelViewProjection;
uniform mat4 mvp;
InverseModelMatrixinverseModelMatrixmat4 uniforme inverseModelMatrix;
InverseViewMatrixinverseViewMatrixmat4 uniforme inverseViewMatrix;
InverseProjectionMatrixinverseProjectionMatrixmat4 uniforme inverseProjectionMatrix;
InverseModelViewMatrixinverseModelViewmat4 uniforme inverseModelView;
InverseViewProjectionMatrixinverseViewProjectionMatrixmat4 uniforme inverseViewProjectionMatrix;
InverseModelViewProjectionMatrixinverseModelViewProjectionmat4 uniforme inverseModelViewProjection;
ModelNormalMatrixmodelNormalMatrixmat3 uniforme modelNormalMatrix;
ModelViewNormalMatrixmodelViewNormalmat3 uniforme modelViewNormal;
ViewportMatrixviewportMatrixmat4 uniforme viewportMatrix;
InverseViewportMatrixinverseViewportMatrixmat4 uniforme inverseViewportMatrix;
AspectRatio
(anchura de la superficie / altura de la superficie)
aspectRatiofloat uniforme aspectRatio;
Exposiciónexposiciónfloat uniforme exposure;
Gammagammafloat uniforme gamma;
Tiempo
(en nano segundos)
tiempofloat uniforme time;
EyePositioneyePositionvec3 uniforme eyePosition;
SkinningPaletteskinningPalette[0]const int maxJoints = 100;
uniform mat4 skinningPalette[maxJoints];

Soporte RHI

Al escribir código de sombreado GLSL 450 para utilizarlo con el backend RHI de Qt 3D, los uniformes predeterminados se proporcionarán como 2 objetos de búfer uniforme.

Las ubicaciones de enlace para estos se establece en los enlaces 0 para los uniformes RenderView y 1 para los uniformes de comandos.

#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);
}

Para los objetos uniform buffer definidos por el usuario, utilice binding empezando en 2 o auto para dejar que Qt 3D resuelva el binding automáticamente. Asegúrese de mantener la coherencia entre las diferentes etapas de sombreado.

#version 450 core

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

layout(location=0) out vec4 fragColor;

void main()
{
    fragColor = myColor;
}

No hay ningún cambio involucrado cuando se trata de la alimentación de valores a los uniformes.

Para el ejemplo anterior, el establecimiento de myColor se podría hacer con:

QParameter *parameter = new QParameter();
parameter->setName("myColor");
parameter->setValue(QVariant::fromValue(QColor(Qt::blue)));

Las texturas todavía tienen que ser definidas como uniformes independientes.

#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));
}

Documentación del tipo de miembro

enum QShaderProgram::Format

Este enum identifica el formato del código de sombreado utilizado.

ConstanteValorDescripción
Qt3DRender::QShaderProgram::GLSL0OpenGL
Qt3DRender::QShaderProgram::SPIRV1Vulkan, OpenGL 5

enum QShaderProgram::ShaderType

Este enum identifica el tipo de sombreador utilizado.

ConstanteValorDescripción
Qt3DRender::QShaderProgram::Vertex0Sombreador de vértices
Qt3DRender::QShaderProgram::Fragment1Sombreador de fragmentos
Qt3DRender::QShaderProgram::TessellationControl2Sombreador de control de teselación
Qt3DRender::QShaderProgram::TessellationEvaluation3Sombreador de evaluación de teselación
Qt3DRender::QShaderProgram::Geometry4Sombreador de geometría
Qt3DRender::QShaderProgram::Compute5Sombreador de cálculo

enum QShaderProgram::Status

Este enum identifica el estado del shader utilizado.

ConstanteValorDescripción
Qt3DRender::QShaderProgram::NotReady0El shader aún no ha sido compilado y enlazado
Qt3DRender::QShaderProgram::Ready1El shader se ha compilado correctamente
Qt3DRender::QShaderProgram::Error2Se ha producido un error al compilar el shader

Documentación de propiedades

computeShaderCode : QByteArray

Contiene el código de sombreado utilizado por este programa de sombreado.

Funciones de acceso:

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

Señal notificadora:

void computeShaderCodeChanged(const QByteArray &computeShaderCode)

format : Format

Contiene el formato del código proporcionado en ShaderProgram. Por defecto es ShaderProgram.GLSL

Funciones de acceso:

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

Señal notificadora:

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

fragmentShaderCode : QByteArray

Contiene el código del sombreador de fragmentos utilizado por este programa de sombreado.

Funciones de acceso:

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

Señal notificadora:

void fragmentShaderCodeChanged(const QByteArray &fragmentShaderCode)

geometryShaderCode : QByteArray

Contiene el código del sombreador de geometría utilizado por este programa de sombreado.

Funciones de acceso:

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

Señal notificadora:

void geometryShaderCodeChanged(const QByteArray &geometryShaderCode)

[read-only] log : QString

Contiene el registro del programa de sombreado actual. Esto es útil para diagnosticar un fallo de compilación del programa de sombreado.

Funciones de acceso:

QString log() const

Señal de notificador:

void logChanged(const QString &log)

[read-only] status : Status

Mantiene el estado del programa de sombreado actual.

Funciones de acceso:

Qt3DRender::QShaderProgram::Status status() const

Señal notificadora:

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

tessellationControlShaderCode : QByteArray

Contiene el código del sombreador de control de teselación utilizado por este programa de sombreado.

Funciones de acceso:

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

Señal notificadora:

void tessellationControlShaderCodeChanged(const QByteArray &tessellationControlShaderCode)

tessellationEvaluationShaderCode : QByteArray

Contiene el código del shader de evaluación de teselación utilizado por este programa de shader.

Funciones de acceso:

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

Señal notificadora:

void tessellationEvaluationShaderCodeChanged(const QByteArray &tessellationEvaluationShaderCode)

vertexShaderCode : QByteArray

Contiene el código del sombreador de vértices utilizado por este programa de sombreado.

Funciones de acceso:

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

Señal notificadora:

void vertexShaderCodeChanged(const QByteArray &vertexShaderCode)

Documentación de la función miembro

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

Devuelve el código shader cargado desde sourceUrl.

Nota: Esta función puede ser invocada a través del sistema de meta-objetos y desde QML. Véase Q_INVOKABLE.

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

Establece el código del shader para type del shader a la shaderCode.

Véase también shaderCode().

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

Devuelve el código del shader para type.

Véase también setShaderCode().

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

Devuelve el estado del programa de sombreado actual.

Nota: Función Getter para el estado de la propiedad.

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