Added mapping of texture modes to #define's to inject into shaders where required

This commit is contained in:
Robert Osfield 2016-11-08 10:28:54 +00:00
parent 1c9219560b
commit 5bfefdae6a
5 changed files with 94 additions and 9 deletions

View File

@ -170,7 +170,7 @@ bool setUpStateSet(osg::ArgumentParser& arguments, osg::StateSet* stateset)
OSG_NOTICE<<"****** texture unit : "<<sstream.str()<<std::endl;
stateset->addUniform(new osg::Uniform(sstream.str().c_str(), static_cast<int>(i)));
#if 0
// fragment shader texture defines
sstream.str("");
sstream<<"TEXTURE_VERT_DECLARE"<<i;
@ -216,6 +216,7 @@ bool setUpStateSet(osg::ArgumentParser& arguments, osg::StateSet* stateset)
sstream<<", "<<i<<"); }";
stateset->setDefine(textureFragBodyDefine, sstream.str());
#endif
}

View File

@ -896,7 +896,7 @@ class OSG_EXPORT State : public Referenced
void print(std::ostream& fout) const;
/** Initialize extension used by osg::State.*/
void initializeExtensionProcs();
virtual void initializeExtensionProcs();
/** Get the helper class for dispatching osg::Arrays as OpenGL attribute data.*/
inline AttributeDispatchers& getAttributeDispatchers() { return _arrayDispatchers; }
@ -1022,6 +1022,8 @@ class OSG_EXPORT State : public Referenced
};
typedef std::map<std::string, GLenum> StringModeMap;
typedef std::map<GLenum, std::string> ModeDefineMap;
typedef std::vector<ModeDefineMap> TextureModeDefineMapList;
typedef std::map<StateAttribute::GLMode,ModeStack> ModeMap;
typedef std::vector<ModeMap> TextureModeMapList;
@ -1251,7 +1253,13 @@ class OSG_EXPORT State : public Referenced
return false;
}
/** Initialize ModeDefineMaps used in fixed function modes to shader defines. Called by initializeExtensionProcs().*/
virtual void initUpModeDefineMaps();
StringModeMap _stringModeMap;
TextureModeDefineMapList _textureModeDefineMapList;
TextureModeDefineMapList _texenvModeDefineMapList;
TextureModeDefineMapList _texgenModeDefineMapList;
ModeMap _modeMap;
AttributeMap _attributeMap;

View File

