Ticket #3640: preprocessor_include_initial_impl.patch

File preprocessor_include_initial_impl.patch, 11.0 KB (added by fabio, 8 years ago)

mikita patch uzipped for easier review

  • binaries/data/mods/public/maps/skirmishes/Acropolis

     
    3030            <Contrast>0.96875</Contrast>
    3131            <Saturation>1</Saturation>
    3232            <Bloom>0.186523</Bloom>
    33             <PostEffect>hdr</PostEffect>
     33            <PostEffect>smaa</PostEffect>
    3434        </Postproc>
    3535    </Environment>
    3636    <Camera>
  • binaries/data/mods/public/shaders/effects/postproc/smaa.xml

     
     1<?xml version="1.0" encoding="utf-8"?>
     2<effect>
     3    <technique>
     4        <require shaders="glsl"/>
     5        <pass shader="glsl/edge_detection"/>
     6        <!--<pass shader="glsl/blending_weights"/>-->
     7        <!--<pass shader="glsl/neighborhood_blending"/>-->
     8    </technique>
     9</effect>
     10 No newline at end of file
  • binaries/data/mods/public/shaders/glsl/edge_detection.fs

     
     1#version 120
     2
     3uniform sampler2D renderedTex;
     4
     5varying vec2 v_tex;
     6
     7void main(void)
     8{
     9    gl_FragColor.rgb = texture2D(renderedTex, v_tex).rgb;
     10    gl_FragColor.a = 1.0;
     11}
     12
     13
  • binaries/data/mods/public/shaders/glsl/edge_detection.vs

     
     1#version 110
     2
     3#include "glsl/header.glsl"
     4
     5#define Y X
     6
     7varying vec2 v_tex;
     8
     9attribute vec3 a_vertex;
     10attribute vec2 a_uv0;
     11
     12void main()
     13
     14  vec2 tmp = vec2(Y);
     15  gl_Position = vec4(a_vertex, 1.0); 
     16  v_tex = a_uv0;
     17}
     18 No newline at end of file
  • binaries/data/mods/public/shaders/glsl/edge_detection.xml

     
     1<?xml version="1.0" encoding="utf-8"?>
     2<program type="glsl">
     3    <vertex file="glsl/edge_detection.vs">
     4        <stream name="pos"/>
     5        <stream name="uv0"/>
     6        <attrib name="a_vertex" semantics="gl_Vertex"/>
     7        <attrib name="a_uv0" semantics="gl_MultiTexCoord0"/>
     8    </vertex>
     9    <fragment file="glsl/edge_detection.fs"/>
     10</program>
  • binaries/data/mods/public/shaders/glsl/header.glsl

     
     1#define X 1.0
     2
     3void m()
     4{
     5}
     6 No newline at end of file
  • source/ps/Preprocessor.cpp

     
    3838
    3939#include "ps/CLogger.h"
    4040
     41#include "ps/Filesystem.h" // to handle #include
     42
    4143// Limit max number of macro arguments to this
    4244#define MAX_MACRO_ARGS 16
    4345
     
    224226    return xt;
    225227}
    226228
     229CPreprocessor::Macro *CPreprocessor::Macro::copy()
     230{
     231    Token mainCopy;
     232
     233    // Copy buffer once
     234    char *buffer = new char[this->Body.Length];
     235    strncpy(buffer, this->Body.String, this->Body.Length);
     236
     237    // Copy each subcomponent of Macro
     238    Token body = Token(this->Body.Type, buffer, this->Body.Length);
     239    Token name = Token(this->Name.Type, buffer, this->Name.Length);
     240    Token value = Token(this->Value.Type, buffer + (this->Value.Buffer - this->Body.Buffer), this->Value.Length);
     241
     242    // FIXME: Ignore Args for now
     243
     244    Macro *macroCopy = new Macro(name);
     245    macroCopy->Body = body;
     246    macroCopy->Value = value;
     247
     248    return macroCopy;
     249}
     250
    227251//---------------------------------------------------------------------------//
    228252
    229253static void DefaultError (void *iData, int iLine, const char *iError,
     
    700724            return false;
    701725
    702726        case Token::TK_KEYWORD:
    703         {
     727        {
    704728            // Try to expand the macro
    705             Macro *m = IsDefined (*vt);
     729            Macro *m = IsDefined (*vt);
    706730            if (m != NULL && !m->Expanding)
    707731            {
    708732                Token x = ExpandMacro (*vt);
     
    715739            // Undefined macro, "expand" to 0 (mimic cpp behaviour)
    716740            oValue = 0;
    717741            break;
    718         }
     742        }
    719743        case Token::TK_TEXT:
    720744        case Token::TK_NUMBER:
    721745            if (!vt->GetValue (oValue))
     
    869893    return t;
    870894}
    871895
    872 bool CPreprocessor::HandleDefine (Token &iBody, int iLine)
     896bool CPreprocessor::HandleDefine(Token &iBody, int iLine)
    873897{
    874898    // Create an additional preprocessor to process macro body
    875     CPreprocessor cpp (iBody, iLine);
     899    CPreprocessor cpp(iBody, iLine);
    876900
    877     Token t = cpp.GetToken (false);
     901    Token t = cpp.GetToken(false);
    878902    if (t.Type != Token::TK_KEYWORD)
    879903    {
    880         Error (iLine, "Macro name expected after #define");
     904        Error(iLine, "Macro name expected after #define");
    881905        return false;
    882906    }
    883907
    884908    bool output_enabled = ((EnableOutput & (EnableOutput + 1)) == 0);
    885909    if (!output_enabled)
    886         return true;
     910        return true;
    887911
    888     Macro *m = new Macro (t);
     912    Macro *m = new Macro(t);
    889913    m->Body = iBody;
    890     t = cpp.GetArguments (m->NumArgs, m->Args, false);
     914    t = cpp.GetArguments(m->NumArgs, m->Args, false);
    891915    while (t.Type == Token::TK_WHITESPACE)
    892         t = cpp.GetToken (false);
     916        t = cpp.GetToken(false);
    893917
    894918    switch (t.Type)
    895919    {
    896         case Token::TK_NEWLINE:
    897         case Token::TK_EOS:
    898             // Assign "" to token
    899             t = Token (Token::TK_TEXT, "", 0);
    900             break;
     920    case Token::TK_NEWLINE:
     921    case Token::TK_EOS:
     922        // Assign "" to token
     923        t = Token(Token::TK_TEXT, "", 0);
     924        break;
    901925
    902         case Token::TK_ERROR:
    903             delete m;
    904             return false;
     926    case Token::TK_ERROR:
     927        delete m;
     928        return false;
    905929
    906         default:
    907             t.Type = Token::TK_TEXT;
    908             ENSURE (t.String + t.Length == cpp.Source);
    909             t.Length = cpp.SourceEnd - t.String;
    910             break;
     930    default:
     931        t.Type = Token::TK_TEXT;
     932        ENSURE(t.String + t.Length == cpp.Source);
     933        t.Length = cpp.SourceEnd - t.String;
     934        break;
    911935    }
    912936
    913937    m->Value = t;
     
    9791003    return true;
    9801004}
    9811005
     1006CPreprocessor::Token CPreprocessor::HandleInclude(Token &iBody, int iLine)
     1007{
     1008    CPreprocessor cpp(iBody, iLine);
     1009
     1010    Token t = cpp.GetToken(false);
     1011
     1012    if (t.Type != Token::TK_STRING)
     1013    {
     1014        Error(iLine, "Expecting a file name after #include, got", &t);
     1015        return Token();
     1016    }
     1017
     1018    std::string filename = std::string(t.String).substr(0, t.Length);
     1019    ENSURE(filename.at(0) == '"' && filename.at(t.Length-1) == '"');
     1020    filename = filename.substr(1, t.Length - 2); // remove '"' symbols from the beginning and from the end of token
     1021
     1022    CVFSFile includedFile;
     1023    if (includedFile.Load(g_VFS, L"shaders/" + wstring_from_utf8(filename)) != PSRETURN_OK)
     1024        Error(iLine, "Warning: File not found", &t);
     1025    const CStr& input = includedFile.GetAsString();
     1026
     1027    size_t len = 0;
     1028    CPreprocessor preprocessor;
     1029    // preprocessor.AddDefines(m_Defines);
     1030    std::string includedSources = preprocessor.Parse(input.c_str(), input.size(), len);
     1031    char *result = new char[len];
     1032    includedSources.copy(result, len);
     1033
     1034    // Copy macros
     1035    Macro *m = preprocessor.MacroList;
     1036    while (m) {
     1037        Macro *copy = m->copy();
     1038        copy->Next = MacroList;
     1039        MacroList = copy;
     1040        m = m->Next;
     1041    }
     1042
     1043    if (!result)
     1044    {
     1045        Error(iLine, "Problem during preprocessing of included file", &t);
     1046        return Token();
     1047    }
     1048
     1049    do
     1050    {
     1051        t = cpp.GetToken(false);
     1052    } while (t.Type == Token::TK_WHITESPACE ||
     1053        t.Type == Token::TK_COMMENT ||
     1054        t.Type == Token::TK_LINECOMMENT);
     1055
     1056    if (t.Type != Token::TK_EOS)
     1057        Error(iLine, "Warning: Ignoring garbage after directive", &t);
     1058
     1059    return Token(Token::TK_STRING, result, len);;
     1060}
     1061
    9821062CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs)
    9831063{
    9841064    if (iNumArgs != 1)
     
    11131193    ((dirlen == sizeof (s) - 1) && (strncmp (directive, s, sizeof (s) - 1) == 0))
    11141194
    11151195    bool rc;
    1116     if (IS_DIRECTIVE ("define"))
    1117         rc = HandleDefine (t, iLine);
     1196    if (IS_DIRECTIVE("define"))
     1197        rc = HandleDefine(t, iLine);
    11181198    else if (IS_DIRECTIVE ("undef"))
    11191199        rc = HandleUnDef (t, iLine);
    11201200    else if (IS_DIRECTIVE ("ifdef"))
     
    11251205        if (rc)
    11261206            EnableOutput ^= 1;
    11271207    }
     1208    else if (IS_DIRECTIVE("include"))
     1209        return HandleInclude(t, iLine);
    11281210    else if (IS_DIRECTIVE ("if"))
    11291211        rc = HandleIf (t, iLine);
    11301212    else if (IS_DIRECTIVE ("else"))
     
    11761258
    11771259void CPreprocessor::Define (const char *iMacroName, const char *iMacroValue)
    11781260{
    1179     Define (iMacroName, strlen(iMacroName), iMacroValue, strlen(iMacroValue));
     1261    Define (iMacroName, strlen(iMacroName), iMacroValue, strlen(iMacroValue));
    11801262}
    11811263
    11821264void CPreprocessor::Define (const char *iMacroName, long iMacroValue)
    11831265{
    1184     Define (iMacroName, strlen(iMacroName), iMacroValue);
     1266    Define (iMacroName, strlen(iMacroName), iMacroValue);
    11851267}
    11861268
    11871269bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen)
     
    12651347
    12661348                if (output_enabled)
    12671349                    output.AppendNL (Line - old_line - t.CountNL ());
     1350                   
    12681351                goto NextToken;
    12691352
    12701353            case Token::TK_LINECONT:
  • source/ps/Preprocessor.h

     
    200200
    201201        /// Expand the macro value (will not work for functions)
    202202        Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros);
     203
     204        /// Performs deep copy of this macro
     205        Macro *copy();
    203206    };
    204207
    205208    friend class CPreprocessor::Macro;
     
    278281     */
    279282    bool HandleIfDef (Token &iBody, int iLine);
    280283
     284    /**
     285    * Handle a \#include directive.
     286    * @param iBody
     287    *     The body of the directive (everything after the directive
     288    *     until end of line).
     289    * @param iLine
     290    *     The line where the directive begins (for error reports)
     291    * @return
     292    *     Included text enclosed in a token
     293    */
     294    Token HandleInclude (Token &iBody, int iLine);
     295
    281296    /**
    282297     * Handle an \#if directive.
    283298     * @param iBody