OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WGEShaderVersionPreprocessor.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
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     // initialize members
00038 }
00039 
00040 WGEShaderVersionPreprocessor::~WGEShaderVersionPreprocessor()
00041 {
00042     // cleanup
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     // Eliminate all #version statements and put it to the beginning.
00054     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00055 
00056     // this is an expression for the #version statement
00057     static const boost::regex versionRegexp( "^[ ]*#[ ]*version[ ]+([123456789][0123456789][0123456789]).*$" );
00058 
00059     // go through each line again
00060     std::string line;
00061     boost::smatch matches;          // the list of matches
00062     bool foundVersion = false;
00063     unsigned int version = 120;     // our default version
00064     std::stringstream completeCode( code );
00065     std::ostringstream cleanedCode;
00066     while( std::getline( completeCode, line ) )
00067     {
00068         if( boost::regex_match( line, matches, versionRegexp ) ) // look for the #version statement
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     // no version statement found, assume 1.2
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     // the ATI compiler needs the version statement to be the first statement in the shader
00087     std::stringstream vs;
00088     vs << "#version " << version << std::endl << cleanedCode.str();
00089     return vs.str();
00090 }
00091 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends