#endif
#include "Effect.hxx"
+#include "EffectBuilder.hxx"
#include "Technique.hxx"
#include "Pass.hxx"
bool realizeTechniques,
const osgDB::ReaderWriter::Options* options)
{
- OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
- EffectMap::iterator itr = effectMap.find(name);
- if (itr != effectMap.end())
- return itr->second.get();
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
+ EffectMap::iterator itr = effectMap.find(name);
+ if (itr != effectMap.end())
+ return itr->second.get();
+ }
string effectFileName(name);
effectFileName += ".eff";
string absFileName
= osgDB::findDataFile(effectFileName, options);
if (absFileName.empty()) {
- SG_LOG(SG_INPUT, SG_WARN, "can't find \"" << effectFileName << "\"");
+ SG_LOG(SG_INPUT, SG_ALERT, "can't find \"" << effectFileName << "\"");
return 0;
}
SGPropertyNode_ptr effectProps = new SGPropertyNode();
- readProperties(absFileName, effectProps.ptr(), 0, true);
- Effect* result = makeEffect(effectProps.ptr(), realizeTechniques, options);
- if (result)
- effectMap.insert(make_pair(name, result));
- return result;
+ try {
+ readProperties(absFileName, effectProps.ptr(), 0, true);
+ }
+ catch (sg_io_exception& e) {
+ SG_LOG(SG_INPUT, SG_ALERT, "error reading \"" << absFileName << "\": "
+ << e.getFormattedMessage());
+ return 0;
+ }
+ ref_ptr<Effect> result = makeEffect(effectProps.ptr(), realizeTechniques,
+ options);
+ if (result.valid()) {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
+ pair<EffectMap::iterator, bool> irslt
+ = effectMap.insert(make_pair(name, result));
+ if (!irslt.second) {
+ // Another thread beat us to it!. Discard our newly
+ // constructed Effect and use the one in the cache.
+ result = irslt.first->second;
+ }
+ }
+ return result.release();
}
}
}
}
- Effect* effect = 0;
+ ref_ptr<Effect> effect;
// Merge with the parent effect, if any
SGPropertyNode_ptr inheritProp = prop->getChild("inherits-from");
Effect* parent = 0;
//prop->removeChild("inherits-from");
parent = makeEffect(inheritProp->getStringValue(), false, options);
if (parent) {
- Effect::Cache* cache = parent->getCache();
Effect::Key key;
+ key.unmerged = prop;
if (options) {
- key = Effect::Key(prop, options->getDatabasePathList());
- } else {
- osgDB::FilePathList dummy;
- key = Effect::Key(prop, dummy);
+ key.paths = options->getDatabasePathList();
+ }
+ Effect::Cache* cache = 0;
+ Effect::Cache::iterator itr;
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>
+ lock(effectMutex);
+ cache = parent->getCache();
+ itr = cache->find(key);
+ if (itr != cache->end())
+ effect = itr->second.get();
}
- Effect::Cache::iterator itr = cache->find(key);
- if (itr != cache->end()) {
- effect = itr->second.get();
- } else {
+ if (!effect.valid()) {
effect = new Effect;
effect->root = new SGPropertyNode;
mergePropertyTrees(effect->root, prop, parent->root);
- cache->insert(make_pair(key, effect));
+ effect->parametersProp = effect->root->getChild("parameters");
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>
+ lock(effectMutex);
+ pair<Effect::Cache::iterator, bool> irslt
+ = cache->insert(make_pair(key, effect));
+ if (!irslt.second)
+ effect = irslt.first->second;
}
} else {
- SG_LOG(SG_INPUT, SG_WARN, "can't find base effect " <<
+ SG_LOG(SG_INPUT, SG_ALERT, "can't find base effect " <<
inheritProp->getStringValue());
+ return 0;
}
- }
- if (!effect) {
+ } else {
effect = new Effect;
effect->root = prop;
+ effect->parametersProp = effect->root->getChild("parameters");
+ }
+ if (realizeTechniques) {
+ try {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>
+ lock(effectMutex);
+ effect->realizeTechniques(options);
+ }
+ catch (BuilderException& e) {
+ SG_LOG(SG_INPUT, SG_ALERT, "Error building technique: "
+ << e.getFormattedMessage());
+ return 0;
+ }
}
- effect->parametersProp = effect->root->getChild("parameters");
- if (realizeTechniques)
- effect->realizeTechniques(options);
- return effect;
+ return effect.release();
}
}