2015-04-14 13:55:56 +00:00
# ifndef OPENMW_COMPONENTS_SCENEUTIL_STATESETCONTROLLER_H
# define OPENMW_COMPONENTS_SCENEUTIL_STATESETCONTROLLER_H
2021-10-05 12:21:12 +00:00
# include <components/sceneutil/nodecallback.hpp>
2015-04-14 13:55:56 +00:00
2021-01-24 09:34:33 +00:00
# include <map>
# include <array>
namespace osgUtil
{
class CullVisitor ;
}
2015-04-14 13:55:56 +00:00
namespace SceneUtil
{
2015-09-21 15:58:57 +00:00
/// @brief Implements efficient per-frame updating of StateSets.
2015-04-14 13:55:56 +00:00
/// @par With a naive update there would be race conditions when the OSG draw thread of the last frame
/// queues up a StateSet that we want to modify for the next frame. To solve this we could set the StateSet to
/// DYNAMIC data variance but that would undo all the benefits of the threading model - having the cull and draw
/// traversals run in parallel can yield up to 200% framerates.
2021-01-24 09:34:33 +00:00
/// @par Must be set as UpdateCallback or CullCallback on a Node. If set as a CullCallback, the StateSetUpdater operates on an empty StateSet,
/// otherwise it operates on a clone of the node's existing StateSet.
/// @par If set as an UpdateCallback, race conditions are prevented using a "double buffering" scheme - we have two StateSets that take turns,
2015-12-03 22:44:15 +00:00
/// one StateSet we can write to, the second one is currently in use by the draw traversal of the last frame.
2021-01-24 09:34:33 +00:00
/// @par If set as a CullCallback, race conditions are prevented by mapping statesets to cull visitors - OSG has two cull visitors that take turns,
/// allowing the updater to automatically scale for the number of views.
/// @note When used as a CullCallback, StateSetUpdater will have no effect on leaf nodes such as osg::Geometry and must be used on branch nodes only.
2015-11-02 22:38:34 +00:00
/// @note Do not add the same StateSetUpdater to multiple nodes.
2021-01-24 09:34:33 +00:00
/// @note Do not add multiple StateSetUpdaters on the same Node as they will conflict - instead use the CompositeStateSetUpdater.
2021-10-05 12:21:12 +00:00
class StateSetUpdater : public SceneUtil : : NodeCallback < StateSetUpdater >
2015-04-14 13:55:56 +00:00
{
public :
2015-04-14 15:29:12 +00:00
StateSetUpdater ( ) ;
StateSetUpdater ( const StateSetUpdater & copy , const osg : : CopyOp & copyop ) ;
2015-04-14 14:41:06 +00:00
2021-10-05 12:21:12 +00:00
void operator ( ) ( osg : : Node * node , osg : : NodeVisitor * nv ) ;
2015-04-14 13:55:56 +00:00
/// Apply state - to override in derived classes
/// @note Due to the double buffering approach you *have* to apply all state
/// even if it has not changed since the last frame.
2015-04-14 14:41:06 +00:00
virtual void apply ( osg : : StateSet * stateset , osg : : NodeVisitor * nv ) { }
2015-04-14 13:55:56 +00:00
2022-04-04 20:51:23 +00:00
/// Apply any state specific to the Left view. Default implementation does nothing. Called after apply() \note requires the updater be a cull callback
virtual void applyLeft ( osg : : StateSet * stateset , osgUtil : : CullVisitor * nv ) { }
/// Apply any state specific to the Right view. Default implementation does nothing. Called after apply() \note requires the updater be a cull callback
virtual void applyRight ( osg : : StateSet * stateset , osgUtil : : CullVisitor * nv ) { }
2015-04-14 13:55:56 +00:00
/// Set default state - optionally override in derived classes
/// @par May be used e.g. to allocate StateAttributes.
virtual void setDefaults ( osg : : StateSet * stateset ) { }
2021-10-06 09:15:47 +00:00
2015-06-01 15:02:44 +00:00
/// Reset mStateSets, forcing a setDefaults() on the next frame. Can be used to change the defaults if needed.
void reset ( ) ;
2015-04-14 13:55:56 +00:00
private :
2021-01-24 09:34:33 +00:00
void applyCull ( osg : : Node * node , osgUtil : : CullVisitor * cv ) ;
void applyUpdate ( osg : : Node * node , osg : : NodeVisitor * nv ) ;
osg : : StateSet * getCvDependentStateset ( osgUtil : : CullVisitor * cv ) ;
std : : array < osg : : ref_ptr < osg : : StateSet > , 2 > mStateSetsUpdate ;
std : : map < osgUtil : : CullVisitor * , osg : : ref_ptr < osg : : StateSet > > mStateSetsCull ;
2015-04-14 13:55:56 +00:00
} ;
2015-04-14 14:41:06 +00:00
/// @brief A variant of the StateSetController that can be made up of multiple controllers all controlling the same target.
2015-04-14 15:29:12 +00:00
class CompositeStateSetUpdater : public StateSetUpdater
2015-04-14 14:41:06 +00:00
{
public :
2015-04-14 15:29:12 +00:00
CompositeStateSetUpdater ( ) ;
CompositeStateSetUpdater ( const CompositeStateSetUpdater & copy , const osg : : CopyOp & copyop ) ;
2015-04-14 14:41:06 +00:00
2015-04-14 15:29:12 +00:00
META_Object ( SceneUtil , CompositeStateSetUpdater )
2015-04-14 14:41:06 +00:00
unsigned int getNumControllers ( ) ;
2015-04-14 15:29:12 +00:00
StateSetUpdater * getController ( int i ) ;
2015-04-14 14:41:06 +00:00
2015-04-14 15:29:12 +00:00
void addController ( StateSetUpdater * ctrl ) ;
2015-04-14 14:41:06 +00:00
2020-10-16 18:18:54 +00:00
void apply ( osg : : StateSet * stateset , osg : : NodeVisitor * nv ) override ;
2015-04-14 14:41:06 +00:00
protected :
2020-10-16 18:18:54 +00:00
void setDefaults ( osg : : StateSet * stateset ) override ;
2015-04-14 14:41:06 +00:00
2015-04-14 15:29:12 +00:00
std : : vector < osg : : ref_ptr < StateSetUpdater > > mCtrls ;
2015-04-14 14:41:06 +00:00
} ;
2015-04-14 13:55:56 +00:00
}
# endif