00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <string>
00026 #include <sstream>
00027 #include <ostream>
00028
00029 #include <boost/regex.hpp>
00030
00031 #include "../../common/WLogger.h"
00032
00033 #include "WGEShaderVersionPreprocessor.h"
00034
00035 WGEShaderVersionPreprocessor::WGEShaderVersionPreprocessor()
00036 {
00037
00038 }
00039
00040 WGEShaderVersionPreprocessor::~WGEShaderVersionPreprocessor()
00041 {
00042
00043 }
00044
00045 std::string WGEShaderVersionPreprocessor::process( const std::string& file, const std::string& code ) const
00046 {
00047 if( !getActive() )
00048 {
00049 return code;
00050 }
00051
00052
00053
00054
00055
00056
00057 static const boost::regex versionRegexp( "^[ ]*#[ ]*version[ ]+([123456789][0123456789][0123456789]).*$" );
00058
00059
00060 std::string line;
00061 boost::smatch matches;
00062 bool foundVersion = false;
00063 unsigned int version = 120;
00064 std::stringstream completeCode( code );
00065 std::ostringstream cleanedCode;
00066 while( std::getline( completeCode, line ) )
00067 {
00068 if( boost::regex_match( line, matches, versionRegexp ) )
00069 {
00070 unsigned int versionNum = boost::lexical_cast< unsigned int >( matches[1] );
00071 version = std::max( versionNum, version );
00072 foundVersion = true;
00073 continue;
00074 }
00075
00076 cleanedCode << line << std::endl;
00077 }
00078
00079
00080 if( !foundVersion )
00081 {
00082 wlog::warn( "WGEShader (" + file + ")" ) << "No version statements in unrolled shader file \"" << file << "\" found. Using default: "
00083 << version << ".";
00084 }
00085
00086
00087 std::stringstream vs;
00088 vs << "#version " << version << std::endl << cleanedCode.str();
00089 return vs.str();
00090 }
00091