OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEShaderVersionPreprocessor.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <sstream>
27 #include <ostream>
28 
29 #include <boost/regex.hpp>
30 
31 #include "../../common/WLogger.h"
32 
33 #include "WGEShaderVersionPreprocessor.h"
34 
36 {
37  // initialize members
38 }
39 
41 {
42  // cleanup
43 }
44 
45 std::string WGEShaderVersionPreprocessor::process( const std::string& file, const std::string& code ) const
46 {
47  if( !getActive() )
48  {
49  return code;
50  }
51 
52  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53  // Eliminate all #version statements and put it to the beginning.
54  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55 
56  // this is an expression for the #version statement
57  static const boost::regex versionRegexp( "^[ ]*#[ ]*version[ ]+([123456789][0123456789][0123456789]).*$" );
58 
59  // go through each line again
60  std::string line;
61  boost::smatch matches; // the list of matches
62  bool foundVersion = false;
63  unsigned int version = 120; // our default version
64  std::stringstream completeCode( code );
65  std::ostringstream cleanedCode;
66  while( std::getline( completeCode, line ) )
67  {
68  if( boost::regex_match( line, matches, versionRegexp ) ) // look for the #version statement
69  {
70  unsigned int versionNum = boost::lexical_cast< unsigned int >( matches[1] );
71  version = std::max( versionNum, version );
72  foundVersion = true;
73  continue;
74  }
75 
76  cleanedCode << line << std::endl;
77  }
78 
79  // no version statement found, assume 1.2
80  if( !foundVersion )
81  {
82  wlog::warn( "WGEShader (" + file + ")" ) << "No version statements in unrolled shader file \"" << file << "\" found. Using default: "
83  << version << ".";
84  }
85 
86  // the ATI compiler needs the version statement to be the first statement in the shader
87  std::stringstream vs;
88  vs << "#version " << version << std::endl << cleanedCode.str();
89  return vs.str();
90 }
91