Stellarium 0.12.1
List of all members | Classes | Public Member Functions | Protected Member Functions | Protected Attributes
StelQGLGLSLShader Class Reference

QGL based StelGLSLShader implementation, used by the QGL2 renderer backend. More...

#include <StelQGLGLSLShader.hpp>

Public Member Functions

 StelQGLGLSLShader (class StelQGL2Renderer *renderer, bool internal)
 Construct a StelQGLGLSLShader owned by specified renderer. More...
 
virtual bool addVertexShader (const QString &source)
 Add a vertex shader from source, compiling it in the process. More...
 
virtual bool addFragmentShader (const QString &source)
 Add a fragment shader from source, compiling it in the process. More...
 
virtual bool build ()
 Build the shader program. More...
 
virtual QString log () const
 Return a string containing the error log of the shader. More...
 
virtual void unlock ()
 Unlock the shader program for modifications. More...
 
virtual bool addVertexShader (const QString &name, const QString &source)
 Add a named (optional) vertex shader. More...
 
virtual bool hasVertexShader (const QString &name) const
 Has a named vertex shader with specified name been added? More...
 
virtual void enableVertexShader (const QString &name)
 Enable previously added named vertex shader. More...
 
virtual void disableVertexShader (const QString &name)
 Disable previously added named vertex shader. More...
 
virtual void bind ()
 Bind the shader, using it for following draw calls. More...
 
virtual void release ()
 Release a bound shader after use. More...
 
virtual void useUnprojectedPositionAttribute ()
 Does this shader need the unprojected position attribute? More...
 
QGLShaderProgram & getProgram ()
 Get a reference to the current underlying shader program. More...
 
bool useUnprojectedPosition () const
 Does this shader need the unprojected vertex position attribute? More...
 
void uploadUniforms ()
 Upload the stored uniform variables. More...
 
void pushUniformStorage ()
 Push uniform storage state. More...
 
void popUniformStorage ()
 Restores pushed uniform storage stage. More...
 
void clearUniforms ()
 Clear all stored uniforms, freeing uniform storage. More...
 
- Public Member Functions inherited from StelGLSLShader
virtual ~StelGLSLShader ()
 Destroy the shader program. More...
 
template<class T >
void setUniformValue (const char *const name, T value)
 Set value of a uniform shader variable. More...
 

Protected Member Functions

virtual void setUniformValue_ (const char *const name, const float value)
 setUniformValue() implementation for type float. More...
 
virtual void setUniformValue_ (const char *const name, const Vec2f value)
 setUniformValue() implementation for type Vec2f. More...
 
virtual void setUniformValue_ (const char *const name, const Vec3f &value)
 setUniformValue() implementation for type Vec3f. More...
 
virtual void setUniformValue_ (const char *const name, const Vec4f &value)
 setUniformValue() implementation for type Vec4f. More...
 
virtual void setUniformValue_ (const char *const name, const Mat4f &m)
 setUniformValue() implementation for type Mat4f. More...
 
virtual void setUniformValue_ (const char *const name, const bool value)
 setUniformValue() implementation for type bool. More...
 
virtual void setUniformValue_ (const char *const name, const int value)
 setUniformValue() implementation for type int. More...
 
- Protected Member Functions inherited from StelGLSLShader
template<class T >
void setUniformValue_ (const char *const name, T value)
 "Default" overload of setUniformValue() implementation. More...
 

Protected Attributes

class StelQGL2Rendererrenderer
 Renderer that created this shader. More...
 
QVector< QGLShader * > defaultVertexShaders
 Vertex shaders that are always linked in (added by the nameless addVertexShader() overload). More...
 
QMap< QString, OptionalShader > namedVertexShaders
 Optional vertex shaders that may be enabled or disabled. More...
 
QVector< QGLShader * > defaultFragmentShaders
 Fragment shaders that are always linked in (added by the nameless addFragmentShader() overload). More...
 
QMap< uintptr_t,
QGLShaderProgram * > 
programCache
 All shader programs linked so far. More...
 
QGLShaderProgram * program
 Currently used shader program (linked from default and currently enabled shaders). More...
 
QString aggregatedLog
 Log aggregated during all addXXXShader() and build() calls. More...
 
State state
 Current state of the shader. More...
 
bool bound
 Is the shader bound for drawing? More...
 
bool useUnprojectedPosition_
 Does this shader need the unprojectedVertex attribute (position before StelProjector projection) ? More...
 
const bool internal
 Is this an internal shader used by the renderer backend? More...
 
unsigned char uniformStorage [UNIFORM_STORAGE]
 Storage used for uniform data. More...
 
