GLSL 1.3 support for vertex-attrib aliasing

This commit is contained in:
Anish Thomas 2016-08-10 14:12:27 +01:00 committed by Robert Osfield
parent e6d4d99edb
commit 2b9dfab1fc

View File

@ -884,9 +884,9 @@ void State::resetVertexAttributeAlias(bool compactAliasing, unsigned int numText
if (compactAliasing) if (compactAliasing)
{ {
unsigned int slot = 0; unsigned int slot = 0;
setUpVertexAttribAlias(_vertexAlias, slot++, "gl_Vertex","osg_Vertex","attribute vec4 "); setUpVertexAttribAlias(_vertexAlias, slot++, "gl_Vertex","osg_Vertex","vec4 ");
setUpVertexAttribAlias(_normalAlias, slot++, "gl_Normal","osg_Normal","attribute vec3 "); setUpVertexAttribAlias(_normalAlias, slot++, "gl_Normal","osg_Normal","vec3 ");
setUpVertexAttribAlias(_colorAlias, slot++, "gl_Color","osg_Color","attribute vec4 "); setUpVertexAttribAlias(_colorAlias, slot++, "gl_Color","osg_Color","vec4 ");
_texCoordAliasList.resize(numTextureUnits); _texCoordAliasList.resize(numTextureUnits);
for(unsigned int i=0; i<_texCoordAliasList.size(); i++) for(unsigned int i=0; i<_texCoordAliasList.size(); i++)
@ -896,20 +896,20 @@ void State::resetVertexAttributeAlias(bool compactAliasing, unsigned int numText
gl_MultiTexCoord<<"gl_MultiTexCoord"<<i; gl_MultiTexCoord<<"gl_MultiTexCoord"<<i;
osg_MultiTexCoord<<"osg_MultiTexCoord"<<i; osg_MultiTexCoord<<"osg_MultiTexCoord"<<i;
setUpVertexAttribAlias(_texCoordAliasList[i], slot++, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "attribute vec4 "); setUpVertexAttribAlias(_texCoordAliasList[i], slot++, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "vec4 ");
} }
setUpVertexAttribAlias(_secondaryColorAlias, slot++, "gl_SecondaryColor","osg_SecondaryColor","attribute vec4 "); setUpVertexAttribAlias(_secondaryColorAlias, slot++, "gl_SecondaryColor","osg_SecondaryColor","vec4 ");
setUpVertexAttribAlias(_fogCoordAlias, slot++, "gl_FogCoord","osg_FogCoord","attribute float "); setUpVertexAttribAlias(_fogCoordAlias, slot++, "gl_FogCoord","osg_FogCoord","float ");
} }
else else
{ {
setUpVertexAttribAlias(_vertexAlias,0, "gl_Vertex","osg_Vertex","attribute vec4 "); setUpVertexAttribAlias(_vertexAlias,0, "gl_Vertex","osg_Vertex","vec4 ");
setUpVertexAttribAlias(_normalAlias, 2, "gl_Normal","osg_Normal","attribute vec3 "); setUpVertexAttribAlias(_normalAlias, 2, "gl_Normal","osg_Normal","vec3 ");
setUpVertexAttribAlias(_colorAlias, 3, "gl_Color","osg_Color","attribute vec4 "); setUpVertexAttribAlias(_colorAlias, 3, "gl_Color","osg_Color","vec4 ");
setUpVertexAttribAlias(_secondaryColorAlias, 4, "gl_SecondaryColor","osg_SecondaryColor","attribute vec4 "); setUpVertexAttribAlias(_secondaryColorAlias, 4, "gl_SecondaryColor","osg_SecondaryColor","vec4 ");
setUpVertexAttribAlias(_fogCoordAlias, 5, "gl_FogCoord","osg_FogCoord","attribute float "); setUpVertexAttribAlias(_fogCoordAlias, 5, "gl_FogCoord","osg_FogCoord","float ");
unsigned int base = 8; unsigned int base = 8;
_texCoordAliasList.resize(numTextureUnits); _texCoordAliasList.resize(numTextureUnits);
@ -920,7 +920,7 @@ void State::resetVertexAttributeAlias(bool compactAliasing, unsigned int numText
gl_MultiTexCoord<<"gl_MultiTexCoord"<<i; gl_MultiTexCoord<<"gl_MultiTexCoord"<<i;
osg_MultiTexCoord<<"osg_MultiTexCoord"<<i; osg_MultiTexCoord<<"osg_MultiTexCoord"<<i;
setUpVertexAttribAlias(_texCoordAliasList[i], base+i, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "attribute vec4 "); setUpVertexAttribAlias(_texCoordAliasList[i], base+i, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "vec4 ");
} }
} }
} }
@ -1442,11 +1442,11 @@ namespace State_Utils
return replacedStr; return replacedStr;
} }
void replaceAndInsertDeclaration(std::string& source, std::string::size_type declPos, const std::string& originalStr, const std::string& newStr, const std::string& declarationPrefix) void replaceAndInsertDeclaration(std::string& source, std::string::size_type declPos, const std::string& originalStr, const std::string& newStr, const std::string& qualifier, const std::string& declarationPrefix)
{ {
if (replace(source, originalStr, newStr)) if (replace(source, originalStr, newStr))
{ {
source.insert(declPos, declarationPrefix + newStr + std::string(";\n")); source.insert(declPos, qualifier + declarationPrefix + newStr + std::string(";\n"));
} }
} }
} }
@ -1457,11 +1457,19 @@ bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
OSG_INFO<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl; OSG_INFO<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl;
std::string attributeQualifier("attribute ");
// find the first legal insertion point for replacement declarations. GLSL requires that nothing // find the first legal insertion point for replacement declarations. GLSL requires that nothing
// precede a "#verson" compiler directive, so we must insert new declarations after it. // precede a "#verson" compiler directive, so we must insert new declarations after it.
std::string::size_type declPos = source.rfind( "#version " ); std::string::size_type declPos = source.rfind( "#version " );
if ( declPos != std::string::npos ) if ( declPos != std::string::npos )
{ {
declPos = source.find(" ", declPos); // move to the first space after "#version"
declPos = source.find_first_not_of(std::string(" "), declPos); // skip all the spaces until you reach the version number
std::string versionNumber(source, declPos, 3);
int glslVersion = atoi(versionNumber.c_str());
OSG_INFO<<"shader version found: "<< glslVersion <<std::endl;
if (glslVersion >= 130) attributeQualifier = "in ";
// found the string, now find the next linefeed and set the insertion point after it. // found the string, now find the next linefeed and set the insertion point after it.
declPos = source.find( '\n', declPos ); declPos = source.find( '\n', declPos );
declPos = declPos != std::string::npos ? declPos+1 : source.length(); declPos = declPos != std::string::npos ? declPos+1 : source.length();
@ -1477,23 +1485,23 @@ bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
State_Utils::replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex"); State_Utils::replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex");
// replace built in uniform // replace built in uniform
State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_ModelViewMatrix", "osg_ModelViewMatrix", "uniform mat4 "); State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_ModelViewMatrix", "osg_ModelViewMatrix", "uniform ", "mat4 ");
State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix", "uniform mat4 "); State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix", "uniform ", "mat4 ");
State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_ProjectionMatrix", "osg_ProjectionMatrix", "uniform mat4 "); State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_ProjectionMatrix", "osg_ProjectionMatrix", "uniform ", "mat4 ");
State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_NormalMatrix", "osg_NormalMatrix", "uniform mat3 "); State_Utils::replaceAndInsertDeclaration(source, declPos, "gl_NormalMatrix", "osg_NormalMatrix", "uniform ", "mat3 ");
} }
if (_useVertexAttributeAliasing) if (_useVertexAttributeAliasing)
{ {
State_Utils::replaceAndInsertDeclaration(source, declPos, _vertexAlias._glName, _vertexAlias._osgName, _vertexAlias._declaration); State_Utils::replaceAndInsertDeclaration(source, declPos, _vertexAlias._glName, _vertexAlias._osgName, attributeQualifier, _vertexAlias._declaration);
State_Utils::replaceAndInsertDeclaration(source, declPos, _normalAlias._glName, _normalAlias._osgName, _normalAlias._declaration); State_Utils::replaceAndInsertDeclaration(source, declPos, _normalAlias._glName, _normalAlias._osgName, attributeQualifier, _normalAlias._declaration);
State_Utils::replaceAndInsertDeclaration(source, declPos, _colorAlias._glName, _colorAlias._osgName, _colorAlias._declaration); State_Utils::replaceAndInsertDeclaration(source, declPos, _colorAlias._glName, _colorAlias._osgName, attributeQualifier, _colorAlias._declaration);
State_Utils::replaceAndInsertDeclaration(source, declPos, _secondaryColorAlias._glName, _secondaryColorAlias._osgName, _secondaryColorAlias._declaration); State_Utils::replaceAndInsertDeclaration(source, declPos, _secondaryColorAlias._glName, _secondaryColorAlias._osgName, attributeQualifier, _secondaryColorAlias._declaration);
State_Utils::replaceAndInsertDeclaration(source, declPos, _fogCoordAlias._glName, _fogCoordAlias._osgName, _fogCoordAlias._declaration); State_Utils::replaceAndInsertDeclaration(source, declPos, _fogCoordAlias._glName, _fogCoordAlias._osgName, attributeQualifier, _fogCoordAlias._declaration);
for (size_t i=0; i<_texCoordAliasList.size(); i++) for (size_t i=0; i<_texCoordAliasList.size(); i++)
{ {
const VertexAttribAlias& texCoordAlias = _texCoordAliasList[i]; const VertexAttribAlias& texCoordAlias = _texCoordAliasList[i];
State_Utils::replaceAndInsertDeclaration(source, declPos, texCoordAlias._glName, texCoordAlias._osgName, texCoordAlias._declaration); State_Utils::replaceAndInsertDeclaration(source, declPos, texCoordAlias._glName, texCoordAlias._osgName, attributeQualifier, texCoordAlias._declaration);
} }
} }
@ -1725,7 +1733,6 @@ void State::print(std::ostream& fout) const
#if 0 #if 0
GraphicsContext* _graphicsContext; GraphicsContext* _graphicsContext;
unsigned int _contextID; unsigned int _contextID;
bool _shaderCompositionEnabled; bool _shaderCompositionEnabled;
bool _shaderCompositionDirty; bool _shaderCompositionDirty;
osg::ref_ptr<ShaderComposer> _shaderComposer; osg::ref_ptr<ShaderComposer> _shaderComposer;