@ -628,7 +628,7 @@ void Shader::PerContextShader::compileShader(osg::State& state)
GLint compiled = GL_FALSE;
// OSG_NOTICE<<"Compiling PerContextShader "<<this<<" ShaderDefine="<<getDefineString()<<std::endl;
bool printOutShaders = osg::getNotifyLevel()>=osg::INFO;//NOTICE;
bool printOutShaders = osg::getNotifyLevel()>=osg::NOTICE;//INFO;
if (_defineStr.empty())
{

View File

@ -27,6 +27,8 @@
#include <osg/Texture1D>
#include <osg/GLDefines>
#include <osg/io_utils>
#include <sstream>
#include <algorithm>
@ -45,6 +47,13 @@
using namespace std;
using namespace osg;
#ifdef WIN32
const char* s_LineEnding = "\r\n";
#else
const char* s_LineEnding = "\n";
#endif
static ApplicationUsageProxy State_e0(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_GL_ERROR_CHECKING <type>","ONCE_PER_ATTRIBUTE | ON | on enables fine grained checking, ONCE_PER_FRAME enables coarse grained checking");
State::State():
@ -299,6 +308,11 @@ void State::initializeExtensionProcs()
_graphicsCostEstimator->calibrate(renderInfo);
}
initUpModeDefineMaps();
}
void State::initUpModeDefineMaps()
{
#define ADDMODE(MODE) _stringModeMap[#MODE] = MODE;
ADDMODE(GL_LIGHTING)
@ -345,6 +359,24 @@ void State::initializeExtensionProcs()
ADDMODE(GL_COLOR_MATERIAL)
unsigned int maxNumTextureUnits = 16;
std::stringstream sstream;
_textureModeDefineMapList.resize(maxNumTextureUnits);
for(unsigned int i=0; i<maxNumTextureUnits; ++i)
{
std::string samplerString("sampler2D");
std::string textureString("texture2D");
std::string textureSuffixString("st");
// fragment shader texture defines
sstream.str("");
sstream<<"#define TEXTURE_VERT_DECLARE"<<i<<" varying vec4 TexCoord"<<i<<";"<<s_LineEnding;
sstream<<"#define TEXTURE_VERT_BODY"<<i<<" { TexCoord"<<i<<" = gl_MultiTexCoord"<<i<<"; if (GL_TEXTURE_GEN_MODE["<<i<<"]!=0) TexCoord0 = texgen(TexCoord"<<i<<", "<<i<<"); }"<<s_LineEnding;
sstream<<"#define TEXTURE_FRAG_DECLARE"<<i<<" uniform sampler2D sampler"<<i<<"; varying vec4 TexCoord"<<i<<";"<<s_LineEnding;
sstream<<"#define TEXTURE_FRAG_BODY"<<i<<"(color) { color = texenv(color, texture2D( sampler"<<i<<", TexCoord"<<i<<".st)"<<", "<<i<<"); }"<<s_LineEnding;
_textureModeDefineMapList[i][GL_TEXTURE_2D] = sstream.str();
}
}
@ -1738,11 +1770,6 @@ bool State::DefineMap::updateCurrentDefines()
return true;
}
#ifdef WIN32
const char* s_LineEnding = "\r\n";
#else
const char* s_LineEnding = "\n";
#endif
void State::getDefineString(std::string& shaderDefineStr, const osg::ShaderDefines& shaderDefines)
{
@ -1774,6 +1801,8 @@ void State::getDefineString(std::string& shaderDefineStr, const osg::ShaderDefin
}
}
inline std::stringstream& str_reset(std::stringstream& sstream) { sstream.str(""); return sstream; }
void State::getDefineString(std::string& shaderDefineStr, const osg::ShaderPragmas& shaderPragmas)
{
if (_defineMap.changed) _defineMap.updateCurrentDefines();
@ -1816,6 +1845,52 @@ void State::getDefineString(std::string& shaderDefineStr, const osg::ShaderPragm
}
}
for(unsigned int i=0; i<_textureModeMapList.size(); ++i)
{
OSG_NOTICE<<" texture unit ="<<i<<std::endl;
const ModeMap& modeMap = _textureModeMapList[i];
bool enableTexGen = false;
Vec4 texgen_multiplier(0.0f,0.0f,0.f,0.0f);
GLenum textureMode = 0;
for(ModeMap::const_iterator tm_itr = modeMap.begin();
tm_itr != modeMap.end();
++tm_itr)
{
GLenum mode = tm_itr->first;
if (tm_itr->second.last_applied_value)
{
if (mode>=GL_TEXTURE_GEN_S && mode<=GL_TEXTURE_GEN_Q)
{
enableTexGen = true;
texgen_multiplier[mode-GL_TEXTURE_GEN_S] = 1.0f;
}
else
{
textureMode = mode;
}
OSG_NOTICE<<" enabled mode="<<std::hex<<mode<<std::dec<<std::endl;
}
else
{
OSG_NOTICE<<" disabled mode="<<std::hex<<mode<<std::dec<<std::endl;
}
}
std::stringstream sstream;
if (textureMode)
{
shaderDefineStr += _textureModeDefineMapList[i][textureMode];
if (enableTexGen)
{
// Need to get the TexGen mode.
OSG_NOTICE<<" enabled TexGen "<<texgen_multiplier<<std::endl;
}
}
}
for(unsigned int i=0; i<shaderPragmas.textureModes.size(); ++i)
{
if (i<_textureModeMapList.size())
@ -1860,7 +1935,7 @@ void State::getDefineString(std::string& shaderDefineStr, const osg::ShaderPragm
convertVertexShaderSourceToOsgBuiltIns(shaderDefineStr);
}
// OSG_NOTICE<<"State::getDefineString(..) "<<shaderDefineStr<<std::endl;
OSG_NOTICE<<"State::getDefineString(..) "<<shaderDefineStr<<std::endl;
}
bool State::supportsShaderRequirements(const osg::ShaderPragmas& shaderPragmas)

View File

@ -59,6 +59,7 @@ void RenderLeaf::render(osg::RenderInfo& renderInfo,RenderLeaf* previous)
// modelview and projection matrices then apply them now.
if (state.getUseModelViewAndProjectionUniforms()) state.applyModelViewAndProjectionUniformsIfRequired();
// draw the drawable
_drawable->draw(renderInfo);
}