]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/Technique.cxx
Compile even if OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION is not set.
[simgear.git] / simgear / scene / material / Technique.cxx
index 5c1d835b683e075a5506b0a8b9d1ba7670862012..aeab9cf365b9b3057c70701b82e6d9165fd02397 100644 (file)
@@ -1,13 +1,19 @@
+
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+
 #include "Technique.hxx"
 #include "Pass.hxx"
 
-#include <boost/bind.hpp>
 #include <boost/foreach.hpp>
 
+#include <iterator>
 #include <vector>
 #include <string>
 
 #include <osg/GLExtensions>
+#include <osg/GL2Extensions>
 #include <osg/Math>
 #include <osgUtil/CullVisitor>
 
@@ -15,6 +21,7 @@
 #include <osgDB/Input>
 #include <osgDB/ParameterOutput>
 
+#include <simgear/props/props.hxx>
 #include <simgear/structure/OSGUtils.hxx>
 
 namespace simgear
@@ -52,16 +59,15 @@ Technique::Technique(bool alwaysValid)
 
 Technique::Technique(const Technique& rhs, const osg::CopyOp& copyop) :
     _contextMap(rhs._contextMap), _alwaysValid(rhs._alwaysValid),
-    _shadowingStateSet(rhs._shadowingStateSet),
+    _shadowingStateSet(copyop(rhs._shadowingStateSet.get())),
     _validExpression(rhs._validExpression),
     _contextIdLocation(rhs._contextIdLocation)
 {
-    using namespace std;
-    using namespace boost;
-    transform(rhs.passes.begin(), rhs.passes.end(),
-              backRefInsertIterator(passes),
-              bind(simgear::clone_ref<Pass>, _1, copyop));
-
+    for (std::vector<ref_ptr<Pass> >::const_iterator itr = rhs.passes.begin(),
+             end = rhs.passes.end();
+         itr != end;
+         ++itr)
+        passes.push_back(static_cast<Pass*>(copyop(itr->get())));
 }
 
 Technique::~Technique()
@@ -185,7 +191,7 @@ void Technique::releaseGLObjects(osg::State* state) const
         pass->releaseGLObjects(state);
     }
     if (state == 0) {
-        for (int i = 0; i < _contextMap.size(); ++i) {
+        for (int i = 0; i < (int)_contextMap.size(); ++i) {
             ContextInfo& info = _contextMap[i];
             Status oldVal = info.valid();
             info.valid.compareAndSwap(oldVal, UNKNOWN);
@@ -213,10 +219,22 @@ class GLVersionExpression : public SGExpression<float>
 public:
     void eval(float& value, const expression::Binding*) const
     {
+#ifdef TECHNIQUE_TEST_EXTENSIONS
+        value = 1.1;
+#else
         value = getGLVersionNumber();
+#endif
     }
 };
 
+Expression* glVersionParser(const SGPropertyNode* exp,
+                            expression::Parser* parser)
+{
+    return new GLVersionExpression();
+}
+
+expression::ExpParserRegistrar glVersionRegistrar("glversion", glVersionParser);
+
 class ExtensionSupportedExpression
     : public GeneralNaryExpression<bool, int>
 {
@@ -237,6 +255,58 @@ protected:
     string _extString;
 };
 
+Expression* extensionSupportedParser(const SGPropertyNode* exp,
+                                     expression::Parser* parser)
+{
+    if (exp->getType() == props::STRING
+        || exp->getType() == props::UNSPECIFIED) {
+        ExtensionSupportedExpression* esp
+            = new ExtensionSupportedExpression(exp->getStringValue());
+        int location = parser->getBindingLayout().addBinding("__contextId",
+                                                             expression::INT);
+        VariableExpression<int>* contextExp
+            = new VariableExpression<int>(location);
+        esp->addOperand(contextExp);
+        return esp;
+    }
+    throw expression::ParseError("extension-supported expression has wrong type");
+}
+
+expression::ExpParserRegistrar
+extensionSupportedRegistrar("extension-supported", extensionSupportedParser);
+
+class GLShaderLanguageExpression : public GeneralNaryExpression<float, int>
+{
+public:
+    void eval(float& value, const expression::Binding* b) const
+    {
+        value = 0.0f;
+        int contextId = getOperand(0)->getValue(b);
+        GL2Extensions* extensions
+            = GL2Extensions::Get(static_cast<unsigned>(contextId), true);
+        if (!extensions)
+            return;
+        if (!extensions->isGlslSupported())
+            return;
+        value = extensions->getLanguageVersion();
+    }
+};
+
+Expression* shaderLanguageParser(const SGPropertyNode* exp,
+                                 expression::Parser* parser)
+{
+    GLShaderLanguageExpression* slexp = new GLShaderLanguageExpression;
+    int location = parser->getBindingLayout().addBinding("__contextId",
+                                                         expression::INT);
+    VariableExpression<int>* contextExp = new VariableExpression<int>(location);
+    slexp->addOperand(contextExp);
+    return slexp;
+}
+
+expression::ExpParserRegistrar shaderLanguageRegistrar("shader-language",
+                                                       glVersionParser);
+
+    
 void Technique::setGLExtensionsPred(float glVersion,
                                     const std::vector<std::string>& extensions)
 {
@@ -246,9 +316,9 @@ void Technique::setGLExtensionsPred(float glVersion,
     int contextLoc = layout.addBinding("__contextId", INT);
     VariableExpression<int>* contextExp
         = new VariableExpression<int>(contextLoc);
-    LessEqualExpression<float>* versionTest
-        = new LessEqualExpression<float>(new SGConstExpression<float>(glVersion),
-                                         new GLVersionExpression);
+    SGExpression<bool>* versionTest
+        = makePredicate<std::less_equal>(new SGConstExpression<float>(glVersion),
+                        new GLVersionExpression);
     AndExpression* extensionsExp = 0;
     for (vector<string>::const_iterator itr = extensions.begin(),
              e = extensions.end();
@@ -273,6 +343,16 @@ void Technique::setGLExtensionsPred(float glVersion,
     setValidExpression(predicate, layout);
 }
 
+void Technique::refreshValidity()
+{
+    for (int i = 0; i < (int)_contextMap.size(); ++i) {
+        ContextInfo& info = _contextMap[i];
+        Status oldVal = info.valid();
+        // What happens if we lose the race here?
+        info.valid.compareAndSwap(oldVal, UNKNOWN);
+    }
+}
+
 bool Technique_writeLocalData(const Object& obj, osgDB::Output& fw)
 {
     const Technique& tniq = static_cast<const Technique&>(obj);
@@ -285,7 +365,7 @@ bool Technique_writeLocalData(const Object& obj, osgDB::Output& fw)
         fw.indent() << "shadowingStateSet\n";
         fw.writeObject(*tniq.getShadowingStateSet());
     }
-    fw.indent() << "passes\n";
+    fw.indent() << "num_passes " << tniq.passes.size() << "\n";
     BOOST_FOREACH(const ref_ptr<Pass>& pass, tniq.passes) {
         fw.writeObject(*pass);
     }