Added support for subsititing $VAR_NAME entries in shaders to enable writing shaders that work across GLSL versions.

This commit is contained in:
Robert Osfield
2017-09-18 18:09:15 +01:00
parent 5ade852172
commit cc7cf54353
5 changed files with 179 additions and 21 deletions

View File

@@ -1216,6 +1216,68 @@ namespace State_Utils
source.insert(declPos, qualifier + declarationPrefix + newStr + std::string(";\n"));
}
}
void replaceVar(const osg::State& state, std::string& str, std::string::size_type start_pos, std::string::size_type num_chars)
{
std::string var_str(str.substr(start_pos+1, num_chars-1));
std::string value;
OSG_NOTICE<<" Need to replace : ["<<var_str<<"] ds="<<state.getDisplaySettings()<< std::endl;
if (state.getActiveDisplaySettings()->getValue(var_str, value))
{
OSG_NOTICE<<" Value : "<<value<<std::endl;
str.replace(start_pos, num_chars, value);
}
else
{
OSG_NOTICE<<" No value assignd, ereasing "<<std::endl;
str.erase(start_pos, num_chars);
}
}
void substitudeEnvVars(const osg::State& state, std::string& str)
{
OSG_NOTICE<<"substitudeEnvVars()"<<std::endl;
std::string::size_type pos = 0;
while (pos<str.size() && ((pos=str.find_first_of("$'\"", pos)) != std::string::npos))
{
if (pos==str.size())
{
OSG_NOTICE<<" Found "<<str[pos]<<" but it's the last character"<<std::endl;
break;
}
if (str[pos]=='"' || str[pos]=='\'')
{
std::string::size_type start_quote = pos;
++pos;
pos = str.find(str[start_quote], pos);
if (pos != std::string::npos) { OSG_NOTICE<<" Found ending string : "<<str.substr(start_quote, pos-start_quote+1)<<std::endl; ++pos; }
else { OSG_NOTICE<<" Found middle string : "<<str.substr(start_quote, std::string::npos)<<std::endl; break; }
}
else
{
std::string::size_type start_var = pos;
++pos;
pos = str.find_first_not_of("ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_", pos);
std::string var_str;
if (pos != std::string::npos)
{
OSG_NOTICE<<" Found $ : "<<str.substr(start_var, pos-start_var)<<" "<<pos<<std::endl;
replaceVar(state, str, start_var, pos-start_var);
pos = start_var;
}
else
{
OSG_NOTICE<<" Found $ at end of string : "<<str.substr(start_var, std::string::npos)<<" "<<pos<<std::endl;
replaceVar(state, str, start_var, str.size()-start_var);
pos = start_var;
}
}
}
}
}
bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
@@ -1224,6 +1286,10 @@ bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
OSG_INFO<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl;
State_Utils::substitudeEnvVars(*this, source);
std::string attributeQualifier("attribute ");
// find the first legal insertion point for replacement declarations. GLSL requires that nothing
@@ -1279,7 +1345,7 @@ bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
}
}
OSG_INFO<<"-------- Converted source "<<std::endl<<source<<std::endl<<"----------------"<<std::endl;
OSG_NOTICE<<"-------- Converted source "<<std::endl<<source<<std::endl<<"----------------"<<std::endl;
return true;
}