shaderMutator.h 8.09 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21 22 23 24 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef _CGoGN_SHADERMUTATOR_H_
#define _CGoGN_SHADERMUTATOR_H_

Thery Sylvain's avatar
Thery Sylvain committed
28 29 30
#undef tolower
#undef toupper

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

namespace CGoGN
{

namespace Utils
{

class ShaderMutator
{
	
public:
46

47
	/// enum used to choose which shader src type to modify
48 49
	enum shaderSrcType { VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER };

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

75
	 /**
76 77 78
	  * changes int constant value in the shader source
	  * @param srcType shader source to use
	  * @param newVal new constant value
Maire Nicolas's avatar
Maire Nicolas committed
79
	  * @return true on success
80
	  */
Maire Nicolas's avatar
Maire Nicolas committed
81
	 bool changeIntConstantValue(shaderSrcType srcType, const std::string& constantName, int newVal);
82

83 84 85 86 87 88 89 90
	 /**
	  * changes float constant value in the shader source
	  * @param srcType shader source to use
	  * @param newVal new constant value
	  * @return true on success
	  */
	 bool changeFloatConstantValue(shaderSrcType srcType, const std::string& constantName, float newValue);

91
	 /**
92 93
	  * inserts code before main function in the shader source
	  * @param srcType shader source to use
94
	  * @param insertedCode source code to insert into shader
Maire Nicolas's avatar
Maire Nicolas committed
95
	  * @return true on success
96
	  */
Maire Nicolas's avatar
Maire Nicolas committed
97
	 bool insertCodeBeforeMainFunction(shaderSrcType srcType, const std::string& insertedCode);
98 99
	 
	 /**
100 101
	  * inserts code at the beginning of main function in the shader source
	  * @param srcType shader source to use
102
	  * @param insertedCode source code to insert into shader
Maire Nicolas's avatar
Maire Nicolas committed
103
	  * @return true on success
104
	  */
Maire Nicolas's avatar
Maire Nicolas committed
105
	 bool insertCodeAtMainFunctionBeginning(shaderSrcType srcType, const std::string& insertedCode);
106 107
	 
	 /**
108
	  * inserts code at the end of main function in the shader source
109
	  * @warning takes the number of opening and closing braces of main function into account
110
	  * @param srcType shader source to use
111
	  * @param insertedCode source code to insert into shader
Maire Nicolas's avatar
Maire Nicolas committed
112
	  * @return true on success
113
	  */
Maire Nicolas's avatar
Maire Nicolas committed
114
	 bool insertCodeAtMainFunctionEnd(shaderSrcType srcType, const std::string& insertedCode);
115
	 
116
	 /// returns the modified vertex shader source code
117 118
	 std::string getModifiedVertexShaderSrc() { return m_vShaderMutation; }
	 
119
	 /// returns the modified fragment shader source code
120 121
	 std::string getModifiedFragmentShaderSrc() { return m_fShaderMutation; }
	 
122
	 /// returns the modified geometry shader source code
123 124 125 126
	 std::string getModifiedGeometryShaderSrc() { return m_gShaderMutation; }
	
private:

127 128
	/// processed shader name stored for log purpose
	std::string m_shaderName;
129
	
130
	/// modified version of the original vertex shader source code
131 132
	std::string m_vShaderMutation;
	
133
	/// modified version of the original fragment shader source code
134 135
	std::string m_fShaderMutation;
	
136
	/// modified version of the original geometry shader source code
137 138 139
	std::string m_gShaderMutation;
	
	/**
140 141 142
	 * checks if the given position is commented
	 * @param pos position in the source (like in a string)
	 * @param src source to analyze
Maire Nicolas's avatar
Maire Nicolas committed
143
	 * @return true if the given position is commented
144
	 */
145
	bool srcIsCommented(size_t pos, const std::string& src);
146 147
	
	/**
Maire Nicolas's avatar
Maire Nicolas committed
148
	 * checks if the given position is one-line commented
149 150
	 * @param pos position in the source (like in a string)
	 * @param src source to analyze
Maire Nicolas's avatar
Maire Nicolas committed
151
	 * @return true if the given position is one-line commented
152
	 */
153
	bool srcIsOneLineCommented(size_t pos, const std::string& src);
154 155
	
	/**
156 157 158
	  * checks if a variable is declared
	  * @param variableName variable to search for
	  * @param src source to analyze
Maire Nicolas's avatar
Maire Nicolas committed
159
	  * @return true if the variable was declared
160
	 */
161
	bool srcContainsVariableDeclaration(const std::string& variableName, std::string& src);
162
	
163
	/**
164 165
	 * sets or changes shading language version if the current version is lower
	 * @param version version to set (110, 120, 150...)
166
	 * @param modifiedSrc shader source code to modify
Maire Nicolas's avatar
Maire Nicolas committed
167
	 * @return true if the version was set or changed
168
	 */
169 170 171
	bool srcSetMinShadingLanguageVersion(int version, std::string& modifiedSrc);

	/**
172 173 174
	 * changes int constant value
	 * @param newVal new constant value
	 * @param constantName constant name as it is declared
175
	 * @param modifiedSrc shader source code to modify
Maire Nicolas's avatar
Maire Nicolas committed
176
	 * @return true on success
177
	 */
178
	bool srcChangeIntConstantValue(int newVal, const std::string& constantName, std::string& modifiedSrc);
179

180 181 182 183 184 185 186 187 188
	/**
	 * changes float constant value
	 * @param newVal new constant value
	 * @param constantName constant name as it is declared
	 * @param modifiedSrc shader source code to modify
	 * @return true on success
	 */
	bool srcChangeFloatConstantValue(float newVal, const std::string& constantName, std::string& modifiedSrc);

189
	/**
190 191
	 * inserts code before main function
	 * @param insertedCode source code to insert
192
	 * @param modifiedSrc shader source code to modify
Maire Nicolas's avatar
Maire Nicolas committed
193
	 * @return true on success
194
	 */
195
	bool srcInsertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc);
196 197
	
	/**
198 199
	 * inserts code at the beginning of main function
	 * @param insertedCode source code to insert
200
	 * @param modifiedSrc shader source code to modify
Maire Nicolas's avatar
Maire Nicolas committed
201
	 * @return true on success
202
	 */
203
	bool srcInsertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc);
204 205

	/**
206 207
	 * inserts code at the end of main function
	 * @param insertedCode source code to insert
208
	 * @param modifiedSrc shader source code to modify
Maire Nicolas's avatar
Maire Nicolas committed
209
	 * @return true on success
210
	 */
211
	bool srcInsertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc);
212 213 214 215 216 217 218 219
	
};

} // namespace Utils

} // namespace CGoGN

#endif