Added object map to DisplaySettings::setObject(std::string, Object*) & getObject(std::string)

This enables caching of data, such as pre-loaded files or other objects
This commit is contained in:
Robert Osfield 2017-12-03 16:43:06 +00:00
parent d0d00eafa8
commit a17b1ac547
5 changed files with 46 additions and 18 deletions

View File

@ -380,6 +380,10 @@ class OSG_EXPORT DisplaySettings : public osg::Referenced
void setValue(const std::string& name, const std::string& value);
bool getValue(const std::string& name, std::string& value, bool use_getenv_fallback=true) const;
void setObject(const std::string& name, osg::Object* object) { _objectMap[name] = object; }
Object* getObject(const std::string& name) { ObjectMap::iterator itr = _objectMap.find(name); return itr!=_objectMap.end() ? itr->second.get() : 0; }
const Object* getObject(const std::string& name) const { ObjectMap::const_iterator itr = _objectMap.find(name); return itr!=_objectMap.end() ? itr->second.get() : 0; }
protected:
virtual ~DisplaySettings();
@ -451,9 +455,11 @@ protected:
unsigned int _shaderPipelineNumTextureUnits;
typedef std::map<std::string, std::string> ValueMap;
typedef std::map<std::string, ref_ptr<Object> > ObjectMap;
mutable OpenThreads::Mutex _valueMapMutex;
mutable ValueMap _valueMap;
mutable ObjectMap _objectMap;
};

View File

@ -874,7 +874,7 @@ void Shader::_computeShaderDefines()
std::string::size_type first_chararcter = find_first(_shaderSource, NoneOf(" \t"), pos, eol);
OSG_NOTICE<<"\nFound pragma line ["<<_shaderSource.substr(first_chararcter, eol-first_chararcter)<<"]"<<std::endl;
OSG_INFO<<"\nFound pragma line ["<<_shaderSource.substr(first_chararcter, eol-first_chararcter)<<"]"<<std::endl;
if (first_chararcter<eol)
{
@ -916,7 +916,7 @@ void Shader::_computeShaderDefines()
pos = eol;
}
#if 1
#if 0
for(ShaderDefines::iterator itr = _shaderPragmas.defines.begin();
itr != _shaderPragmas.defines.end();
++itr)

View File

@ -704,26 +704,17 @@ void StateSet::setGlobalDefaults()
itr != files.end();
++itr)
{
std::string fileName = *itr;
std::string ext;
std::string::size_type dot = fileName.find_last_of('.');
if (dot!=std::string::npos) ext = std::string(fileName.begin()+dot+1,fileName.end());
OSG_NOTICE<<" filename "<<*itr<<", "<<ext<<std::endl;
if (ext=="vert")
// look up object for specified filename - this will be preloaded by osgDB::Registry
osg::Object* object = DisplaySettings::instance()->getObject(*itr);
osg::Shader* shader = dynamic_cast<osg::Shader*>(object);
if (shader)
{
OSG_NOTICE<<"vertex shader: "<<*itr<<std::endl;
program->addShader(Shader::readShaderFile( Shader::VERTEX, fileName));
program->addShader(shader);
}
else if (ext=="frag")
{
OSG_NOTICE<<"fragment shader: "<<*itr<<std::endl;
program->addShader(Shader::readShaderFile( Shader::FRAGMENT, fileName));
}
}
}
else
if (program->getNumShaders()==0)
{
OSG_NOTICE<<"void StateSet::setGlobalDefaults() ShaderPipeline enabled, numTextUnits = "<<DisplaySettings::instance()->getShaderPipelineNumTextureUnits()<<std::endl;

View File

@ -297,3 +297,33 @@ osg::ref_ptr<Node> osgDB::readRefNodeFiles(osg::ArgumentParser& arguments,const
}
}
/////////////////////////////////////////////////////////////
//
// Helper proxy class for pre-loading shader pipeline shaders
//
struct LoadShaderShaderPipelineFilesProxy
{
LoadShaderShaderPipelineFilesProxy()
{
// pre-load any ShaderPipeline shaders
if (osg::DisplaySettings::instance()->getShaderPipeline())
{
OSG_INFO<<"LoadShaderShaderPipelineFilesProxy() Pre-loading the ShaderPipeline shaders"<<std::endl;
for(osg::DisplaySettings::Filenames::const_iterator itr = osg::DisplaySettings::instance()->getShaderPipelineFiles().begin();
itr != osg::DisplaySettings::instance()->getShaderPipelineFiles().end();
++itr)
{
osg::ref_ptr<osg::Shader> shader = osgDB::readRefShaderFile(*itr);
if (shader.valid())
{
OSG_INFO<<" read shader "<<*itr<<std::endl;
osg::DisplaySettings::instance()->setObject(*itr, shader.get());
}
}
}
}
};
static LoadShaderShaderPipelineFilesProxy s_LoadShaderShaderPipelineFilesProxy;

View File

@ -27,6 +27,7 @@
#include <osgDB/Registry>
#include <osgDB/FileUtils>
#include <osgDB/ReadFile>
#include <osgDB/FileNameUtils>
#include <osgDB/fstream>
#include <osgDB/Archive>