Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QSGMaterialShader Class

The QSGMaterialShader class represents an OpenGL shader program in the renderer. More...

 #include <QSGMaterialShader>

Inherited by: QSGSimpleMaterialShader.

Public Types

class RenderState

Public Functions

QSGMaterialShader()
virtual ~QSGMaterialShader()
virtual void activate()
virtual char const * const * attributeNames() const = 0
virtual void deactivate()
QOpenGLShaderProgram * program()
virtual void updateState(const RenderState & state, QSGMaterial * newMaterial, QSGMaterial * oldMaterial)

Protected Functions

virtual void compile()
virtual const char * fragmentShader() const = 0
virtual void initialize()
virtual const char * vertexShader() const = 0

Detailed Description

The QSGMaterialShader class represents an OpenGL shader program in the renderer.

The QSGMaterialShader API is very low-level. A more convenient API, which provides almost all the same features, is available through QSGSimpleMaterialShader.

The QSGMaterial and QSGMaterialShader form a tight relationship. For one scene graph (including nested graphs), there is one unique QSGMaterialShader instance which encapsulates the QOpenGLShaderProgram the scene graph uses to render that material, such as a shader to flat coloring of geometry. Each QSGGeometryNode can have a unique QSGMaterial containing the how the shader should be configured when drawing that node, such as the actual color to used to render the geometry.

An instance of QSGMaterialShader is never created explicitely by the user, it will be created on demand by the scene graph through QSGMaterial::createShader(). The scene graph will make sure that there is only one instance of each shader implementation through a scene graph.

The source code returned from vertexShader() is used to control what the material does with the vertiex data that comes in from the geometry. The source code returned from the fragmentShader() is used to control what how the material should fill each individual pixel in the geometry. The vertex and fragment source code is queried once during initialization, changing what is returned from these functions later will not have any effect.

The activate() function is called by the scene graph when a shader is is starting to be used. The deactivate function is called by the scene graph when the shader is no longer going to be used. While active, the scene graph may make one or more calls to updateState() which will update the state of the shader for each individual geometry to render.

The attributeNames() returns the name of the attributes used in the vertexShader(). These are used in the default implementation of activate() and deactive() to decide whice vertex registers are enabled.

The initialize() function is called during program creation to allow subclasses to prepare for use, such as resolve uniform names in the vertexShader() and fragmentShader().

A minimal example:

 class Shader : public QSGMaterialShader
 {
 public:
     const char *vertexShader() const {
         return
         "attribute highp vec4 vertex;          \n"
         "uniform highp mat4 matrix;            \n"
         "void main() {                         \n"
         "    gl_Position = matrix * vertex;    \n"
         "}";
     }

     const char *fragmentShader() const {
         return
         "uniform lowp float opacity;                            \n"
         "void main() {                                          \n"
                 "    gl_FragColor = vec4(1, 0, 0, 1) * opacity; \n"
         "}";
     }

     char const *const *attributeNames() const
     {
         static char const *const names[] = { "vertex", 0 };
         return names;
     }

     void initialize()
     {
         QSGMaterialShader::initialize();
         m_id_matrix = program()->uniformLocation("matrix");
         m_id_opacity = program()->uniformLocation("opacity");
     }

     void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
     {
         Q_ASSERT(program()->isLinked());
         if (state.isMatrixDirty())
             program()->setUniformValue(m_id_matrix, state.combinedMatrix());
         if (state.isOpacityDirty())
             program()->setUniformValue(m_id_opacity, state.opacity());
     }

 private:
     int m_id_matrix;
     int m_id_opacity;
 };

Warning: Instances of QSGMaterialShader belongs to the Scene Graph rendering thread, and cannot be used from the GUI thread.

Member Function Documentation

QSGMaterialShader::QSGMaterialShader()

Creates a new QSGMaterialShader.

QSGMaterialShader::~QSGMaterialShader() [virtual]

void QSGMaterialShader::activate() [virtual]

This function is called by the scene graph to indicate that geometry is about to be rendered using this shader.

State that is global for all uses of the shader, independent of the geometry that is being drawn, can be setup in this function.

If reimplemented, make sure to either call the base class implementation to enable the vertex attribute registers.

char const * const * QSGMaterialShader::attributeNames() const [pure virtual]

void QSGMaterialShader::compile() [virtual protected]

This function is called when the shader is initialized to compile the actual QOpenGLShaderProgram. Do not call it explicitely.

The default implementation will extract the vertexShader() and fragmentShader() and bind the names returned from attributeNames() to consecutive vertex attribute registers starting at 0.

void QSGMaterialShader::deactivate() [virtual]

This function is called by the scene graph to indicate that geometry will no longer to be rendered using this shader.

If reimplemented, make sure to either call the base class implementation to disable the vertex attribute registers.

const char * QSGMaterialShader::fragmentShader() const [pure virtual protected]

void QSGMaterialShader::initialize() [virtual protected]

QOpenGLShaderProgram * QSGMaterialShader::program()

void QSGMaterialShader::updateState(const RenderState & state, QSGMaterial * newMaterial, QSGMaterial * oldMaterial) [virtual]

This function is called by the scene graph before geometry is rendered to make sure the shader is in the right state.

The current rendering state is passed from the scene graph. If the state indicates that any state is dirty, the updateState implementation must update accordingly for the geometry to render correctly.

The subclass specific state, such as the color of a flat color material, should be extracted from newMaterial to update the color uniforms accordingly.

The oldMaterial can be used to minimze state changes when updating material states. The oldMaterial is 0 if this shader was just activated.

See also activate() and deactivate().

const char * QSGMaterialShader::vertexShader() const [pure virtual protected]

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web