From b55f75111ea606925e5ff2654be9d02effcef638 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 13 Oct 2010 15:03:02 +0000 Subject: [PATCH] Added support for using a custom osg::Geometry that attempts to force the OpenGL driver to download the texture object to graphics card. Calling IncrementalCompileOperation::assignForceTextureDownloadGeometry() assigns a geometry to the job. --- include/osgUtil/IncrementalCompileOperation | 34 ++++++++++----- src/osgUtil/IncrementalCompileOperation.cpp | 46 ++++++++++++++++++++- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/include/osgUtil/IncrementalCompileOperation b/include/osgUtil/IncrementalCompileOperation index 437d43200..d8f4b0146 100644 --- a/include/osgUtil/IncrementalCompileOperation +++ b/include/osgUtil/IncrementalCompileOperation @@ -15,6 +15,7 @@ #define OSGUTIL_INCREMENTALCOMPILEOPERATOR #include +#include namespace osgUtil { @@ -167,6 +168,16 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation OpenThreads::Mutex* getCompiledMutex() { return &_compiledMutex; } CompileSets& getCompiled() { return _compiled; } + /** Assign a geometry and associated StateSet than is applied after each texture compile to atttempt to force the OpenGL + * drive to download the texture object to OpenGL graphics card.*/ + void assignForceTextureDownloadGeometry(); + + /** Set the osg::Geometry to apply after each texture compile to atttempt to force the OpenGL + * drive to download the texture object to OpenGL graphics card.*/ + void setForceTextureDownloadGeometry(osg::Geometry* geom) { _forceTextureDownloadGeometry = geom; } + osg::Geometry* getForceTextureDownloadGeometry() { return _forceTextureDownloadGeometry.get(); } + const osg::Geometry* getForceTextureDownloadGeometry() const { return _forceTextureDownloadGeometry.get(); } + protected: virtual ~IncrementalCompileOperation(); @@ -174,23 +185,24 @@ class OSGUTIL_EXPORT IncrementalCompileOperation : public osg::GraphicsOperation // forward declare to keep within class namespace class CollectStateToCompile; - double _targetFrameRate; - double _minimumTimeAvailableForGLCompileAndDeletePerFrame; - unsigned int _maximumNumOfObjectsToCompilePerFrame; - double _flushTimeRatio; - double _conservativeTimeRatio; + double _targetFrameRate; + double _minimumTimeAvailableForGLCompileAndDeletePerFrame; + unsigned int _maximumNumOfObjectsToCompilePerFrame; + double _flushTimeRatio; + double _conservativeTimeRatio; - OpenThreads::Mutex _toCompileMutex; - CompileSets _toCompile; + osg::ref_ptr _forceTextureDownloadGeometry; + + OpenThreads::Mutex _toCompileMutex; + CompileSets _toCompile; - OpenThreads::Mutex _compiledMutex; - CompileSets _compiled; + OpenThreads::Mutex _compiledMutex; + CompileSets _compiled; - ContextSet _contexts; + ContextSet _contexts; }; - } #endif diff --git a/src/osgUtil/IncrementalCompileOperation.cpp b/src/osgUtil/IncrementalCompileOperation.cpp index 576a9b631..ba933f51f 100644 --- a/src/osgUtil/IncrementalCompileOperation.cpp +++ b/src/osgUtil/IncrementalCompileOperation.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include @@ -50,6 +52,33 @@ IncrementalCompileOperation::~IncrementalCompileOperation() { } +void IncrementalCompileOperation::assignForceTextureDownloadGeometry() +{ + osg::Geometry* geometry = new osg::Geometry; + + osg::Vec3Array* vertices = new osg::Vec3Array; + vertices->push_back(osg::Vec3(0.0f,0.0f,0.0f)); + geometry->setVertexArray(vertices); + + osg::Vec2Array* texcoords = new osg::Vec2Array; + texcoords->push_back(osg::Vec2(0.0f,0.0f)); + geometry->setTexCoordArray(0, texcoords); + + geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,1)); + + osg::StateSet* stateset = geometry->getOrCreateStateSet(); + stateset->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON); + + osg::Depth* depth = new osg::Depth; + depth->setWriteMask(false); + stateset->setAttribute(depth); + + osg::ColorMask* colorMask = new osg::ColorMask(false,false,false,false); + stateset->setAttribute(colorMask); + + _forceTextureDownloadGeometry = geometry; +} + void IncrementalCompileOperation::assignContexts(Contexts& contexts) { for(Contexts::iterator itr = contexts.begin(); @@ -365,7 +394,22 @@ void IncrementalCompileOperation::operator () (osg::GraphicsContext* context) while(!cd._textures.empty() && osg::Timer::instance()->delta_s(startTick, osg::Timer::instance()->tick()) < compileTime) { - cd._textures.back()->apply(*renderInfo.getState()); + if (_forceTextureDownloadGeometry.get()) + { + if (_forceTextureDownloadGeometry->getStateSet()) + { + renderInfo.getState()->apply(_forceTextureDownloadGeometry->getStateSet()); + } + + renderInfo.getState()->applyTextureAttribute(0, cd._textures.back().get()); + + _forceTextureDownloadGeometry->draw(renderInfo); + } + else + { + cd._textures.back()->apply(*renderInfo.getState()); + } + cd._textures.pop_back(); }