Stellarium 0.12.0
Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · Renderer · File Structure

StelQGLGLSLShader Class Reference

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

#include <StelQGLGLSLShader.hpp>

List of all members.

Classes

struct  OptionalShader
 Simple struct that adds an "enabled" flag to a shader.

Public Member Functions

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

Protected Member Functions

virtual void setUniformValue_ (const char *const name, const float value)
 setUniformValue() implementation for type float.
virtual void setUniformValue_ (const char *const name, const Vec2f value)
 setUniformValue() implementation for type Vec2f.
virtual void setUniformValue_ (const char *const name, const Vec3f &value)
 setUniformValue() implementation for type Vec3f.
virtual void setUniformValue_ (const char *const name, const Vec4f &value)
 setUniformValue() implementation for type Vec4f.
virtual void setUniformValue_ (const char *const name, const Mat4f &m)
 setUniformValue() implementation for type Mat4f.
virtual void setUniformValue_ (const char *const name, const bool value)
 setUniformValue() implementation for type bool.
virtual void setUniformValue_ (const char *const name, const int value)
 setUniformValue() implementation for type int.

Protected Attributes

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

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:
renderer Renderer that created this shader.
internal Is 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:
source Source 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:
name Name of the shader.
source Source code of the shader.

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:
source Source code of the shader.
Returns:
true if succesfully added and compiled, false otherwise.

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  )  [inline, virtual]

Disable previously added named vertex shader.

The shader must be unlocked when this is called.

Parameters:
name Name of the shader to disable.

Implements StelGLSLShader.

Definition at line 171 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::enableVertexShader ( const QString &  name  )  [inline, virtual]

Enable previously added named vertex shader.

The shader must be unlocked when this is called.

Parameters:
name Name 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 [inline, virtual]

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 [inline, virtual]

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 int  value 
) [inline, protected, virtual]

setUniformValue() implementation for type int.

Implements StelGLSLShader.

Definition at line 521 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const   name,
const bool  value 
) [inline, protected, virtual]

setUniformValue() implementation for type bool.

Implements StelGLSLShader.

Definition at line 506 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const   name,
const Mat4f value 
) [inline, protected, virtual]

setUniformValue() implementation for type Mat4f.

Implements StelGLSLShader.

Definition at line 491 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const   name,
const Vec4f value 
) [inline, protected, virtual]

setUniformValue() implementation for type Vec4f.

Implements StelGLSLShader.

Definition at line 476 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const   name,
const Vec3f value 
) [inline, protected, virtual]

setUniformValue() implementation for type Vec3f.

Implements StelGLSLShader.

Definition at line 461 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const   name,
const Vec2f  value 
) [inline, protected, virtual]

setUniformValue() implementation for type Vec2f.

Implements StelGLSLShader.

Definition at line 446 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::setUniformValue_ ( const char *const   name,
const float  value 
) [inline, protected, virtual]

setUniformValue() implementation for type float.

Implements StelGLSLShader.

Definition at line 432 of file StelQGLGLSLShader.hpp.

virtual void StelQGLGLSLShader::unlock (  )  [inline, virtual]

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 (  )  [inline, virtual]

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.

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.

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.

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

Definition at line 400 of file StelQGLGLSLShader.hpp.

Number of items in uniformStorageUsedStack and uniformCountStack.

Definition at line 430 of file StelQGLGLSLShader.hpp.

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.

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:
Generated on Thu Jan 31 14:05:46 2013 for Stellarium by  doxygen 1.6.3