X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscene%2Fmaterial%2FEffect.cxx;h=3ad9316216fd0904e039f9c8caeb79f76092ab2c;hb=d04cf4d8978866eb80a1639b6d4ddfe387338c77;hp=d75a1ac4bb14365e7755ee496e3101a8c910b14a;hpb=54c4055af3b106371ed57852c4ad5f1dd087badc;p=simgear.git diff --git a/simgear/scene/material/Effect.cxx b/simgear/scene/material/Effect.cxx index d75a1ac4..3ad93162 100644 --- a/simgear/scene/material/Effect.cxx +++ b/simgear/scene/material/Effect.cxx @@ -30,14 +30,18 @@ #include #include #include +#include +#include #include +#include #include #include #include #include #include +#include #include #include #include @@ -47,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -61,6 +66,7 @@ #include #include +#include #include #include #include @@ -78,11 +84,13 @@ using namespace osgUtil; using namespace effect; Effect::Effect() + : _cache(0), _isRealized(false) { } Effect::Effect(const Effect& rhs, const CopyOp& copyop) - : root(rhs.root), parametersProp(rhs.parametersProp) + : root(rhs.root), parametersProp(rhs.parametersProp), _cache(0), + _isRealized(rhs._isRealized) { typedef vector > TechniqueList; for (TechniqueList::const_iterator itr = rhs.techniques.begin(), @@ -132,10 +140,11 @@ void Effect::releaseGLObjects(osg::State* state) const Effect::~Effect() { + delete _cache; } void buildPass(Effect* effect, Technique* tniq, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { Pass* pass = new Pass; tniq->passes.push_back(pass); @@ -180,12 +189,12 @@ osg::Vec4f getColor(const SGPropertyNode* prop) struct LightingBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options); + const SGReaderWriterXMLOptions* options); }; void LightingBuilder::buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); if (!realProp) @@ -199,7 +208,7 @@ InstallAttributeBuilder installLighting("lighting"); struct ShadeModelBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); if (!realProp) @@ -221,7 +230,7 @@ InstallAttributeBuilder installShadeModel("shade-model"); struct CullFaceBuilder : PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); if (!realProp) { @@ -246,6 +255,24 @@ struct CullFaceBuilder : PassAttributeBuilder InstallAttributeBuilder installCullFace("cull-face"); +struct ColorMaskBuilder : PassAttributeBuilder +{ + void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, + const SGReaderWriterXMLOptions* options) + { + const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); + if (!realProp) + return; + + ColorMask *mask = new ColorMask; + Vec4 m = getColor(realProp); + mask->setMask(m.r(), m.g(), m.b(), m.a()); + pass->setAttributeAndModes(mask); + } +}; + +InstallAttributeBuilder installColorMask("color-mask"); + EffectNameValue renderingHintInit[] = { { "default", StateSet::DEFAULT_BIN }, @@ -258,7 +285,7 @@ EffectPropertyMap renderingHints(renderingHintInit); struct HintBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); if (!realProp) @@ -274,7 +301,7 @@ InstallAttributeBuilder installHint("rendering-hint"); struct RenderBinBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { if (!isAttributeActive(effect, prop)) return; @@ -301,7 +328,7 @@ InstallAttributeBuilder installRenderBin("render-bin"); struct MaterialBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options); + const SGReaderWriterXMLOptions* options); }; EffectNameValue colorModeInit[] = @@ -317,7 +344,7 @@ EffectPropertyMap colorModes(colorModeInit); void MaterialBuilder::buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { if (!isAttributeActive(effect, prop)) return; @@ -387,7 +414,7 @@ EffectPropertyMap blendFuncModes(blendFuncModesInit); struct BlendBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { if (!isAttributeActive(effect, prop)) return; @@ -468,6 +495,99 @@ struct BlendBuilder : public PassAttributeBuilder InstallAttributeBuilder installBlend("blend"); + +EffectNameValue stencilFunctionInit[] = +{ + {"never", Stencil::NEVER }, + {"less", Stencil::LESS}, + {"equal", Stencil::EQUAL}, + {"less-or-equal", Stencil::LEQUAL}, + {"greater", Stencil::GREATER}, + {"not-equal", Stencil::NOTEQUAL}, + {"greater-or-equal", Stencil::GEQUAL}, + {"always", Stencil::ALWAYS} +}; + +EffectPropertyMap stencilFunction(stencilFunctionInit); + +EffectNameValue stencilOperationInit[] = +{ + {"keep", Stencil::KEEP}, + {"zero", Stencil::ZERO}, + {"replace", Stencil::REPLACE}, + {"increase", Stencil::INCR}, + {"decrease", Stencil::DECR}, + {"invert", Stencil::INVERT}, + {"increase-wrap", Stencil::INCR_WRAP}, + {"decrease-wrap", Stencil::DECR_WRAP} +}; + +EffectPropertyMap stencilOperation(stencilOperationInit); + +struct StencilBuilder : public PassAttributeBuilder +{ + void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, + const SGReaderWriterXMLOptions* options) + { + if (!isAttributeActive(effect, prop)) + return; + + const SGPropertyNode* pmode = getEffectPropertyChild(effect, prop, + "mode"); + if (pmode && !pmode->getValue()) { + pass->setMode(GL_STENCIL, StateAttribute::OFF); + return; + } + const SGPropertyNode* pfunction + = getEffectPropertyChild(effect, prop, "function"); + const SGPropertyNode* pvalue + = getEffectPropertyChild(effect, prop, "value"); + const SGPropertyNode* pmask + = getEffectPropertyChild(effect, prop, "mask"); + const SGPropertyNode* psfail + = getEffectPropertyChild(effect, prop, "stencil-fail"); + const SGPropertyNode* pzfail + = getEffectPropertyChild(effect, prop, "z-fail"); + const SGPropertyNode* ppass + = getEffectPropertyChild(effect, prop, "pass"); + + Stencil::Function func = Stencil::ALWAYS; // Always pass + int ref = 0; + unsigned int mask = ~0u; // All bits on + Stencil::Operation sfailop = Stencil::KEEP; // Keep the old values as default + Stencil::Operation zfailop = Stencil::KEEP; + Stencil::Operation passop = Stencil::KEEP; + + ref_ptr stencilFunc = new Stencil; + + if (pfunction) + findAttr(stencilFunction, pfunction, func); + if (pvalue) + ref = pvalue->getIntValue(); + if (pmask) + mask = pmask->getIntValue(); + + if (psfail) + findAttr(stencilOperation, psfail, sfailop); + if (pzfail) + findAttr(stencilOperation, pzfail, zfailop); + if (ppass) + findAttr(stencilOperation, ppass, passop); + + // Set the stencil operation + stencilFunc->setFunction(func, ref, mask); + + // Set the operation, s-fail, s-pass/z-fail, s-pass/z-pass + stencilFunc->setOperation(sfailop, zfailop, passop); + + // Add the operation to pass + pass->setAttributeAndModes(stencilFunc.get()); + } +}; + +InstallAttributeBuilder installStencil("stencil"); + + EffectNameValue alphaComparisonInit[] = { {"never", AlphaFunc::NEVER}, @@ -485,7 +605,7 @@ alphaComparison(alphaComparisonInit); struct AlphaTestBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { if (!isAttributeActive(effect, prop)) return; @@ -535,10 +655,50 @@ InstallAttributeBuilder installAlphaTest("alpha-test"); InstallAttributeBuilder textureUnitBuilder("texture-unit"); -typedef map > ProgramMap; +// Shader key, used both for shaders with relative and absolute names +typedef pair ShaderKey; + +struct ProgramKey +{ + typedef pair AttribKey; + osgDB::FilePathList paths; + vector shaders; + vector attributes; + struct EqualTo + { + bool operator()(const ProgramKey& lhs, const ProgramKey& rhs) const + { + return (lhs.paths.size() == rhs.paths.size() + && equal(lhs.paths.begin(), lhs.paths.end(), + rhs.paths.begin()) + && lhs.shaders.size() == rhs.shaders.size() + && equal (lhs.shaders.begin(), lhs.shaders.end(), + rhs.shaders.begin()) + && lhs.attributes.size() == rhs.attributes.size() + && equal(lhs.attributes.begin(), lhs.attributes.end(), + rhs.attributes.begin())); + } + }; +}; + +size_t hash_value(const ProgramKey& key) +{ + size_t seed = 0; + boost::hash_range(seed, key.paths.begin(), key.paths.end()); + boost::hash_range(seed, key.shaders.begin(), key.shaders.end()); + boost::hash_range(seed, key.attributes.begin(), key.attributes.end()); + return seed; +} + +// XXX Should these be protected by a mutex? Probably + +typedef tr1::unordered_map, + boost::hash, ProgramKey::EqualTo> +ProgramMap; ProgramMap programMap; -typedef map > ShaderMap; +typedef tr1::unordered_map, boost::hash > +ShaderMap; ShaderMap shaderMap; void reload_shaders() @@ -546,7 +706,7 @@ void reload_shaders() for(ShaderMap::iterator sitr = shaderMap.begin(); sitr != shaderMap.end(); ++sitr) { Shader *shader = sitr->second.get(); - string fileName = osgDB::findDataFile(sitr->first); + string fileName = osgDB::findDataFile(sitr->first.first); if (!fileName.empty()) { shader->loadShaderSourceFromFile(fileName); } @@ -556,42 +716,55 @@ void reload_shaders() struct ShaderProgramBuilder : PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options); + const SGReaderWriterXMLOptions* options); }; void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* + const SGReaderWriterXMLOptions* options) { + using namespace boost; if (!isAttributeActive(effect, prop)) return; PropertyList pVertShaders = prop->getChildren("vertex-shader"); PropertyList pFragShaders = prop->getChildren("fragment-shader"); - string programKey; + PropertyList pAttributes = prop->getChildren("attribute"); + ProgramKey prgKey; for (PropertyList::iterator itr = pVertShaders.begin(), e = pVertShaders.end(); itr != e; ++itr) - { - programKey += (*itr)->getStringValue(); - programKey += ";"; - } + prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(), + Shader::VERTEX)); for (PropertyList::iterator itr = pFragShaders.begin(), e = pFragShaders.end(); itr != e; ++itr) - { - programKey += (*itr)->getStringValue(); - programKey += ";"; + prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(), + Shader::FRAGMENT)); + for (PropertyList::iterator itr = pAttributes.begin(), + e = pAttributes.end(); + itr != e; + ++itr) { + const SGPropertyNode* pName = getEffectPropertyChild(effect, *itr, + "name"); + const SGPropertyNode* pIndex = getEffectPropertyChild(effect, *itr, + "index"); + if (!pName || ! pIndex) + throw BuilderException("malformed attribute property"); + prgKey.attributes + .push_back(ProgramKey::AttribKey(pName->getStringValue(), + pIndex->getValue())); } + if (options) + prgKey.paths = options->getDatabasePathList(); Program* program = 0; - ProgramMap::iterator pitr = programMap.find(programKey); + ProgramMap::iterator pitr = programMap.find(prgKey); if (pitr != programMap.end()) { program = pitr->second.get(); } else { program = new Program; - program->setName(programKey); // Add vertex shaders, then fragment shaders PropertyList& pvec = pVertShaders; Shader::Type stype = Shader::VERTEX; @@ -600,24 +773,29 @@ void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass, nameItr != e; ++nameItr) { string shaderName = (*nameItr)->getStringValue(); - ShaderMap::iterator sitr = shaderMap.find(shaderName); + 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 { - string fileName = osgDB::findDataFile(shaderName, options); - if (!fileName.empty()) { - ref_ptr shader = new Shader(stype); - if (shader->loadShaderSourceFromFile(fileName)) { - program->addShader(shader.get()); - shaderMap.insert(make_pair(shaderName, shader)); - } + ref_ptr shader = new Shader(stype); + if (shader->loadShaderSourceFromFile(fileName)) { + program->addShader(shader.get()); + shaderMap.insert(ShaderMap::value_type(skey, shader)); } } } pvec = pFragShaders; stype = Shader::FRAGMENT; } - programMap.insert(make_pair(programKey, program)); + BOOST_FOREACH(const ProgramKey::AttribKey& key, prgKey.attributes) { + program->addBindAttribLocation(key.first, key.second); + } + programMap.insert(ProgramMap::value_type(prgKey, program)); } pass->setAttributeAndModes(program); } @@ -638,7 +816,7 @@ EffectPropertyMap uniformTypes(uniformTypesInit); struct UniformBuilder :public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { if (!isAttributeActive(effect, prop)) return; @@ -712,7 +890,7 @@ InstallAttributeBuilder installUniform("uniform"); struct NameBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { // name can't use string name = prop->getStringValue(); @@ -734,7 +912,7 @@ EffectPropertyMap polygonModeModes(polygonModeModesInit); struct PolygonModeBuilder : public PassAttributeBuilder { void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { if (!isAttributeActive(effect, prop)) return; @@ -758,8 +936,89 @@ struct PolygonModeBuilder : public PassAttributeBuilder }; InstallAttributeBuilder installPolygonMode("polygon-mode"); + +struct VertexProgramTwoSideBuilder : public PassAttributeBuilder +{ + void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, + const SGReaderWriterXMLOptions* options) + { + const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); + if (!realProp) + return; + pass->setMode(GL_VERTEX_PROGRAM_TWO_SIDE, + (realProp->getValue() + ? StateAttribute::ON : StateAttribute::OFF)); + } +}; + +InstallAttributeBuilder +installTwoSide("vertex-program-two-side"); + +struct VertexProgramPointSizeBuilder : public PassAttributeBuilder +{ + void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, + const SGReaderWriterXMLOptions* options) + { + const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop); + if (!realProp) + return; + pass->setMode(GL_VERTEX_PROGRAM_POINT_SIZE, + (realProp->getValue() + ? StateAttribute::ON : StateAttribute::OFF)); + } +}; + +InstallAttributeBuilder +installPointSize("vertex-program-point-size"); + +EffectNameValue depthFunctionInit[] = +{ + {"never", Depth::NEVER}, + {"less", Depth::LESS}, + {"equal", Depth::EQUAL}, + {"lequal", Depth::LEQUAL}, + {"greater", Depth::GREATER}, + {"notequal", Depth::NOTEQUAL}, + {"gequal", Depth::GEQUAL}, + {"always", Depth::ALWAYS} +}; +EffectPropertyMap depthFunction(depthFunctionInit); + +struct DepthBuilder : public PassAttributeBuilder +{ + void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop, + const SGReaderWriterXMLOptions* options) + { + if (!isAttributeActive(effect, prop)) + return; + ref_ptr depth = new Depth; + const SGPropertyNode* pfunc + = getEffectPropertyChild(effect, prop, "function"); + if (pfunc) { + Depth::Function func = Depth::LESS; + findAttr(depthFunction, pfunc, func); + depth->setFunction(func); + } + const SGPropertyNode* pnear + = getEffectPropertyChild(effect, prop, "near"); + if (pnear) + depth->setZNear(pnear->getValue()); + const SGPropertyNode* pfar + = getEffectPropertyChild(effect, prop, "far"); + if (pfar) + depth->setZFar(pnear->getValue()); + const SGPropertyNode* pmask + = getEffectPropertyChild(effect, prop, "write-mask"); + if (pmask) + depth->setWriteMask(pmask->getValue()); + pass->setAttribute(depth.get()); + } +}; + +InstallAttributeBuilder installDepth("depth"); + void buildTechnique(Effect* effect, const SGPropertyNode* prop, - const osgDB::ReaderWriter::Options* options) + const SGReaderWriterXMLOptions* options) { Technique* tniq = new Technique; effect->techniques.push_back(tniq); @@ -865,13 +1124,16 @@ bool makeParametersFromStateSet(SGPropertyNode* effectRoot, const StateSet* ss) // Walk the techniques property tree, building techniques and // passes. -bool Effect::realizeTechniques(const osgDB::ReaderWriter::Options* options) +bool Effect::realizeTechniques(const SGReaderWriterXMLOptions* options) { + if (_isRealized) + return true; PropertyList tniqList = root->getChildren("technique"); for (PropertyList::iterator itr = tniqList.begin(), e = tniqList.end(); itr != e; ++itr) buildTechnique(this, *itr, options); + _isRealized = true; return true; } @@ -895,6 +1157,27 @@ void Effect::InitializeCallback::doUpdate(osg::Node* node, osg::NodeVisitor* nv) } } +bool Effect::Key::EqualTo::operator()(const Effect::Key& lhs, + const Effect::Key& rhs) const +{ + if (lhs.paths.size() != rhs.paths.size() + || !equal(lhs.paths.begin(), lhs.paths.end(), rhs.paths.begin())) + return false; + if (lhs.unmerged.valid() && rhs.unmerged.valid()) + return props::Compare()(lhs.unmerged, rhs.unmerged); + else + return lhs.unmerged == rhs.unmerged; +} + +size_t hash_value(const Effect::Key& key) +{ + size_t seed = 0; + if (key.unmerged.valid()) + boost::hash_combine(seed, *key.unmerged); + boost::hash_range(seed, key.paths.begin(), key.paths.end()); + return seed; +} + bool Effect_writeLocalData(const Object& obj, osgDB::Output& fw) { const Effect& effect = static_cast(obj);