void * uniformStoragePointer
 Pointer to uniformStorage[uniformStorageUsed]. Avoids breaking strict aliasing. More...
 
unsigned char uniformTypes [MAX_UNIFORMS]
 Types of consecutive uniforms in uniformStorage. More...
 
const char * uniformNames [MAX_UNIFORMS]
 Names of consecutive uniforms in uniformStorage. More...
 
int uniformStorageUsed
 Bytes in uniformStorage used up at this moment. More...
 
int uniformCount
 Number of uniforms in uniformStorage at this moment. More...
 
unsigned int uniformStorageUsedStack [MAX_UNIFORMS]
 Stack of uniformStorageUsed values used with pushUniformStorage/popUniformStorage. More...
 
unsigned char uniformCountStack [MAX_UNIFORMS]
 Stack of uniformCount values used with pushUniformStorage/popUniformStorage. More...
 
int uniformStorageStackSize
 Number of items in uniformStorageUsedStack and uniformCountStack. More...
 

Detailed Description

QGL based StelGLSLShader implementation, used by the QGL2 renderer backend.

Note
This is an internal class of the Renderer subsystem and should not be used elsewhere.

Definition at line 38 of file StelQGLGLSLShader.hpp.

Constructor & Destructor Documentation

StelQGLGLSLShader::StelQGLGLSLShader ( class StelQGL2Renderer renderer,
bool  internal 
)

Construct a StelQGLGLSLShader owned by specified renderer.

Used by QGL2 renderer backend.

Parameters
rendererRenderer that created this shader.
internalIs this shader internal to the renderer backend? (Not directly used by the user)

Member Function Documentation

virtual bool StelQGLGLSLShader::addFragmentShader ( const QString &  source)
virtual

Add a fragment shader from source, compiling it in the process.

This operation can fail. In case of failure, use log() to find out the cause.

The shader must be unlocked when this is called.

Parameters
sourceSource code of the shader.
Returns
true if succesfully added and compiled, false otherwise.

Implements StelGLSLShader.

virtual bool StelQGLGLSLShader::addVertexShader ( const QString &  source)
virtual

Add a vertex shader from source, compiling it in the process.

This operation can fail. In case of failure, use log() to find out the cause.

The shader must be unlocked when this is called.

Parameters
sourceSource code of the shader.
Returns
true if succesfully added and compiled, false otherwise.

Implements StelGLSLShader.

virtual bool StelQGLGLSLShader::addVertexShader ( const QString &  name,
const QString &  source 
)
virtual

Add a named (optional) vertex shader.

Named vertex shaders can be disabled and reenabled. (Unnamed shaders are always enabled.) This allows to dynamically exchange implementations of functions, making shaders modular. GLSL projection is an example of this.

The shader must be unlocked when this is called.

Parameters
nameName of the shader.
sourceSource code of the shader.

Implements StelGLSLShader.

virtual void StelQGLGLSLShader::bind ( )
virtual

Bind the shader, using it for following draw calls.

This must be called before setting uniforms. The shader must be built when this is called.

Note that the shader must be released after any draw calls using the shader to allow default shaders to return to use.

Implements StelGLSLShader.

virtual bool StelQGLGLSLShader::build ( )
virtual

Build the shader program.

This must be called before the shader can be bound.

This operation can fail. In case of failure, use log() to find out the cause. If build() fails, the shader should be assumed to no longer be usable and be destroyed.

Returns
true if succesfully built, false otherwise.

Implements StelGLSLShader.

void StelQGLGLSLShader::clearUniforms ( )
inline

Clear all stored uniforms, freeing uniform storage.

Called by release().

Definition at line 320 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::disableVertexShader ( const QString &  name)
inlinevirtual

Disable previously added named vertex shader.

The shader must be unlocked when this is called.

Parameters
nameName of the shader to disable.

Implements StelGLSLShader.

Definition at line 171 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::enableVertexShader ( const QString &  name)
inlinevirtual

Enable previously added named vertex shader.

The shader must be unlocked when this is called.

Parameters
nameName of the shader to enable.

Implements StelGLSLShader.

Definition at line 158 of file StelQGLGLSLShader.hpp.

QGLShaderProgram& StelQGLGLSLShader::getProgram ( )
inline

Get a reference to the current underlying shader program.

Used by QGL2 renderer backend.

Definition at line 196 of file StelQGLGLSLShader.hpp.

virtual bool StelQGLGLSLShader::hasVertexShader ( const QString &  name) const
inlinevirtual

Has a named vertex shader with specified name been added?

Implements StelGLSLShader.

Definition at line 153 of file StelQGLGLSLShader.hpp.

