shaderMutator.h 6.96 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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#ifndef _CGoGN_SHADERMUTATOR_H_
#define _CGoGN_SHADERMUTATOR_H_

#include "Utils/cgognStream.h"
#include <boost/regex.hpp>
#include <string>
31
#include <sstream>
32 33 34 35 36 37 38 39 40 41 42

namespace CGoGN
{

namespace Utils
{

class ShaderMutator
{
	
public:
43

44
	/// enum used to choose which shader src type to modify
45 46
	enum shaderSrcType { VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER };

47 48
	/**
	 * constructor
49 50 51
	 * @param vertShaderSrc vertex shader source to store
	 * @param fragShaderSrc fragment shader source to store
	 * @param geomShaderSrc geometry shader source to store (optional)
52
	 */
53
	 ShaderMutator(const std::string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc = "");
54 55
	 
	 /**
56 57 58
	  * checks if a variable is declared in the shader source
	  * @param srcType shader source to use
	  * @param variableName variable to search for
59
	  */
60
	 bool containsVariableDeclaration(shaderSrcType srcType, const std::string& variableName);
61 62
	 
	 /**
63
	  * sets or changes shading language version in the shader source
64
	  * - only if the current version is lower
65 66
	  * @param srcType shader source to use
	  * @param version version to set (110, 120, 150...)
67
	  */
68
	 void setMinShadingLanguageVersion(shaderSrcType srcType, int version);
69

70
	 /**
71 72 73
	  * changes int constant value in the shader source
	  * @param srcType shader source to use
	  * @param newVal new constant value
74
	  */
75
	 void changeIntConstantValue(shaderSrcType srcType, const std::string& constantName, int newVal);
76

77
	 /**
78 79
	  * inserts code before main function in the shader source
	  * @param srcType shader source to use
80 81
	  * @param insertedCode source code to insert into shader
	  */
82
	 void insertCodeBeforeMainFunction(shaderSrcType srcType, const std::string& insertedCode);
83 84
	 
	 /**
85 86
	  * inserts code at the beginning of main function in the shader source
	  * @param srcType shader source to use
87 88
	  * @param insertedCode source code to insert into shader
	  */
89
	 void insertCodeAtMainFunctionBeginning(shaderSrcType srcType, const std::string& insertedCode);
90 91
	 
	 /**
92
	  * inserts code at the end of main function in the shader source
93
	  * @warning takes the number of opening and closing braces of main function into account
94
	  * @param srcType shader source to use
95 96
	  * @param insertedCode source code to insert into shader
	  */
97
	 void insertCodeAtMainFunctionEnd(shaderSrcType srcType, const std::string& insertedCode);
98
	 
99
	 /// returns the modified vertex shader source code
100 101
	 std::string getModifiedVertexShaderSrc() { return m_vShaderMutation; }
	 
102
	 /// returns the modified fragment shader source code
103 104
	 std::string getModifiedFragmentShaderSrc() { return m_fShaderMutation; }
	 
105
	 /// returns the modified geometry shader source code
106 107 108 109
	 std::string getModifiedGeometryShaderSrc() { return m_gShaderMutation; }
	
private:

110 111
	/// processed shader name stored for log purpose
	std::string m_shaderName;
112
	
113
	/// modified version of the original vertex shader source code
114 115
	std::string m_vShaderMutation;
	
116
	/// modified version of the original fragment shader source code
117 118
	std::string m_fShaderMutation;
	
119
	/// modified version of the original geometry shader source code
120 121 122
	std::string m_gShaderMutation;
	
	/**
123 124 125
	 * checks if the given position is commented
	 * @param pos position in the source (like in a string)
	 * @param src source to analyze
126
	 */
127
	bool srcIsCommented(size_t pos, const std::string& src);
128 129
	
	/**
130 131 132
	 * checks if the given position is commented with a one-line comment
	 * @param pos position in the source (like in a string)
	 * @param src source to analyze
133
	 */
134
	bool srcIsOneLineCommented(size_t pos, const std::string& src);
135 136
	
	/**
137 138 139
	  * checks if a variable is declared
	  * @param variableName variable to search for
	  * @param src source to analyze
140
	 */
141
	bool srcContainsVariableDeclaration(const std::string& variableName, std::string& src);
142
	
143
	/**
144 145
	 * sets or changes shading language version if the current version is lower
	 * @param version version to set (110, 120, 150...)
146 147
	 * @param modifiedSrc shader source code to modify
	 */
148 149 150
	bool srcSetMinShadingLanguageVersion(int version, std::string& modifiedSrc);

	/**
151 152 153
	 * changes int constant value
	 * @param newVal new constant value
	 * @param constantName constant name as it is declared
154 155
	 * @param modifiedSrc shader source code to modify
	 */
156
	bool srcChangeIntConstantValue(int newVal, const std::string& constantName, std::string& modifiedSrc);
157

158
	/**
159 160
	 * inserts code before main function
	 * @param insertedCode source code to insert
161 162
	 * @param modifiedSrc shader source code to modify
	 */
163
	bool srcInsertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc);
164 165
	
	/**
166 167
	 * inserts code at the beginning of main function
	 * @param insertedCode source code to insert
168 169
	 * @param modifiedSrc shader source code to modify
	 */
170
	bool srcInsertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc);
171 172

	/**
173 174
	 * inserts code at the end of main function
	 * @param insertedCode source code to insert
175 176
	 * @param modifiedSrc shader source code to modify
	 */
177
	bool srcInsertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc);
178 179 180 181 182 183 184 185
	
};

} // namespace Utils

} // namespace CGoGN

#endif