OpenWalnut  1.5.0dev
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 <algorithm>
26 #include <ostream>
27 #include <sstream>
28 #include <string>
29 
30 #include <boost/regex.hpp>
31 
32 #include "../../common/WLogger.h"
33 #include "../../common/WStringUtils.h"
34 
35 #include "WGEShaderVersionPreprocessor.h"
36 
38 {
39  // initialize members
40 }
41 
43 {
44  // cleanup
45 }
46 
47 std::string WGEShaderVersionPreprocessor::process( const std::string& file, const std::string& code ) const
48 {
49  if( !getActive() )
50  {
51  return code;
52  }
53 
54  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55  // Eliminate all #version statements and put it to the beginning.
56  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57 
58  // this is an expression for the #version statement
59  static const boost::regex versionRegexp( "^[ ]*#[ ]*version[ ]+([123456789][0123456789][0123456789]).*$" );
60 
61  // go through each line again
62  std::string line;
63  boost::smatch matches; // the list of matches
64  bool foundVersion = false;
65  unsigned int version = 120; // our default version
66  std::stringstream completeCode( code );
67  std::ostringstream cleanedCode;
68  while( std::getline( completeCode, line ) )
69  {
70  if( boost::regex_match( line, matches, versionRegexp ) ) // look for the #version statement
71  {
72  unsigned int versionNum = string_utils::fromString< unsigned int >( matches[1] );
73  version = std::max( versionNum, version );
74  foundVersion = true;
75  cleanedCode << "// " << line << std::endl;
76  continue;
77  }
78 
79  cleanedCode << line << std::endl;
80  }
81 
82  // no version statement found, assume 1.2
83  if( !foundVersion )
84  {
85  wlog::warn( "WGEShader (" + file + ")" ) << "No version statements in unrolled shader file \"" << file << "\" found. Using default: "
86  << version << ".";
87  }
88 
89  // the ATI compiler needs the version statement to be the first statement in the shader
90  std::stringstream vs;
91  vs << "#version " << version << std::endl
92  << "#line 1 " << std::endl << cleanedCode.str();
93  // wlog::debug( "WGEShader (" + file + ")" ) << "Using GLSL version " << version;
94  return vs.str();
95 }
96 
bool getActive() const
If the preprocessor is active, this returns true.
WGEShaderVersionPreprocessor()
Default constructor.
virtual std::string process(const std::string &file, const std::string &code) const
Process the whole code.
WStreamedLogger warn(const std::string &source)
Logging a warning message.
Definition: WLogger.h:309