virtual QString StelQGLGLSLShader::log ( ) const
inlinevirtual

Return a string containing the error log of the shader.

If the shader is succesfully built, it will contain a success message. It might also contain GLSL code warnings.

Implements StelGLSLShader.

Definition at line 133 of file StelQGLGLSLShader.hpp.

void StelQGLGLSLShader::popUniformStorage ( )
inline

Restores pushed uniform storage stage.

See Also
pushUniformStorage

Definition at line 305 of file StelQGLGLSLShader.hpp.

void StelQGLGLSLShader::pushUniformStorage ( )
inline

Push uniform storage state.

This is an optimization used internally by the renderer backend. This "saves" the state of uniform storage, allowing to "restore" it by calling popUniformStorage(). This prevents the internal uniform-setting in repeated draw calls from filling the entire uniform storage.

How this works:

Setting uniforms just appends new data to uniform storage; setting the same uniform multiple times just uses more data (determining if a uniform with this name was set already would be expensive).

pushUniformStorage() pushes the number of uniforms and used storage to an internal stack. popUniformStorage() restores this state, throwing away any uniforms set since the push.

Definition at line 291 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::release ( )
virtual

Release a bound shader after use.

This must be called after using the shader so that the StelRenderer backend can go back to using default shaders.

It also must be called before bind()-ing another StelGLSLShader.

Implements StelGLSLShader.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const float  value 
)
inlineprotectedvirtual

setUniformValue() implementation for type float.

Implements StelGLSLShader.

Definition at line 432 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const Vec2f  value 
)
inlineprotectedvirtual

setUniformValue() implementation for type Vec2f.

Implements StelGLSLShader.

Definition at line 446 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const Vec3f value 
)
inlineprotectedvirtual

setUniformValue() implementation for type Vec3f.

Implements StelGLSLShader.

Definition at line 461 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const Vec4f value 
)
inlineprotectedvirtual

setUniformValue() implementation for type Vec4f.

Implements StelGLSLShader.

Definition at line 476 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const Mat4f value 
)
inlineprotectedvirtual

setUniformValue() implementation for type Mat4f.

Implements StelGLSLShader.

Definition at line 491 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const bool  value 
)
inlineprotectedvirtual

setUniformValue() implementation for type bool.

Implements StelGLSLShader.

Definition at line 506 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const  name,
const int  value 
)
inlineprotectedvirtual

setUniformValue() implementation for type int.

Implements StelGLSLShader.

Definition at line 521 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::unlock ( )
inlinevirtual

Unlock the shader program for modifications.

Allows to, add, enable, disable vertex/fragment shaders.

This can be called even if the shader is bound (which is used for last-moment modifications, like GLSL projection in renderer backend), but it must be rebuilt before drawing and releasing.

Implements StelGLSLShader.

Definition at line 138 of file StelQGLGLSLShader.hpp.

void StelQGLGLSLShader::uploadUniforms ( )
inline

Upload the stored uniform variables.

Called when the internally used shader program has been bound, before drawing, by the drawVertexBufferBackend() member function of the renderer backend.

Definition at line 215 of file StelQGLGLSLShader.hpp.

bool StelQGLGLSLShader::useUnprojectedPosition ( ) const
inline

Does this shader need the unprojected vertex position attribute?

Used by vertex buffer backend to determine if this attribute should be provided.

Definition at line 206 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::useUnprojectedPositionAttribute ( )
inlinevirtual

Does this shader need the unprojected position attribute?

If called, the shader will have to declare another vertex attribute, unprojectedVertex, for vertex position before StelProjector projection. Useful e.g. for lighting.

Implements StelGLSLShader.

Definition at line 188 of file StelQGLGLSLShader.hpp.

Member Data Documentation

QString StelQGLGLSLShader::aggregatedLog
protected

Log aggregated during all addXXXShader() and build() calls.

May be aggregated from multiple vertex programs if built multiple times with different shaders enabled.

Definition at line 372 of file StelQGLGLSLShader.hpp.

bool StelQGLGLSLShader::bound
protected

Is the shader bound for drawing?

Definition at line 378 of file StelQGLGLSLShader.hpp.

QVector<QGLShader*> StelQGLGLSLShader::defaultFragmentShaders
protected

Fragment shaders that are always linked in (added by the nameless addFragmentShader() overload).

Definition at line 341 of file StelQGLGLSLShader.hpp.

QVector<QGLShader*> StelQGLGLSLShader::defaultVertexShaders
protected

Vertex shaders that are always linked in (added by the nameless addVertexShader() overload).

Definition at line 334 of file StelQGLGLSLShader.hpp.

