From fde3961a1b5e3266384e56893741dea74d891dca Mon Sep 17 00:00:00 2001 From: Maire Nicolas Date: Tue, 28 Jun 2011 16:53:23 +0200 Subject: [PATCH] =?UTF-8?q?Rajout=20d'une=20m=C3=A9thode=20dans=20ShaderMu?= =?UTF-8?q?tator=20pour=20changer=20la=20valeur=20d'une=20constante=20de?= =?UTF-8?q?=20type=20int=20+=20gros=20cleanup.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apps/Tuto/stage_shader_number_two.ui | 18 +- include/Utils/clippingShader.h | 3 - include/Utils/shaderMutator.h | 116 +++---- src/Utils/clippingShader.cpp | 30 +- src/Utils/qtgl.cpp | 4 - src/Utils/shaderMutator.cpp | 456 ++++++++++++++++++--------- 6 files changed, 365 insertions(+), 262 deletions(-) diff --git a/Apps/Tuto/stage_shader_number_two.ui b/Apps/Tuto/stage_shader_number_two.ui index 4b3da80d..7a973611 100644 --- a/Apps/Tuto/stage_shader_number_two.ui +++ b/Apps/Tuto/stage_shader_number_two.ui @@ -24,7 +24,7 @@ - 1 + 0 @@ -269,22 +269,6 @@ - - check_drawFaces - toggled(bool) - combo_faceLighting - setVisible(bool) - - - 65 - 98 - - - 65 - 127 - - - check_drawNormals toggled(bool) diff --git a/include/Utils/clippingShader.h b/include/Utils/clippingShader.h index 82064400..b8c9ae2f 100644 --- a/include/Utils/clippingShader.h +++ b/include/Utils/clippingShader.h @@ -27,9 +27,6 @@ #include "Utils/GLSLShader.h" #include "Geometry/vector_gen.h" -#include "glm/glm.hpp" -#include "Geometry/matrix.h" -#include "Utils/trackball.h" #include "Utils/cgognStream.h" #include "Utils/shaderMutator.h" #include "Utils/drawer.h" diff --git a/include/Utils/shaderMutator.h b/include/Utils/shaderMutator.h index 3942f7d6..b5fc692f 100644 --- a/include/Utils/shaderMutator.h +++ b/include/Utils/shaderMutator.h @@ -40,6 +40,12 @@ class ShaderMutator { public: + + /** + * enums used to choose which shader src to modify + */ + enum shaderSrcType { VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER }; + /** * constructor * @param vertShaderSrc the vertex shader source to store @@ -49,85 +55,48 @@ public: 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 vertex shader source + * check if a variable is declared in the shader source + * @param srcType the shader source to use (vertex, fragment or geometry) * @param variableName the variable to search for */ - bool VS_containsVariableDeclaration(const std::string& variableName); - - /** - * check if a variable is declared in the fragment shader source - * @param variableName the variable to search for - */ - bool FS_containsVariableDeclaration(const std::string& variableName); + bool containsVariableDeclaration(shaderSrcType srcType, const std::string& variableName); /** - * check if a variable is declared in the geometry shader source - * @param variableName the variable to search for - */ - bool GS_containsVariableDeclaration(const std::string& variableName); - - /** - * set or change shading language version if the current version is lower + * set or change shading language version in the shader source + * - only if the current version is lower + * @param srcType the shader source to use (vertex, fragment or geometry) * @param version the version to set (110, 120, 150...) */ - void VS_FS_GS_setMinShadingLanguageVersion(int version); + void setMinShadingLanguageVersion(shaderSrcType srcType, int version); /** - * insert code before main function into shader vertex source code - * @param insertedCode source code to insert into shader - */ - void VS_insertCodeBeforeMainFunction(const std::string& insertedCode); - - /** - * insert code before main function into shader fragment source code - * @param insertedCode source code to insert into shader - */ - void FS_insertCodeBeforeMainFunction(const std::string& insertedCode); - - /** - * insert code before main function into shader geometry source code - * @param insertedCode source code to insert into shader + * change int constant value in the shader source + * @param srcType the shader source to use (vertex, fragment or geometry) + * @param newVal the new value */ - void GS_insertCodeBeforeMainFunction(const std::string& insertedCode); - - /** - * insert code at the beginning of main function into shader vertex source code - * @param insertedCode source code to insert into shader - */ - void VS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode); - - /** - * insert code at the beginning of main function into shader fragment source code - * @param insertedCode source code to insert into shader - */ - void FS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode); - - /** - * insert code at the beginning of main function into shader geometry source code - * @param insertedCode source code to insert into shader - */ - void GS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode); - + void changeIntConstantValue(shaderSrcType srcType, int newVal); + /** - * insert code at the beginning of main function into shader vertex source code - * @warning takes the number of opening and closing braces of main function into account + * insert code before main function into shader source + * @param srcType the shader source to use (vertex, fragment or geometry) * @param insertedCode source code to insert into shader */ - void VS_insertCodeAtMainFunctionEnd(const std::string& insertedCode); + void insertCodeBeforeMainFunction(shaderSrcType srcType, const std::string& insertedCode); /** - * insert code at the beginning of main function into shader fragment source code - * @warning takes the number of opening and closing braces of main function into account + * insert code at the beginning of main function into shader source + * @param srcType the shader source to use (vertex, fragment or geometry) * @param insertedCode source code to insert into shader */ - void FS_insertCodeAtMainFunctionEnd(const std::string& insertedCode); + void insertCodeAtMainFunctionBeginning(shaderSrcType srcType, const std::string& insertedCode); /** - * insert code at the beginning of main function into shader geometry source code + * insert code at the end of main function into shader source * @warning takes the number of opening and closing braces of main function into account + * @param srcType the shader source to use (vertex, fragment or geometry) * @param insertedCode source code to insert into shader */ - void GS_insertCodeAtMainFunctionEnd(const std::string& insertedCode); + void insertCodeAtMainFunctionEnd(shaderSrcType srcType, const std::string& insertedCode); /** * returns the modified vertex shader source code @@ -167,52 +136,59 @@ private: std::string m_gShaderMutation; /** - * verify if the given position in the string is commented or not + * check if the given position in the source is commented * @param pos the position - * @param str the string to analyze + * @param src the source to analyze */ - bool isCommented(size_t pos, const std::string& str); + bool srcIsCommented(size_t pos, const std::string& src); /** - * verify if the given position in the string is commented with a one-line comment or not + * check if the given position in the source is commented with a one-line comment * @param pos the position - * @param str the string to analyze + * @param src the source to analyze */ - bool isOneLineCommented(size_t pos, const std::string& str); + bool srcIsOneLineCommented(size_t pos, const std::string& src); /** - * check if a variable is declared in a source code or not + * check if a variable is declared in a source code * @param variable the variable to search for */ - bool containsVariableDeclaration(const std::string& variableName, std::string& src); + bool srcContainsVariableDeclaration(const std::string& variableName, std::string& src); /** * set or change shading language version if the current version is lower * @param version the version to set (110, 120, 150...) * @param modifiedSrc shader source code to modify */ - bool setMinShadingLanguageVersion(int version, std::string& modifiedSrc); + bool srcSetMinShadingLanguageVersion(int version, std::string& modifiedSrc); + + /** + * change int constant value + * @param newVal the new value + * @param modifiedSrc shader source code to modify + */ + bool srcChangeIntConstantValue(int newVal, std::string& modifiedSrc); /** * insert code before main function into source code * @param insertedCode source code to insert into shader * @param modifiedSrc shader source code to modify */ - bool insertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc); + bool srcInsertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc); /** * insert code at the beginning of main function into source code * @param insertedCode source code to insert into shader * @param modifiedSrc shader source code to modify */ - bool insertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc); + bool srcInsertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc); /** * insert code at the end of main function into source code * @param insertedCode source code to insert into shader * @param modifiedSrc shader source code to modify */ - bool insertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc); + bool srcInsertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc); }; diff --git a/src/Utils/clippingShader.cpp b/src/Utils/clippingShader.cpp index 50f5e9ba..080e382a 100644 --- a/src/Utils/clippingShader.cpp +++ b/src/Utils/clippingShader.cpp @@ -261,7 +261,8 @@ void ClippingShader::setPlaneClipping(int planesCount) " gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n"; - // If the previous plane 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) if (getClippingPlanesCount() == 0) { originalVertShaderSrc = getVertexShaderSrc(); @@ -276,7 +277,7 @@ void ClippingShader::setPlaneClipping(int planesCount) ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc, ""); // First check if the vertex shader contains the VertexPosition attribute - if (!SM.VS_containsVariableDeclaration("VertexPosition")) + if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition")) { CGoGNerr << "ERROR - " @@ -289,16 +290,15 @@ void ClippingShader::setPlaneClipping(int planesCount) } // Modify vertex shader source code - SM.VS_insertCodeBeforeMainFunction(VS_head_insertion); - SM.VS_insertCodeAtMainFunctionBeginning(VS_mainBegin_insertion); - - // Following code insertions need at least shading language 120 (GLSL arrays) - SM.VS_FS_GS_setMinShadingLanguageVersion(120); + SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion); + SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainBegin_insertion); + // Modify fragment shader source code - SM.FS_insertCodeBeforeMainFunction(FS_head_insertion); - SM.FS_insertCodeAtMainFunctionEnd(FS_mainEnd_insertion); - SM.FS_insertCodeAtMainFunctionBeginning(FS_mainBegin_insertion); + 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()); @@ -356,11 +356,6 @@ void ClippingShader::updateClippingUniforms() sendColorAttenuationFactorUniform(); } -void ClippingShader::displayClippingPlane() -{ - m_planeDrawer->callList(); -} - void ClippingShader::sendClippingPlanesUniform() { bind(); @@ -373,6 +368,11 @@ void ClippingShader::sendColorAttenuationFactorUniform() glUniform1f(m_unif_colorAttenuationFactor, m_colorAttenuationFactor); } +void ClippingShader::displayClippingPlane() +{ + m_planeDrawer->callList(); +} + } // namespace Utils } // namespace CGoGN diff --git a/src/Utils/qtgl.cpp b/src/Utils/qtgl.cpp index 33f5c34d..838f33e0 100644 --- a/src/Utils/qtgl.cpp +++ b/src/Utils/qtgl.cpp @@ -326,8 +326,6 @@ void GLWidget::keyPressEvent(QKeyEvent* event) m_state_modifier = event->modifiers(); - std::cout << "Press : " << m_state_modifier << std::endl; - int k = event->key(); if ( (k >= 65) && (k <= 91) && !(event->modifiers() & Qt::ShiftModifier) ) k += 32; @@ -343,8 +341,6 @@ void GLWidget::keyReleaseEvent(QKeyEvent *event) m_state_modifier = event->modifiers(); int k = event->key(); - std::cout << "Up : " << m_state_modifier << std::endl; - // align on axis if ((k == 'Z') && (event->modifiers() & Qt::ShiftModifier)) { diff --git a/src/Utils/shaderMutator.cpp b/src/Utils/shaderMutator.cpp index a0c10a38..acfffeb2 100644 --- a/src/Utils/shaderMutator.cpp +++ b/src/Utils/shaderMutator.cpp @@ -30,6 +30,12 @@ namespace CGoGN namespace Utils { +/*********************************************** + * + * Public Section + * + ***********************************************/ + ShaderMutator::ShaderMutator(const std:: string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc) { // Store the shader name @@ -41,167 +47,265 @@ ShaderMutator::ShaderMutator(const std:: string& shaderName, const std::string& m_gShaderMutation = geomShaderSrc; } -bool ShaderMutator::VS_containsVariableDeclaration(const std::string& variableName) +bool ShaderMutator::containsVariableDeclaration(shaderSrcType srcType, const std::string& variableName) { - return containsVariableDeclaration(variableName, m_vShaderMutation); -} + bool result = false; -bool ShaderMutator::FS_containsVariableDeclaration(const std::string& variableName) -{ - return containsVariableDeclaration(variableName, m_fShaderMutation); -} + switch (srcType) + { + case VERTEX_SHADER : + result = srcContainsVariableDeclaration(variableName, m_vShaderMutation); + break; -bool ShaderMutator::GS_containsVariableDeclaration(const std::string& variableName) -{ - return containsVariableDeclaration(variableName, m_gShaderMutation); -} + case FRAGMENT_SHADER : + result = srcContainsVariableDeclaration(variableName, m_fShaderMutation); + break; -void ShaderMutator::VS_FS_GS_setMinShadingLanguageVersion(int version) -{ - setMinShadingLanguageVersion(version, m_vShaderMutation); - setMinShadingLanguageVersion(version, m_fShaderMutation); - setMinShadingLanguageVersion(version, m_gShaderMutation); -} - -void ShaderMutator::VS_insertCodeBeforeMainFunction(const std::string& insertedCode) -{ - if (!insertCodeBeforeMainFunction(insertedCode, m_vShaderMutation)) - { - CGoGNerr - << "ERROR - " - << "ShaderMutator::VS_insertCodeBeforeMainFunction : " - << "Unable to insert source code in vertex shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration" - << CGoGNendl; + case GEOMETRY_SHADER : + result = srcContainsVariableDeclaration(variableName, m_gShaderMutation); + break; } -} -void ShaderMutator::FS_insertCodeBeforeMainFunction(const std::string& insertedCode) -{ - if (!insertCodeBeforeMainFunction(insertedCode, m_fShaderMutation)) - { - CGoGNerr - << "ERROR - " - << "ShaderMutator::FS_insertCodeBeforeMainFunction : " - << "Unable to insert source code in fragment shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration" - << CGoGNendl; - } + return result; } -void ShaderMutator::GS_insertCodeBeforeMainFunction(const std::string& insertedCode) +void ShaderMutator::setMinShadingLanguageVersion(shaderSrcType srcType, int version) { - if (!insertCodeBeforeMainFunction(insertedCode, m_gShaderMutation)) + switch (srcType) { - CGoGNerr - << "ERROR - " - << "ShaderMutator::GS_insertCodeBeforeMainFunction : " - << "Unable to insert source code in geometry shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration" - << CGoGNendl; - } -} + case VERTEX_SHADER : + srcSetMinShadingLanguageVersion(version, m_vShaderMutation); + break; -void ShaderMutator::VS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode) -{ - if (!insertCodeAtMainFunctionBeginning(insertedCode, m_vShaderMutation)) - { - CGoGNerr - << "ERROR - " - << "ShaderMutator::VS_insertCodeAtMainFunctionBeginnning : " - << "Unable to insert source code vertex shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration" - << CGoGNendl; - } -} + case FRAGMENT_SHADER : + srcSetMinShadingLanguageVersion(version, m_fShaderMutation); + break; -void ShaderMutator::FS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode) -{ - if (!insertCodeAtMainFunctionBeginning(insertedCode, m_fShaderMutation)) - { - CGoGNerr - << "ERROR - " - << "ShaderMutator::FS_insertCodeAtMainFunctionBeginnning : " - << "Unable to insert source code in fragment shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration" - << CGoGNendl; + case GEOMETRY_SHADER : + srcSetMinShadingLanguageVersion(version, m_gShaderMutation); + break; } } -void ShaderMutator::GS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode) +void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal) { - if (!insertCodeAtMainFunctionBeginning(insertedCode, m_gShaderMutation)) + switch (srcType) { - CGoGNerr - << "ERROR - " - << "ShaderMutator::GS_insertCodeAtMainFunctionBeginnning : " - << "Unable to insert source code in geometry shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration" - << CGoGNendl; + case VERTEX_SHADER : + if (!srcChangeIntConstantValue(newVal, m_vShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::changeIntConstantValue : " + << "Unable to change int constant value in vertex shader of " + << m_shaderName + << ". Constant declaration not found" + << CGoGNendl; + + return; + } + break; + + case FRAGMENT_SHADER : + if (!srcChangeIntConstantValue(newVal, m_fShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::changeIntConstantValue : " + << "Unable to change int constant value in fragment shader of " + << m_shaderName + << ". Constant declaration not found" + << CGoGNendl; + + return; + } + break; + + case GEOMETRY_SHADER : + if (!srcChangeIntConstantValue(newVal, m_gShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::changeIntConstantValue : " + << "Unable to change int constant value in geometry shader of " + << m_shaderName + << ". Constant declaration not found" + << CGoGNendl; + + return; + } + break; } } -void ShaderMutator::VS_insertCodeAtMainFunctionEnd(const std::string& insertedCode) +void ShaderMutator::insertCodeBeforeMainFunction(shaderSrcType srcType, const std::string& insertedCode) { - if (!insertCodeAtMainFunctionEnd(insertedCode, m_vShaderMutation)) + switch (srcType) { - CGoGNerr - << "ERROR - " - << "ShaderMutator::VS_insertCodeAtMainFunctionEnd : " - << "Unable to insert source code in vertex shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration " - << "and as many '{' as '}' in main" - << CGoGNendl; + case VERTEX_SHADER : + if (!srcInsertCodeBeforeMainFunction(insertedCode, m_vShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeBeforeMainFunction : " + << "Unable to insert source code in vertex shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration" + << CGoGNendl; + + return; + } + break; + + case FRAGMENT_SHADER : + if (!srcInsertCodeBeforeMainFunction(insertedCode, m_fShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeBeforeMainFunction : " + << "Unable to insert source code in fragment shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration" + << CGoGNendl; + + return; + } + break; + + case GEOMETRY_SHADER : + if (!srcInsertCodeBeforeMainFunction(insertedCode, m_gShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeBeforeMainFunction : " + << "Unable to insert source code in geometry shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration" + << CGoGNendl; + + return; + } + break; } } -void ShaderMutator::FS_insertCodeAtMainFunctionEnd(const std::string& insertedCode) +void ShaderMutator::insertCodeAtMainFunctionBeginning(shaderSrcType srcType, const std::string& insertedCode) { - if (!insertCodeAtMainFunctionEnd(insertedCode, m_fShaderMutation)) + switch (srcType) { - CGoGNerr - << "ERROR - " - << "ShaderMutator::FS_insertCodeAtMainFunctionEnd : " - << "Unable to insert source code in fragment shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration " - << "and as many '{' as '}' in main" - << CGoGNendl; + case VERTEX_SHADER : + if (!srcInsertCodeAtMainFunctionBeginning(insertedCode, m_vShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeAtMainFunctionBeginnning : " + << "Unable to insert source code in vertex shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration" + << CGoGNendl; + + return; + } + break; + + case FRAGMENT_SHADER : + if (!srcInsertCodeAtMainFunctionBeginning(insertedCode, m_fShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeAtMainFunctionBeginnning : " + << "Unable to insert source code in fragment shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration" + << CGoGNendl; + + return; + } + break; + + case GEOMETRY_SHADER : + if (!srcInsertCodeAtMainFunctionBeginning(insertedCode, m_gShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeAtMainFunctionBeginnning : " + << "Unable to insert source code in geometry shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration" + << CGoGNendl; + + return; + } + break; } } -void ShaderMutator::GS_insertCodeAtMainFunctionEnd(const std::string& insertedCode) +void ShaderMutator::insertCodeAtMainFunctionEnd(shaderSrcType srcType, const std::string& insertedCode) { - if (!insertCodeAtMainFunctionEnd(insertedCode, m_gShaderMutation)) + switch (srcType) { - CGoGNerr - << "ERROR - " - << "ShaderMutator::GS_insertCodeAtMainFunctionEnd : " - << "Unable to insert source code in geometry shader of " - << m_shaderName - << ". You should check if the shader has a main function declaration " - << "and as many '{' as '}' in main" - << CGoGNendl; - } -} - + case VERTEX_SHADER : + if (!srcInsertCodeAtMainFunctionEnd(insertedCode, m_vShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeAtMainFunctionEnd : " + << "Unable to insert source code in vertex shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration " + << "and as many '{' as '}' in main" + << CGoGNendl; + + return; + } + break; + case FRAGMENT_SHADER : + if (!srcInsertCodeAtMainFunctionEnd(insertedCode, m_fShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeAtMainFunctionEnd : " + << "Unable to insert source code in fragment shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration " + << "and as many '{' as '}' in main" + << CGoGNendl; + + return; + } + break; + case GEOMETRY_SHADER : + if (!srcInsertCodeAtMainFunctionEnd(insertedCode, m_gShaderMutation)) + { + CGoGNerr + << "ERROR - " + << "ShaderMutator::insertCodeAtMainFunctionEnd : " + << "Unable to insert source code in geometry shader of " + << m_shaderName + << ". You should check if the shader has a main function declaration " + << "and as many '{' as '}' in main" + << CGoGNendl; + + return; + } + break; + } +} +/*********************************************** + * + * Private Section + * + ***********************************************/ -bool ShaderMutator::isCommented(size_t pos, const std::string& str) +bool ShaderMutator::srcIsCommented(size_t pos, const std::string& src) { - // Verify that the given position is not out of the string - if (pos >= str.length()) + // Verify that the given position is not out of the source + if (pos >= src.length()) { CGoGNerr << "ERROR - " @@ -211,33 +315,33 @@ bool ShaderMutator::isCommented(size_t pos, const std::string& str) return false; } - // Look backward in the string to see if there is any comment symbol (// or /* */) + // Look backward in the source to see if there is any comment symbol (// or /* */) // First look for one-line comments - if (isOneLineCommented(pos, str)) + if (srcIsOneLineCommented(pos, src)) return true; // Now look for multi-line comments size_t i; for (i = pos; i > 0; i--) { - if (str[i] == '/') + if (src[i] == '/') { // End of multi-line comment - if (str[i-1] == '*') + if (src[i-1] == '*') { // Verify that the end of multi-line comment is not one-line commented ! - if (!isOneLineCommented(i, str)) + if (!srcIsOneLineCommented(i, src)) return false; } } - else if (str[i] == '*') + else if (src[i] == '*') { // Beginning of multi-line comment - if (str[i-1] == '/') + if (src[i-1] == '/') { // Verify that the beginning of multi-line comment is not one-line commented ! - if (!isOneLineCommented(i, str)) + if (!srcIsOneLineCommented(i, src)) return true; } } @@ -248,10 +352,10 @@ bool ShaderMutator::isCommented(size_t pos, const std::string& str) } -bool ShaderMutator::isOneLineCommented(size_t pos, const std::string& str) +bool ShaderMutator::srcIsOneLineCommented(size_t pos, const std::string& src) { - // Verify that the given position is not out of the string - if (pos >= str.length()) + // Verify that the given position is not out of the source + if (pos >= src.length()) { CGoGNerr << "ERROR - " @@ -261,15 +365,15 @@ bool ShaderMutator::isOneLineCommented(size_t pos, const std::string& str) return false; } - // Look backward in the string to see if there is any "//" + // Look backward in the source to see if there is any "//" size_t i; for (i = pos; i > 0; i--) { // As soon as a '\n' is found, any other "//" will not affect this line anymore - if (str[i] == '\n') + if (src[i] == '\n') return false; - else if (str[i] == '/') - if (str[i-1] == '/') + else if (src[i] == '/') + if (src[i-1] == '/') return true; } @@ -277,7 +381,7 @@ bool ShaderMutator::isOneLineCommented(size_t pos, const std::string& str) return false; } -bool ShaderMutator::containsVariableDeclaration(const std::string& variableName, std::string& src) +bool ShaderMutator::srcContainsVariableDeclaration(const std::string& variableName, std::string& src) { // Regular expression for variable declaration // <',' OR white-space[1 or more times]> <',' OR ';' OR white-space> @@ -295,7 +399,7 @@ bool ShaderMutator::containsVariableDeclaration(const std::string& variableName, size_t startPosition = std::distance(src.begin(), matches[0].first); // Finish if the matched variable is the good one (i.e. not commented) - if (!isCommented(startPosition, src)) + if (!srcIsCommented(startPosition, src)) return true; // Else continue to search for it after last match else @@ -306,11 +410,11 @@ bool ShaderMutator::containsVariableDeclaration(const std::string& variableName, return false; } -bool ShaderMutator::setMinShadingLanguageVersion(int version, std::string& modifiedSrc) +bool ShaderMutator::srcSetMinShadingLanguageVersion(int version, std::string& modifiedSrc) { // Regular expression for shading language version - // <#version> [1 or more times] - boost::regex version_re("#version (\\d+)"); + // <#version> [1 or more times] [1 or more times] + boost::regex version_re("#version\\s+(\\d+)"); // Matches results boost::match_results matches; @@ -330,9 +434,9 @@ bool ShaderMutator::setMinShadingLanguageVersion(int version, std::string& modif size_t startPosition = std::distance(modifiedSrc.begin(), matches[0].first); // Change the version number if the matched "#version ..." is the good one (i.e. not commented) - if (!isCommented(startPosition, modifiedSrc)) + if (!srcIsCommented(startPosition, modifiedSrc)) { - // Match[1] should be the version number + // The submatch Match[1] should be the version number std::string oldVersion(matches[1].first, matches[1].second); int oldVersionValue = atoi(oldVersion.c_str()); size_t oldVersionLength = oldVersion.length(); @@ -361,7 +465,53 @@ bool ShaderMutator::setMinShadingLanguageVersion(int version, std::string& modif return true; } -bool ShaderMutator::insertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc) +bool ShaderMutator::srcChangeIntConstantValue(int newVal, std::string& modifiedSrc) +{ + // Regular expression for constant expression + // <#define> [1 or more times] [1 or more times] + boost::regex const_re("#define\\s+(\\d+)"); + + // Matches results + boost::match_results matches; + + // Build the constant value string + std::string newValStr; + std::stringstream ss; + ss << newVal; + newValStr = ss.str(); + + // Search for the first expression that matches and isn't commented + std::string::iterator start = modifiedSrc.begin(); + std::string::iterator end = modifiedSrc.end(); + while (regex_search(start, end, matches, const_re, boost::format_first_only)) + { + // Start position of the match + size_t startPosition = std::distance(modifiedSrc.begin(), matches[0].first); + + // Change the constant value if the matched "#define ..." is the good one (i.e. not commented) + if (!srcIsCommented(startPosition, modifiedSrc)) + { + // The submatch Match[1] should be the old constant value + std::string oldValStr(matches[1].first, matches[1].second); + size_t oldValLength = oldValStr.length(); + size_t oldValPosition = std::distance(modifiedSrc.begin(), matches[1].first); + + // Replace the constant value + modifiedSrc.replace(oldValPosition, oldValLength, newValStr); + return true; + } + // Else continue to search for it after last match + else + { + start = matches[0].second; + } + } + + // At this point no correct match was found + return false; +} + +bool ShaderMutator::srcInsertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc) { // Regular expression for main function // [1 or more times]
[0 or more times] <'('> @@ -379,7 +529,7 @@ bool ShaderMutator::insertCodeBeforeMainFunction(const std::string& insertedCode size_t startPosition = std::distance(modifiedSrc.begin(), matches[0].first); // Insert and finish if the matched "main" is the good one (i.e. not commented) - if (!isCommented(startPosition, modifiedSrc)) + if (!srcIsCommented(startPosition, modifiedSrc)) { modifiedSrc.insert(startPosition, insertedCode); return true; @@ -395,7 +545,7 @@ bool ShaderMutator::insertCodeBeforeMainFunction(const std::string& insertedCode return false; } -bool ShaderMutator::insertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc) +bool ShaderMutator::srcInsertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc) { // Regular expression for main function // [1 or more times]
[0 or more times] @@ -418,7 +568,7 @@ bool ShaderMutator::insertCodeAtMainFunctionBeginning(const std::string& inserte size_t endPosition = std::distance(modifiedSrc.begin(), matches[0].second); // Insert and finish if the matched "main" is the good one (i.e. not commented) - if (!isCommented(startPosition, modifiedSrc)) + if (!srcIsCommented(startPosition, modifiedSrc)) { modifiedSrc.insert(endPosition, insertedCode); return true; @@ -434,7 +584,7 @@ bool ShaderMutator::insertCodeAtMainFunctionBeginning(const std::string& inserte return false; } -bool ShaderMutator::insertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc) +bool ShaderMutator::srcInsertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc) { // Regular expression for main function // [1 or more times]
[0 or more times] @@ -448,7 +598,7 @@ bool ShaderMutator::insertCodeAtMainFunctionEnd(const std::string& insertedCode, // Search for the first expression that matches and isn't commented std::string::iterator start = modifiedSrc.begin(); std::string::iterator end = modifiedSrc.end(); - size_t mainFirstBracePos = 0; // The aim is to find this position + size_t mainFirstBracePos = 0; // The aim is first to find this position while (regex_search(start, end, matches, main_re, boost::format_first_only) && (mainFirstBracePos == 0)) { // Start position of the match @@ -458,7 +608,7 @@ bool ShaderMutator::insertCodeAtMainFunctionEnd(const std::string& insertedCode, size_t endPosition = std::distance(modifiedSrc.begin(), matches[0].second); // Get the main first brace position if the matched "main" is the good one (i.e. not commented) - if (!isCommented(startPosition, modifiedSrc)) + if (!srcIsCommented(startPosition, modifiedSrc)) mainFirstBracePos = endPosition; // Else continue to search for it after last match else @@ -478,12 +628,12 @@ bool ShaderMutator::insertCodeAtMainFunctionEnd(const std::string& insertedCode, { closestOpeningBracePos = modifiedSrc.find_first_of('{', closestBracePos + 1); // If this brace appears to be commented, try to get the next one - while ((closestOpeningBracePos != std::string::npos) && isCommented(closestOpeningBracePos, modifiedSrc)) + while ((closestOpeningBracePos != std::string::npos) && srcIsCommented(closestOpeningBracePos, modifiedSrc)) closestOpeningBracePos = modifiedSrc.find_first_of('{', closestOpeningBracePos + 1); closestClosingBracePos = modifiedSrc.find_first_of('}', closestBracePos + 1); // If this brace appears to be commented, try to get the next one - while ((closestClosingBracePos != std::string::npos) && isCommented(closestClosingBracePos, modifiedSrc)) + while ((closestClosingBracePos != std::string::npos) && srcIsCommented(closestClosingBracePos, modifiedSrc)) closestClosingBracePos = modifiedSrc.find_first_of('}', closestClosingBracePos + 1); // Happens if there is not enough "}" for the corresponding "{" -- GitLab