osgEarth 2.1.1
Public Member Functions | Private Member Functions | Private Attributes

osgEarth::ArrayUniform Class Reference

List of all members.

Public Member Functions

 ArrayUniform ()
 ArrayUniform (const std::string &name, osg::Uniform::Type type, osg::StateSet *stateSet, unsigned size=1)
void attach (const std::string &name, osg::Uniform::Type type, osg::StateSet *stateSet, unsigned size=1)
void detach ()
void setElement (unsigned index, int value)
void setElement (unsigned index, unsigned value)
void setElement (unsigned index, bool value)
void setElement (unsigned index, float value)
void setElement (unsigned index, const osg::Matrix &value)
void setElement (unsigned index, const osg::Vec3 &value)
bool getElement (unsigned index, int &out_value) const
bool getElement (unsigned index, unsigned &out_value) const
bool getElement (unsigned index, bool &out_value) const
bool getElement (unsigned index, float &out_value) const
bool getElement (unsigned index, osg::Matrix &out_value) const
bool getElement (unsigned index, osg::Vec3 &out_value) const
bool isValid () const
int getNumElements () const

Private Member Functions

void ensureCapacity (unsigned newSize)

Private Attributes

osg::ref_ptr< osg::Uniform > _uniform
osg::ref_ptr< osg::Uniform > _uniformAlt
osg::observer_ptr< osg::StateSet > _stateSet

Detailed Description

Helper class for dealing with array uniforms. Array uniform naming works differently on different drivers (ATI vs NVIDIA), so this class helps mitigate those differences.

Definition at line 61 of file ShaderUtils.


Constructor & Destructor Documentation

osgEarth::ArrayUniform::ArrayUniform ( ) [inline]

Empty array uniform

Definition at line 65 of file ShaderUtils.

{ }
ArrayUniform::ArrayUniform ( const std::string &  name,
osg::Uniform::Type  type,
osg::StateSet *  stateSet,
unsigned  size = 1 
)

Creates or retrieves a named uniform array.

Definition at line 174 of file ShaderUtils.cpp.

{
    attach( name, type, stateSet, size );
}

Here is the call graph for this function:


Member Function Documentation

void ArrayUniform::attach ( const std::string &  name,
osg::Uniform::Type  type,
osg::StateSet *  stateSet,
unsigned  size = 1 
)

Definition at line 180 of file ShaderUtils.cpp.

{
    _uniform    = stateSet->getUniform( name );
    _uniformAlt = stateSet->getUniform( name + "[0]" );

    if ( !isValid() )
    {
        _uniform    = new osg::Uniform( type, name, size );
        _uniformAlt = new osg::Uniform( type, name + "[0]", size );
        stateSet->addUniform( _uniform.get() );
        stateSet->addUniform( _uniformAlt.get() );
    }

    _stateSet = stateSet;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ArrayUniform::detach ( )

Definition at line 361 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        osg::ref_ptr<osg::StateSet> stateSet_safe = _stateSet.get();
        if ( stateSet_safe.valid() )
        {
            stateSet_safe->removeUniform( _uniform->getName() );
            stateSet_safe->removeUniform( _uniformAlt->getName() );

            _uniform = 0L;
            _uniformAlt = 0L;
            _stateSet = 0L;

            stateSet_safe.release(); // don't want to unref delete
        }
    }
}

Here is the call graph for this function:

void ArrayUniform::ensureCapacity ( unsigned  newSize) [private]

Definition at line 300 of file ShaderUtils.cpp.

