BsSLFXCompiler.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsSLFXCompiler.h"
  4. #include "RenderAPI/BsGpuProgram.h"
  5. #include <regex>
  6. #include "Material/BsShader.h"
  7. #include "Material/BsTechnique.h"
  8. #include "Material/BsPass.h"
  9. #include "RenderAPI/BsSamplerState.h"
  10. #include "RenderAPI/BsRenderAPI.h"
  11. #include "Debug/BsDebug.h"
  12. #include "Material/BsShaderManager.h"
  13. #include "Material/BsShaderInclude.h"
  14. #include "Math/BsMatrix4.h"
  15. #include "Resources/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. case Xsc::Reflection::Filter::ComparisonMinMagMipPoint:
  306. desc.minFilter = FO_POINT;
  307. desc.magFilter = FO_POINT;
  308. desc.mipFilter = FO_POINT;
  309. break;
  310. case Xsc::Reflection::Filter::MinMagPointMipLinear:
  311. case Xsc::Reflection::Filter::ComparisonMinMagPointMipLinear:
  312. desc.minFilter = FO_POINT;
  313. desc.magFilter = FO_POINT;
  314. desc.mipFilter = FO_LINEAR;
  315. break;
  316. case Xsc::Reflection::Filter::MinPointMagLinearMipPoint:
  317. case Xsc::Reflection::Filter::ComparisonMinPointMagLinearMipPoint:
  318. desc.minFilter = FO_POINT;
  319. desc.magFilter = FO_LINEAR;
  320. desc.mipFilter = FO_POINT;
  321. break;
  322. case Xsc::Reflection::Filter::MinPointMagMipLinear:
  323. case Xsc::Reflection::Filter::ComparisonMinPointMagMipLinear:
  324. desc.minFilter = FO_POINT;
  325. desc.magFilter = FO_LINEAR;
  326. desc.mipFilter = FO_LINEAR;
  327. break;
  328. case Xsc::Reflection::Filter::MinLinearMagMipPoint:
  329. case Xsc::Reflection::Filter::ComparisonMinLinearMagMipPoint:
  330. desc.minFilter = FO_LINEAR;
  331. desc.magFilter = FO_POINT;
  332. desc.mipFilter = FO_POINT;
  333. break;
  334. case Xsc::Reflection::Filter::MinLinearMagPointMipLinear:
  335. case Xsc::Reflection::Filter::ComparisonMinLinearMagPointMipLinear:
  336. desc.minFilter = FO_LINEAR;
  337. desc.magFilter = FO_POINT;
  338. desc.mipFilter = FO_LINEAR;
  339. break;
  340. case Xsc::Reflection::Filter::MinMagLinearMipPoint:
  341. case Xsc::Reflection::Filter::ComparisonMinMagLinearMipPoint:
  342. desc.minFilter = FO_LINEAR;
  343. desc.magFilter = FO_LINEAR;
  344. desc.mipFilter = FO_POINT;
  345. break;
  346. case Xsc::Reflection::Filter::MinMagMipLinear:
  347. case Xsc::Reflection::Filter::ComparisonMinMagMipLinear:
  348. desc.minFilter = FO_LINEAR;
  349. desc.magFilter = FO_LINEAR;
  350. desc.mipFilter = FO_LINEAR;
  351. break;
  352. case Xsc::Reflection::Filter::Anisotropic:
  353. case Xsc::Reflection::Filter::ComparisonAnisotropic:
  354. desc.minFilter = FO_ANISOTROPIC;
  355. desc.magFilter = FO_ANISOTROPIC;
  356. desc.minFilter = FO_ANISOTROPIC;
  357. break;
  358. default:
  359. break;
  360. }
  361. return SamplerState::create(desc);
  362. }
  363. void parseParameters(const Xsc::Reflection::ReflectionData& reflData, SHADER_DESC& desc)
  364. {
  365. for(auto& entry : reflData.uniforms)
  366. {
  367. if ((entry.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0)
  368. continue;
  369. String ident = entry.ident.c_str();
  370. switch(entry.type)
  371. {
  372. case Xsc::Reflection::UniformType::UniformBuffer:
  373. desc.setParamBlockAttribs(entry.ident.c_str(), false, GPBU_STATIC);
  374. break;
  375. case Xsc::Reflection::UniformType::Buffer:
  376. {
  377. GpuParamObjectType objType = ReflTypeToTextureType((Xsc::Reflection::BufferType)entry.baseType);
  378. if(objType != GPOT_UNKNOWN)
  379. {
  380. if (entry.defaultValue == -1)
  381. desc.addParameter(ident, ident, objType);
  382. else
  383. desc.addParameter(ident, ident, objType, getBuiltinTexture(entry.defaultValue));
  384. }
  385. else
  386. {
  387. objType = ReflTypeToBufferType((Xsc::Reflection::BufferType)entry.baseType);
  388. desc.addParameter(ident, ident, objType);
  389. }
  390. }
  391. break;
  392. case Xsc::Reflection::UniformType::Sampler:
  393. {
  394. auto findIter = reflData.samplerStates.find(entry.ident);
  395. if (findIter != reflData.samplerStates.end())
  396. {
  397. String alias = findIter->second.alias.c_str();
  398. if(findIter->second.isNonDefault)
  399. {
  400. SPtr<SamplerState> defaultVal = parseSamplerState(findIter->second);
  401. desc.addParameter(ident, ident, GPOT_SAMPLER2D, defaultVal);
  402. if (!alias.empty())
  403. desc.addParameter(ident, alias, GPOT_SAMPLER2D, defaultVal);
  404. }
  405. else
  406. {
  407. desc.addParameter(ident, ident, GPOT_SAMPLER2D);
  408. if (!alias.empty())
  409. desc.addParameter(ident, alias, GPOT_SAMPLER2D);
  410. }
  411. }
  412. else
  413. {
  414. desc.addParameter(ident, ident, GPOT_SAMPLER2D);
  415. }
  416. break;
  417. }
  418. case Xsc::Reflection::UniformType::Variable:
  419. {
  420. bool isBlockInternal = false;
  421. if(entry.uniformBlock != -1)
  422. {
  423. std::string blockName = reflData.constantBuffers[entry.uniformBlock].ident;
  424. for (auto& uniform : reflData.uniforms)
  425. {
  426. if (uniform.type == Xsc::Reflection::UniformType::UniformBuffer && uniform.ident == blockName)
  427. {
  428. isBlockInternal = (uniform.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0;
  429. break;
  430. }
  431. }
  432. }
  433. if (!isBlockInternal)
  434. {
  435. GpuParamDataType type = ReflTypeToDataType((Xsc::Reflection::DataType)entry.baseType);
  436. if ((entry.flags & Xsc::Reflection::Uniform::Flags::Color) != 0 &&
  437. (type == GPDT_FLOAT3 || type == GPDT_FLOAT4))
  438. {
  439. type = GPDT_COLOR;
  440. }
  441. if (entry.defaultValue == -1)
  442. desc.addParameter(ident, ident, type);
  443. else
  444. {
  445. const Xsc::Reflection::DefaultValue& defVal = reflData.defaultValues[entry.defaultValue];
  446. desc.addParameter(ident, ident, type, StringID::NONE, 1, 0, (UINT8*)defVal.matrix);
  447. }
  448. }
  449. }
  450. break;
  451. case Xsc::Reflection::UniformType::Struct: break;
  452. default: ;
  453. }
  454. }
  455. }
  456. String crossCompile(const String& hlsl, GpuProgramType type, bool vulkan, bool optionalEntry, UINT32& startBindingSlot,
  457. SHADER_DESC* shaderDesc = nullptr, Vector<GpuProgramType>* detectedTypes = nullptr)
  458. {
  459. SPtr<StringStream> input = bs_shared_ptr_new<StringStream>();
  460. if (vulkan)
  461. *input << "#define VULKAN 1" << std::endl;
  462. else
  463. *input << "#define OPENGL 1" << std::endl;
  464. *input << hlsl;
  465. Xsc::ShaderInput inputDesc;
  466. inputDesc.shaderVersion = Xsc::InputShaderVersion::HLSL5;
  467. inputDesc.sourceCode = input;
  468. inputDesc.extensions = Xsc::Extensions::LayoutAttribute;
  469. switch (type)
  470. {
  471. case GPT_VERTEX_PROGRAM:
  472. inputDesc.shaderTarget = Xsc::ShaderTarget::VertexShader;
  473. inputDesc.entryPoint = "vsmain";
  474. break;
  475. case GPT_GEOMETRY_PROGRAM:
  476. inputDesc.shaderTarget = Xsc::ShaderTarget::GeometryShader;
  477. inputDesc.entryPoint = "gsmain";
  478. break;
  479. case GPT_HULL_PROGRAM:
  480. inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationControlShader;
  481. inputDesc.entryPoint = "hsmain";
  482. break;
  483. case GPT_DOMAIN_PROGRAM:
  484. inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationEvaluationShader;
  485. inputDesc.entryPoint = "dsmain";
  486. break;
  487. case GPT_FRAGMENT_PROGRAM:
  488. inputDesc.shaderTarget = Xsc::ShaderTarget::FragmentShader;
  489. inputDesc.entryPoint = "fsmain";
  490. break;
  491. case GPT_COMPUTE_PROGRAM:
  492. inputDesc.shaderTarget = Xsc::ShaderTarget::ComputeShader;
  493. inputDesc.entryPoint = "csmain";
  494. break;
  495. }
  496. StringStream output;
  497. Xsc::ShaderOutput outputDesc;
  498. outputDesc.sourceCode = &output;
  499. outputDesc.options.autoBinding = vulkan;
  500. outputDesc.options.autoBindingStartSlot = startBindingSlot;
  501. outputDesc.options.separateShaders = true;
  502. outputDesc.options.separateSamplers = false;
  503. outputDesc.nameMangling.inputPrefix = "bs_";
  504. outputDesc.nameMangling.useAlwaysSemantics = true;
  505. outputDesc.nameMangling.renameBufferFields = true;
  506. if (vulkan)
  507. outputDesc.shaderVersion = Xsc::OutputShaderVersion::VKSL450;
  508. else
  509. outputDesc.shaderVersion = Xsc::OutputShaderVersion::GLSL450;
  510. XscLog log;
  511. Xsc::Reflection::ReflectionData reflectionData;
  512. bool compileSuccess = Xsc::CompileShader(inputDesc, outputDesc, &log, &reflectionData);
  513. if (!compileSuccess)
  514. {
  515. // If enabled, don't fail if entry point isn't found
  516. bool done = true;
  517. if(optionalEntry)
  518. {
  519. bool entryFound = false;
  520. for (auto& entry : reflectionData.functions)
  521. {
  522. if(entry.ident == inputDesc.entryPoint)
  523. {
  524. entryFound = true;
  525. break;
  526. }
  527. }
  528. if (!entryFound)
  529. done = false;
  530. }
  531. if (done)
  532. {
  533. StringStream logOutput;
  534. log.getMessages(logOutput);
  535. LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
  536. return "";
  537. }
  538. }
  539. for (auto& entry : reflectionData.constantBuffers)
  540. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  541. for (auto& entry : reflectionData.textures)
  542. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  543. for (auto& entry : reflectionData.storageBuffers)
  544. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  545. if(detectedTypes != nullptr)
  546. {
  547. for(auto& entry : reflectionData.functions)
  548. {
  549. if (entry.ident == "vsmain")
  550. detectedTypes->push_back(GPT_VERTEX_PROGRAM);
  551. else if (entry.ident == "fsmain")
  552. detectedTypes->push_back(GPT_FRAGMENT_PROGRAM);
  553. else if (entry.ident == "gsmain")
  554. detectedTypes->push_back(GPT_GEOMETRY_PROGRAM);
  555. else if (entry.ident == "dsmain")
  556. detectedTypes->push_back(GPT_DOMAIN_PROGRAM);
  557. else if (entry.ident == "hsmain")
  558. detectedTypes->push_back(GPT_HULL_PROGRAM);
  559. else if (entry.ident == "csmain")
  560. detectedTypes->push_back(GPT_COMPUTE_PROGRAM);
  561. }
  562. // If no entry points found, and error occurred, report error
  563. if(!compileSuccess && detectedTypes->size() == 0)
  564. {
  565. StringStream logOutput;
  566. log.getMessages(logOutput);
  567. LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
  568. return "";
  569. }
  570. }
  571. if (shaderDesc != nullptr)
  572. parseParameters(reflectionData, *shaderDesc);
  573. return output.str();
  574. }
  575. // Convert HLSL code to GLSL
  576. String HLSLtoGLSL(const String& hlsl, GpuProgramType type, bool vulkan, UINT32& startBindingSlot)
  577. {
  578. return crossCompile(hlsl, type, vulkan, false, startBindingSlot);
  579. }
  580. void reflectHLSL(const String& hlsl, SHADER_DESC& shaderDesc, Vector<GpuProgramType>& entryPoints)
  581. {
  582. UINT32 dummy = 0;
  583. crossCompile(hlsl, GPT_VERTEX_PROGRAM, false, true, dummy, &shaderDesc, &entryPoints);
  584. }
  585. BSLFXCompileResult BSLFXCompiler::compile(const String& name, const String& source,
  586. const UnorderedMap<String, String>& defines)
  587. {
  588. BSLFXCompileResult output;
  589. String parsedSource = source;
  590. ParseState* parseState = parseStateCreate();
  591. for(auto& define : defines)
  592. {
  593. if (define.first.size() == 0)
  594. continue;
  595. addDefine(parseState, define.first.c_str());
  596. if(define.second.size() > 0)
  597. addDefineExpr(parseState, define.second.c_str());
  598. }
  599. parseFX(parseState, parsedSource.c_str());
  600. if (parseState->hasError > 0)
  601. {
  602. output.errorMessage = parseState->errorMessage;
  603. output.errorLine = parseState->errorLine;
  604. output.errorColumn = parseState->errorColumn;
  605. if(parseState->errorFile != nullptr)
  606. output.errorFile = parseState->errorFile;
  607. parseStateDelete(parseState);
  608. }
  609. else
  610. {
  611. // Only enable for debug purposes
  612. //SLFXDebugPrint(parseState->rootNode, "");
  613. Vector<String> codeBlocks;
  614. CodeString* codeString = parseState->codeStrings;
  615. while(codeString != nullptr)
  616. {
  617. while ((INT32)codeBlocks.size() <= codeString->index)
  618. codeBlocks.push_back(String());
  619. codeBlocks[codeString->index] = String(codeString->code, codeString->size);
  620. codeString = codeString->next;
  621. }
  622. output = parseShader(name, parseState, codeBlocks);
  623. StringStream gpuProgError;
  624. bool hasError = false;
  625. if (output.shader != nullptr)
  626. {
  627. Vector<SPtr<Technique>> techniques = output.shader->getCompatibleTechniques();
  628. for (auto& technique : techniques)
  629. {
  630. UINT32 numPasses = technique->getNumPasses();
  631. for (UINT32 i = 0; i < numPasses; i++)
  632. {
  633. SPtr<Pass> pass = technique->getPass(i);
  634. auto checkCompileStatus = [&](const String& prefix, const SPtr<GpuProgram>& prog)
  635. {
  636. if (prog != nullptr)
  637. {
  638. prog->blockUntilCoreInitialized();
  639. if (!prog->isCompiled())
  640. {
  641. hasError = true;
  642. gpuProgError << prefix << ": " << prog->getCompileErrorMessage() << std::endl;
  643. }
  644. }
  645. };
  646. checkCompileStatus("Vertex program", pass->getVertexProgram());
  647. checkCompileStatus("Fragment program", pass->getFragmentProgram());
  648. checkCompileStatus("Geometry program", pass->getGeometryProgram());
  649. checkCompileStatus("Hull program", pass->getHullProgram());
  650. checkCompileStatus("Domain program", pass->getDomainProgram());
  651. checkCompileStatus("Compute program", pass->getComputeProgram());
  652. }
  653. }
  654. }
  655. if (hasError)
  656. {
  657. output.errorMessage = "Failed compiling GPU program(s): " + gpuProgError.str();
  658. output.errorLine = 0;
  659. output.errorColumn = 0;
  660. }
  661. }
  662. return output;
  663. }
  664. void BSLFXCompiler::parseFX(ParseState* parseState, const char* source)
  665. {
  666. yyscan_t scanner;
  667. YY_BUFFER_STATE state;
  668. if (yylex_init_extra(parseState, &scanner))
  669. return;
  670. // If debug output from lexer is needed uncomment this and add %debug option to lexer file
  671. //yyset_debug(true, scanner);
  672. // If debug output from parser is needed uncomment this and add %debug option to parser file
  673. //yydebug = true;
  674. state = yy_scan_string(source, scanner);
  675. if (yyparse(parseState, scanner))
  676. return;
  677. yy_delete_buffer(state, scanner);
  678. yylex_destroy(scanner);
  679. }
  680. BSLFXCompiler::TechniqueMetaData BSLFXCompiler::parseTechniqueMetaData(ASTFXNode* technique)
  681. {
  682. TechniqueMetaData metaData;
  683. metaData.renderer = RendererAny;
  684. metaData.language = "hlsl";
  685. metaData.isMixin = technique->type == NT_Mixin;
  686. for (int i = 0; i < technique->options->count; i++)
  687. {
  688. NodeOption* option = &technique->options->entries[i];
  689. switch (option->type)
  690. {
  691. case OT_Renderer:
  692. metaData.renderer = parseRenderer(removeQuotes(option->value.strValue));
  693. break;
  694. case OT_Tags:
  695. {
  696. ASTFXNode* tagsNode = option->value.nodePtr;
  697. for (int j = 0; j < tagsNode->options->count; j++)
  698. {
  699. NodeOption* tagOption = &tagsNode->options->entries[j];
  700. if (tagOption->type == OT_TagValue)
  701. metaData.tags.push_back(removeQuotes(tagOption->value.strValue));
  702. }
  703. }
  704. break;
  705. case OT_Identifier:
  706. metaData.name = option->value.strValue;
  707. break;
  708. case OT_Mixin:
  709. metaData.includes.push_back(option->value.strValue);
  710. break;
  711. default:
  712. break;
  713. }
  714. }
  715. return metaData;
  716. }
  717. StringID BSLFXCompiler::parseRenderer(const String& name)
  718. {
  719. if (name == "Any")
  720. return RendererAny;
  721. else if (name == "Default")
  722. return RendererDefault;
  723. return RendererAny;
  724. }
  725. QueueSortType BSLFXCompiler::parseSortType(CullAndSortModeValue sortType)
  726. {
  727. switch (sortType)
  728. {
  729. case CASV_BackToFront:
  730. return QueueSortType::BackToFront;
  731. case CASV_FrontToBack:
  732. return QueueSortType::FrontToBack;
  733. case CASV_None:
  734. return QueueSortType::None;
  735. }
  736. return QueueSortType::None;
  737. }
  738. CompareFunction BSLFXCompiler::parseCompFunc(CompFuncValue compFunc)
  739. {
  740. switch (compFunc)
  741. {
  742. case CFV_Pass:
  743. return CMPF_ALWAYS_PASS;
  744. case CFV_Fail:
  745. return CMPF_ALWAYS_FAIL;
  746. case CFV_LT:
  747. return CMPF_LESS;
  748. case CFV_LTE:
  749. return CMPF_LESS_EQUAL;
  750. case CFV_EQ:
  751. return CMPF_EQUAL;
  752. case CFV_NEQ:
  753. return CMPF_NOT_EQUAL;
  754. case CFV_GT:
  755. return CMPF_GREATER;
  756. case CFV_GTE:
  757. return CMPF_GREATER_EQUAL;
  758. }
  759. return CMPF_ALWAYS_PASS;
  760. }
  761. BlendFactor BSLFXCompiler::parseBlendFactor(OpValue factor)
  762. {
  763. switch (factor)
  764. {
  765. case OV_One:
  766. return BF_ONE;
  767. case OV_Zero:
  768. return BF_ZERO;
  769. case OV_DestColor:
  770. return BF_DEST_COLOR;
  771. case OV_SrcColor:
  772. return BF_SOURCE_COLOR;
  773. case OV_InvDestColor:
  774. return BF_INV_DEST_COLOR;
  775. case OV_InvSrcColor:
  776. return BF_INV_SOURCE_COLOR;
  777. case OV_DestAlpha:
  778. return BF_DEST_ALPHA;
  779. case OV_SrcAlpha:
  780. return BF_SOURCE_ALPHA;
  781. case OV_InvDestAlpha:
  782. return BF_INV_DEST_ALPHA;
  783. case OV_InvSrcAlpha:
  784. return BF_INV_SOURCE_ALPHA;
  785. default:
  786. break;
  787. }
  788. return BF_ONE;
  789. }
  790. BlendOperation BSLFXCompiler::parseBlendOp(BlendOpValue op)
  791. {
  792. switch (op)
  793. {
  794. case BOV_Add:
  795. return BO_ADD;
  796. case BOV_Max:
  797. return BO_MAX;
  798. case BOV_Min:
  799. return BO_MIN;
  800. case BOV_Subtract:
  801. return BO_SUBTRACT;
  802. case BOV_RevSubtract:
  803. return BO_REVERSE_SUBTRACT;
  804. }
  805. return BO_ADD;
  806. }
  807. StencilOperation BSLFXCompiler::parseStencilOp(OpValue op)
  808. {
  809. switch (op)
  810. {
  811. case OV_Keep:
  812. return SOP_KEEP;
  813. case OV_Zero:
  814. return SOP_ZERO;
  815. case OV_Replace:
  816. return SOP_REPLACE;
  817. case OV_Incr:
  818. return SOP_INCREMENT;
  819. case OV_Decr:
  820. return SOP_DECREMENT;
  821. case OV_IncrWrap:
  822. return SOP_INCREMENT_WRAP;
  823. case OV_DecrWrap:
  824. return SOP_DECREMENT_WRAP;
  825. case OV_Invert:
  826. return SOP_INVERT;
  827. default:
  828. break;
  829. }
  830. return SOP_KEEP;
  831. }
  832. CullingMode BSLFXCompiler::parseCullMode(CullAndSortModeValue cm)
  833. {
  834. switch (cm)
  835. {
  836. case CASV_None:
  837. return CULL_NONE;
  838. case CASV_CW:
  839. return CULL_CLOCKWISE;
  840. case CASV_CCW:
  841. return CULL_COUNTERCLOCKWISE;
  842. }
  843. return CULL_COUNTERCLOCKWISE;
  844. }
  845. PolygonMode BSLFXCompiler::parseFillMode(FillModeValue fm)
  846. {
  847. if (fm == FMV_Wire)
  848. return PM_WIREFRAME;
  849. return PM_SOLID;
  850. }
  851. void BSLFXCompiler::parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  852. {
  853. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  854. return;
  855. for (int i = 0; i < stencilOpNode->options->count; i++)
  856. {
  857. NodeOption* option = &stencilOpNode->options->entries[i];
  858. switch (option->type)
  859. {
  860. case OT_Fail:
  861. desc.frontStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  862. break;
  863. case OT_ZFail:
  864. desc.frontStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  865. break;
  866. case OT_PassOp:
  867. desc.frontStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  868. break;
  869. case OT_CompareFunc:
  870. desc.frontStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  871. break;
  872. default:
  873. break;
  874. }
  875. }
  876. }
  877. void BSLFXCompiler::parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  878. {
  879. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  880. return;
  881. for (int i = 0; i < stencilOpNode->options->count; i++)
  882. {
  883. NodeOption* option = &stencilOpNode->options->entries[i];
  884. switch (option->type)
  885. {
  886. case OT_Fail:
  887. desc.backStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  888. break;
  889. case OT_ZFail:
  890. desc.backStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  891. break;
  892. case OT_PassOp:
  893. desc.backStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  894. break;
  895. case OT_CompareFunc:
  896. desc.backStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  897. break;
  898. default:
  899. break;
  900. }
  901. }
  902. }
  903. void BSLFXCompiler::parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  904. {
  905. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  906. return;
  907. for (int i = 0; i < blendDefNode->options->count; i++)
  908. {
  909. NodeOption* option = &blendDefNode->options->entries[i];
  910. switch (option->type)
  911. {
  912. case OT_Source:
  913. desc.srcBlend = parseBlendFactor((OpValue)option->value.intValue);
  914. break;
  915. case OT_Dest:
  916. desc.dstBlend = parseBlendFactor((OpValue)option->value.intValue);
  917. break;
  918. case OT_Op:
  919. desc.blendOp = parseBlendOp((BlendOpValue)option->value.intValue);
  920. break;
  921. default:
  922. break;
  923. }
  924. }
  925. }
  926. void BSLFXCompiler::parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  927. {
  928. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  929. return;
  930. for (int i = 0; i < blendDefNode->options->count; i++)
  931. {
  932. NodeOption* option = &blendDefNode->options->entries[i];
  933. switch (option->type)
  934. {
  935. case OT_Source:
  936. desc.srcBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  937. break;
  938. case OT_Dest:
  939. desc.dstBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  940. break;
  941. case OT_Op:
  942. desc.blendOpAlpha = parseBlendOp((BlendOpValue)option->value.intValue);
  943. break;
  944. default:
  945. break;
  946. }
  947. }
  948. }
  949. void BSLFXCompiler::parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode)
  950. {
  951. if (targetNode == nullptr || targetNode->type != NT_Target)
  952. return;
  953. UINT32 index = 0;
  954. for (int i = 0; i < targetNode->options->count; i++)
  955. {
  956. NodeOption* option = &targetNode->options->entries[i];
  957. switch (option->type)
  958. {
  959. case OT_Index:
  960. index = option->value.intValue;
  961. break;
  962. default:
  963. break;
  964. }
  965. }
  966. if (index >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  967. return;
  968. RENDER_TARGET_BLEND_STATE_DESC& rtDesc = desc.renderTargetDesc[index];
  969. for (int i = 0; i < targetNode->options->count; i++)
  970. {
  971. NodeOption* option = &targetNode->options->entries[i];
  972. switch (option->type)
  973. {
  974. case OT_Enabled:
  975. rtDesc.blendEnable = option->value.intValue > 0;
  976. break;
  977. case OT_Color:
  978. parseColorBlendDef(rtDesc, option->value.nodePtr);
  979. break;
  980. case OT_Alpha:
  981. parseAlphaBlendDef(rtDesc, option->value.nodePtr);
  982. break;
  983. case OT_WriteMask:
  984. rtDesc.renderTargetWriteMask = option->value.intValue;
  985. break;
  986. default:
  987. break;
  988. }
  989. }
  990. }
  991. bool BSLFXCompiler::parseBlendState(PassData& desc, ASTFXNode* blendNode)
  992. {
  993. if (blendNode == nullptr || blendNode->type != NT_Blend)
  994. return false;
  995. bool isDefault = true;
  996. for (int i = 0; i < blendNode->options->count; i++)
  997. {
  998. NodeOption* option = &blendNode->options->entries[i];
  999. switch (option->type)
  1000. {
  1001. case OT_AlphaToCoverage:
  1002. desc.blendDesc.alphaToCoverageEnable = option->value.intValue > 0;
  1003. isDefault = false;
  1004. break;
  1005. case OT_IndependantBlend:
  1006. desc.blendDesc.independantBlendEnable = option->value.intValue > 0;
  1007. isDefault = false;
  1008. break;
  1009. case OT_Target:
  1010. parseRenderTargetBlendState(desc.blendDesc, option->value.nodePtr);
  1011. isDefault = false;
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. }
  1017. return !isDefault;
  1018. }
  1019. bool BSLFXCompiler::parseRasterizerState(PassData& desc, ASTFXNode* rasterNode)
  1020. {
  1021. if (rasterNode == nullptr || rasterNode->type != NT_Raster)
  1022. return false;
  1023. bool isDefault = true;
  1024. for (int i = 0; i < rasterNode->options->count; i++)
  1025. {
  1026. NodeOption* option = &rasterNode->options->entries[i];
  1027. switch (option->type)
  1028. {
  1029. case OT_FillMode:
  1030. desc.rasterizerDesc.polygonMode = parseFillMode((FillModeValue)option->value.intValue);
  1031. isDefault = false;
  1032. break;
  1033. case OT_CullMode:
  1034. desc.rasterizerDesc.cullMode = parseCullMode((CullAndSortModeValue)option->value.intValue);
  1035. isDefault = false;
  1036. break;
  1037. case OT_DepthBias:
  1038. desc.rasterizerDesc.depthBias = option->value.floatValue;
  1039. isDefault = false;
  1040. break;
  1041. case OT_SDepthBias:
  1042. desc.rasterizerDesc.slopeScaledDepthBias = option->value.floatValue;
  1043. isDefault = false;
  1044. break;
  1045. case OT_DepthClip:
  1046. desc.rasterizerDesc.depthClipEnable = option->value.intValue > 0;
  1047. isDefault = false;
  1048. break;
  1049. case OT_Scissor:
  1050. desc.rasterizerDesc.scissorEnable = option->value.intValue > 0;
  1051. isDefault = false;
  1052. break;
  1053. case OT_Multisample:
  1054. desc.rasterizerDesc.multisampleEnable = option->value.intValue > 0;
  1055. isDefault = false;
  1056. break;
  1057. case OT_AALine:
  1058. desc.rasterizerDesc.antialiasedLineEnable = option->value.intValue > 0;
  1059. isDefault = false;
  1060. break;
  1061. default:
  1062. break;
  1063. }
  1064. }
  1065. return !isDefault;
  1066. }
  1067. bool BSLFXCompiler::parseDepthState(PassData& passData, ASTFXNode* depthNode)
  1068. {
  1069. if (depthNode == nullptr || depthNode->type != NT_Depth)
  1070. return false;
  1071. bool isDefault = true;
  1072. for (int i = 0; i < depthNode->options->count; i++)
  1073. {
  1074. NodeOption* option = &depthNode->options->entries[i];
  1075. switch (option->type)
  1076. {
  1077. case OT_DepthRead:
  1078. passData.depthStencilDesc.depthReadEnable = option->value.intValue > 0;
  1079. isDefault = false;
  1080. break;
  1081. case OT_DepthWrite:
  1082. passData.depthStencilDesc.depthWriteEnable = option->value.intValue > 0;
  1083. isDefault = false;
  1084. break;
  1085. case OT_CompareFunc:
  1086. passData.depthStencilDesc.depthComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  1087. isDefault = false;
  1088. break;
  1089. default:
  1090. break;
  1091. }
  1092. }
  1093. return !isDefault;
  1094. }
  1095. bool BSLFXCompiler::parseStencilState(PassData& passData, ASTFXNode* stencilNode)
  1096. {
  1097. if (stencilNode == nullptr || stencilNode->type != NT_Stencil)
  1098. return false;
  1099. bool isDefault = true;
  1100. for (int i = 0; i < stencilNode->options->count; i++)
  1101. {
  1102. NodeOption* option = &stencilNode->options->entries[i];
  1103. switch (option->type)
  1104. {
  1105. case OT_Enabled:
  1106. passData.depthStencilDesc.stencilEnable = option->value.intValue > 0;
  1107. isDefault = false;
  1108. break;
  1109. case OT_StencilReadMask:
  1110. passData.depthStencilDesc.stencilReadMask = (UINT8)option->value.intValue;
  1111. isDefault = false;
  1112. break;
  1113. case OT_StencilWriteMask:
  1114. passData.depthStencilDesc.stencilWriteMask = (UINT8)option->value.intValue;
  1115. isDefault = false;
  1116. break;
  1117. case OT_StencilOpFront:
  1118. parseStencilFront(passData.depthStencilDesc, option->value.nodePtr);
  1119. isDefault = false;
  1120. break;
  1121. case OT_StencilOpBack:
  1122. parseStencilBack(passData.depthStencilDesc, option->value.nodePtr);
  1123. isDefault = false;
  1124. break;
  1125. case OT_StencilRef:
  1126. passData.stencilRefValue = option->value.intValue;
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. }
  1132. return !isDefault;
  1133. }
  1134. void BSLFXCompiler::parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData)
  1135. {
  1136. if (codeNode == nullptr || (codeNode->type != NT_Code))
  1137. {
  1138. return;
  1139. }
  1140. UINT32 index = (UINT32)-1;
  1141. for (int j = 0; j < codeNode->options->count; j++)
  1142. {
  1143. if (codeNode->options->entries[j].type == OT_Index)
  1144. index = codeNode->options->entries[j].value.intValue;
  1145. }
  1146. if (index != (UINT32)-1 && index < (UINT32)codeBlocks.size())
  1147. {
  1148. switch (codeNode->type)
  1149. {
  1150. case NT_Code:
  1151. passData.code += codeBlocks[index];
  1152. break;
  1153. default:
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. void BSLFXCompiler::parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData)
  1159. {
  1160. if (passNode == nullptr || passNode->type != NT_Pass)
  1161. return;
  1162. for (int i = 0; i < passNode->options->count; i++)
  1163. {
  1164. NodeOption* option = &passNode->options->entries[i];
  1165. switch (option->type)
  1166. {
  1167. case OT_Blend:
  1168. passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
  1169. break;
  1170. case OT_Raster:
  1171. passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
  1172. break;
  1173. case OT_Depth:
  1174. passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
  1175. break;
  1176. case OT_Stencil:
  1177. passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
  1178. break;
  1179. case OT_Code:
  1180. parseCodeBlock(option->value.nodePtr, codeBlocks, passData);
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. }
  1186. }
  1187. void BSLFXCompiler::parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData)
  1188. {
  1189. if (techniqueNode == nullptr || (techniqueNode->type != NT_Technique && techniqueNode->type != NT_Mixin))
  1190. return;
  1191. // There must always be at least one pass
  1192. if(techniqueData.passes.empty())
  1193. {
  1194. techniqueData.passes.push_back(PassData());
  1195. techniqueData.passes.back().seqIdx = 0;
  1196. }
  1197. PassData combinedCommonPassData;
  1198. UINT32 nextPassIdx = 0;
  1199. // Go in reverse because options are added in reverse order during parsing
  1200. for (int i = techniqueNode->options->count - 1; i >= 0; i--)
  1201. {
  1202. NodeOption* option = &techniqueNode->options->entries[i];
  1203. switch (option->type)
  1204. {
  1205. case OT_Pass:
  1206. {
  1207. UINT32 passIdx = nextPassIdx;
  1208. PassData* passData = nullptr;
  1209. for (auto& entry : techniqueData.passes)
  1210. {
  1211. if (entry.seqIdx == passIdx)
  1212. passData = &entry;
  1213. }
  1214. if (passData == nullptr)
  1215. {
  1216. techniqueData.passes.push_back(PassData());
  1217. passData = &techniqueData.passes.back();
  1218. passData->seqIdx = passIdx;
  1219. }
  1220. nextPassIdx = std::max(nextPassIdx, passIdx) + 1;
  1221. passData->code = combinedCommonPassData.code + passData->code;
  1222. ASTFXNode* passNode = option->value.nodePtr;
  1223. parsePass(passNode, codeBlocks, *passData);
  1224. }
  1225. break;
  1226. case OT_Code:
  1227. {
  1228. PassData commonPassData;
  1229. parseCodeBlock(option->value.nodePtr, codeBlocks, commonPassData);
  1230. for (auto& passData : techniqueData.passes)
  1231. passData.code += commonPassData.code;
  1232. combinedCommonPassData.code += commonPassData.code;
  1233. }
  1234. break;
  1235. default:
  1236. break;
  1237. }
  1238. }
  1239. // Parse common pass states
  1240. for (int i = 0; i < techniqueNode->options->count; i++)
  1241. {
  1242. NodeOption* option = &techniqueNode->options->entries[i];
  1243. switch (option->type)
  1244. {
  1245. case OT_Blend:
  1246. for (auto& passData : techniqueData.passes)
  1247. passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
  1248. break;
  1249. case OT_Raster:
  1250. for (auto& passData : techniqueData.passes)
  1251. passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
  1252. break;
  1253. case OT_Depth:
  1254. for (auto& passData : techniqueData.passes)
  1255. passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
  1256. break;
  1257. case OT_Stencil:
  1258. for (auto& passData : techniqueData.passes)
  1259. passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
  1260. break;
  1261. default:
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. void BSLFXCompiler::parseOptions(ASTFXNode* optionsNode, SHADER_DESC& shaderDesc)
  1267. {
  1268. if (optionsNode == nullptr || optionsNode->type != NT_Options)
  1269. return;
  1270. for (int i = optionsNode->options->count - 1; i >= 0; i--)
  1271. {
  1272. NodeOption* option = &optionsNode->options->entries[i];
  1273. switch (option->type)
  1274. {
  1275. case OT_Separable:
  1276. shaderDesc.separablePasses = option->value.intValue > 1;
  1277. break;
  1278. case OT_Sort:
  1279. shaderDesc.queueSortType = parseSortType((CullAndSortModeValue)option->value.intValue);
  1280. break;
  1281. case OT_Priority:
  1282. shaderDesc.queuePriority = option->value.intValue;
  1283. break;
  1284. case OT_Transparent:
  1285. shaderDesc.flags |= (UINT32)ShaderFlags::Transparent;
  1286. break;
  1287. default:
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. BSLFXCompileResult BSLFXCompiler::parseShader(const String& name, ParseState* parseState, Vector<String>& codeBlocks)
  1293. {
  1294. BSLFXCompileResult output;
  1295. if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
  1296. {
  1297. parseStateDelete(parseState);
  1298. output.errorMessage = "Root not is null or not a shader.";
  1299. return output;
  1300. }
  1301. SHADER_DESC shaderDesc;
  1302. Vector<pair<ASTFXNode*, TechniqueData>> techniqueData;
  1303. // Go in reverse because options are added in reverse order during parsing
  1304. for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
  1305. {
  1306. NodeOption* option = &parseState->rootNode->options->entries[i];
  1307. switch (option->type)
  1308. {
  1309. case OT_Options:
  1310. parseOptions(option->value.nodePtr, shaderDesc);
  1311. break;
  1312. case OT_Technique:
  1313. {
  1314. // We initially parse only meta-data, so we can handle out-of-order technique definitions
  1315. TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
  1316. techniqueData.push_back(std::make_pair(option->value.nodePtr, TechniqueData()));
  1317. TechniqueData& data = techniqueData.back().second;
  1318. data.metaData = metaData;
  1319. break;
  1320. }
  1321. default:
  1322. break;
  1323. }
  1324. }
  1325. bool* techniqueWasParsed = bs_stack_alloc<bool>((UINT32)techniqueData.size());
  1326. std::function<bool(const TechniqueMetaData&, TechniqueData&)> parseInherited =
  1327. [&](const TechniqueMetaData& metaData, TechniqueData& outTechnique)
  1328. {
  1329. for (auto riter = metaData.includes.rbegin(); riter != metaData.includes.rend(); ++riter)
  1330. {
  1331. const String& includes = *riter;
  1332. UINT32 baseIdx = -1;
  1333. for(UINT32 i = 0; i < (UINT32)techniqueData.size(); i++)
  1334. {
  1335. auto& entry = techniqueData[i];
  1336. if (!entry.second.metaData.isMixin)
  1337. continue;
  1338. if (entry.second.metaData.name == includes)
  1339. {
  1340. bool matches = entry.second.metaData.language == metaData.language || entry.second.metaData.language == "Any";
  1341. matches &= entry.second.metaData.renderer == metaData.renderer || entry.second.metaData.renderer == RendererAny;
  1342. // We want the last matching technique, in order to allow techniques to override each other
  1343. if (matches)
  1344. baseIdx = i;
  1345. }
  1346. }
  1347. if (baseIdx != -1)
  1348. {
  1349. auto& entry = techniqueData[baseIdx];
  1350. // Was already parsed previously, don't parse it multiple times (happens when multiple techniques
  1351. // include the same mixin)
  1352. if (techniqueWasParsed[baseIdx])
  1353. continue;
  1354. if (!parseInherited(entry.second.metaData, outTechnique))
  1355. return false;
  1356. parseTechnique(entry.first, codeBlocks, outTechnique);
  1357. techniqueWasParsed[baseIdx] = true;
  1358. }
  1359. else
  1360. {
  1361. output.errorMessage = "Mixin \"" + includes + "\" cannot be found.";
  1362. return false;
  1363. }
  1364. }
  1365. return true;
  1366. };
  1367. // Actually parse techniques
  1368. for (auto& entry : techniqueData)
  1369. {
  1370. const TechniqueMetaData& metaData = entry.second.metaData;
  1371. if (metaData.isMixin)
  1372. continue;
  1373. bs_zero_out(techniqueWasParsed, techniqueData.size());
  1374. if (!parseInherited(metaData, entry.second))
  1375. {
  1376. parseStateDelete(parseState);
  1377. bs_stack_free(techniqueWasParsed);
  1378. return output;
  1379. }
  1380. parseTechnique(entry.first, codeBlocks, entry.second);
  1381. }
  1382. bs_stack_free(techniqueWasParsed);
  1383. // Parse extended HLSL code and generate per-program code, also convert to GLSL/VKSL
  1384. UINT32 end = (UINT32)techniqueData.size();
  1385. for(UINT32 i = 0; i < end; i++)
  1386. {
  1387. const TechniqueMetaData& metaData = techniqueData[i].second.metaData;
  1388. if (metaData.isMixin)
  1389. continue;
  1390. TechniqueData& hlslTechnique = techniqueData[i].second;
  1391. TechniqueData glslTechnique = techniqueData[i].second;
  1392. glslTechnique.metaData.language = "glsl";
  1393. TechniqueData vkslTechnique = techniqueData[i].second;
  1394. vkslTechnique.metaData.language = "vksl";
  1395. UINT32 numPasses = (UINT32)hlslTechnique.passes.size();
  1396. for(UINT32 j = 0; j < numPasses; j++)
  1397. {
  1398. PassData& hlslPassData = hlslTechnique.passes[j];
  1399. PassData& glslPassData = glslTechnique.passes[j];
  1400. PassData& vkslPassData = vkslTechnique.passes[j];
  1401. // Clean non-standard HLSL
  1402. static const std::regex regex("\\[\\s*layout\\s*\\(.*\\)\\s*\\]|\\[\\s*internal\\s*\\]|\\[\\s*color\\s*\\]|\\[\\s*alias\\s*\\(.*\\)\\s*\\]");
  1403. hlslPassData.code = regex_replace(hlslPassData.code, regex, "");
  1404. // Find valid entry points and parameters
  1405. // Note: XShaderCompiler needs to do a full pass when doing reflection, and for each individual program
  1406. // type. If performance is ever important here it could be good to update XShaderCompiler so it can
  1407. // somehow save the AST and then re-use it for multiple actions.
  1408. Vector<GpuProgramType> types;
  1409. reflectHLSL(glslPassData.code, shaderDesc, types);
  1410. UINT32 glslBinding = 0;
  1411. UINT32 vkslBinding = 0;
  1412. // Cross-compile for all detected shader types
  1413. // Note: I'm just copying HLSL code as-is. This code will contain all entry points which could have
  1414. // an effect on compile time. It would be ideal to remove dead code depending on program type. This would
  1415. // involve adding a HLSL code generator to XShaderCompiler.
  1416. for(auto& type : types)
  1417. {
  1418. switch(type)
  1419. {
  1420. case GPT_VERTEX_PROGRAM:
  1421. hlslPassData.vertexCode = hlslPassData.code;
  1422. glslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM, false, glslBinding);
  1423. vkslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM, true, vkslBinding);
  1424. break;
  1425. case GPT_FRAGMENT_PROGRAM:
  1426. hlslPassData.fragmentCode = hlslPassData.code;
  1427. glslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM, false, glslBinding);
  1428. vkslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM, true, vkslBinding);
  1429. break;
  1430. case GPT_GEOMETRY_PROGRAM:
  1431. hlslPassData.geometryCode = hlslPassData.code;
  1432. glslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM, false, glslBinding);
  1433. vkslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM, true, vkslBinding);
  1434. break;
  1435. case GPT_HULL_PROGRAM:
  1436. hlslPassData.hullCode = hlslPassData.code;
  1437. glslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM, false, glslBinding);
  1438. vkslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM, true, vkslBinding);
  1439. break;
  1440. case GPT_DOMAIN_PROGRAM:
  1441. hlslPassData.domainCode = hlslPassData.code;
  1442. glslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM, false, glslBinding);
  1443. vkslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM, true, vkslBinding);
  1444. break;
  1445. case GPT_COMPUTE_PROGRAM:
  1446. hlslPassData.computeCode = hlslPassData.code;
  1447. glslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM, false, glslBinding);
  1448. vkslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM, true, vkslBinding);
  1449. break;
  1450. }
  1451. }
  1452. }
  1453. techniqueData.push_back(std::make_pair(techniqueData[i].first, glslTechnique));
  1454. techniqueData.push_back(std::make_pair(techniqueData[i].first, vkslTechnique));
  1455. }
  1456. Vector<SPtr<Technique>> techniques;
  1457. for(auto& entry : techniqueData)
  1458. {
  1459. const TechniqueMetaData& metaData = entry.second.metaData;
  1460. if (metaData.isMixin)
  1461. continue;
  1462. Map<UINT32, SPtr<Pass>, std::greater<UINT32>> passes;
  1463. for (auto& passData : entry.second.passes)
  1464. {
  1465. PASS_DESC passDesc;
  1466. if (!passData.blendIsDefault)
  1467. passDesc.blendState = BlendState::create(passData.blendDesc);
  1468. if (!passData.rasterizerIsDefault)
  1469. passDesc.rasterizerState = RasterizerState::create(passData.rasterizerDesc);
  1470. if (!passData.depthStencilIsDefault)
  1471. passDesc.depthStencilState = DepthStencilState::create(passData.depthStencilDesc);
  1472. GPU_PROGRAM_DESC desc;
  1473. desc.language = metaData.language;
  1474. bool isHLSL = desc.language == "hlsl";
  1475. if (!passData.vertexCode.empty())
  1476. {
  1477. desc.entryPoint = isHLSL ? "vsmain" : "main";
  1478. desc.source = passData.vertexCode;
  1479. desc.type = GPT_VERTEX_PROGRAM;
  1480. passDesc.vertexProgram = GpuProgram::create(desc);
  1481. }
  1482. if (!passData.fragmentCode.empty())
  1483. {
  1484. desc.entryPoint = isHLSL ? "fsmain" : "main";
  1485. desc.source = passData.fragmentCode;
  1486. desc.type = GPT_FRAGMENT_PROGRAM;
  1487. passDesc.fragmentProgram = GpuProgram::create(desc);
  1488. }
  1489. if (!passData.geometryCode.empty())
  1490. {
  1491. desc.entryPoint = isHLSL ? "gsmain" : "main";
  1492. desc.source = passData.geometryCode;
  1493. desc.type = GPT_GEOMETRY_PROGRAM;
  1494. passDesc.geometryProgram = GpuProgram::create(desc);
  1495. }
  1496. if (!passData.hullCode.empty())
  1497. {
  1498. desc.entryPoint = isHLSL ? "hsmain" : "main";
  1499. desc.source = passData.hullCode;
  1500. desc.type = GPT_HULL_PROGRAM;
  1501. passDesc.hullProgram = GpuProgram::create(desc);
  1502. }
  1503. if (!passData.domainCode.empty())
  1504. {
  1505. desc.entryPoint = isHLSL ? "dsmain" : "main";
  1506. desc.source = passData.domainCode;
  1507. desc.type = GPT_DOMAIN_PROGRAM;
  1508. passDesc.domainProgram = GpuProgram::create(desc);
  1509. }
  1510. if (!passData.computeCode.empty())
  1511. {
  1512. desc.entryPoint = isHLSL ? "csmain" : "main";
  1513. desc.source = passData.computeCode;
  1514. desc.type = GPT_COMPUTE_PROGRAM;
  1515. passDesc.computeProgram = GpuProgram::create(desc);
  1516. }
  1517. passDesc.stencilRefValue = passData.stencilRefValue;
  1518. SPtr<Pass> pass = Pass::create(passDesc);
  1519. if (pass != nullptr)
  1520. passes[passData.seqIdx] = pass;
  1521. }
  1522. Vector<SPtr<Pass>> orderedPasses;
  1523. for (auto& KVP : passes)
  1524. orderedPasses.push_back(KVP.second);
  1525. if (orderedPasses.size() > 0)
  1526. {
  1527. SPtr<Technique> technique = Technique::create(metaData.language, metaData.renderer, metaData.tags,
  1528. orderedPasses);
  1529. techniques.push_back(technique);
  1530. }
  1531. }
  1532. Vector<String> includes;
  1533. IncludeLink* includeLink = parseState->includes;
  1534. while(includeLink != nullptr)
  1535. {
  1536. String includeFilename = includeLink->data->filename;
  1537. auto iterFind = std::find(includes.begin(), includes.end(), includeFilename);
  1538. if (iterFind == includes.end())
  1539. includes.push_back(includeFilename);
  1540. includeLink = includeLink->next;
  1541. }
  1542. parseStateDelete(parseState);
  1543. output.shader = Shader::_createPtr(name, shaderDesc, techniques);
  1544. output.shader->setIncludeFiles(includes);
  1545. return output;
  1546. }
  1547. String BSLFXCompiler::removeQuotes(const char* input)
  1548. {
  1549. UINT32 len = (UINT32)strlen(input);
  1550. String output(len - 2, ' ');
  1551. for (UINT32 i = 0; i < (len - 2); i++)
  1552. output[i] = input[i + 1];
  1553. return output;
  1554. }
  1555. }