ColorMask *mask = new ColorMask;
Vec4 m = getColor(realProp);
- mask->setMask(m.r(), m.g(), m.b(), m.a());
+ mask->setMask(m.r() > 0.0, m.g() > 0.0, m.b() > 0.0, m.a() > 0.0);
pass->setAttributeAndModes(mask);
}
};
// Shader key, used both for shaders with relative and absolute names
typedef pair<string, Shader::Type> ShaderKey;
+inline ShaderKey makeShaderKey(SGPropertyNode_ptr& ptr, Shader::Type shaderType)
+{
+ return ShaderKey(ptr->getStringValue(), shaderType);
+}
+
struct ProgramKey
{
typedef pair<string, int> AttribKey;
boost::hash<ProgramKey>, ProgramKey::EqualTo>
ProgramMap;
ProgramMap programMap;
+ProgramMap resolvedProgramMap; // map with resolved shader file names
typedef tr1::unordered_map<ShaderKey, ref_ptr<Shader>, boost::hash<ShaderKey> >
ShaderMap;
PropertyList pFragShaders = prop->getChildren("fragment-shader");
PropertyList pAttributes = prop->getChildren("attribute");
ProgramKey prgKey;
- for (PropertyList::iterator itr = pVertShaders.begin(),
- e = pVertShaders.end();
- itr != e;
- ++itr)
- prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
- Shader::VERTEX));
- for (PropertyList::iterator itr = pGeomShaders.begin(),
- e = pGeomShaders.end();
- itr != e;
- ++itr)
- prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
- Shader::GEOMETRY));
- for (PropertyList::iterator itr = pFragShaders.begin(),
- e = pFragShaders.end();
- itr != e;
- ++itr)
- prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
- Shader::FRAGMENT));
+ std::back_insert_iterator<vector<ShaderKey> > inserter(prgKey.shaders);
+ transform(pVertShaders.begin(), pVertShaders.end(), inserter,
+ boost::bind(makeShaderKey, _1, Shader::VERTEX));
+ transform(pGeomShaders.begin(), pGeomShaders.end(), inserter,
+ boost::bind(makeShaderKey, _1, Shader::GEOMETRY));
+ transform(pFragShaders.begin(), pFragShaders.end(), inserter,
+ boost::bind(makeShaderKey, _1, Shader::FRAGMENT));
for (PropertyList::iterator itr = pAttributes.begin(),
e = pAttributes.end();
itr != e;
ProgramMap::iterator pitr = programMap.find(prgKey);
if (pitr != programMap.end()) {
program = pitr->second.get();
- } else {
- program = new Program;
- // Add vertex shaders, then fragment shaders
- PropertyList& pvec = pVertShaders;
- Shader::Type stype = Shader::VERTEX;
- for (int i = 0; i < 3; ++i) {
- for (PropertyList::iterator nameItr = pvec.begin(), e = pvec.end();
- nameItr != e;
- ++nameItr) {
- string shaderName = (*nameItr)->getStringValue();
- string fileName = osgDB::findDataFile(shaderName, options);
- if (fileName.empty())
- throw BuilderException(string("couldn't find shader ") +
- shaderName);
- ShaderKey skey(fileName, stype);
- ShaderMap::iterator sitr = shaderMap.find(skey);
- if (sitr != shaderMap.end()) {
- program->addShader(sitr->second.get());
- } else {
- ref_ptr<Shader> shader = new Shader(stype);
- if (shader->loadShaderSourceFromFile(fileName)) {
- program->addShader(shader.get());
- shaderMap.insert(ShaderMap::value_type(skey, shader));
- }
- }
- }
- if (i == 0) {
- pvec = pGeomShaders;
- stype = Shader::GEOMETRY;
- } else {
- pvec = pFragShaders;
- stype = Shader::FRAGMENT;
+ pass->setAttributeAndModes(program);
+ return;
+ }
+ // The program wasn't in the map using the load path passed in with
+ // the options, but it might have already been loaded using a
+ // different load path i.e., its shaders were found in the fg data
+ // directory. So, resolve the shaders' file names and look in the
+ // resolvedProgramMap for a program using those shaders.
+ ProgramKey resolvedKey;
+ resolvedKey.attributes = prgKey.attributes;
+ BOOST_FOREACH(const ShaderKey& shaderKey, prgKey.shaders)
+ {
+ const string& shaderName = shaderKey.first;
+ Shader::Type stype = shaderKey.second;
+ string fileName = osgDB::findDataFile(shaderName, options);
+ if (fileName.empty())
+ throw BuilderException(string("couldn't find shader ") +
+ shaderName);
+ resolvedKey.shaders.push_back(ShaderKey(fileName, stype));
+ }
+ ProgramMap::iterator resitr = resolvedProgramMap.find(resolvedKey);
+ if (resitr != resolvedProgramMap.end()) {
+ program = resitr->second.get();
+ programMap.insert(ProgramMap::value_type(prgKey, program));
+ pass->setAttributeAndModes(program);
+ return;
+ }
+ program = new Program;
+ BOOST_FOREACH(const ShaderKey& skey, resolvedKey.shaders)
+ {
+ const string& fileName = skey.first;
+ Shader::Type stype = skey.second;
+ ShaderMap::iterator sitr = shaderMap.find(skey);
+ if (sitr != shaderMap.end()) {
+ program->addShader(sitr->second.get());
+ } else {
+ ref_ptr<Shader> shader = new Shader(stype);
+ if (shader->loadShaderSourceFromFile(fileName)) {
+ program->addShader(shader.get());
+ shaderMap.insert(ShaderMap::value_type(skey, shader));
}
}
- BOOST_FOREACH(const ProgramKey::AttribKey& key, prgKey.attributes) {
- program->addBindAttribLocation(key.first, key.second);
- }
- const SGPropertyNode* pGeometryVerticesOut = getEffectPropertyChild(effect, prop, "geometry-vertices-out");
- if ( pGeometryVerticesOut ) {
- program->setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, pGeometryVerticesOut->getIntValue() );
- }
- const SGPropertyNode* pGeometryInputType = getEffectPropertyChild(effect, prop, "geometry-input-type");
- if ( pGeometryInputType ) {
- GLint type;
- findAttr( geometryInputType, pGeometryInputType->getStringValue(), type );
- program->setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, type );
- }
- const SGPropertyNode* pGeometryOutputType = getEffectPropertyChild(effect, prop, "geometry-output-type");
- if ( pGeometryOutputType ) {
- GLint type;
- findAttr( geometryOutputType, pGeometryOutputType->getStringValue(), type );
- program->setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, type );
- }
-
- programMap.insert(ProgramMap::value_type(prgKey, program));
}
+ BOOST_FOREACH(const ProgramKey::AttribKey& key, prgKey.attributes) {
+ program->addBindAttribLocation(key.first, key.second);
+ }
+ const SGPropertyNode* pGeometryVerticesOut
+ = getEffectPropertyChild(effect, prop, "geometry-vertices-out");
+ if (pGeometryVerticesOut)
+ program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT,
+ pGeometryVerticesOut->getIntValue());
+ const SGPropertyNode* pGeometryInputType
+ = getEffectPropertyChild(effect, prop, "geometry-input-type");
+ if (pGeometryInputType) {
+ GLint type;
+ findAttr(geometryInputType, pGeometryInputType->getStringValue(), type);
+ program->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, type);
+ }
+ const SGPropertyNode* pGeometryOutputType
+ = getEffectPropertyChild(effect, prop, "geometry-output-type");
+ if (pGeometryOutputType) {
+ GLint type;
+ findAttr(geometryOutputType, pGeometryOutputType->getStringValue(),
+ type);
+ program->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, type);
+ }
+ programMap.insert(ProgramMap::value_type(prgKey, program));
+ resolvedProgramMap.insert(ProgramMap::value_type(resolvedKey, program));
pass->setAttributeAndModes(program);
}
};
EffectPropertyMap<Uniform::Type> uniformTypes(uniformTypesInit);
+// Optimization hack for common uniforms.
+// XXX protect these with a mutex?
+
+ref_ptr<Uniform> texture0;
+ref_ptr<Uniform> colorMode[3];
+
struct UniformBuilder :public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
const SGReaderWriterXMLOptions* options)
{
+ if (!texture0.valid()) {
+ texture0 = new Uniform(Uniform::SAMPLER_2D, "texture");
+ texture0->set(0);
+ texture0->setDataVariance(Object::STATIC);
+ for (int i = 0; i < 3; ++i) {
+ colorMode[i] = new Uniform(Uniform::INT, "colorMode");
+ colorMode[i]->set(i);
+ colorMode[i]->setDataVariance(Object::STATIC);
+ }
+ }
if (!isAttributeActive(effect, prop))
return;
const SGPropertyNode* nameProp = prop->getChild("name");
static_cast<bool (Uniform::*)(const Vec4&)>(&Uniform::set),
vec4Names, options);
break;
+ case Uniform::INT:
case Uniform::SAMPLER_1D:
case Uniform::SAMPLER_2D:
case Uniform::SAMPLER_3D:
default: // avoid compiler warning
break;
}
+ // optimize common uniforms
+ if (uniformType == Uniform::SAMPLER_2D || uniformType == Uniform::INT)
+ {
+ int val;
+ uniform->get(val);
+ if (uniformType == Uniform::SAMPLER_2D && val == 0
+ && name == "texture") {
+ uniform = texture0;
+ } else if (uniformType == Uniform::INT && val >= 0 && val < 3
+ && name == "colorMode") {
+ uniform = colorMode[val];
+ }
+ }
pass->addUniform(uniform.get());
}
};