{
    if ( isValid() && _uniform->getNumElements() < newSize )
    {
        osg::ref_ptr<osg::StateSet> stateSet_safe = _stateSet.get();
        if ( stateSet_safe.valid() )
        {
            osg::ref_ptr<osg::Uniform> _oldUniform    = _uniform.get();
            osg::ref_ptr<osg::Uniform> _oldUniformAlt = _oldUniform.get();

            stateSet_safe->removeUniform( _uniform->getName() );
            stateSet_safe->removeUniform( _uniformAlt->getName() );

            _uniform    = new osg::Uniform( _uniform->getType(), _uniform->getName(), newSize );
            _uniformAlt = new osg::Uniform( _uniform->getType(), _uniform->getName() + "[0]", newSize );

            switch( _oldUniform->getInternalArrayType(_oldUniform->getType()) )
            {
            case GL_FLOAT:
              {
                for( unsigned i = 0; i < _oldUniform->getNumElements(); ++i )
                {
                  float value;
                  _oldUniform->getElement(i, value);
                  setElement( i, value );
                }
              }
              break;

            case GL_INT:
              {
                for( unsigned i = 0; i < _oldUniform->getNumElements(); ++i )
                {
                  int value;
                  _oldUniform->getElement(i, value);
                  setElement( i, value );
                }
              }
              break;

            case GL_UNSIGNED_INT:
              {
                for( unsigned i = 0; i < _oldUniform->getNumElements(); ++i )
                {
                  unsigned value;
                  _oldUniform->getElement(i, value);
                  setElement( i, value );
                }
              }
              break;
            }

            stateSet_safe->addUniform( _uniform.get() );
            stateSet_safe->addUniform( _uniformAlt.get() );

            stateSet_safe.release(); // don't want to unref delete
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool ArrayUniform::getElement ( unsigned  index,
int &  out_value 
) const

Definition at line 263 of file ShaderUtils.cpp.

{
    return isValid() ? _uniform->getElement( index, out_value ) : false;
}

Here is the call graph for this function:

bool ArrayUniform::getElement ( unsigned  index,
unsigned &  out_value 
) const

Definition at line 269 of file ShaderUtils.cpp.

{
    return isValid() ? _uniform->getElement( index, out_value ) : false;
}

Here is the call graph for this function:

bool ArrayUniform::getElement ( unsigned  index,
bool &  out_value 
) const

Definition at line 275 of file ShaderUtils.cpp.

{
    return isValid() ? _uniform->getElement( index, out_value ) : false;
}

Here is the call graph for this function:

bool ArrayUniform::getElement ( unsigned  index,
float &  out_value 
) const

Definition at line 281 of file ShaderUtils.cpp.

{
    return isValid() ? _uniform->getElement( index, out_value ) : false;
}

Here is the call graph for this function:

bool ArrayUniform::getElement ( unsigned  index,
osg::Matrix &  out_value 
) const

Definition at line 287 of file ShaderUtils.cpp.

{
    return isValid() ? _uniform->getElement( index, out_value ) : false;
}

Here is the call graph for this function:

bool ArrayUniform::getElement ( unsigned  index,
osg::Vec3 &  out_value 
) const

Definition at line 293 of file ShaderUtils.cpp.

{
    return isValid() ? _uniform->getElement( index, out_value ) : false;
}

Here is the call graph for this function:

int osgEarth::ArrayUniform::getNumElements ( ) const [inline]

Definition at line 104 of file ShaderUtils.

{ return isValid() ? _uniform->getNumElements() : -1; }
bool osgEarth::ArrayUniform::isValid ( ) const [inline]

Definition at line 103 of file ShaderUtils.

{ return _uniform.valid() && _uniformAlt.valid(); }

Here is the caller graph for this function:

void ArrayUniform::setElement ( unsigned  index,
const osg::Matrix &  value 
)

Definition at line 241 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        ensureCapacity( index+1 );
        _uniform->setElement( index, value );
        _uniformAlt->setElement( index, value );
    }
}

Here is the call graph for this function:

void ArrayUniform::setElement ( unsigned  index,
float  value 
)

Definition at line 230 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        ensureCapacity( index+1 );
        _uniform->setElement( index, value );
        _uniformAlt->setElement( index, value );
    }
}

Here is the call graph for this function:

void ArrayUniform::setElement ( unsigned  index,
unsigned  value 
)

Definition at line 208 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        ensureCapacity( index+1 );
        _uniform->setElement( index, value );
        _uniformAlt->setElement( index, value );
    }
}

Here is the call graph for this function:

void ArrayUniform::setElement ( unsigned  index,
int  value 
)

creates an array uniform helper from an existing stateset

Definition at line 197 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        ensureCapacity( index+1 );
        _uniform->setElement( index, value );
        _uniformAlt->setElement( index, value );
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ArrayUniform::setElement ( unsigned  index,
bool  value 
)

Definition at line 219 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        ensureCapacity( index+1 );
        _uniform->setElement( index, value );
        _uniformAlt->setElement( index, value );
    }
}

Here is the call graph for this function:

void ArrayUniform::setElement ( unsigned  index,
const osg::Vec3 &  value 
)

Definition at line 252 of file ShaderUtils.cpp.

{
    if ( isValid() )
    {
        ensureCapacity( index+1 );
        _uniform->setElement( index, value );
        _uniformAlt->setElement( index, value );
    }
}

Here is the call graph for this function:


Member Data Documentation

osg::observer_ptr<osg::StateSet> osgEarth::ArrayUniform::_stateSet [private]

Definition at line 109 of file ShaderUtils.

osg::ref_ptr<osg::Uniform> osgEarth::ArrayUniform::_uniform [private]

Definition at line 107 of file ShaderUtils.

osg::ref_ptr<osg::Uniform> osgEarth::ArrayUniform::_uniformAlt [private]

Definition at line 108 of file ShaderUtils.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines