shaderMutator.cpp 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/shaderMutator.h"

namespace CGoGN
{

namespace Utils
{

ShaderMutator::ShaderMutator(const std:: string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc)
{
	// Store the shader name
	m_shaderName = shaderName;

	// Store the shader source codes
	m_vShaderMutation = vertShaderSrc;
	m_fShaderMutation = fragShaderSrc;
	m_gShaderMutation = geomShaderSrc;
}

bool ShaderMutator::VS_containsVariableDeclaration(const std::string& variableName)
{
	return containsVariableDeclaration(variableName, m_vShaderMutation);
}

bool ShaderMutator::FS_containsVariableDeclaration(const std::string& variableName)
{
	return containsVariableDeclaration(variableName, m_fShaderMutation);
}

bool ShaderMutator::GS_containsVariableDeclaration(const std::string& variableName)
{
	return containsVariableDeclaration(variableName, 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;
	}
}

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;
	}
}

void ShaderMutator::GS_insertCodeBeforeMainFunction(const std::string& insertedCode)
{
	if (!insertCodeBeforeMainFunction(insertedCode, m_gShaderMutation))
	{
		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;
	}
}

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;
	}
}

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;
	}
}

void ShaderMutator::GS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode)
{
	if (!insertCodeAtMainFunctionBeginning(insertedCode, m_gShaderMutation))
	{
		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;
	}
}

void ShaderMutator::VS_insertCodeAtMainFunctionEnd(const std::string& insertedCode)
{
	if (!insertCodeAtMainFunctionEnd(insertedCode, m_vShaderMutation))
	{
		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 "
153
		<< "and as many '{' as '}' in main"
154 155 156 157 158 159 160 161 162 163 164 165 166 167
		<< CGoGNendl;
	}
}

void ShaderMutator::FS_insertCodeAtMainFunctionEnd(const std::string& insertedCode)
{
	if (!insertCodeAtMainFunctionEnd(insertedCode, m_fShaderMutation))
	{
		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 "
168
		<< "and as many '{' as '}' in main"
169 170 171 172 173 174 175 176 177 178 179 180 181 182
		<< CGoGNendl;
	}
}

void ShaderMutator::GS_insertCodeAtMainFunctionEnd(const std::string& insertedCode)
{
	if (!insertCodeAtMainFunctionEnd(insertedCode, m_gShaderMutation))
	{
		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 "
183
		<< "and as many '{' as '}' in main"
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
		<< CGoGNendl;
	}
}







bool ShaderMutator::isCommented(size_t pos, const std::string& str)
{
	// Verify that the given position is not out of the string
	if (pos >= str.length())
	{
		CGoGNerr
		<< "ERROR - "
		<< "ShaderMutator::isCommented : "
		<< "Given position is out of range"
		<< CGoGNendl;
		return false;
	}
	
	// Look backward in the string to see if there is any comment symbol (// or /* */)
	
	// First look for one-line comments
	if (isOneLineCommented(pos, str))
		return true;
	
	// Now look for multi-line comments 
	size_t i;
	for (i = pos; i > 0; i--)
	{	
		if (str[i] == '/')
		{
			// End of multi-line comment
			if (str[i-1] == '*')
			{
				// Verify that the end of multi-line comment is not one-line commented !
				if (!isOneLineCommented(i, str))
					return false;
			}
		}
		else if (str[i] == '*')
		{
			// Beginning of multi-line comment
			if (str[i-1] == '/')
			{
				// Verify that the beginning of multi-line comment is not one-line commented !
				if (!isOneLineCommented(i, str))
					return true;
			}
		}
	}
	
	// No one-line or multi-line comments were found
	return false;
	
}

bool ShaderMutator::isOneLineCommented(size_t pos, const std::string& str)
{
	// Verify that the given position is not out of the string
	if (pos >= str.length())
	{
		CGoGNerr
		<< "ERROR - "
		<< "ShaderMutator::isOneLineCommented : "
		<< "Given position is out of range"
		<< CGoGNendl;
		return false;
	}
	
	// Look backward in the string 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')
			return false;
		else if (str[i] == '/')
			if (str[i-1] == '/')
				return true;
	}
	
	// No one-line comments were found
	return false;
}

bool ShaderMutator::containsVariableDeclaration(const std::string& variableName, std::string& src)
{
	// Regular expression for variable declaration
	// <',' OR white-space[1 or more times]> <variableName> <',' OR ';' OR white-space>
	boost::regex var_re("(,|\\s+)" + variableName + "(,|;|\\s)");
	
	// Matches results
	boost::match_results <std::string::iterator> matches;
	
	// Search for the first expression that matches and isn't commented
	std::string::iterator start = src.begin();
	std::string::iterator end = src.end();
	while (regex_search(start, end, matches, var_re, boost::format_first_only))
	{
		// Start position of the match
		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))
			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::insertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc)
{
	// Regular expression for main function
	// <void> <white-space>[1 or more times] <main> <white-space>[0 or more times] <'('>
	boost::regex main_re("(void)\\s+(main)\\s*\\(");
	
	// Matches results
	boost::match_results <std::string::iterator> matches;
	
	// 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, main_re, boost::format_first_only))
	{
		// Start position of the match
		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))
		{
			modifiedSrc.insert(startPosition, insertedCode);
			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::insertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc)
{
	// Regular expression for main function
	// <void> <white-space>[1 or more times] <main> <white-space>[0 or more times]
	// <'('> <white-space>[0 or more times] <')'>
	// <white-space>[0 or more times] <'{'>
	boost::regex main_re("(void)\\s+(main)\\s*\\(\\s*\\)\\s*\\{");
	
	// Matches results
	boost::match_results <std::string::iterator> matches;
	
	// 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, main_re, boost::format_first_only))
	{
		// Start position of the match
		size_t startPosition = std::distance(modifiedSrc.begin(), matches[0].first);
		
		// End position of the match
		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))
		{
			modifiedSrc.insert(endPosition, insertedCode);
			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::insertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc)
{
	// Regular expression for main function
	// <void> <white-space>[1 or more times] <main> <white-space>[0 or more times]
	// <'('> <white-space>[0 or more times] <')'>
	// <white-space>[0 or more times] <'{'>
	boost::regex main_re("(void)\\s+(main)\\s*\\(\\s*\\)\\s*\\{");
	
	// Matches results
	boost::match_results <std::string::iterator> matches;
	
	// 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
	while (regex_search(start, end, matches, main_re, boost::format_first_only) && (mainFirstBracePos == 0))
	{
		// Start position of the match
		size_t startPosition = std::distance(modifiedSrc.begin(), matches[0].first);
		
		// End position of the match
		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))
			mainFirstBracePos = endPosition;
		// Else continue to search for it after last match
		else
			start = matches[0].second;
	}
	
	// If mainFirstBracePos is still zero, no correct match was found
	if (mainFirstBracePos == 0)
		return false;
	
	// Else, it is now possible to count the opening and closing braces till the final closing brace of the main function is reached
	int bracesCounter = 1;  // =1 since the first opening brace is counted in, it will be =0 when the final closing brace is reached
	size_t closestBracePos = mainFirstBracePos;
	size_t closestOpeningBracePos;
	size_t closestClosingBracePos;
	while (bracesCounter != 0)
	{
		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))
			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))
			closestClosingBracePos = modifiedSrc.find_first_of('}', closestClosingBracePos + 1);

		// Happens if there is not enough "}" for the corresponding "{"
		if (closestClosingBracePos == std::string::npos)
			return false;

		// Refresh the closest brace position, and increment or decrement the counter
		if (closestClosingBracePos < closestOpeningBracePos)
		{
			closestBracePos = closestClosingBracePos;
			bracesCounter -= 1;
		}
		else
		{
			closestBracePos = closestOpeningBracePos;
			bracesCounter += 1;
		}
	}

	// We should now have the final '}' of the main function
	size_t mainLastBracePos = closestBracePos;
	
	// Insert the source there
	modifiedSrc.insert(mainLastBracePos, insertedCode);
	
	return true;
}

} // namespace Utils

} // namespace CGoGN