BsSLFXCompiler.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsSLFXCompiler.h"
  4. #include "BsGpuProgram.h"
  5. #include <regex>
  6. #include "BsShader.h"
  7. #include "BsTechnique.h"
  8. #include "BsPass.h"
  9. #include "BsSamplerState.h"
  10. #include "BsRenderAPI.h"
  11. #include "BsDebug.h"
  12. #include "BsShaderManager.h"
  13. #include "BsShaderInclude.h"
  14. #include "BsMatrix4.h"
  15. #include "BsBuiltinResources.h"
  16. #define XSC_ENABLE_LANGUAGE_EXT 1
  17. #include "Xsc/Xsc.h"
  18. extern "C" {
  19. #include "BsMMAlloc.h"
  20. #include "BsParserFX.h"
  21. #include "BsLexerFX.h"
  22. }
  23. using namespace std;
  24. namespace bs
  25. {
  26. // Print out the FX AST, only for debug purposes
  27. void SLFXDebugPrint(ASTFXNode* node, String indent)
  28. {
  29. LOGDBG(indent + "NODE " + toString(node->type));
  30. for (int i = 0; i < node->options->count; i++)
  31. {
  32. OptionDataType odt = OPTION_LOOKUP[(int)node->options->entries[i].type].dataType;
  33. if (odt == ODT_Complex)
  34. {
  35. LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type));
  36. SLFXDebugPrint(node->options->entries[i].value.nodePtr, indent + "\t");
  37. continue;
  38. }
  39. String value;
  40. switch (odt)
  41. {
  42. case ODT_Bool:
  43. value = toString(node->options->entries[i].value.intValue != 0);
  44. break;
  45. case ODT_Int:
  46. value = toString(node->options->entries[i].value.intValue);
  47. break;
  48. case ODT_Float:
  49. value = toString(node->options->entries[i].value.floatValue);
  50. break;
  51. case ODT_String:
  52. value = node->options->entries[i].value.strValue;
  53. break;
  54. case ODT_Matrix:
  55. {
  56. Matrix4 mat4 = *(Matrix4*)(node->options->entries[i].value.matrixValue);
  57. value = toString(mat4);
  58. }
  59. break;
  60. default:
  61. break;
  62. }
  63. LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type) + " = " + value);
  64. }
  65. }
  66. class XscLog : public Xsc::Log
  67. {
  68. public:
  69. void SubmitReport(const Xsc::Report& report) override
  70. {
  71. switch (report.Type())
  72. {
  73. case Xsc::ReportTypes::Info:
  74. mInfos.push_back({ FullIndent(), report });
  75. break;
  76. case Xsc::ReportTypes::Warning:
  77. mWarnings.push_back({ FullIndent(), report });
  78. break;
  79. case Xsc::ReportTypes::Error:
  80. mErrors.push_back({ FullIndent(), report });
  81. break;
  82. }
  83. }
  84. void getMessages(StringStream& output)
  85. {
  86. printAndClearReports(output, mInfos);
  87. printAndClearReports(output, mWarnings, (mWarnings.size() == 1 ? "WARNING" : "WARNINGS"));
  88. printAndClearReports(output, mErrors, (mErrors.size() == 1 ? "ERROR" : "ERRORS"));
  89. }
  90. private:
  91. struct IndentReport
  92. {
  93. std::string indent;
  94. Xsc::Report report;
  95. };
  96. static void printMultiLineString(StringStream& output, const std::string& str, const std::string& indent)
  97. {
  98. // Determine at which position the actual text begins (excluding the "error (X:Y) : " or the like)
  99. auto textStartPos = str.find(" : ");
  100. if (textStartPos != std::string::npos)
  101. textStartPos += 3;
  102. else
  103. textStartPos = 0;
  104. std::string newLineIndent(textStartPos, ' ');
  105. size_t start = 0;
  106. bool useNewLineIndent = false;
  107. while (start < str.size())
  108. {
  109. output << indent;
  110. if (useNewLineIndent)
  111. output << newLineIndent;
  112. // Print next line
  113. auto end = str.find('\n', start);
  114. if (end != std::string::npos)
  115. {
  116. output << str.substr(start, end - start);
  117. start = end + 1;
  118. }
  119. else
  120. {
  121. output << str.substr(start);
  122. start = end;
  123. }
  124. output << std::endl;
  125. useNewLineIndent = true;
  126. }
  127. }
  128. void printReport(StringStream& output, const IndentReport& r)
  129. {
  130. // Print optional context description
  131. if (!r.report.Context().empty())
  132. printMultiLineString(output, r.report.Context(), r.indent);
  133. // Print report message
  134. const auto& msg = r.report.Message();
  135. printMultiLineString(output, msg, r.indent);
  136. // Print optional line and line-marker
  137. if (r.report.HasLine())
  138. {
  139. const auto& line = r.report.Line();
  140. const auto& marker = r.report.Marker();
  141. // Print line
  142. output << r.indent << line << std::endl;
  143. // Print line marker
  144. output << r.indent << marker << std::endl;
  145. }
  146. // Print optional hints
  147. for (const auto& hint : r.report.GetHints())
  148. output << r.indent << hint << std::endl;
  149. }
  150. void printAndClearReports(StringStream& output, Vector<IndentReport>& reports, const String& headline = "")
  151. {
  152. if (!reports.empty())
  153. {
  154. if (!headline.empty())
  155. {
  156. String s = toString(reports.size()) + " " + headline;
  157. output << s << std::endl;
  158. output << String(s.size(), '-') << std::endl;
  159. }
  160. for (const auto& r : reports)
  161. printReport(output, r);
  162. reports.clear();
  163. }
  164. }
  165. Vector<IndentReport> mInfos;
  166. Vector<IndentReport> mWarnings;
  167. Vector<IndentReport> mErrors;
  168. };
  169. GpuParamObjectType ReflTypeToTextureType(Xsc::Reflection::BufferType type)
  170. {
  171. switch(type)
  172. {
  173. case Xsc::Reflection::BufferType::RWTexture1D: return GPOT_RWTEXTURE1D;
  174. case Xsc::Reflection::BufferType::RWTexture1DArray: return GPOT_RWTEXTURE1DARRAY;
  175. case Xsc::Reflection::BufferType::RWTexture2D: return GPOT_RWTEXTURE2D;
  176. case Xsc::Reflection::BufferType::RWTexture2DArray: return GPOT_RWTEXTURE2DARRAY;
  177. case Xsc::Reflection::BufferType::RWTexture3D: return GPOT_RWTEXTURE3D;
  178. case Xsc::Reflection::BufferType::Texture1D: return GPOT_TEXTURE1D;
  179. case Xsc::Reflection::BufferType::Texture1DArray: return GPOT_TEXTURE1DARRAY;
  180. case Xsc::Reflection::BufferType::Texture2D: return GPOT_TEXTURE2D;
  181. case Xsc::Reflection::BufferType::Texture2DArray: return GPOT_TEXTURE2DARRAY;
  182. case Xsc::Reflection::BufferType::Texture3D: return GPOT_TEXTURE3D;
  183. case Xsc::Reflection::BufferType::TextureCube: return GPOT_TEXTURECUBE;
  184. case Xsc::Reflection::BufferType::TextureCubeArray: return GPOT_TEXTURECUBEARRAY;
  185. case Xsc::Reflection::BufferType::Texture2DMS: return GPOT_TEXTURE2DMS;
  186. case Xsc::Reflection::BufferType::Texture2DMSArray: return GPOT_TEXTURE2DMSARRAY;
  187. default: return GPOT_UNKNOWN;
  188. }
  189. }
  190. GpuParamObjectType ReflTypeToBufferType(Xsc::Reflection::BufferType type)
  191. {
  192. switch(type)
  193. {
  194. case Xsc::Reflection::BufferType::Buffer: return GPOT_RWTYPED_BUFFER;
  195. case Xsc::Reflection::BufferType::StructuredBuffer: return GPOT_STRUCTURED_BUFFER;
  196. case Xsc::Reflection::BufferType::ByteAddressBuffer: return GPOT_BYTE_BUFFER;
  197. case Xsc::Reflection::BufferType::RWBuffer: return GPOT_RWTYPED_BUFFER;
  198. case Xsc::Reflection::BufferType::RWStructuredBuffer: return GPOT_RWSTRUCTURED_BUFFER;
  199. case Xsc::Reflection::BufferType::RWByteAddressBuffer: return GPOT_RWBYTE_BUFFER;
  200. case Xsc::Reflection::BufferType::AppendStructuredBuffer: return GPOT_RWAPPEND_BUFFER;
  201. case Xsc::Reflection::BufferType::ConsumeStructuredBuffer: return GPOT_RWCONSUME_BUFFER;
  202. default: return GPOT_UNKNOWN;
  203. }
  204. }
  205. GpuParamDataType ReflTypeToDataType(Xsc::Reflection::DataType type)
  206. {
  207. switch (type)
  208. {
  209. case Xsc::Reflection::DataType::Bool: return GPDT_BOOL;
  210. case Xsc::Reflection::DataType::Float: return GPDT_FLOAT1;
  211. case Xsc::Reflection::DataType::Float2: return GPDT_FLOAT2;
  212. case Xsc::Reflection::DataType::Float3: return GPDT_FLOAT3;
  213. case Xsc::Reflection::DataType::Float4: return GPDT_FLOAT4;
  214. case Xsc::Reflection::DataType::UInt:
  215. case Xsc::Reflection::DataType::Int:
  216. return GPDT_INT1;
  217. case Xsc::Reflection::DataType::UInt2:
  218. case Xsc::Reflection::DataType::Int2:
  219. return GPDT_INT2;
  220. case Xsc::Reflection::DataType::UInt3:
  221. case Xsc::Reflection::DataType::Int3:
  222. return GPDT_INT3;
  223. case Xsc::Reflection::DataType::UInt4:
  224. case Xsc::Reflection::DataType::Int4:
  225. return GPDT_INT4;
  226. case Xsc::Reflection::DataType::Float2x2: return GPDT_MATRIX_2X2;
  227. case Xsc::Reflection::DataType::Float2x3: return GPDT_MATRIX_2X3;
  228. case Xsc::Reflection::DataType::Float2x4: return GPDT_MATRIX_2X4;
  229. case Xsc::Reflection::DataType::Float3x2: return GPDT_MATRIX_3X4;
  230. case Xsc::Reflection::DataType::Float3x3: return GPDT_MATRIX_3X3;
  231. case Xsc::Reflection::DataType::Float3x4: return GPDT_MATRIX_3X4;
  232. case Xsc::Reflection::DataType::Float4x2: return GPDT_MATRIX_4X2;
  233. case Xsc::Reflection::DataType::Float4x3: return GPDT_MATRIX_4X3;
  234. case Xsc::Reflection::DataType::Float4x4: return GPDT_MATRIX_4X4;
  235. default: return GPDT_UNKNOWN;
  236. }
  237. }
  238. HTexture getBuiltinTexture(UINT32 idx)
  239. {
  240. if (idx == 1)
  241. return BuiltinResources::getTexture(BuiltinTexture::White);
  242. else if (idx == 2)
  243. return BuiltinResources::getTexture(BuiltinTexture::Black);
  244. else if (idx == 3)
  245. return BuiltinResources::getTexture(BuiltinTexture::Normal);
  246. return HTexture();
  247. }
  248. TextureAddressingMode parseTexAddrMode(Xsc::Reflection::TextureAddressMode addrMode)
  249. {
  250. switch (addrMode)
  251. {
  252. case Xsc::Reflection::TextureAddressMode::Border:
  253. return TAM_BORDER;
  254. case Xsc::Reflection::TextureAddressMode::Clamp:
  255. return TAM_CLAMP;
  256. case Xsc::Reflection::TextureAddressMode::Mirror:
  257. case Xsc::Reflection::TextureAddressMode::MirrorOnce:
  258. return TAM_MIRROR;
  259. case Xsc::Reflection::TextureAddressMode::Wrap:
  260. default:
  261. return TAM_WRAP;
  262. }
  263. }
  264. CompareFunction parseCompFunction(Xsc::Reflection::ComparisonFunc compFunc)
  265. {
  266. switch(compFunc)
  267. {
  268. case Xsc::Reflection::ComparisonFunc::Always:
  269. default:
  270. return CMPF_ALWAYS_PASS;
  271. case Xsc::Reflection::ComparisonFunc::Never:
  272. return CMPF_ALWAYS_FAIL;
  273. case Xsc::Reflection::ComparisonFunc::Equal:
  274. return CMPF_EQUAL;
  275. case Xsc::Reflection::ComparisonFunc::Greater:
  276. return CMPF_GREATER;
  277. case Xsc::Reflection::ComparisonFunc::GreaterEqual:
  278. return CMPF_GREATER_EQUAL;
  279. case Xsc::Reflection::ComparisonFunc::Less:
  280. return CMPF_LESS;
  281. case Xsc::Reflection::ComparisonFunc::LessEqual:
  282. return CMPF_LESS_EQUAL;
  283. case Xsc::Reflection::ComparisonFunc::NotEqual:
  284. return CMPF_NOT_EQUAL;
  285. }
  286. }
  287. SPtr<SamplerState> parseSamplerState(const Xsc::Reflection::SamplerState& sampState)
  288. {
  289. SAMPLER_STATE_DESC desc;
  290. desc.addressMode.u = parseTexAddrMode(sampState.addressU);
  291. desc.addressMode.v = parseTexAddrMode(sampState.addressV);
  292. desc.addressMode.w = parseTexAddrMode(sampState.addressW);
  293. desc.borderColor[0] = sampState.borderColor[0];
  294. desc.borderColor[1] = sampState.borderColor[1];
  295. desc.borderColor[2] = sampState.borderColor[2];
  296. desc.borderColor[3] = sampState.borderColor[3];
  297. desc.comparisonFunc = parseCompFunction(sampState.comparisonFunc);
  298. desc.maxAniso = sampState.maxAnisotropy;
  299. desc.mipMax = sampState.maxLOD;
  300. desc.mipMin = sampState.minLOD;
  301. desc.mipmapBias = sampState.mipLODBias;
  302. switch (sampState.filter)
  303. {
  304. case Xsc::Reflection::Filter::MinMagMipPoint:
  305. desc.minFilter = FO_POINT;
  306. desc.magFilter = FO_POINT;
  307. desc.mipFilter = FO_POINT;
  308. break;
  309. case Xsc::Reflection::Filter::MinMagPointMipLinear:
  310. desc.minFilter = FO_POINT;
  311. desc.magFilter = FO_POINT;
  312. desc.mipFilter = FO_LINEAR;
  313. break;
  314. case Xsc::Reflection::Filter::MinPointMagLinearMipPoint:
  315. desc.minFilter = FO_POINT;
  316. desc.magFilter = FO_LINEAR;
  317. desc.mipFilter = FO_POINT;
  318. break;
  319. case Xsc::Reflection::Filter::MinPointMagMipLinear:
  320. desc.minFilter = FO_POINT;
  321. desc.magFilter = FO_LINEAR;
  322. desc.mipFilter = FO_LINEAR;
  323. break;
  324. case Xsc::Reflection::Filter::MinLinearMagMipPoint:
  325. desc.minFilter = FO_LINEAR;
  326. desc.magFilter = FO_POINT;
  327. desc.mipFilter = FO_POINT;
  328. break;
  329. case Xsc::Reflection::Filter::MinLinearMagPointMipLinear:
  330. desc.minFilter = FO_LINEAR;
  331. desc.magFilter = FO_POINT;
  332. desc.mipFilter = FO_LINEAR;
  333. break;
  334. case Xsc::Reflection::Filter::MinMagLinearMipPoint:
  335. desc.minFilter = FO_LINEAR;
  336. desc.magFilter = FO_LINEAR;
  337. desc.mipFilter = FO_POINT;
  338. break;
  339. case Xsc::Reflection::Filter::MinMagMipLinear:
  340. desc.minFilter = FO_LINEAR;
  341. desc.magFilter = FO_LINEAR;
  342. desc.mipFilter = FO_LINEAR;
  343. break;
  344. case Xsc::Reflection::Filter::Anisotropic:
  345. desc.minFilter = FO_ANISOTROPIC;
  346. desc.magFilter = FO_ANISOTROPIC;
  347. desc.minFilter = FO_ANISOTROPIC;
  348. break;
  349. case Xsc::Reflection::Filter::ComparisonMinMagMipPoint:
  350. desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  351. desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  352. desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  353. break;
  354. case Xsc::Reflection::Filter::ComparisonMinMagPointMipLinear:
  355. desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  356. desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  357. desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  358. break;
  359. case Xsc::Reflection::Filter::ComparisonMinPointMagLinearMipPoint:
  360. desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  361. desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  362. desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  363. break;
  364. case Xsc::Reflection::Filter::ComparisonMinPointMagMipLinear:
  365. desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  366. desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  367. desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  368. break;
  369. case Xsc::Reflection::Filter::ComparisonMinLinearMagMipPoint:
  370. desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  371. desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  372. desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  373. break;
  374. case Xsc::Reflection::Filter::ComparisonMinLinearMagPointMipLinear:
  375. desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  376. desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  377. desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  378. break;
  379. case Xsc::Reflection::Filter::ComparisonMinMagLinearMipPoint:
  380. desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  381. desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  382. desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
  383. break;
  384. case Xsc::Reflection::Filter::ComparisonMinMagMipLinear:
  385. desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  386. desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  387. desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
  388. break;
  389. case Xsc::Reflection::Filter::ComparisonAnisotropic:
  390. desc.minFilter = (FilterOptions)(FO_ANISOTROPIC | FO_USE_COMPARISON);
  391. desc.magFilter = (FilterOptions)(FO_ANISOTROPIC | FO_USE_COMPARISON);
  392. desc.minFilter = (FilterOptions)(FO_ANISOTROPIC | FO_USE_COMPARISON);
  393. break;
  394. default:
  395. break;
  396. }
  397. return SamplerState::create(desc);
  398. }
  399. void parseParameters(const Xsc::Reflection::ReflectionData& reflData, SHADER_DESC& desc)
  400. {
  401. for(auto& entry : reflData.uniforms)
  402. {
  403. if ((entry.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0)
  404. continue;
  405. String ident = entry.ident.c_str();
  406. switch(entry.type)
  407. {
  408. case Xsc::Reflection::UniformType::UniformBuffer:
  409. desc.setParamBlockAttribs(entry.ident.c_str(), false, GPBU_STATIC);
  410. break;
  411. case Xsc::Reflection::UniformType::Buffer:
  412. {
  413. GpuParamObjectType objType = ReflTypeToTextureType((Xsc::Reflection::BufferType)entry.baseType);
  414. if(objType != GPOT_UNKNOWN)
  415. {
  416. if (entry.defaultValue == -1)
  417. desc.addParameter(ident, ident, objType);
  418. else
  419. desc.addParameter(ident, ident, objType, getBuiltinTexture(entry.defaultValue));
  420. }
  421. else
  422. {
  423. objType = ReflTypeToBufferType((Xsc::Reflection::BufferType)entry.baseType);
  424. desc.addParameter(ident, ident, objType);
  425. }
  426. }
  427. break;
  428. case Xsc::Reflection::UniformType::Sampler:
  429. {
  430. auto findIter = reflData.samplerStates.find(entry.ident);
  431. if (findIter == reflData.samplerStates.end())
  432. desc.addParameter(ident, ident, GPOT_SAMPLER2D);
  433. else
  434. {
  435. SPtr<SamplerState> defaultVal = parseSamplerState(findIter->second);
  436. desc.addParameter(ident, ident, GPOT_SAMPLER2D, defaultVal);
  437. }
  438. break;
  439. }
  440. case Xsc::Reflection::UniformType::Variable:
  441. {
  442. bool isBlockInternal = false;
  443. if(entry.uniformBlock != -1)
  444. {
  445. std::string blockName = reflData.constantBuffers[entry.uniformBlock].ident;
  446. for (auto& uniform : reflData.uniforms)
  447. {
  448. if (uniform.type == Xsc::Reflection::UniformType::UniformBuffer && uniform.ident == blockName)
  449. {
  450. isBlockInternal = (uniform.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0;
  451. break;
  452. }
  453. }
  454. }
  455. if (!isBlockInternal)
  456. {
  457. GpuParamDataType type = ReflTypeToDataType((Xsc::Reflection::DataType)entry.baseType);
  458. if ((entry.flags & Xsc::Reflection::Uniform::Flags::Color) != 0 &&
  459. (type == GPDT_FLOAT3 || type == GPDT_FLOAT4))
  460. {
  461. type = GPDT_COLOR;
  462. }
  463. if (entry.defaultValue == -1)
  464. desc.addParameter(ident, ident, type);
  465. else
  466. {
  467. const Xsc::Reflection::DefaultValue& defVal = reflData.defaultValues[entry.defaultValue];
  468. desc.addParameter(ident, ident, type, StringID::NONE, 1, 0, (UINT8*)defVal.matrix);
  469. }
  470. }
  471. }
  472. break;
  473. case Xsc::Reflection::UniformType::Struct: break;
  474. default: ;
  475. }
  476. }
  477. }
  478. String crossCompile(const String& hlsl, GpuProgramType type, bool vulkan, bool optionalEntry, UINT32& startBindingSlot,
  479. SHADER_DESC* shaderDesc = nullptr, Vector<GpuProgramType>* detectedTypes = nullptr)
  480. {
  481. SPtr<StringStream> input = bs_shared_ptr_new<StringStream>();
  482. if (vulkan)
  483. *input << "#define VULKAN 1" << std::endl;
  484. else
  485. *input << "#define OPENGL 1" << std::endl;
  486. *input << hlsl;
  487. Xsc::ShaderInput inputDesc;
  488. inputDesc.shaderVersion = Xsc::InputShaderVersion::HLSL5;
  489. inputDesc.sourceCode = input;
  490. inputDesc.extensions = Xsc::Extensions::LayoutAttribute;
  491. switch (type)
  492. {
  493. case GPT_VERTEX_PROGRAM:
  494. inputDesc.shaderTarget = Xsc::ShaderTarget::VertexShader;
  495. inputDesc.entryPoint = "vsmain";
  496. break;
  497. case GPT_GEOMETRY_PROGRAM:
  498. inputDesc.shaderTarget = Xsc::ShaderTarget::GeometryShader;
  499. inputDesc.entryPoint = "gsmain";
  500. break;
  501. case GPT_HULL_PROGRAM:
  502. inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationControlShader;
  503. inputDesc.entryPoint = "hsmain";
  504. break;
  505. case GPT_DOMAIN_PROGRAM:
  506. inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationEvaluationShader;
  507. inputDesc.entryPoint = "dsmain";
  508. break;
  509. case GPT_FRAGMENT_PROGRAM:
  510. inputDesc.shaderTarget = Xsc::ShaderTarget::FragmentShader;
  511. inputDesc.entryPoint = "fsmain";
  512. break;
  513. case GPT_COMPUTE_PROGRAM:
  514. inputDesc.shaderTarget = Xsc::ShaderTarget::ComputeShader;
  515. inputDesc.entryPoint = "csmain";
  516. break;
  517. }
  518. StringStream output;
  519. Xsc::ShaderOutput outputDesc;
  520. outputDesc.sourceCode = &output;
  521. outputDesc.options.autoBinding = vulkan;
  522. outputDesc.options.autoBindingStartSlot = startBindingSlot;
  523. outputDesc.options.separateShaders = true;
  524. outputDesc.options.separateSamplers = false;
  525. outputDesc.nameMangling.inputPrefix = "bs_";
  526. outputDesc.nameMangling.useAlwaysSemantics = true;
  527. outputDesc.nameMangling.renameBufferFields = true;
  528. if (vulkan)
  529. outputDesc.shaderVersion = Xsc::OutputShaderVersion::VKSL450;
  530. else
  531. outputDesc.shaderVersion = Xsc::OutputShaderVersion::GLSL450;
  532. XscLog log;
  533. Xsc::Reflection::ReflectionData reflectionData;
  534. bool compileSuccess = Xsc::CompileShader(inputDesc, outputDesc, &log, &reflectionData);
  535. if (!compileSuccess)
  536. {
  537. // If enabled, don't fail if entry point isn't found
  538. bool done = true;
  539. if(optionalEntry)
  540. {
  541. bool entryFound = false;
  542. for (auto& entry : reflectionData.functions)
  543. {
  544. if(entry.ident == inputDesc.entryPoint)
  545. {
  546. entryFound = true;
  547. break;
  548. }
  549. }
  550. if (!entryFound)
  551. done = false;
  552. }
  553. if (done)
  554. {
  555. StringStream logOutput;
  556. log.getMessages(logOutput);
  557. LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
  558. return "";
  559. }
  560. }
  561. for (auto& entry : reflectionData.constantBuffers)
  562. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  563. for (auto& entry : reflectionData.textures)
  564. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  565. for (auto& entry : reflectionData.storageBuffers)
  566. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  567. if(detectedTypes != nullptr)
  568. {
  569. for(auto& entry : reflectionData.functions)
  570. {
  571. if (entry.ident == "vsmain")
  572. detectedTypes->push_back(GPT_VERTEX_PROGRAM);
  573. else if (entry.ident == "fsmain")
  574. detectedTypes->push_back(GPT_FRAGMENT_PROGRAM);
  575. else if (entry.ident == "gsmain")
  576. detectedTypes->push_back(GPT_GEOMETRY_PROGRAM);
  577. else if (entry.ident == "dsmain")
  578. detectedTypes->push_back(GPT_DOMAIN_PROGRAM);
  579. else if (entry.ident == "hsmain")
  580. detectedTypes->push_back(GPT_HULL_PROGRAM);
  581. else if (entry.ident == "csmain")
  582. detectedTypes->push_back(GPT_COMPUTE_PROGRAM);
  583. }
  584. // If no entry points found, and error occurred, report error
  585. if(!compileSuccess && detectedTypes->size() == 0)
  586. {
  587. StringStream logOutput;
  588. log.getMessages(logOutput);
  589. LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
  590. return "";
  591. }
  592. }
  593. if (shaderDesc != nullptr)
  594. parseParameters(reflectionData, *shaderDesc);
  595. return output.str();
  596. }
  597. // Convert HLSL code to GLSL
  598. String HLSLtoGLSL(const String& hlsl, GpuProgramType type, bool vulkan, UINT32& startBindingSlot)
  599. {
  600. return crossCompile(hlsl, type, vulkan, false, startBindingSlot);
  601. }
  602. void reflectHLSL(const String& hlsl, SHADER_DESC& shaderDesc, Vector<GpuProgramType>& entryPoints)
  603. {
  604. UINT32 dummy = 0;
  605. crossCompile(hlsl, GPT_VERTEX_PROGRAM, false, true, dummy, &shaderDesc, &entryPoints);
  606. }
  607. BSLFXCompileResult BSLFXCompiler::compile(const String& name, const String& source,
  608. const UnorderedMap<String, String>& defines)
  609. {
  610. BSLFXCompileResult output;
  611. String parsedSource = source;
  612. ParseState* parseState = parseStateCreate();
  613. for(auto& define : defines)
  614. {
  615. if (define.first.size() == 0)
  616. continue;
  617. addDefine(parseState, define.first.c_str());
  618. if(define.second.size() > 0)
  619. addDefineExpr(parseState, define.second.c_str());
  620. }
  621. parseFX(parseState, parsedSource.c_str());
  622. if (parseState->hasError > 0)
  623. {
  624. output.errorMessage = parseState->errorMessage;
  625. output.errorLine = parseState->errorLine;
  626. output.errorColumn = parseState->errorColumn;
  627. if(parseState->errorFile != nullptr)
  628. output.errorFile = parseState->errorFile;
  629. parseStateDelete(parseState);
  630. }
  631. else
  632. {
  633. // Only enable for debug purposes
  634. //SLFXDebugPrint(parseState->rootNode, "");
  635. Vector<String> codeBlocks;
  636. CodeString* codeString = parseState->codeStrings;
  637. while(codeString != nullptr)
  638. {
  639. while ((INT32)codeBlocks.size() <= codeString->index)
  640. codeBlocks.push_back(String());
  641. codeBlocks[codeString->index] = String(codeString->code, codeString->size);
  642. codeString = codeString->next;
  643. }
  644. output = parseShader(name, parseState, codeBlocks);
  645. StringStream gpuProgError;
  646. bool hasError = false;
  647. if (output.shader != nullptr)
  648. {
  649. Vector<SPtr<Technique>> techniques = output.shader->getCompatibleTechniques();
  650. for (auto& technique : techniques)
  651. {
  652. UINT32 numPasses = technique->getNumPasses();
  653. for (UINT32 i = 0; i < numPasses; i++)
  654. {
  655. SPtr<Pass> pass = technique->getPass(i);
  656. auto checkCompileStatus = [&](const String& prefix, const SPtr<GpuProgram>& prog)
  657. {
  658. if (prog != nullptr)
  659. {
  660. prog->blockUntilCoreInitialized();
  661. if (!prog->isCompiled())
  662. {
  663. hasError = true;
  664. gpuProgError << prefix << ": " << prog->getCompileErrorMessage() << std::endl;
  665. }
  666. }
  667. };
  668. checkCompileStatus("Vertex program", pass->getVertexProgram());
  669. checkCompileStatus("Fragment program", pass->getFragmentProgram());
  670. checkCompileStatus("Geometry program", pass->getGeometryProgram());
  671. checkCompileStatus("Hull program", pass->getHullProgram());
  672. checkCompileStatus("Domain program", pass->getDomainProgram());
  673. checkCompileStatus("Compute program", pass->getComputeProgram());
  674. }
  675. }
  676. }
  677. if (hasError)
  678. {
  679. output.errorMessage = "Failed compiling GPU program(s): " + gpuProgError.str();
  680. output.errorLine = 0;
  681. output.errorColumn = 0;
  682. }
  683. }
  684. return output;
  685. }
  686. void BSLFXCompiler::parseFX(ParseState* parseState, const char* source)
  687. {
  688. yyscan_t scanner;
  689. YY_BUFFER_STATE state;
  690. if (yylex_init_extra(parseState, &scanner))
  691. return;
  692. // If debug output from lexer is needed uncomment this and add %debug option to lexer file
  693. //yyset_debug(true, scanner);
  694. // If debug output from parser is needed uncomment this and add %debug option to parser file
  695. //yydebug = true;
  696. state = yy_scan_string(source, scanner);
  697. if (yyparse(parseState, scanner))
  698. return;
  699. yy_delete_buffer(state, scanner);
  700. yylex_destroy(scanner);
  701. }
  702. BSLFXCompiler::TechniqueMetaData BSLFXCompiler::parseTechniqueMetaData(ASTFXNode* technique)
  703. {
  704. TechniqueMetaData metaData;
  705. metaData.renderer = RendererAny;
  706. metaData.language = "hlsl";
  707. metaData.isMixin = technique->type == NT_Mixin;
  708. for (int i = 0; i < technique->options->count; i++)
  709. {
  710. NodeOption* option = &technique->options->entries[i];
  711. switch (option->type)
  712. {
  713. case OT_Renderer:
  714. metaData.renderer = parseRenderer(removeQuotes(option->value.strValue));
  715. break;
  716. case OT_Tags:
  717. {
  718. ASTFXNode* tagsNode = option->value.nodePtr;
  719. for (int j = 0; j < tagsNode->options->count; j++)
  720. {
  721. NodeOption* tagOption = &tagsNode->options->entries[j];
  722. if (tagOption->type == OT_TagValue)
  723. metaData.tags.push_back(removeQuotes(tagOption->value.strValue));
  724. }
  725. }
  726. break;
  727. case OT_Identifier:
  728. metaData.name = option->value.strValue;
  729. break;
  730. case OT_Mixin:
  731. metaData.includes.push_back(option->value.strValue);
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. return metaData;
  738. }
  739. StringID BSLFXCompiler::parseRenderer(const String& name)
  740. {
  741. if (name == "Any")
  742. return RendererAny;
  743. else if (name == "Default")
  744. return RendererDefault;
  745. return RendererAny;
  746. }
  747. QueueSortType BSLFXCompiler::parseSortType(CullAndSortModeValue sortType)
  748. {
  749. switch (sortType)
  750. {
  751. case CASV_BackToFront:
  752. return QueueSortType::BackToFront;
  753. case CASV_FrontToBack:
  754. return QueueSortType::FrontToBack;
  755. case CASV_None:
  756. return QueueSortType::None;
  757. }
  758. return QueueSortType::None;
  759. }
  760. CompareFunction BSLFXCompiler::parseCompFunc(CompFuncValue compFunc)
  761. {
  762. switch (compFunc)
  763. {
  764. case CFV_Pass:
  765. return CMPF_ALWAYS_PASS;
  766. case CFV_Fail:
  767. return CMPF_ALWAYS_FAIL;
  768. case CFV_LT:
  769. return CMPF_LESS;
  770. case CFV_LTE:
  771. return CMPF_LESS_EQUAL;
  772. case CFV_EQ:
  773. return CMPF_EQUAL;
  774. case CFV_NEQ:
  775. return CMPF_NOT_EQUAL;
  776. case CFV_GT:
  777. return CMPF_GREATER;
  778. case CFV_GTE:
  779. return CMPF_GREATER_EQUAL;
  780. }
  781. return CMPF_ALWAYS_PASS;
  782. }
  783. BlendFactor BSLFXCompiler::parseBlendFactor(OpValue factor)
  784. {
  785. switch (factor)
  786. {
  787. case OV_One:
  788. return BF_ONE;
  789. case OV_Zero:
  790. return BF_ZERO;
  791. case OV_DestColor:
  792. return BF_DEST_COLOR;
  793. case OV_SrcColor:
  794. return BF_SOURCE_COLOR;
  795. case OV_InvDestColor:
  796. return BF_INV_DEST_COLOR;
  797. case OV_InvSrcColor:
  798. return BF_INV_SOURCE_COLOR;
  799. case OV_DestAlpha:
  800. return BF_DEST_ALPHA;
  801. case OV_SrcAlpha:
  802. return BF_SOURCE_ALPHA;
  803. case OV_InvDestAlpha:
  804. return BF_INV_DEST_ALPHA;
  805. case OV_InvSrcAlpha:
  806. return BF_INV_SOURCE_ALPHA;
  807. default:
  808. break;
  809. }
  810. return BF_ONE;
  811. }
  812. BlendOperation BSLFXCompiler::parseBlendOp(BlendOpValue op)
  813. {
  814. switch (op)
  815. {
  816. case BOV_Add:
  817. return BO_ADD;
  818. case BOV_Max:
  819. return BO_MAX;
  820. case BOV_Min:
  821. return BO_MIN;
  822. case BOV_Subtract:
  823. return BO_SUBTRACT;
  824. case BOV_RevSubtract:
  825. return BO_REVERSE_SUBTRACT;
  826. }
  827. return BO_ADD;
  828. }
  829. StencilOperation BSLFXCompiler::parseStencilOp(OpValue op)
  830. {
  831. switch (op)
  832. {
  833. case OV_Keep:
  834. return SOP_KEEP;
  835. case OV_Zero:
  836. return SOP_ZERO;
  837. case OV_Replace:
  838. return SOP_REPLACE;
  839. case OV_Incr:
  840. return SOP_INCREMENT;
  841. case OV_Decr:
  842. return SOP_DECREMENT;
  843. case OV_IncrWrap:
  844. return SOP_INCREMENT_WRAP;
  845. case OV_DecrWrap:
  846. return SOP_DECREMENT_WRAP;
  847. case OV_Invert:
  848. return SOP_INVERT;
  849. default:
  850. break;
  851. }
  852. return SOP_KEEP;
  853. }
  854. CullingMode BSLFXCompiler::parseCullMode(CullAndSortModeValue cm)
  855. {
  856. switch (cm)
  857. {
  858. case CASV_None:
  859. return CULL_NONE;
  860. case CASV_CW:
  861. return CULL_CLOCKWISE;
  862. case CASV_CCW:
  863. return CULL_COUNTERCLOCKWISE;
  864. }
  865. return CULL_COUNTERCLOCKWISE;
  866. }
  867. PolygonMode BSLFXCompiler::parseFillMode(FillModeValue fm)
  868. {
  869. if (fm == FMV_Wire)
  870. return PM_WIREFRAME;
  871. return PM_SOLID;
  872. }
  873. void BSLFXCompiler::parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  874. {
  875. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  876. return;
  877. for (int i = 0; i < stencilOpNode->options->count; i++)
  878. {
  879. NodeOption* option = &stencilOpNode->options->entries[i];
  880. switch (option->type)
  881. {
  882. case OT_Fail:
  883. desc.frontStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  884. break;
  885. case OT_ZFail:
  886. desc.frontStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  887. break;
  888. case OT_PassOp:
  889. desc.frontStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  890. break;
  891. case OT_CompareFunc:
  892. desc.frontStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  893. break;
  894. default:
  895. break;
  896. }
  897. }
  898. }
  899. void BSLFXCompiler::parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  900. {
  901. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  902. return;
  903. for (int i = 0; i < stencilOpNode->options->count; i++)
  904. {
  905. NodeOption* option = &stencilOpNode->options->entries[i];
  906. switch (option->type)
  907. {
  908. case OT_Fail:
  909. desc.backStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  910. break;
  911. case OT_ZFail:
  912. desc.backStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  913. break;
  914. case OT_PassOp:
  915. desc.backStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  916. break;
  917. case OT_CompareFunc:
  918. desc.backStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  919. break;
  920. default:
  921. break;
  922. }
  923. }
  924. }
  925. void BSLFXCompiler::parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  926. {
  927. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  928. return;
  929. for (int i = 0; i < blendDefNode->options->count; i++)
  930. {
  931. NodeOption* option = &blendDefNode->options->entries[i];
  932. switch (option->type)
  933. {
  934. case OT_Source:
  935. desc.srcBlend = parseBlendFactor((OpValue)option->value.intValue);
  936. break;
  937. case OT_Dest:
  938. desc.dstBlend = parseBlendFactor((OpValue)option->value.intValue);
  939. break;
  940. case OT_Op:
  941. desc.blendOp = parseBlendOp((BlendOpValue)option->value.intValue);
  942. break;
  943. default:
  944. break;
  945. }
  946. }
  947. }
  948. void BSLFXCompiler::parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  949. {
  950. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  951. return;
  952. for (int i = 0; i < blendDefNode->options->count; i++)
  953. {
  954. NodeOption* option = &blendDefNode->options->entries[i];
  955. switch (option->type)
  956. {
  957. case OT_Source:
  958. desc.srcBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  959. break;
  960. case OT_Dest:
  961. desc.dstBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  962. break;
  963. case OT_Op:
  964. desc.blendOpAlpha = parseBlendOp((BlendOpValue)option->value.intValue);
  965. break;
  966. default:
  967. break;
  968. }
  969. }
  970. }
  971. void BSLFXCompiler::parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode)
  972. {
  973. if (targetNode == nullptr || targetNode->type != NT_Target)
  974. return;
  975. UINT32 index = 0;
  976. for (int i = 0; i < targetNode->options->count; i++)
  977. {
  978. NodeOption* option = &targetNode->options->entries[i];
  979. switch (option->type)
  980. {
  981. case OT_Index:
  982. index = option->value.intValue;
  983. break;
  984. default:
  985. break;
  986. }
  987. }
  988. if (index >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  989. return;
  990. RENDER_TARGET_BLEND_STATE_DESC& rtDesc = desc.renderTargetDesc[index];
  991. for (int i = 0; i < targetNode->options->count; i++)
  992. {
  993. NodeOption* option = &targetNode->options->entries[i];
  994. switch (option->type)
  995. {
  996. case OT_Enabled:
  997. rtDesc.blendEnable = option->value.intValue > 0;
  998. break;
  999. case OT_Color:
  1000. parseColorBlendDef(rtDesc, option->value.nodePtr);
  1001. break;
  1002. case OT_Alpha:
  1003. parseAlphaBlendDef(rtDesc, option->value.nodePtr);
  1004. break;
  1005. case OT_WriteMask:
  1006. rtDesc.renderTargetWriteMask = option->value.intValue;
  1007. break;
  1008. default:
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. bool BSLFXCompiler::parseBlendState(PassData& desc, ASTFXNode* blendNode)
  1014. {
  1015. if (blendNode == nullptr || blendNode->type != NT_Blend)
  1016. return false;
  1017. bool isDefault = true;
  1018. for (int i = 0; i < blendNode->options->count; i++)
  1019. {
  1020. NodeOption* option = &blendNode->options->entries[i];
  1021. switch (option->type)
  1022. {
  1023. case OT_AlphaToCoverage:
  1024. desc.blendDesc.alphaToCoverageEnable = option->value.intValue > 0;
  1025. isDefault = false;
  1026. break;
  1027. case OT_IndependantBlend:
  1028. desc.blendDesc.independantBlendEnable = option->value.intValue > 0;
  1029. isDefault = false;
  1030. break;
  1031. case OT_Target:
  1032. parseRenderTargetBlendState(desc.blendDesc, option->value.nodePtr);
  1033. isDefault = false;
  1034. break;
  1035. default:
  1036. break;
  1037. }
  1038. }
  1039. return !isDefault;
  1040. }
  1041. bool BSLFXCompiler::parseRasterizerState(PassData& desc, ASTFXNode* rasterNode)
  1042. {
  1043. if (rasterNode == nullptr || rasterNode->type != NT_Raster)
  1044. return false;
  1045. bool isDefault = true;
  1046. for (int i = 0; i < rasterNode->options->count; i++)
  1047. {
  1048. NodeOption* option = &rasterNode->options->entries[i];
  1049. switch (option->type)
  1050. {
  1051. case OT_FillMode:
  1052. desc.rasterizerDesc.polygonMode = parseFillMode((FillModeValue)option->value.intValue);
  1053. isDefault = false;
  1054. break;
  1055. case OT_CullMode:
  1056. desc.rasterizerDesc.cullMode = parseCullMode((CullAndSortModeValue)option->value.intValue);
  1057. isDefault = false;
  1058. break;
  1059. case OT_DepthBias:
  1060. desc.rasterizerDesc.depthBias = option->value.floatValue;
  1061. isDefault = false;
  1062. break;
  1063. case OT_SDepthBias:
  1064. desc.rasterizerDesc.slopeScaledDepthBias = option->value.floatValue;
  1065. isDefault = false;
  1066. break;
  1067. case OT_DepthClip:
  1068. desc.rasterizerDesc.depthClipEnable = option->value.intValue > 0;
  1069. isDefault = false;
  1070. break;
  1071. case OT_Scissor:
  1072. desc.rasterizerDesc.scissorEnable = option->value.intValue > 0;
  1073. isDefault = false;
  1074. break;
  1075. case OT_Multisample:
  1076. desc.rasterizerDesc.multisampleEnable = option->value.intValue > 0;
  1077. isDefault = false;
  1078. break;
  1079. case OT_AALine:
  1080. desc.rasterizerDesc.antialiasedLineEnable = option->value.intValue > 0;
  1081. isDefault = false;
  1082. break;
  1083. default:
  1084. break;
  1085. }
  1086. }
  1087. return !isDefault;
  1088. }
  1089. bool BSLFXCompiler::parseDepthState(PassData& passData, ASTFXNode* depthNode)
  1090. {
  1091. if (depthNode == nullptr || depthNode->type != NT_Depth)
  1092. return false;
  1093. bool isDefault = true;
  1094. for (int i = 0; i < depthNode->options->count; i++)
  1095. {
  1096. NodeOption* option = &depthNode->options->entries[i];
  1097. switch (option->type)
  1098. {
  1099. case OT_DepthRead:
  1100. passData.depthStencilDesc.depthReadEnable = option->value.intValue > 0;
  1101. isDefault = false;
  1102. break;
  1103. case OT_DepthWrite:
  1104. passData.depthStencilDesc.depthWriteEnable = option->value.intValue > 0;
  1105. isDefault = false;
  1106. break;
  1107. case OT_CompareFunc:
  1108. passData.depthStencilDesc.depthComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  1109. isDefault = false;
  1110. break;
  1111. default:
  1112. break;
  1113. }
  1114. }
  1115. return !isDefault;
  1116. }
  1117. bool BSLFXCompiler::parseStencilState(PassData& passData, ASTFXNode* stencilNode)
  1118. {
  1119. if (stencilNode == nullptr || stencilNode->type != NT_Stencil)
  1120. return false;
  1121. bool isDefault = true;
  1122. for (int i = 0; i < stencilNode->options->count; i++)
  1123. {
  1124. NodeOption* option = &stencilNode->options->entries[i];
  1125. switch (option->type)
  1126. {
  1127. case OT_Enabled:
  1128. passData.depthStencilDesc.stencilEnable = option->value.intValue > 0;
  1129. isDefault = false;
  1130. break;
  1131. case OT_StencilReadMask:
  1132. passData.depthStencilDesc.stencilReadMask = (UINT8)option->value.intValue;
  1133. isDefault = false;
  1134. break;
  1135. case OT_StencilWriteMask:
  1136. passData.depthStencilDesc.stencilWriteMask = (UINT8)option->value.intValue;
  1137. isDefault = false;
  1138. break;
  1139. case OT_StencilOpFront:
  1140. parseStencilFront(passData.depthStencilDesc, option->value.nodePtr);
  1141. isDefault = false;
  1142. break;
  1143. case OT_StencilOpBack:
  1144. parseStencilBack(passData.depthStencilDesc, option->value.nodePtr);
  1145. isDefault = false;
  1146. break;
  1147. case OT_StencilRef:
  1148. passData.stencilRefValue = option->value.intValue;
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. }
  1154. return !isDefault;
  1155. }
  1156. void BSLFXCompiler::parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData)
  1157. {
  1158. if (codeNode == nullptr || (codeNode->type != NT_Code))
  1159. {
  1160. return;
  1161. }
  1162. UINT32 index = (UINT32)-1;
  1163. for (int j = 0; j < codeNode->options->count; j++)
  1164. {
  1165. if (codeNode->options->entries[j].type == OT_Index)
  1166. index = codeNode->options->entries[j].value.intValue;
  1167. }
  1168. if (index != (UINT32)-1 && index < (UINT32)codeBlocks.size())
  1169. {
  1170. switch (codeNode->type)
  1171. {
  1172. case NT_Code:
  1173. passData.code += codeBlocks[index];
  1174. break;
  1175. default:
  1176. break;
  1177. }
  1178. }
  1179. }
  1180. void BSLFXCompiler::parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData)
  1181. {
  1182. if (passNode == nullptr || passNode->type != NT_Pass)
  1183. return;
  1184. for (int i = 0; i < passNode->options->count; i++)
  1185. {
  1186. NodeOption* option = &passNode->options->entries[i];
  1187. switch (option->type)
  1188. {
  1189. case OT_Blend:
  1190. passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
  1191. break;
  1192. case OT_Raster:
  1193. passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
  1194. break;
  1195. case OT_Depth:
  1196. passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
  1197. break;
  1198. case OT_Stencil:
  1199. passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
  1200. break;
  1201. case OT_Code:
  1202. parseCodeBlock(option->value.nodePtr, codeBlocks, passData);
  1203. break;
  1204. default:
  1205. break;
  1206. }
  1207. }
  1208. }
  1209. void BSLFXCompiler::parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData)
  1210. {
  1211. if (techniqueNode == nullptr || (techniqueNode->type != NT_Technique && techniqueNode->type != NT_Mixin))
  1212. return;
  1213. // There must always be at least one pass
  1214. if(techniqueData.passes.empty())
  1215. {
  1216. techniqueData.passes.push_back(PassData());
  1217. techniqueData.passes.back().seqIdx = 0;
  1218. }
  1219. PassData combinedCommonPassData;
  1220. UINT32 nextPassIdx = 0;
  1221. // Go in reverse because options are added in reverse order during parsing
  1222. for (int i = techniqueNode->options->count - 1; i >= 0; i--)
  1223. {
  1224. NodeOption* option = &techniqueNode->options->entries[i];
  1225. switch (option->type)
  1226. {
  1227. case OT_Pass:
  1228. {
  1229. UINT32 passIdx = nextPassIdx;
  1230. PassData* passData = nullptr;
  1231. for (auto& entry : techniqueData.passes)
  1232. {
  1233. if (entry.seqIdx == passIdx)
  1234. passData = &entry;
  1235. }
  1236. if (passData == nullptr)
  1237. {
  1238. techniqueData.passes.push_back(PassData());
  1239. passData = &techniqueData.passes.back();
  1240. passData->seqIdx = passIdx;
  1241. }
  1242. nextPassIdx = std::max(nextPassIdx, passIdx) + 1;
  1243. passData->code = combinedCommonPassData.code + passData->code;
  1244. ASTFXNode* passNode = option->value.nodePtr;
  1245. parsePass(passNode, codeBlocks, *passData);
  1246. }
  1247. break;
  1248. case OT_Code:
  1249. {
  1250. PassData commonPassData;
  1251. parseCodeBlock(option->value.nodePtr, codeBlocks, commonPassData);
  1252. for (auto& passData : techniqueData.passes)
  1253. passData.code += commonPassData.code;
  1254. combinedCommonPassData.code += commonPassData.code;
  1255. }
  1256. break;
  1257. default:
  1258. break;
  1259. }
  1260. }
  1261. // Parse common pass states
  1262. for (int i = 0; i < techniqueNode->options->count; i++)
  1263. {
  1264. NodeOption* option = &techniqueNode->options->entries[i];
  1265. switch (option->type)
  1266. {
  1267. case OT_Blend:
  1268. for (auto& passData : techniqueData.passes)
  1269. passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
  1270. break;
  1271. case OT_Raster:
  1272. for (auto& passData : techniqueData.passes)
  1273. passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
  1274. break;
  1275. case OT_Depth:
  1276. for (auto& passData : techniqueData.passes)
  1277. passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
  1278. break;
  1279. case OT_Stencil:
  1280. for (auto& passData : techniqueData.passes)
  1281. passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
  1282. break;
  1283. default:
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. void BSLFXCompiler::parseOptions(ASTFXNode* optionsNode, SHADER_DESC& shaderDesc)
  1289. {
  1290. if (optionsNode == nullptr || optionsNode->type != NT_Options)
  1291. return;
  1292. for (int i = optionsNode->options->count - 1; i >= 0; i--)
  1293. {
  1294. NodeOption* option = &optionsNode->options->entries[i];
  1295. switch (option->type)
  1296. {
  1297. case OT_Separable:
  1298. shaderDesc.separablePasses = option->value.intValue > 1;
  1299. break;
  1300. case OT_Sort:
  1301. shaderDesc.queueSortType = parseSortType((CullAndSortModeValue)option->value.intValue);
  1302. break;
  1303. case OT_Priority:
  1304. shaderDesc.queuePriority = option->value.intValue;
  1305. break;
  1306. case OT_Transparent:
  1307. shaderDesc.flags |= (UINT32)ShaderFlags::Transparent;
  1308. break;
  1309. default:
  1310. break;
  1311. }
  1312. }
  1313. }
  1314. BSLFXCompileResult BSLFXCompiler::parseShader(const String& name, ParseState* parseState, Vector<String>& codeBlocks)
  1315. {
  1316. BSLFXCompileResult output;
  1317. if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
  1318. {
  1319. parseStateDelete(parseState);
  1320. output.errorMessage = "Root not is null or not a shader.";
  1321. return output;
  1322. }
  1323. SHADER_DESC shaderDesc;
  1324. Vector<pair<ASTFXNode*, TechniqueData>> techniqueData;
  1325. // Go in reverse because options are added in reverse order during parsing
  1326. for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
  1327. {
  1328. NodeOption* option = &parseState->rootNode->options->entries[i];
  1329. switch (option->type)
  1330. {
  1331. case OT_Options:
  1332. parseOptions(option->value.nodePtr, shaderDesc);
  1333. break;
  1334. case OT_Technique:
  1335. {
  1336. // We initially parse only meta-data, so we can handle out-of-order technique definitions
  1337. TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
  1338. techniqueData.push_back(std::make_pair(option->value.nodePtr, TechniqueData()));
  1339. TechniqueData& data = techniqueData.back().second;
  1340. data.metaData = metaData;
  1341. break;
  1342. }
  1343. default:
  1344. break;
  1345. }
  1346. }
  1347. bool* techniqueWasParsed = bs_stack_alloc<bool>((UINT32)techniqueData.size());
  1348. std::function<bool(const TechniqueMetaData&, TechniqueData&)> parseInherited =
  1349. [&](const TechniqueMetaData& metaData, TechniqueData& outTechnique)
  1350. {
  1351. for (auto riter = metaData.includes.rbegin(); riter != metaData.includes.rend(); ++riter)
  1352. {
  1353. const String& includes = *riter;
  1354. UINT32 baseIdx = -1;
  1355. for(UINT32 i = 0; i < (UINT32)techniqueData.size(); i++)
  1356. {
  1357. auto& entry = techniqueData[i];
  1358. if (!entry.second.metaData.isMixin)
  1359. continue;
  1360. if (entry.second.metaData.name == includes)
  1361. {
  1362. bool matches = entry.second.metaData.language == metaData.language || entry.second.metaData.language == "Any";
  1363. matches &= entry.second.metaData.renderer == metaData.renderer || entry.second.metaData.renderer == RendererAny;
  1364. // We want the last matching technique, in order to allow techniques to override each other
  1365. if (matches)
  1366. baseIdx = i;
  1367. }
  1368. }
  1369. if (baseIdx != -1)
  1370. {
  1371. auto& entry = techniqueData[baseIdx];
  1372. // Was already parsed previously, don't parse it multiple times (happens when multiple techniques
  1373. // include the same mixin)
  1374. if (techniqueWasParsed[baseIdx])
  1375. continue;
  1376. if (!parseInherited(entry.second.metaData, outTechnique))
  1377. return false;
  1378. parseTechnique(entry.first, codeBlocks, outTechnique);
  1379. techniqueWasParsed[baseIdx] = true;
  1380. }
  1381. else
  1382. {
  1383. output.errorMessage = "Mixin \"" + includes + "\" cannot be found.";
  1384. return false;
  1385. }
  1386. }
  1387. return true;
  1388. };
  1389. // Actually parse techniques
  1390. for (auto& entry : techniqueData)
  1391. {
  1392. const TechniqueMetaData& metaData = entry.second.metaData;
  1393. if (metaData.isMixin)
  1394. continue;
  1395. bs_zero_out(techniqueWasParsed, techniqueData.size());
  1396. if (!parseInherited(metaData, entry.second))
  1397. {
  1398. parseStateDelete(parseState);
  1399. return output;
  1400. }
  1401. parseTechnique(entry.first, codeBlocks, entry.second);
  1402. }
  1403. bs_stack_free(techniqueWasParsed);
  1404. // Parse extended HLSL code and generate per-program code, also convert to GLSL/VKSL
  1405. UINT32 end = (UINT32)techniqueData.size();
  1406. for(UINT32 i = 0; i < end; i++)
  1407. {
  1408. const TechniqueMetaData& metaData = techniqueData[i].second.metaData;
  1409. if (metaData.isMixin)
  1410. continue;
  1411. TechniqueData& hlslTechnique = techniqueData[i].second;
  1412. TechniqueData glslTechnique = techniqueData[i].second;
  1413. glslTechnique.metaData.language = "glsl";
  1414. TechniqueData vkslTechnique = techniqueData[i].second;
  1415. vkslTechnique.metaData.language = "vksl";
  1416. UINT32 numPasses = (UINT32)hlslTechnique.passes.size();
  1417. for(UINT32 j = 0; j < numPasses; j++)
  1418. {
  1419. PassData& hlslPassData = hlslTechnique.passes[j];
  1420. PassData& glslPassData = glslTechnique.passes[j];
  1421. PassData& vkslPassData = vkslTechnique.passes[j];
  1422. // Clean non-standard HLSL
  1423. static const std::regex regex("\\[\\s*layout\\s*\\(.*\\)\\s*\\]|\\[\\s*internal\\s*\\]|\\[\\s*color\\s*\\]");
  1424. hlslPassData.code = regex_replace(hlslPassData.code, regex, "");
  1425. // Find valid entry points and parameters
  1426. // Note: XShaderCompiler needs to do a full pass when doing reflection, and for each individual program
  1427. // type. If performance is ever important here it could be good to update XShaderCompiler so it can
  1428. // somehow save the AST and then re-use it for multiple actions.
  1429. Vector<GpuProgramType> types;
  1430. reflectHLSL(glslPassData.code, shaderDesc, types);
  1431. UINT32 glslBinding = 0;
  1432. UINT32 vkslBinding = 0;
  1433. // Cross-compile for all detected shader types
  1434. // Note: I'm just copying HLSL code as-is. This code will contain all entry points which could have
  1435. // an effect on compile time. It would be ideal to remove dead code depending on program type. This would
  1436. // involve adding a HLSL code generator to XShaderCompiler.
  1437. for(auto& type : types)
  1438. {
  1439. switch(type)
  1440. {
  1441. case GPT_VERTEX_PROGRAM:
  1442. hlslPassData.vertexCode = hlslPassData.code;
  1443. glslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM, false, glslBinding);
  1444. vkslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM, true, vkslBinding);
  1445. break;
  1446. case GPT_FRAGMENT_PROGRAM:
  1447. hlslPassData.fragmentCode = hlslPassData.code;
  1448. glslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM, false, glslBinding);
  1449. vkslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM, true, vkslBinding);
  1450. break;
  1451. case GPT_GEOMETRY_PROGRAM:
  1452. hlslPassData.geometryCode = hlslPassData.code;
  1453. glslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM, false, glslBinding);
  1454. vkslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM, true, vkslBinding);
  1455. break;
  1456. case GPT_HULL_PROGRAM:
  1457. hlslPassData.hullCode = hlslPassData.code;
  1458. glslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM, false, glslBinding);
  1459. vkslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM, true, vkslBinding);
  1460. break;
  1461. case GPT_DOMAIN_PROGRAM:
  1462. hlslPassData.domainCode = hlslPassData.code;
  1463. glslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM, false, glslBinding);
  1464. vkslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM, true, vkslBinding);
  1465. break;
  1466. case GPT_COMPUTE_PROGRAM:
  1467. hlslPassData.computeCode = hlslPassData.code;
  1468. glslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM, false, glslBinding);
  1469. vkslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM, true, vkslBinding);
  1470. break;
  1471. }
  1472. }
  1473. }
  1474. techniqueData.push_back(std::make_pair(techniqueData[i].first, glslTechnique));
  1475. techniqueData.push_back(std::make_pair(techniqueData[i].first, vkslTechnique));
  1476. }
  1477. Vector<SPtr<Technique>> techniques;
  1478. for(auto& entry : techniqueData)
  1479. {
  1480. const TechniqueMetaData& metaData = entry.second.metaData;
  1481. if (metaData.isMixin)
  1482. continue;
  1483. Map<UINT32, SPtr<Pass>, std::greater<UINT32>> passes;
  1484. for (auto& passData : entry.second.passes)
  1485. {
  1486. PASS_DESC passDesc;
  1487. if (!passData.blendIsDefault)
  1488. passDesc.blendState = BlendState::create(passData.blendDesc);
  1489. if (!passData.rasterizerIsDefault)
  1490. passDesc.rasterizerState = RasterizerState::create(passData.rasterizerDesc);
  1491. if (!passData.depthStencilIsDefault)
  1492. passDesc.depthStencilState = DepthStencilState::create(passData.depthStencilDesc);
  1493. GPU_PROGRAM_DESC desc;
  1494. desc.language = metaData.language;
  1495. if (!passData.vertexCode.empty())
  1496. {
  1497. desc.entryPoint = "vsmain";
  1498. desc.source = passData.vertexCode;
  1499. desc.type = GPT_VERTEX_PROGRAM;
  1500. passDesc.vertexProgram = GpuProgram::create(desc);
  1501. }
  1502. if (!passData.fragmentCode.empty())
  1503. {
  1504. desc.entryPoint = "fsmain";
  1505. desc.source = passData.fragmentCode;
  1506. desc.type = GPT_FRAGMENT_PROGRAM;
  1507. passDesc.fragmentProgram = GpuProgram::create(desc);
  1508. }
  1509. if (!passData.geometryCode.empty())
  1510. {
  1511. desc.entryPoint = "gsmain";
  1512. desc.source = passData.geometryCode;
  1513. desc.type = GPT_GEOMETRY_PROGRAM;
  1514. passDesc.geometryProgram = GpuProgram::create(desc);
  1515. }
  1516. if (!passData.hullCode.empty())
  1517. {
  1518. desc.entryPoint = "hsmain";
  1519. desc.source = passData.hullCode;
  1520. desc.type = GPT_HULL_PROGRAM;
  1521. passDesc.hullProgram = GpuProgram::create(desc);
  1522. }
  1523. if (!passData.domainCode.empty())
  1524. {
  1525. desc.entryPoint = "dsmain";
  1526. desc.source = passData.domainCode;
  1527. desc.type = GPT_DOMAIN_PROGRAM;
  1528. passDesc.domainProgram = GpuProgram::create(desc);
  1529. }
  1530. if (!passData.computeCode.empty())
  1531. {
  1532. desc.entryPoint = "csmain";
  1533. desc.source = passData.computeCode;
  1534. desc.type = GPT_COMPUTE_PROGRAM;
  1535. passDesc.computeProgram = GpuProgram::create(desc);
  1536. }
  1537. passDesc.stencilRefValue = passData.stencilRefValue;
  1538. SPtr<Pass> pass = Pass::create(passDesc);
  1539. if (pass != nullptr)
  1540. passes[passData.seqIdx] = pass;
  1541. }
  1542. Vector<SPtr<Pass>> orderedPasses;
  1543. for (auto& KVP : passes)
  1544. orderedPasses.push_back(KVP.second);
  1545. if (orderedPasses.size() > 0)
  1546. {
  1547. SPtr<Technique> technique = Technique::create(metaData.language, metaData.renderer, metaData.tags,
  1548. orderedPasses);
  1549. techniques.push_back(technique);
  1550. }
  1551. }
  1552. Vector<String> includes;
  1553. IncludeLink* includeLink = parseState->includes;
  1554. while(includeLink != nullptr)
  1555. {
  1556. String includeFilename = includeLink->data->filename;
  1557. auto iterFind = std::find(includes.begin(), includes.end(), includeFilename);
  1558. if (iterFind == includes.end())
  1559. includes.push_back(includeFilename);
  1560. includeLink = includeLink->next;
  1561. }
  1562. parseStateDelete(parseState);
  1563. output.shader = Shader::_createPtr(name, shaderDesc, techniques);
  1564. output.shader->setIncludeFiles(includes);
  1565. return output;
  1566. }
  1567. String BSLFXCompiler::removeQuotes(const char* input)
  1568. {
  1569. UINT32 len = (UINT32)strlen(input);
  1570. String output(len - 2, ' ');
  1571. for (UINT32 i = 0; i < (len - 2); i++)
  1572. output[i] = input[i + 1];
  1573. return output;
  1574. }
  1575. }