Ticket #3640: preprocessor_include_initial_impl.patch
File preprocessor_include_initial_impl.patch, 11.0 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/maps/skirmishes/Acropolis
30 30 <Contrast>0.96875</Contrast> 31 31 <Saturation>1</Saturation> 32 32 <Bloom>0.186523</Bloom> 33 <PostEffect> hdr</PostEffect>33 <PostEffect>smaa</PostEffect> 34 34 </Postproc> 35 35 </Environment> 36 36 <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 3 uniform sampler2D renderedTex; 4 5 varying vec2 v_tex; 6 7 void 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 7 varying vec2 v_tex; 8 9 attribute vec3 a_vertex; 10 attribute vec2 a_uv0; 11 12 void 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 3 void m() 4 { 5 } 6 No newline at end of file -
source/ps/Preprocessor.cpp
38 38 39 39 #include "ps/CLogger.h" 40 40 41 #include "ps/Filesystem.h" // to handle #include 42 41 43 // Limit max number of macro arguments to this 42 44 #define MAX_MACRO_ARGS 16 43 45 … … 224 226 return xt; 225 227 } 226 228 229 CPreprocessor::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 227 251 //---------------------------------------------------------------------------// 228 252 229 253 static void DefaultError (void *iData, int iLine, const char *iError, … … 700 724 return false; 701 725 702 726 case Token::TK_KEYWORD: 703 727 { 704 728 // Try to expand the macro 705 729 Macro *m = IsDefined (*vt); 706 730 if (m != NULL && !m->Expanding) 707 731 { 708 732 Token x = ExpandMacro (*vt); … … 715 739 // Undefined macro, "expand" to 0 (mimic cpp behaviour) 716 740 oValue = 0; 717 741 break; 718 742 } 719 743 case Token::TK_TEXT: 720 744 case Token::TK_NUMBER: 721 745 if (!vt->GetValue (oValue)) … … 869 893 return t; 870 894 } 871 895 872 bool CPreprocessor::HandleDefine 896 bool CPreprocessor::HandleDefine(Token &iBody, int iLine) 873 897 { 874 898 // Create an additional preprocessor to process macro body 875 CPreprocessor cpp 899 CPreprocessor cpp(iBody, iLine); 876 900 877 Token t = cpp.GetToken 901 Token t = cpp.GetToken(false); 878 902 if (t.Type != Token::TK_KEYWORD) 879 903 { 880 Error 904 Error(iLine, "Macro name expected after #define"); 881 905 return false; 882 906 } 883 907 884 908 bool output_enabled = ((EnableOutput & (EnableOutput + 1)) == 0); 885 909 if (!output_enabled) 886 910 return true; 887 911 888 Macro *m = new Macro 912 Macro *m = new Macro(t); 889 913 m->Body = iBody; 890 t = cpp.GetArguments 914 t = cpp.GetArguments(m->NumArgs, m->Args, false); 891 915 while (t.Type == Token::TK_WHITESPACE) 892 t = cpp.GetToken 916 t = cpp.GetToken(false); 893 917 894 918 switch (t.Type) 895 919 { 896 897 898 899 t = Token(Token::TK_TEXT, "", 0);900 920 case Token::TK_NEWLINE: 921 case Token::TK_EOS: 922 // Assign "" to token 923 t = Token(Token::TK_TEXT, "", 0); 924 break; 901 925 902 903 904 926 case Token::TK_ERROR: 927 delete m; 928 return false; 905 929 906 907 908 ENSURE(t.String + t.Length == cpp.Source);909 910 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; 911 935 } 912 936 913 937 m->Value = t; … … 979 1003 return true; 980 1004 } 981 1005 1006 CPreprocessor::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 982 1062 CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs) 983 1063 { 984 1064 if (iNumArgs != 1) … … 1113 1193 ((dirlen == sizeof (s) - 1) && (strncmp (directive, s, sizeof (s) - 1) == 0)) 1114 1194 1115 1195 bool rc; 1116 if (IS_DIRECTIVE 1117 rc = HandleDefine 1196 if (IS_DIRECTIVE("define")) 1197 rc = HandleDefine(t, iLine); 1118 1198 else if (IS_DIRECTIVE ("undef")) 1119 1199 rc = HandleUnDef (t, iLine); 1120 1200 else if (IS_DIRECTIVE ("ifdef")) … … 1125 1205 if (rc) 1126 1206 EnableOutput ^= 1; 1127 1207 } 1208 else if (IS_DIRECTIVE("include")) 1209 return HandleInclude(t, iLine); 1128 1210 else if (IS_DIRECTIVE ("if")) 1129 1211 rc = HandleIf (t, iLine); 1130 1212 else if (IS_DIRECTIVE ("else")) … … 1176 1258 1177 1259 void CPreprocessor::Define (const char *iMacroName, const char *iMacroValue) 1178 1260 { 1179 1261 Define (iMacroName, strlen(iMacroName), iMacroValue, strlen(iMacroValue)); 1180 1262 } 1181 1263 1182 1264 void CPreprocessor::Define (const char *iMacroName, long iMacroValue) 1183 1265 { 1184 1266 Define (iMacroName, strlen(iMacroName), iMacroValue); 1185 1267 } 1186 1268 1187 1269 bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen) … … 1265 1347 1266 1348 if (output_enabled) 1267 1349 output.AppendNL (Line - old_line - t.CountNL ()); 1350 1268 1351 goto NextToken; 1269 1352 1270 1353 case Token::TK_LINECONT: -
source/ps/Preprocessor.h
200 200 201 201 /// Expand the macro value (will not work for functions) 202 202 Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros); 203 204 /// Performs deep copy of this macro 205 Macro *copy(); 203 206 }; 204 207 205 208 friend class CPreprocessor::Macro; … … 278 281 */ 279 282 bool HandleIfDef (Token &iBody, int iLine); 280 283 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 281 296 /** 282 297 * Handle an \#if directive. 283 298 * @param iBody