Coin
4.0.3
Coin3D core library
|
The SoCache class is the superclass for all internal cache classes. More...
#include <Inventor/caches/SoCache.h>
Public Member Functions | |
SoCache (SoState *const state) | |
virtual void | addCacheDependency (const SoState *state, SoCache *cache) |
void | addElement (const SoElement *const elem) |
const SoElement * | getInvalidElement (const SoState *const state) const |
void | invalidate (void) |
virtual SbBool | isValid (const SoState *state) const |
void | ref (void) |
void | unref (SoState *state=NULL) |
Protected Member Functions | |
virtual | ~SoCache () |
virtual void | destroy (SoState *state) |
The SoCache class is the superclass for all internal cache classes.
It organizes reference counting to make it possible to share cache instances. It also organizes a list of elements that will affect the cache. If any of the elements have changed since the cache was created, the cache is invalid.
The cache element test algorithm in Coin works like this:
Every element that is read before it is written when a cache is created is stored in the SoCache's element list. This is done to detect when something outside the cache changes.
Example: you have a SoCoordinate3 node outside an SoSeparator, but an SoIndexedFaceSet inside the SoSeparator. If the SoSeparator creates a cache, SoIndexedFaceSet will read SoCoordinateElement, and since SoCoordinateElement hasn't been set after the cache was opened, the cache stores that element in the cache's list of element dependencies.
At the next frame, the SoSeparator will test if the cache is valid, and will then test all dependency elements. If one of the elements doesn't match, the cache is not valid and can't be used.
That's the basics. There are some steps you have to do when creating a cache to make the cache dependencies work. Basically you have to do it like this:
First you reset and store the old value of the cache invalid-flag. Then you push the state so that the cache can detect when something outside the cache is changed (and to be able to change the cache element). Next, you create the cache - don't forget to ref it. Finally, set the current cache in the cache element and build the cache. After building the cache, you pop the state and restore the invalid-cache flag.
When building the cache, all elements that are read will be copied into the cache (using SoElement::copyMatchInfo()), and these copied elements are used to test the validity of the cache (in SoCache::isValid()).
You don't have to manually add element dependencies. They will automatically be picked up when creating the cache. This is handled in SoElement::getConstElement().
If you want the cache to be invalidated when some field inside your node is changed, it's common to overload the notify()-method, and call SoCache::invalidate() whenever the notify()-method for your node is called. See for instance SoShape::notify().
Also, don't delete the cache in your notify() method. Wait until the next time the cache is needed before unref-ing the old cache.
|
protectedvirtual |
Destructor
Adds dependencies from cache to this cache.
Adds elem to the list of elements this cache depends on.
Can be overridden by subclasses to clean up before they are deleted. Default method does nothing.
Reimplemented in SoGLRenderCache.
Returns the element that caused the invalidation. Returns NULL if the cache is valid, or if the cache was not invalidated because of an element.
Return TRUE if this cache is valid, FALSE otherwise.
Reimplemented in SoGLRenderCache, and SoPrimitiveVertexCache.
Decreases the reference count by one. When the reference count reaches zero, the cache is deleted. The SoCache::destroy() method is called before the destructor is called.