1 #include "Technique.hxx"
4 #include <boost/bind.hpp>
5 #include <boost/foreach.hpp>
10 #include <osg/GLExtensions>
12 #include <osgUtil/CullVisitor>
14 #include <osgDB/Registry>
15 #include <osgDB/Input>
16 #include <osgDB/ParameterOutput>
18 #include <simgear/props/props.hxx>
19 #include <simgear/structure/OSGUtils.hxx>
24 using namespace osgUtil;
29 struct ValidateOperation : GraphicsOperation
31 ValidateOperation(Technique* technique_)
32 : GraphicsOperation(opName, false), technique(technique_)
35 virtual void operator() (GraphicsContext* gc);
36 osg::ref_ptr<Technique> technique;
37 static const std::string opName;
40 const std::string ValidateOperation::opName("ValidateOperation");
43 void ValidateOperation::operator() (GraphicsContext* gc)
45 technique->validateInContext(gc);
49 Technique::Technique(bool alwaysValid)
50 : _alwaysValid(alwaysValid), _contextIdLocation(-1)
54 Technique::Technique(const Technique& rhs, const osg::CopyOp& copyop) :
55 _contextMap(rhs._contextMap), _alwaysValid(rhs._alwaysValid),
56 _shadowingStateSet(rhs._shadowingStateSet),
57 _validExpression(rhs._validExpression),
58 _contextIdLocation(rhs._contextIdLocation)
61 using namespace boost;
62 transform(rhs.passes.begin(), rhs.passes.end(),
63 backRefInsertIterator(passes),
64 bind(simgear::clone_ref<Pass>, _1, copyop));
68 Technique::~Technique()
72 Technique::Status Technique::valid(osg::RenderInfo* renderInfo)
76 unsigned contextID = renderInfo->getContextID();
77 ContextInfo& contextInfo = _contextMap[contextID];
78 Status status = contextInfo.valid();
79 if (status != UNKNOWN)
81 Status newStatus = QUERY_IN_PROGRESS;
82 // lock and spawn validity check.
83 if (!contextInfo.valid.compareAndSwap(status, newStatus)) {
84 // Lost the race with another thread spawning a request
85 return contextInfo.valid();
87 ref_ptr<ValidateOperation> validOp = new ValidateOperation(this);
88 GraphicsContext* context = renderInfo->getState()->getGraphicsContext();
89 GraphicsThread* thread = context->getGraphicsThread();
91 thread->add(validOp.get());
93 context->add(validOp.get());
97 Technique::Status Technique::getValidStatus(const RenderInfo* renderInfo) const
101 ContextInfo& contextInfo = _contextMap[renderInfo->getContextID()];
102 return contextInfo.valid();
105 void Technique::validateInContext(GraphicsContext* gc)
107 unsigned int contextId = gc->getState()->getContextID();
108 ContextInfo& contextInfo = _contextMap[contextId];
109 Status oldVal = contextInfo.valid();
110 Status newVal = INVALID;
111 expression::FixedLengthBinding<1> binding;
112 binding.getBindings()[_contextIdLocation].val.intVal = contextId;
113 if (_validExpression->getValue(&binding))
115 contextInfo.valid.compareAndSwap(oldVal, newVal);
120 enum NumDrawables {NUM_DRAWABLES = 128};
123 EffectGeode::DrawablesIterator
124 Technique::processDrawables(const EffectGeode::DrawablesIterator& begin,
125 const EffectGeode::DrawablesIterator& end,
127 bool isCullingActive)
129 RefMatrix& matrix = *cv->getModelViewMatrix();
130 float depth[NUM_DRAWABLES];
131 EffectGeode::DrawablesIterator itr = begin;
133 = cv->getComputeNearFarMode() != CullVisitor::DO_NOT_COMPUTE_NEAR_FAR;
134 for (int i = 0; i < NUM_DRAWABLES && itr != end; ++itr, ++i) {
135 Drawable* drawable = itr->get();
136 const BoundingBox& bb = drawable->getBound();
137 if ((drawable->getCullCallback()
138 && drawable->getCullCallback()->cull(cv, drawable,
139 &cv->getRenderInfo()))
140 || (isCullingActive && cv->isCulled(bb))) {
144 if (computeNearFar && bb.valid()) {
145 if (!cv->updateCalculatedNearFar(matrix, *drawable, false)) {
150 depth[i] = (bb.valid()
151 ? cv->getDistanceFromEyePoint(bb.center(), false)
156 EffectGeode::DrawablesIterator drawablesEnd = itr;
157 BOOST_FOREACH(ref_ptr<Pass>& pass, passes)
159 cv->pushStateSet(pass.get());
161 for (itr = begin; itr != drawablesEnd; ++itr, ++i) {
162 if (depth[i] != FLT_MAX)
163 cv->addDrawableAndDepth(itr->get(), &matrix, depth[i]);
170 void Technique::resizeGLObjectBuffers(unsigned int maxSize)
172 if (_shadowingStateSet.valid())
173 _shadowingStateSet->resizeGLObjectBuffers(maxSize);
174 BOOST_FOREACH(ref_ptr<Pass>& pass, passes) {
175 pass->resizeGLObjectBuffers(maxSize);
177 _contextMap.resize(maxSize);
180 void Technique::releaseGLObjects(osg::State* state) const
182 if (_shadowingStateSet.valid())
183 _shadowingStateSet->releaseGLObjects(state);
184 BOOST_FOREACH(const ref_ptr<Pass>& pass, passes)
186 pass->releaseGLObjects(state);
189 for (int i = 0; i < _contextMap.size(); ++i) {
190 ContextInfo& info = _contextMap[i];
191 Status oldVal = info.valid();
192 info.valid.compareAndSwap(oldVal, UNKNOWN);
195 ContextInfo& info = _contextMap[state->getContextID()];
196 Status oldVal = info.valid();
197 info.valid.compareAndSwap(oldVal, UNKNOWN);
201 void Technique::setValidExpression(SGExpressionb* exp,
202 const simgear::expression
203 ::BindingLayout& layout)
205 using namespace simgear::expression;
206 _validExpression = exp;
207 VariableBinding binding;
208 if (layout.findBinding("__contextId", binding))
209 _contextIdLocation = binding.location;
212 class GLVersionExpression : public SGExpression<float>
215 void eval(float& value, const expression::Binding*) const
217 value = getGLVersionNumber();
221 Expression* glVersionParser(const SGPropertyNode* exp,
222 expression::Parser* parser)
224 return new GLVersionExpression();
227 expression::ExpParserRegistrar glVersionRegistrar("glversion", glVersionParser);
229 class ExtensionSupportedExpression
230 : public GeneralNaryExpression<bool, int>
233 ExtensionSupportedExpression() {}
234 ExtensionSupportedExpression(const string& extString)
235 : _extString(extString)
238 const string& getExtensionString() { return _extString; }
239 void setExtensionString(const string& extString) { _extString = extString; }
240 void eval(bool&value, const expression::Binding* b) const
242 int contextId = getOperand(0)->getValue(b);
243 value = isGLExtensionSupported((unsigned)contextId, _extString.c_str());
249 Expression* extensionSupportedParser(const SGPropertyNode* exp,
250 expression::Parser* parser)
252 if (exp->getType() == props::STRING
253 || exp->getType() == props::UNSPECIFIED)
254 return new ExtensionSupportedExpression(exp->getStringValue());
255 throw expression::ParseError("extension-supported expression has wrong type");
258 expression::ExpParserRegistrar
259 extensionSupportedRegistrar("extension-supported", extensionSupportedParser);
261 void Technique::setGLExtensionsPred(float glVersion,
262 const std::vector<std::string>& extensions)
265 using namespace expression;
266 BindingLayout layout;
267 int contextLoc = layout.addBinding("__contextId", INT);
268 VariableExpression<int>* contextExp
269 = new VariableExpression<int>(contextLoc);
270 SGExpression<bool>* versionTest
271 = makePredicate<std::less_equal>(new SGConstExpression<float>(glVersion),
272 new GLVersionExpression);
274 LessEqualExpression<float>* versionTest
275 = new LessEqualExpression<float>(new SGConstExpression<float>(glVersion),
276 new GLVersionExpression);
278 AndExpression* extensionsExp = 0;
279 for (vector<string>::const_iterator itr = extensions.begin(),
280 e = extensions.end();
284 extensionsExp = new AndExpression;
285 ExtensionSupportedExpression* supported
286 = new ExtensionSupportedExpression(*itr);
287 supported->addOperand(contextExp);
288 extensionsExp->addOperand(supported);
290 SGExpressionb* predicate = 0;
292 OrExpression* orExp = new OrExpression;
293 orExp->addOperand(versionTest);
294 orExp->addOperand(extensionsExp);
297 predicate = versionTest;
299 setValidExpression(predicate, layout);
302 bool Technique_writeLocalData(const Object& obj, osgDB::Output& fw)
304 const Technique& tniq = static_cast<const Technique&>(obj);
305 fw.indent() << "alwaysValid "
306 << (tniq.getAlwaysValid() ? "TRUE\n" : "FALSE\n");
308 fw.indent() << "glVersion " << tniq.getGLVersion() << "\n";
310 if (tniq.getShadowingStateSet()) {
311 fw.indent() << "shadowingStateSet\n";
312 fw.writeObject(*tniq.getShadowingStateSet());
314 fw.indent() << "num_passes " << tniq.passes.size() << "\n";
315 BOOST_FOREACH(const ref_ptr<Pass>& pass, tniq.passes) {
316 fw.writeObject(*pass);
323 osgDB::RegisterDotOsgWrapperProxy TechniqueProxy
326 "simgear::Technique",
327 "Object simgear::Technique",
329 &Technique_writeLocalData