const bool StelQGLGLSLShader::internal
protected

Is this an internal shader used by the renderer backend?

Needed to avoing bind/release from calling renderer backend custom shader bind/release.

Definition at line 387 of file StelQGLGLSLShader.hpp.

QMap<QString, OptionalShader> StelQGLGLSLShader::namedVertexShaders
protected

Optional vertex shaders that may be enabled or disabled.

Definition at line 337 of file StelQGLGLSLShader.hpp.

QGLShaderProgram* StelQGLGLSLShader::program
protected

Currently used shader program (linked from default and currently enabled shaders).

Definition at line 366 of file StelQGLGLSLShader.hpp.

QMap<uintptr_t, QGLShaderProgram*> StelQGLGLSLShader::programCache
protected

All shader programs linked so far.

When build() is called, the default and currently enabled optional shaders are linked to a shader program. As re-linking shaders on each frame would be too expensive, any shader combination is only linked once and then retrieved from this cache.

build() is called for each draw call (due to modular shaders being used for vertex projection), so the lookup must be very fast. The ID used for lookup - a 64 bit unsigned integer, is a sum of pointers of all shaders linked in the program.

It is not impossible for false positives to happen, but it's very unlikely. 64bit is not going to overflow any soon (you need a 16EiB address space for that), and as shaders are never deleted or moved in memory, two shaders can never have identical pointers. Then only risk is that two sets of pointers will accidentally add up to the same number, but this is very unlikely as well.

In case this happens, some very simple hash algorithm (still on pointers) might be used instead.

Definition at line 363 of file StelQGLGLSLShader.hpp.

class StelQGL2Renderer* StelQGLGLSLShader::renderer
protected

Renderer that created this shader.

Definition at line 331 of file StelQGLGLSLShader.hpp.

State StelQGLGLSLShader::state
protected

Current state of the shader.

Definition at line 375 of file StelQGLGLSLShader.hpp.

int StelQGLGLSLShader::uniformCount
protected

Number of uniforms in uniformStorage at this moment.

Definition at line 417 of file StelQGLGLSLShader.hpp.

unsigned char StelQGLGLSLShader::uniformCountStack[MAX_UNIFORMS]
protected

Stack of uniformCount values used with pushUniformStorage/popUniformStorage.

See Also
pushUniformStorage, popUniformStorage

Definition at line 427 of file StelQGLGLSLShader.hpp.

const char* StelQGLGLSLShader::uniformNames[MAX_UNIFORMS]
protected

Names of consecutive uniforms in uniformStorage.

We don't own these strings, we just have pointers to them. This means the user code needs to preserve the string until release() is called.

Definition at line 411 of file StelQGLGLSLShader.hpp.

unsigned char StelQGLGLSLShader::uniformStorage[UNIFORM_STORAGE]
protected

Storage used for uniform data.

As we're linking shaders dynamically and the final shader program is only bound directly before drawing, we need to delay uniform upload until that point. Therefore, setUniformValue_ functions just add data to this storage. The data is then uploaded by uploadUniforms().

See Also
uploadUniforms, clearUniforms, pushUniformStorage

Definition at line 397 of file StelQGLGLSLShader.hpp.

void* StelQGLGLSLShader::uniformStoragePointer
protected

Pointer to uniformStorage[uniformStorageUsed]. Avoids breaking strict aliasing.

Definition at line 400 of file StelQGLGLSLShader.hpp.

int StelQGLGLSLShader::uniformStorageStackSize
protected

Number of items in uniformStorageUsedStack and uniformCountStack.

Definition at line 430 of file StelQGLGLSLShader.hpp.

int StelQGLGLSLShader::uniformStorageUsed
protected

Bytes in uniformStorage used up at this moment.

Definition at line 414 of file StelQGLGLSLShader.hpp.

unsigned int StelQGLGLSLShader::uniformStorageUsedStack[MAX_UNIFORMS]
protected

Stack of uniformStorageUsed values used with pushUniformStorage/popUniformStorage.

See Also
pushUniformStorage, popUniformStorage

Definition at line 422 of file StelQGLGLSLShader.hpp.

unsigned char StelQGLGLSLShader::uniformTypes[MAX_UNIFORMS]
protected

Types of consecutive uniforms in uniformStorage.

Needed to interpret uniformStorage data when uploading.

Definition at line 405 of file StelQGLGLSLShader.hpp.

bool StelQGLGLSLShader::useUnprojectedPosition_
protected

Does this shader need the unprojectedVertex attribute (position before StelProjector projection) ?

Definition at line 382 of file StelQGLGLSLShader.hpp.


The documentation for this class was generated from the following file: