Commit 7d0e253b authored by Maire Nicolas's avatar Maire Nicolas

Changement du nombre de plans optimisé dans ClippingShader : changer la...

Changement du nombre de plans optimisé dans ClippingShader : changer la constante dans le shader au lieu de tout réinserer.
parent fde3961a
...@@ -52,7 +52,7 @@ public: ...@@ -52,7 +52,7 @@ public:
* @param fragShaderSrc the fragment shader source to store * @param fragShaderSrc the fragment shader source to store
* @param geomShaderSrc the geometry shader source to store * @param geomShaderSrc the geometry shader source to store
*/ */
ShaderMutator(const std::string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc); ShaderMutator(const std::string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc = "");
/** /**
* check if a variable is declared in the shader source * check if a variable is declared in the shader source
...@@ -74,7 +74,7 @@ public: ...@@ -74,7 +74,7 @@ public:
* @param srcType the shader source to use (vertex, fragment or geometry) * @param srcType the shader source to use (vertex, fragment or geometry)
* @param newVal the new value * @param newVal the new value
*/ */
void changeIntConstantValue(shaderSrcType srcType, int newVal); void changeIntConstantValue(shaderSrcType srcType, const std::string& constantName, int newVal);
/** /**
* insert code before main function into shader source * insert code before main function into shader source
...@@ -167,7 +167,7 @@ private: ...@@ -167,7 +167,7 @@ private:
* @param newVal the new value * @param newVal the new value
* @param modifiedSrc shader source code to modify * @param modifiedSrc shader source code to modify
*/ */
bool srcChangeIntConstantValue(int newVal, std::string& modifiedSrc); bool srcChangeIntConstantValue(int newVal, const std::string& constantName, std::string& modifiedSrc);
/** /**
* insert code before main function into source code * insert code before main function into source code
......
...@@ -260,49 +260,67 @@ void ClippingShader::setPlaneClipping(int planesCount) ...@@ -260,49 +260,67 @@ void ClippingShader::setPlaneClipping(int planesCount)
" if (clip_MinDistanceToPlanes > 0.0)\n" " if (clip_MinDistanceToPlanes > 0.0)\n"
" gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n"; " gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n";
// Previous planes count
int previousPlanesCount = getClippingPlanesCount();
// If the previous planes count was zero, the previous shader source codes were the original ones. Store them // If the previous planes count was zero, the previous shader source codes were the original ones. Store them
// (the planes count is initially zero when the object is constructed) // (the planes count is initially zero when the object is constructed)
if (getClippingPlanesCount() == 0) if (previousPlanesCount == 0)
{ {
originalVertShaderSrc = getVertexShaderSrc(); originalVertShaderSrc = getVertexShaderSrc();
originalFragShaderSrc = getFragmentShaderSrc(); originalFragShaderSrc = getFragmentShaderSrc();
} }
// If the given plane count is > 0, modify the original shader sources // If the given plane count is > 0, modify the shader sources
if (planesCount > 0) if (planesCount > 0)
{ {
// Use a shader mutator // If the previous planes count wasn't zero, there is only one constant to change in the fragment shader
ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc, ""); if (previousPlanesCount != 0)
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition"))
{ {
CGoGNerr // Use a shader mutator
<< "ERROR - " ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());
<< "ClippingShader::setPlaneClipping"
<< " - Could not process shader "
<< m_nameVS
<< " source code : no VertexPosition attribute found"
<< CGoGNendl;
return;
}
// Modify vertex shader source code
SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainBegin_insertion);
// Modify fragment shader source code // Modify the constant in the fragment shader
SM.setMinShadingLanguageVersion(ShaderMutator::FRAGMENT_SHADER, 120); // Following code insertions need at least shading language 120 (GLSL arrays) SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", planesCount);
SM.insertCodeBeforeMainFunction(ShaderMutator::FRAGMENT_SHADER, FS_head_insertion);
SM.insertCodeAtMainFunctionEnd(ShaderMutator::FRAGMENT_SHADER, FS_mainEnd_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::FRAGMENT_SHADER, FS_mainBegin_insertion);
// Reload both shaders // Reload fragment shader
reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str()); reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str()); }
// Else the whole clipping code must be inserted in the original shader sources
else
{
// Use a shader mutator
ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc);
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition"))
{
CGoGNerr
<< "ERROR - "
<< "ClippingShader::setPlaneClipping"
<< " - Could not process shader "
<< m_nameVS
<< " source code : no VertexPosition attribute found"
<< CGoGNendl;
return;
}
// Modify vertex shader source code
SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainBegin_insertion);
// Modify fragment shader source code
SM.setMinShadingLanguageVersion(ShaderMutator::FRAGMENT_SHADER, 120); // Following code insertions need at least shading language 120 (GLSL arrays)
SM.insertCodeBeforeMainFunction(ShaderMutator::FRAGMENT_SHADER, FS_head_insertion);
SM.insertCodeAtMainFunctionEnd(ShaderMutator::FRAGMENT_SHADER, FS_mainEnd_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::FRAGMENT_SHADER, FS_mainBegin_insertion);
// Reload both shaders
reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
}
} }
// Else no clipping is wanted anymore, so get back the original shader sources // Else no clipping is wanted anymore, so get back the original shader sources
......
...@@ -87,12 +87,12 @@ void ShaderMutator::setMinShadingLanguageVersion(shaderSrcType srcType, int vers ...@@ -87,12 +87,12 @@ void ShaderMutator::setMinShadingLanguageVersion(shaderSrcType srcType, int vers
} }
} }
void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal) void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, const std::string& constantName, int newVal)
{ {
switch (srcType) switch (srcType)
{ {
case VERTEX_SHADER : case VERTEX_SHADER :
if (!srcChangeIntConstantValue(newVal, m_vShaderMutation)) if (!srcChangeIntConstantValue(newVal, constantName, m_vShaderMutation))
{ {
CGoGNerr CGoGNerr
<< "ERROR - " << "ERROR - "
...@@ -107,7 +107,7 @@ void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal) ...@@ -107,7 +107,7 @@ void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal)
break; break;
case FRAGMENT_SHADER : case FRAGMENT_SHADER :
if (!srcChangeIntConstantValue(newVal, m_fShaderMutation)) if (!srcChangeIntConstantValue(newVal, constantName, m_fShaderMutation))
{ {
CGoGNerr CGoGNerr
<< "ERROR - " << "ERROR - "
...@@ -122,7 +122,7 @@ void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal) ...@@ -122,7 +122,7 @@ void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal)
break; break;
case GEOMETRY_SHADER : case GEOMETRY_SHADER :
if (!srcChangeIntConstantValue(newVal, m_gShaderMutation)) if (!srcChangeIntConstantValue(newVal, constantName, m_gShaderMutation))
{ {
CGoGNerr CGoGNerr
<< "ERROR - " << "ERROR - "
...@@ -465,11 +465,11 @@ bool ShaderMutator::srcSetMinShadingLanguageVersion(int version, std::string& mo ...@@ -465,11 +465,11 @@ bool ShaderMutator::srcSetMinShadingLanguageVersion(int version, std::string& mo
return true; return true;
} }
bool ShaderMutator::srcChangeIntConstantValue(int newVal, std::string& modifiedSrc) bool ShaderMutator::srcChangeIntConstantValue(int newVal, const std::string& constantName, std::string& modifiedSrc)
{ {
// Regular expression for constant expression // Regular expression for constant expression
// <#define> <white-space>[1 or more times] <digit>[1 or more times] // <#define> <white-space>[1 or more times] <constant name> <white-space>[1 or more times] <digit>[1 or more times]
boost::regex const_re("#define\\s+(\\d+)"); boost::regex const_re("#define\\s+" + constantName + "\\s+(\\d+)");
// Matches results // Matches results
boost::match_results <std::string::iterator> matches; boost::match_results <std::string::iterator> matches;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment