BsSLFXCompiler.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. extern "C" {
  17. #include "BsMMAlloc.h"
  18. #include "BsParserFX.h"
  19. #include "BsLexerFX.h"
  20. }
  21. using namespace std;
  22. namespace bs
  23. {
  24. // Print out the FX AST, only for debug purposes
  25. void SLFXDebugPrint(ASTFXNode* node, String indent)
  26. {
  27. LOGDBG(indent + "NODE " + toString(node->type));
  28. for (int i = 0; i < node->options->count; i++)
  29. {
  30. OptionDataType odt = OPTION_LOOKUP[(int)node->options->entries[i].type].dataType;
  31. if (odt == ODT_Complex)
  32. {
  33. LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type));
  34. SLFXDebugPrint(node->options->entries[i].value.nodePtr, indent + "\t");
  35. continue;
  36. }
  37. String value;
  38. switch (odt)
  39. {
  40. case ODT_Bool:
  41. value = toString(node->options->entries[i].value.intValue != 0);
  42. break;
  43. case ODT_Int:
  44. value = toString(node->options->entries[i].value.intValue);
  45. break;
  46. case ODT_Float:
  47. value = toString(node->options->entries[i].value.floatValue);
  48. break;
  49. case ODT_String:
  50. value = node->options->entries[i].value.strValue;
  51. break;
  52. case ODT_Matrix:
  53. {
  54. Matrix4 mat4 = *(Matrix4*)(node->options->entries[i].value.matrixValue);
  55. value = toString(mat4);
  56. }
  57. break;
  58. default:
  59. break;
  60. }
  61. LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type) + " = " + value);
  62. }
  63. }
  64. BSLFXCompileResult BSLFXCompiler::compile(const String& source, const UnorderedMap<String, String>& defines)
  65. {
  66. BSLFXCompileResult output;
  67. String parsedSource = source;
  68. ParseState* parseState = parseStateCreate();
  69. for(auto& define : defines)
  70. {
  71. if (define.first.size() == 0)
  72. continue;
  73. addDefine(parseState, define.first.c_str());
  74. if(define.second.size() > 0)
  75. addDefineExpr(parseState, define.second.c_str());
  76. }
  77. parseFX(parseState, parsedSource.c_str());
  78. if (parseState->hasError > 0)
  79. {
  80. output.errorMessage = parseState->errorMessage;
  81. output.errorLine = parseState->errorLine;
  82. output.errorColumn = parseState->errorColumn;
  83. if(parseState->errorFile != nullptr)
  84. output.errorFile = parseState->errorFile;
  85. parseStateDelete(parseState);
  86. }
  87. else
  88. {
  89. // Only enable for debug purposes
  90. //SLFXDebugPrint(parseState->rootNode, "");
  91. Vector<String> codeBlocks;
  92. CodeString* codeString = parseState->codeStrings;
  93. while(codeString != nullptr)
  94. {
  95. while ((INT32)codeBlocks.size() <= codeString->index)
  96. codeBlocks.push_back(String());
  97. codeBlocks[codeString->index] = String(codeString->code, codeString->size);
  98. codeString = codeString->next;
  99. }
  100. output = parseShader("Shader", parseState, codeBlocks);
  101. StringStream gpuProgError;
  102. bool hasError = false;
  103. if (output.shader != nullptr)
  104. {
  105. Vector<SPtr<Technique>> techniques = output.shader->getCompatibleTechniques();
  106. for (auto& technique : techniques)
  107. {
  108. UINT32 numPasses = technique->getNumPasses();
  109. for (UINT32 i = 0; i < numPasses; i++)
  110. {
  111. SPtr<Pass> pass = technique->getPass(i);
  112. auto checkCompileStatus = [&](const String& prefix, const SPtr<GpuProgram>& prog)
  113. {
  114. if (prog != nullptr)
  115. {
  116. prog->blockUntilCoreInitialized();
  117. if (!prog->isCompiled())
  118. {
  119. hasError = true;
  120. gpuProgError << prefix << ": " << prog->getCompileErrorMessage() << std::endl;
  121. }
  122. }
  123. };
  124. checkCompileStatus("Vertex program", pass->getVertexProgram());
  125. checkCompileStatus("Fragment program", pass->getFragmentProgram());
  126. checkCompileStatus("Geometry program", pass->getGeometryProgram());
  127. checkCompileStatus("Hull program", pass->getHullProgram());
  128. checkCompileStatus("Domain program", pass->getDomainProgram());
  129. checkCompileStatus("Compute program", pass->getComputeProgram());
  130. }
  131. }
  132. }
  133. if (hasError)
  134. {
  135. output.shader = nullptr;
  136. output.errorMessage = "Failed compiling GPU program(s): " + gpuProgError.str();
  137. output.errorLine = 0;
  138. output.errorColumn = 0;
  139. }
  140. }
  141. return output;
  142. }
  143. void BSLFXCompiler::parseFX(ParseState* parseState, const char* source)
  144. {
  145. yyscan_t scanner;
  146. YY_BUFFER_STATE state;
  147. if (yylex_init_extra(parseState, &scanner))
  148. return;
  149. // If debug output from lexer is needed uncomment this and add %debug option to lexer file
  150. //yyset_debug(true, scanner);
  151. // If debug output from parser is needed uncomment this and add %debug option to parser file
  152. //yydebug = true;
  153. state = yy_scan_string(source, scanner);
  154. if (yyparse(parseState, scanner))
  155. return;
  156. yy_delete_buffer(state, scanner);
  157. yylex_destroy(scanner);
  158. }
  159. BSLFXCompiler::TechniqueMetaData BSLFXCompiler::parseTechniqueMetaData(ASTFXNode* technique)
  160. {
  161. TechniqueMetaData metaData;
  162. metaData.renderer = RendererAny;
  163. metaData.language = "Any";
  164. for (int i = 0; i < technique->options->count; i++)
  165. {
  166. NodeOption* option = &technique->options->entries[i];
  167. switch (option->type)
  168. {
  169. case OT_Renderer:
  170. metaData.renderer = parseRenderer(removeQuotes(option->value.strValue));
  171. break;
  172. case OT_Language:
  173. parseLanguage(removeQuotes(option->value.strValue), metaData.renderAPI, metaData.language);
  174. break;
  175. case OT_Tags:
  176. {
  177. ASTFXNode* tagsNode = option->value.nodePtr;
  178. for (int j = 0; j < tagsNode->options->count; j++)
  179. {
  180. NodeOption* tagOption = &tagsNode->options->entries[j];
  181. if (tagOption->type == OT_TagValue)
  182. metaData.tags.push_back(removeQuotes(tagOption->value.strValue));
  183. }
  184. }
  185. break;
  186. case OT_Base:
  187. metaData.baseName = removeQuotes(option->value.strValue);
  188. break;
  189. case OT_Inherits:
  190. metaData.inherits.push_back(removeQuotes(option->value.strValue));
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. return metaData;
  197. }
  198. StringID BSLFXCompiler::parseRenderer(const String& name)
  199. {
  200. if (name == "Any")
  201. return RendererAny;
  202. else if (name == "Default")
  203. return RendererDefault;
  204. return RendererAny;
  205. }
  206. void BSLFXCompiler::parseLanguage(const String& name, StringID& renderAPI, String& language)
  207. {
  208. if (name == "HLSL" || name == "HLSL11")
  209. {
  210. renderAPI = RenderAPIDX11;
  211. language = "hlsl";
  212. }
  213. else if (name == "HLSL9")
  214. {
  215. renderAPI = RenderAPIDX9;
  216. language = "hlsl9";
  217. }
  218. else if (name == "GLSL")
  219. {
  220. renderAPI = RenderAPIOpenGL;
  221. language = "glsl";
  222. }
  223. else // "Any"
  224. {
  225. renderAPI = RenderAPIAny;
  226. language = "";
  227. }
  228. }
  229. GpuParamBlockUsage BSLFXCompiler::parseBlockUsage(BufferUsageValue usage)
  230. {
  231. if (usage == BUV_Dynamic)
  232. return GPBU_DYNAMIC;
  233. return GPBU_STATIC;
  234. }
  235. UINT32 BSLFXCompiler::parseFilterMode(FilterValue filter)
  236. {
  237. switch (filter)
  238. {
  239. case FV_Point:
  240. return FO_POINT;
  241. case FV_Linear:
  242. return FO_LINEAR;
  243. case FV_Anisotropic:
  244. return FO_ANISOTROPIC;
  245. case FV_PointCmp:
  246. return FO_POINT | FO_USE_COMPARISON;
  247. case FV_LinearCmp:
  248. return FO_LINEAR | FO_USE_COMPARISON;
  249. case FV_AnisotropicCmp:
  250. return FO_ANISOTROPIC | FO_USE_COMPARISON;
  251. default:
  252. break;
  253. }
  254. return FO_NONE;
  255. }
  256. QueueSortType BSLFXCompiler::parseSortType(QueueSortTypeValue sortType)
  257. {
  258. switch (sortType)
  259. {
  260. case QST_BackToFront:
  261. return QueueSortType::BackToFront;
  262. case QST_FrontToBack:
  263. return QueueSortType::FrontToBack;
  264. case QST_None:
  265. return QueueSortType::None;
  266. }
  267. return QueueSortType::None;
  268. }
  269. CompareFunction BSLFXCompiler::parseCompFunc(CompFuncValue compFunc)
  270. {
  271. switch (compFunc)
  272. {
  273. case CFV_Pass:
  274. return CMPF_ALWAYS_PASS;
  275. case CFV_Fail:
  276. return CMPF_ALWAYS_FAIL;
  277. case CFV_LT:
  278. return CMPF_LESS;
  279. case CFV_LTE:
  280. return CMPF_LESS_EQUAL;
  281. case CFV_EQ:
  282. return CMPF_EQUAL;
  283. case CFV_NEQ:
  284. return CMPF_NOT_EQUAL;
  285. case CFV_GT:
  286. return CMPF_GREATER;
  287. case CFV_GTE:
  288. return CMPF_GREATER_EQUAL;
  289. }
  290. return CMPF_ALWAYS_PASS;
  291. }
  292. TextureAddressingMode BSLFXCompiler::parseAddrMode(AddrModeValue addrMode)
  293. {
  294. switch (addrMode)
  295. {
  296. case AMV_Wrap:
  297. return TAM_WRAP;
  298. case AMV_Mirror:
  299. return TAM_MIRROR;
  300. case AMV_Clamp:
  301. return TAM_CLAMP;
  302. case AMV_Border:
  303. return TAM_BORDER;
  304. }
  305. return TAM_WRAP;
  306. }
  307. BlendFactor BSLFXCompiler::parseBlendFactor(OpValue factor)
  308. {
  309. switch (factor)
  310. {
  311. case OV_One:
  312. return BF_ONE;
  313. case OV_Zero:
  314. return BF_ZERO;
  315. case OV_DestColor:
  316. return BF_DEST_COLOR;
  317. case OV_SrcColor:
  318. return BF_SOURCE_COLOR;
  319. case OV_InvDestColor:
  320. return BF_INV_DEST_COLOR;
  321. case OV_InvSrcColor:
  322. return BF_INV_SOURCE_COLOR;
  323. case OV_DestAlpha:
  324. return BF_DEST_ALPHA;
  325. case OV_SrcAlpha:
  326. return BF_SOURCE_ALPHA;
  327. case OV_InvDestAlpha:
  328. return BF_INV_DEST_ALPHA;
  329. case OV_InvSrcAlpha:
  330. return BF_INV_SOURCE_ALPHA;
  331. default:
  332. break;
  333. }
  334. return BF_ONE;
  335. }
  336. BlendOperation BSLFXCompiler::parseBlendOp(BlendOpValue op)
  337. {
  338. switch (op)
  339. {
  340. case BOV_Add:
  341. return BO_ADD;
  342. case BOV_Max:
  343. return BO_MAX;
  344. case BOV_Min:
  345. return BO_MIN;
  346. case BOV_Subtract:
  347. return BO_SUBTRACT;
  348. case BOV_RevSubtract:
  349. return BO_REVERSE_SUBTRACT;
  350. }
  351. return BO_ADD;
  352. }
  353. void BSLFXCompiler::parseParamType(ParamType type, bool& isObjType, UINT32& typeId)
  354. {
  355. struct ParamData
  356. {
  357. UINT32 type;
  358. bool isObjType;
  359. };
  360. static bool initialized = false;
  361. static ParamData lookup[PT_Count];
  362. if (!initialized)
  363. {
  364. lookup[PT_Float] = { GPDT_FLOAT1, false };
  365. lookup[PT_Float2] = { GPDT_FLOAT2, false };
  366. lookup[PT_Float3] = { GPDT_FLOAT3, false };
  367. lookup[PT_Float4] = { GPDT_FLOAT4, false };
  368. lookup[PT_Int] = { GPDT_INT1, false };
  369. lookup[PT_Int2] = { GPDT_INT2, false };
  370. lookup[PT_Int3] = { GPDT_INT3, false };
  371. lookup[PT_Int4] = { GPDT_INT4, false };
  372. lookup[PT_Color] = { GPDT_COLOR, false };
  373. lookup[PT_Mat2x2] = { GPDT_MATRIX_2X2, false };
  374. lookup[PT_Mat2x3] = { GPDT_MATRIX_2X3, false };
  375. lookup[PT_Mat2x4] = { GPDT_MATRIX_2X4, false };
  376. lookup[PT_Mat3x2] = { GPDT_MATRIX_3X2, false };
  377. lookup[PT_Mat3x3] = { GPDT_MATRIX_3X3, false };
  378. lookup[PT_Mat3x4] = { GPDT_MATRIX_3X4, false };
  379. lookup[PT_Mat4x2] = { GPDT_MATRIX_4X2, false };
  380. lookup[PT_Mat4x3] = { GPDT_MATRIX_4X3, false };
  381. lookup[PT_Mat4x4] = { GPDT_MATRIX_4X4, false };
  382. lookup[PT_Sampler1D] = { GPOT_SAMPLER1D, true };
  383. lookup[PT_Sampler2D] = { GPOT_SAMPLER2D, true };
  384. lookup[PT_Sampler3D] = { GPOT_SAMPLER3D, true };
  385. lookup[PT_SamplerCUBE] = { GPOT_SAMPLERCUBE, true };
  386. lookup[PT_Sampler2DMS] = { GPOT_SAMPLER2DMS, true };
  387. lookup[PT_Texture1D] = { GPOT_TEXTURE1D, true };
  388. lookup[PT_Texture2D] = { GPOT_TEXTURE2D, true };
  389. lookup[PT_Texture3D] = { GPOT_TEXTURE3D, true };
  390. lookup[PT_TextureCUBE] = { GPOT_TEXTURECUBE, true };
  391. lookup[PT_Texture2DMS] = { GPOT_TEXTURE2DMS, true };
  392. lookup[PT_RWTexture1D] = { GPOT_RWTEXTURE1D, true };
  393. lookup[PT_RWTexture2D] = { GPOT_RWTEXTURE2D, true };
  394. lookup[PT_RWTexture3D] = { GPOT_RWTEXTURE3D, true };
  395. lookup[PT_RWTexture2DMS] = { GPOT_RWTEXTURE2DMS, true };
  396. lookup[PT_ByteBuffer] = { GPOT_BYTE_BUFFER, true };
  397. lookup[PT_StructBuffer] = { GPOT_STRUCTURED_BUFFER, true };
  398. lookup[PT_TypedBufferRW] = { GPOT_RWTYPED_BUFFER, true };
  399. lookup[PT_ByteBufferRW] = { GPOT_RWBYTE_BUFFER, true };
  400. lookup[PT_StructBufferRW] = { GPOT_RWSTRUCTURED_BUFFER, true };
  401. lookup[PT_AppendBuffer] = { GPOT_RWAPPEND_BUFFER, true };
  402. lookup[PT_ConsumeBuffer] = { GPOT_RWCONSUME_BUFFER, true };
  403. initialized = true;
  404. }
  405. isObjType = lookup[type].isObjType;
  406. typeId = lookup[type].type;
  407. }
  408. StencilOperation BSLFXCompiler::parseStencilOp(OpValue op)
  409. {
  410. switch (op)
  411. {
  412. case OV_Keep:
  413. return SOP_KEEP;
  414. case OV_Zero:
  415. return SOP_ZERO;
  416. case OV_Replace:
  417. return SOP_REPLACE;
  418. case OV_Incr:
  419. return SOP_INCREMENT;
  420. case OV_Decr:
  421. return SOP_DECREMENT;
  422. case OV_IncrWrap:
  423. return SOP_INCREMENT_WRAP;
  424. case OV_DecrWrap:
  425. return SOP_DECREMENT_WRAP;
  426. case OV_Invert:
  427. return SOP_INVERT;
  428. default:
  429. break;
  430. }
  431. return SOP_KEEP;
  432. }
  433. CullingMode BSLFXCompiler::parseCullMode(CullModeValue cm)
  434. {
  435. switch (cm)
  436. {
  437. case CMV_None:
  438. return CULL_NONE;
  439. case CMV_CW:
  440. return CULL_CLOCKWISE;
  441. case CMV_CCW:
  442. return CULL_COUNTERCLOCKWISE;
  443. }
  444. return CULL_COUNTERCLOCKWISE;
  445. }
  446. PolygonMode BSLFXCompiler::parseFillMode(FillModeValue fm)
  447. {
  448. if (fm == FMV_Wire)
  449. return PM_WIREFRAME;
  450. return PM_SOLID;
  451. }
  452. void BSLFXCompiler::parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  453. {
  454. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  455. return;
  456. for (int i = 0; i < stencilOpNode->options->count; i++)
  457. {
  458. NodeOption* option = &stencilOpNode->options->entries[i];
  459. switch (option->type)
  460. {
  461. case OT_Fail:
  462. desc.frontStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  463. break;
  464. case OT_ZFail:
  465. desc.frontStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  466. break;
  467. case OT_PassOp:
  468. desc.frontStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  469. break;
  470. case OT_CompareFunc:
  471. desc.frontStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  472. break;
  473. default:
  474. break;
  475. }
  476. }
  477. }
  478. void BSLFXCompiler::parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  479. {
  480. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  481. return;
  482. for (int i = 0; i < stencilOpNode->options->count; i++)
  483. {
  484. NodeOption* option = &stencilOpNode->options->entries[i];
  485. switch (option->type)
  486. {
  487. case OT_Fail:
  488. desc.backStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  489. break;
  490. case OT_ZFail:
  491. desc.backStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  492. break;
  493. case OT_PassOp:
  494. desc.backStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  495. break;
  496. case OT_CompareFunc:
  497. desc.backStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. }
  504. void BSLFXCompiler::parseAddrMode(SAMPLER_STATE_DESC& desc, ASTFXNode* addrModeNode)
  505. {
  506. if (addrModeNode == nullptr || addrModeNode->type != NT_AddrMode)
  507. return;
  508. for (int i = 0; i < addrModeNode->options->count; i++)
  509. {
  510. NodeOption* option = &addrModeNode->options->entries[i];
  511. switch (option->type)
  512. {
  513. case OT_U:
  514. desc.addressMode.u = parseAddrMode((AddrModeValue)option->value.intValue);
  515. break;
  516. case OT_V:
  517. desc.addressMode.v = parseAddrMode((AddrModeValue)option->value.intValue);
  518. break;
  519. case OT_W:
  520. desc.addressMode.w = parseAddrMode((AddrModeValue)option->value.intValue);
  521. break;
  522. default:
  523. break;
  524. }
  525. }
  526. }
  527. void BSLFXCompiler::parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  528. {
  529. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  530. return;
  531. for (int i = 0; i < blendDefNode->options->count; i++)
  532. {
  533. NodeOption* option = &blendDefNode->options->entries[i];
  534. switch (option->type)
  535. {
  536. case OT_Source:
  537. desc.srcBlend = parseBlendFactor((OpValue)option->value.intValue);
  538. break;
  539. case OT_Dest:
  540. desc.dstBlend = parseBlendFactor((OpValue)option->value.intValue);
  541. break;
  542. case OT_Op:
  543. desc.blendOp = parseBlendOp((BlendOpValue)option->value.intValue);
  544. break;
  545. default:
  546. break;
  547. }
  548. }
  549. }
  550. void BSLFXCompiler::parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  551. {
  552. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  553. return;
  554. for (int i = 0; i < blendDefNode->options->count; i++)
  555. {
  556. NodeOption* option = &blendDefNode->options->entries[i];
  557. switch (option->type)
  558. {
  559. case OT_Source:
  560. desc.srcBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  561. break;
  562. case OT_Dest:
  563. desc.dstBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  564. break;
  565. case OT_Op:
  566. desc.blendOpAlpha = parseBlendOp((BlendOpValue)option->value.intValue);
  567. break;
  568. default:
  569. break;
  570. }
  571. }
  572. }
  573. void BSLFXCompiler::parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode)
  574. {
  575. if (targetNode == nullptr || targetNode->type != NT_Target)
  576. return;
  577. UINT32 index = 0;
  578. for (int i = 0; i < targetNode->options->count; i++)
  579. {
  580. NodeOption* option = &targetNode->options->entries[i];
  581. switch (option->type)
  582. {
  583. case OT_Index:
  584. index = option->value.intValue;
  585. break;
  586. default:
  587. break;
  588. }
  589. }
  590. if (index >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  591. return;
  592. RENDER_TARGET_BLEND_STATE_DESC& rtDesc = desc.renderTargetDesc[index];
  593. for (int i = 0; i < targetNode->options->count; i++)
  594. {
  595. NodeOption* option = &targetNode->options->entries[i];
  596. switch (option->type)
  597. {
  598. case OT_Blend:
  599. rtDesc.blendEnable = option->value.intValue > 0;
  600. break;
  601. case OT_Color:
  602. parseColorBlendDef(rtDesc, option->value.nodePtr);
  603. break;
  604. case OT_Alpha:
  605. parseAlphaBlendDef(rtDesc, option->value.nodePtr);
  606. break;
  607. case OT_WriteMask:
  608. rtDesc.renderTargetWriteMask = option->value.intValue;
  609. break;
  610. default:
  611. break;
  612. }
  613. }
  614. }
  615. bool BSLFXCompiler::parseBlendState(BLEND_STATE_DESC& desc, ASTFXNode* passNode)
  616. {
  617. if (passNode == nullptr || (passNode->type != NT_Pass && passNode->type != NT_Technique))
  618. return false;
  619. bool isDefault = true;
  620. for (int i = 0; i < passNode->options->count; i++)
  621. {
  622. NodeOption* option = &passNode->options->entries[i];
  623. switch (option->type)
  624. {
  625. case OT_AlphaToCoverage:
  626. desc.alphaToCoverageEnable = option->value.intValue > 0;
  627. isDefault = false;
  628. break;
  629. case OT_IndependantBlend:
  630. desc.independantBlendEnable = option->value.intValue > 0;
  631. isDefault = false;
  632. break;
  633. case OT_Target:
  634. parseRenderTargetBlendState(desc, option->value.nodePtr);
  635. isDefault = false;
  636. break;
  637. default:
  638. break;
  639. }
  640. }
  641. return !isDefault;
  642. }
  643. bool BSLFXCompiler::parseRasterizerState(RASTERIZER_STATE_DESC& desc, ASTFXNode* passNode)
  644. {
  645. if (passNode == nullptr || (passNode->type != NT_Pass && passNode->type != NT_Technique))
  646. return false;
  647. bool isDefault = true;
  648. for (int i = 0; i < passNode->options->count; i++)
  649. {
  650. NodeOption* option = &passNode->options->entries[i];
  651. switch (option->type)
  652. {
  653. case OT_FillMode:
  654. desc.polygonMode = parseFillMode((FillModeValue)option->value.intValue);
  655. isDefault = false;
  656. break;
  657. case OT_CullMode:
  658. desc.cullMode = parseCullMode((CullModeValue)option->value.intValue);
  659. isDefault = false;
  660. break;
  661. case OT_DepthBias:
  662. desc.depthBias = option->value.floatValue;
  663. isDefault = false;
  664. break;
  665. case OT_SDepthBias:
  666. desc.slopeScaledDepthBias = option->value.floatValue;
  667. isDefault = false;
  668. break;
  669. case OT_DepthClip:
  670. desc.depthClipEnable = option->value.intValue > 0;
  671. isDefault = false;
  672. break;
  673. case OT_Scissor:
  674. desc.scissorEnable = option->value.intValue > 0;
  675. isDefault = false;
  676. break;
  677. case OT_Multisample:
  678. desc.multisampleEnable = option->value.intValue > 0;
  679. isDefault = false;
  680. break;
  681. case OT_AALine:
  682. desc.antialiasedLineEnable = option->value.intValue > 0;
  683. isDefault = false;
  684. break;
  685. default:
  686. break;
  687. }
  688. }
  689. return !isDefault;
  690. }
  691. bool BSLFXCompiler::parseDepthStencilState(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* passNode)
  692. {
  693. if (passNode == nullptr || (passNode->type != NT_Pass && passNode->type != NT_Technique))
  694. return false;
  695. bool isDefault = true;
  696. for (int i = 0; i < passNode->options->count; i++)
  697. {
  698. NodeOption* option = &passNode->options->entries[i];
  699. switch (option->type)
  700. {
  701. case OT_DepthRead:
  702. desc.depthReadEnable = option->value.intValue > 0;
  703. isDefault = false;
  704. break;
  705. case OT_DepthWrite:
  706. desc.depthWriteEnable = option->value.intValue > 0;
  707. isDefault = false;
  708. break;
  709. case OT_CompareFunc:
  710. desc.depthComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  711. isDefault = false;
  712. break;
  713. case OT_Stencil:
  714. desc.stencilEnable = option->value.intValue > 0;
  715. isDefault = false;
  716. break;
  717. case OT_StencilReadMask:
  718. desc.stencilReadMask = (UINT8)option->value.intValue;
  719. isDefault = false;
  720. break;
  721. case OT_StencilWriteMask:
  722. desc.stencilWriteMask = (UINT8)option->value.intValue;
  723. isDefault = false;
  724. break;
  725. case OT_StencilOpFront:
  726. parseStencilFront(desc, option->value.nodePtr);
  727. isDefault = false;
  728. break;
  729. case OT_StencilOpBack:
  730. parseStencilBack(desc, option->value.nodePtr);
  731. isDefault = false;
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. return !isDefault;
  738. }
  739. SPtr<SamplerState> BSLFXCompiler::parseSamplerState(ASTFXNode* samplerStateNode)
  740. {
  741. if (samplerStateNode == nullptr || samplerStateNode->type != NT_SamplerState)
  742. return nullptr;
  743. SAMPLER_STATE_DESC desc;
  744. bool isDefault = true;
  745. for (int i = 0; i < samplerStateNode->options->count; i++)
  746. {
  747. NodeOption* option = &samplerStateNode->options->entries[i];
  748. switch (option->type)
  749. {
  750. case OT_AddrMode:
  751. parseAddrMode(desc, option->value.nodePtr);
  752. isDefault = false;
  753. break;
  754. case OT_MinFilter:
  755. desc.minFilter = (FilterOptions)parseFilterMode((FilterValue)option->value.intValue);
  756. isDefault = false;
  757. break;
  758. case OT_MagFilter:
  759. desc.magFilter = (FilterOptions)parseFilterMode((FilterValue)option->value.intValue);
  760. isDefault = false;
  761. break;
  762. case OT_MipFilter:
  763. desc.mipFilter = (FilterOptions)parseFilterMode((FilterValue)option->value.intValue);
  764. isDefault = false;
  765. break;
  766. case OT_MaxAniso:
  767. desc.maxAniso = option->value.intValue;
  768. isDefault = false;
  769. break;
  770. case OT_MipBias:
  771. desc.mipmapBias = option->value.floatValue;
  772. isDefault = false;
  773. break;
  774. case OT_MipMin:
  775. desc.mipMin = option->value.floatValue;
  776. isDefault = false;
  777. break;
  778. case OT_MipMax:
  779. desc.mipMax = option->value.floatValue;
  780. isDefault = false;
  781. break;
  782. case OT_BorderColor:
  783. desc.borderColor = Color(option->value.matrixValue[0], option->value.matrixValue[1],
  784. option->value.matrixValue[2], option->value.matrixValue[3]);
  785. isDefault = false;
  786. break;
  787. case OT_CompareFunc:
  788. desc.comparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  789. isDefault = false;
  790. break;
  791. default:
  792. break;
  793. }
  794. }
  795. if (isDefault)
  796. return nullptr;
  797. return SamplerState::create(desc);
  798. }
  799. void BSLFXCompiler::parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData)
  800. {
  801. if (codeNode == nullptr || (codeNode->type != NT_CodeCommon && codeNode->type != NT_CodeVertex &&
  802. codeNode->type != NT_CodeFragment && codeNode->type != NT_CodeGeometry && codeNode->type != NT_CodeHull &&
  803. codeNode->type != NT_CodeDomain && codeNode->type != NT_CodeCompute))
  804. {
  805. return;
  806. }
  807. UINT32 index = (UINT32)-1;
  808. for (int j = 0; j < codeNode->options->count; j++)
  809. {
  810. if (codeNode->options->entries[j].type == OT_Index)
  811. index = codeNode->options->entries[j].value.intValue;
  812. }
  813. if (index != (UINT32)-1 && index < (UINT32)codeBlocks.size())
  814. {
  815. switch (codeNode->type)
  816. {
  817. case NT_CodeVertex:
  818. passData.vertexCode += codeBlocks[index];
  819. break;
  820. case NT_CodeFragment:
  821. passData.fragmentCode += codeBlocks[index];
  822. break;
  823. case NT_CodeGeometry:
  824. passData.geometryCode += codeBlocks[index];
  825. break;
  826. case NT_CodeHull:
  827. passData.hullCode += codeBlocks[index];
  828. break;
  829. case NT_CodeDomain:
  830. passData.domainCode += codeBlocks[index];
  831. break;
  832. case NT_CodeCompute:
  833. passData.computeCode += codeBlocks[index];
  834. break;
  835. case NT_CodeCommon:
  836. passData.commonCode += codeBlocks[index];
  837. break;
  838. default:
  839. break;
  840. }
  841. }
  842. }
  843. void BSLFXCompiler::parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData)
  844. {
  845. if (passNode == nullptr || passNode->type != NT_Pass)
  846. return;
  847. passData.blendIsDefault &= !parseBlendState(passData.blendDesc, passNode);
  848. passData.rasterizerIsDefault &= !parseRasterizerState(passData.rasterizerDesc, passNode);
  849. passData.depthStencilIsDefault &= !parseDepthStencilState(passData.depthStencilDesc, passNode);
  850. for (int i = 0; i < passNode->options->count; i++)
  851. {
  852. NodeOption* option = &passNode->options->entries[i];
  853. switch (option->type)
  854. {
  855. case OT_StencilRef:
  856. passData.stencilRefValue = option->value.intValue;
  857. break;
  858. case OT_Code:
  859. parseCodeBlock(option->value.nodePtr, codeBlocks, passData);
  860. break;
  861. default:
  862. break;
  863. }
  864. }
  865. }
  866. void BSLFXCompiler::parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData)
  867. {
  868. if (techniqueNode == nullptr || techniqueNode->type != NT_Technique)
  869. return;
  870. PassData combinedCommonPassData;
  871. UINT32 nextPassIdx = 0;
  872. // Go in reverse because options are added in reverse order during parsing
  873. for (int i = techniqueNode->options->count - 1; i >= 0; i--)
  874. {
  875. NodeOption* option = &techniqueNode->options->entries[i];
  876. switch (option->type)
  877. {
  878. case OT_Pass:
  879. {
  880. UINT32 passIdx = nextPassIdx;
  881. ASTFXNode* passNode = option->value.nodePtr;
  882. for (int j = 0; j < passNode->options->count; j++)
  883. {
  884. NodeOption* passOption = &passNode->options->entries[j];
  885. switch (passOption->type)
  886. {
  887. case OT_Index:
  888. passIdx = passOption->value.intValue;
  889. break;
  890. default:
  891. break;
  892. }
  893. }
  894. PassData* passData = nullptr;
  895. for (auto& entry : techniqueData.passes)
  896. {
  897. if (entry.seqIdx == passIdx)
  898. passData = &entry;
  899. }
  900. if (passData == nullptr)
  901. {
  902. techniqueData.passes.push_back(PassData());
  903. passData = &techniqueData.passes.back();
  904. passData->seqIdx = passIdx;
  905. }
  906. nextPassIdx = std::max(nextPassIdx, passIdx) + 1;
  907. passData->vertexCode = combinedCommonPassData.vertexCode + passData->vertexCode;
  908. passData->fragmentCode = combinedCommonPassData.fragmentCode + passData->fragmentCode;
  909. passData->geometryCode = combinedCommonPassData.geometryCode + passData->geometryCode;
  910. passData->hullCode = combinedCommonPassData.hullCode + passData->hullCode;
  911. passData->domainCode = combinedCommonPassData.domainCode + passData->domainCode;
  912. passData->computeCode = combinedCommonPassData.computeCode + passData->computeCode;
  913. passData->commonCode = combinedCommonPassData.commonCode + passData->commonCode;
  914. parsePass(passNode, codeBlocks, *passData);
  915. }
  916. break;
  917. case OT_Code:
  918. {
  919. PassData commonPassData;
  920. parseCodeBlock(option->value.nodePtr, codeBlocks, commonPassData);
  921. for (auto& passData : techniqueData.passes)
  922. {
  923. passData.vertexCode += commonPassData.vertexCode;
  924. passData.fragmentCode += commonPassData.fragmentCode;
  925. passData.geometryCode += commonPassData.geometryCode;
  926. passData.hullCode += commonPassData.hullCode;
  927. passData.domainCode += commonPassData.domainCode;
  928. passData.computeCode += commonPassData.computeCode;
  929. passData.commonCode += commonPassData.commonCode;
  930. }
  931. combinedCommonPassData.vertexCode += commonPassData.vertexCode;
  932. combinedCommonPassData.fragmentCode += commonPassData.fragmentCode;
  933. combinedCommonPassData.geometryCode += commonPassData.geometryCode;
  934. combinedCommonPassData.hullCode += commonPassData.hullCode;
  935. combinedCommonPassData.domainCode += commonPassData.domainCode;
  936. combinedCommonPassData.computeCode += commonPassData.computeCode;
  937. combinedCommonPassData.commonCode += commonPassData.commonCode;
  938. }
  939. break;
  940. default:
  941. break;
  942. }
  943. }
  944. // Parse common pass states
  945. for (auto& passData : techniqueData.passes)
  946. {
  947. passData.blendIsDefault &= !parseBlendState(passData.blendDesc, techniqueNode);
  948. passData.rasterizerIsDefault &= !parseRasterizerState(passData.rasterizerDesc, techniqueNode);
  949. passData.depthStencilIsDefault &= !parseDepthStencilState(passData.depthStencilDesc, techniqueNode);
  950. }
  951. }
  952. void BSLFXCompiler::parseParameters(SHADER_DESC& desc, ASTFXNode* parametersNode)
  953. {
  954. if (parametersNode == nullptr || parametersNode->type != NT_Parameters)
  955. return;
  956. for (int i = 0; i < parametersNode->options->count; i++)
  957. {
  958. NodeOption* option = &parametersNode->options->entries[i];
  959. if (option->type != OT_Parameter)
  960. continue;
  961. ASTFXNode* parameter = option->value.nodePtr;
  962. String name;
  963. String alias;
  964. UINT32 typeId = 0;
  965. bool isObjType = false;
  966. StringID semantic;
  967. SPtr<SamplerState> samplerState = nullptr;
  968. float defaultValue[16];
  969. HTexture defaultTexture;
  970. bool hasDefaultValue = false;
  971. for (int j = 0; j < parameter->options->count; j++)
  972. {
  973. NodeOption* paramOption = &parameter->options->entries[j];
  974. switch (paramOption->type)
  975. {
  976. case OT_Identifier:
  977. name = paramOption->value.strValue;
  978. break;
  979. case OT_Alias:
  980. alias = removeQuotes(paramOption->value.strValue);
  981. break;
  982. case OT_ParamType:
  983. parseParamType((ParamType)paramOption->value.intValue, isObjType, typeId);
  984. break;
  985. case OT_ParamValue:
  986. memcpy(defaultValue, paramOption->value.matrixValue, sizeof(defaultValue));
  987. hasDefaultValue = true;
  988. break;
  989. case OT_ParamStrValue:
  990. {
  991. String defaultTextureName = removeQuotes(paramOption->value.strValue);
  992. defaultTexture = getBuiltinTexture(defaultTextureName);
  993. hasDefaultValue = true;
  994. }
  995. break;
  996. case OT_Auto:
  997. semantic = removeQuotes(paramOption->value.strValue);
  998. break;
  999. case OT_SamplerState:
  1000. samplerState = parseSamplerState(paramOption->value.nodePtr);
  1001. break;
  1002. default:
  1003. break;
  1004. }
  1005. }
  1006. if (name.empty())
  1007. continue;
  1008. auto addParameter = [&](const String& paramName, const String& gpuVarName)
  1009. {
  1010. if (isObjType)
  1011. {
  1012. GpuParamObjectType objType = (GpuParamObjectType)typeId;
  1013. if (Shader::isSampler(objType))
  1014. {
  1015. if(hasDefaultValue)
  1016. desc.addParameter(paramName, gpuVarName, objType, samplerState, semantic);
  1017. else
  1018. desc.addParameter(paramName, gpuVarName, objType, semantic);
  1019. }
  1020. else if(Shader::isTexture(objType))
  1021. {
  1022. if(hasDefaultValue)
  1023. desc.addParameter(paramName, gpuVarName, objType, defaultTexture, semantic);
  1024. else
  1025. desc.addParameter(paramName, gpuVarName, objType, semantic);
  1026. }
  1027. else
  1028. desc.addParameter(paramName, gpuVarName, objType, semantic);
  1029. }
  1030. else
  1031. {
  1032. if (hasDefaultValue)
  1033. desc.addParameter(paramName, gpuVarName, (GpuParamDataType)typeId, semantic, 1, 0, (UINT8*)defaultValue);
  1034. else
  1035. desc.addParameter(paramName, gpuVarName, (GpuParamDataType)typeId, semantic);
  1036. }
  1037. };
  1038. addParameter(name, name);
  1039. if (!alias.empty())
  1040. addParameter(name, alias);
  1041. }
  1042. }
  1043. void BSLFXCompiler::parseBlocks(SHADER_DESC& desc, ASTFXNode* blocksNode)
  1044. {
  1045. if (blocksNode == nullptr || blocksNode->type != NT_Blocks)
  1046. return;
  1047. for (int i = 0; i < blocksNode->options->count; i++)
  1048. {
  1049. NodeOption* option = &blocksNode->options->entries[i];
  1050. if (option->type != OT_Block)
  1051. continue;
  1052. ASTFXNode* parameter = option->value.nodePtr;
  1053. String name;
  1054. bool shared = false;
  1055. GpuParamBlockUsage usage = GPBU_STATIC;
  1056. StringID semantic;
  1057. for (int j = 0; j < parameter->options->count; j++)
  1058. {
  1059. NodeOption* paramOption = &parameter->options->entries[j];
  1060. switch (paramOption->type)
  1061. {
  1062. case OT_Identifier:
  1063. name = paramOption->value.strValue;
  1064. break;
  1065. case OT_Shared:
  1066. shared = paramOption->value.intValue > 0;
  1067. break;
  1068. case OT_Usage:
  1069. usage = parseBlockUsage((BufferUsageValue)paramOption->value.intValue);
  1070. break;
  1071. case OT_Auto:
  1072. semantic = removeQuotes(paramOption->value.strValue);
  1073. break;
  1074. default:
  1075. break;
  1076. }
  1077. }
  1078. if (name.empty())
  1079. continue;
  1080. desc.setParamBlockAttribs(name, shared, usage, semantic);
  1081. }
  1082. }
  1083. BSLFXCompileResult BSLFXCompiler::parseShader(const String& name, ParseState* parseState, Vector<String>& codeBlocks)
  1084. {
  1085. BSLFXCompileResult output;
  1086. if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
  1087. {
  1088. parseStateDelete(parseState);
  1089. output.errorMessage = "Root not is null or not a shader.";
  1090. return output;
  1091. }
  1092. SHADER_DESC shaderDesc;
  1093. Vector<pair<ASTFXNode*, TechniqueData>> techniqueData;
  1094. // Go in reverse because options are added in reverse order during parsing
  1095. for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
  1096. {
  1097. NodeOption* option = &parseState->rootNode->options->entries[i];
  1098. switch (option->type)
  1099. {
  1100. case OT_Separable:
  1101. shaderDesc.separablePasses = option->value.intValue > 1;
  1102. break;
  1103. case OT_Sort:
  1104. shaderDesc.queueSortType = parseSortType((QueueSortTypeValue)option->value.intValue);
  1105. break;
  1106. case OT_Priority:
  1107. shaderDesc.queuePriority = option->value.intValue;
  1108. break;
  1109. case OT_Transparent:
  1110. shaderDesc.flags |= (UINT32)ShaderFlags::Transparent;
  1111. break;
  1112. case OT_Technique:
  1113. {
  1114. TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
  1115. techniqueData.push_back(std::make_pair(option->value.nodePtr, TechniqueData()));
  1116. TechniqueData& data = techniqueData.back().second;
  1117. data.metaData = metaData;
  1118. if (data.metaData.baseName.empty())
  1119. {
  1120. std::function<bool(TechniqueMetaData&)> parseInherited = [&](TechniqueMetaData& metaData)
  1121. {
  1122. for (auto riter = metaData.inherits.rbegin(); riter != metaData.inherits.rend(); ++riter)
  1123. {
  1124. const String& inherits = *riter;
  1125. bool foundBase = false;
  1126. for (auto& entry : techniqueData)
  1127. {
  1128. if (entry.second.metaData.baseName == inherits)
  1129. {
  1130. bool matches = entry.second.metaData.language == metaData.language || entry.second.metaData.language == "Any";
  1131. matches &= entry.second.metaData.renderer == metaData.renderer || entry.second.metaData.renderer == RendererAny;
  1132. if (matches)
  1133. {
  1134. if (!parseInherited(entry.second.metaData))
  1135. return false;
  1136. parseTechnique(entry.first, codeBlocks, data);
  1137. foundBase = true;
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. if (!foundBase)
  1143. {
  1144. output.errorMessage = "Base technique \"" + inherits + "\" cannot be found.";
  1145. return false;
  1146. }
  1147. }
  1148. return true;
  1149. };
  1150. if (!parseInherited(metaData))
  1151. {
  1152. parseStateDelete(parseState);
  1153. return output;
  1154. }
  1155. parseTechnique(option->value.nodePtr, codeBlocks, data);
  1156. }
  1157. break;
  1158. }
  1159. case OT_Parameters:
  1160. parseParameters(shaderDesc, option->value.nodePtr);
  1161. break;
  1162. case OT_Blocks:
  1163. parseBlocks(shaderDesc, option->value.nodePtr);
  1164. break;
  1165. default:
  1166. break;
  1167. }
  1168. }
  1169. Vector<SPtr<Technique>> techniques;
  1170. for(auto& entry : techniqueData)
  1171. {
  1172. const TechniqueMetaData& metaData = entry.second.metaData;
  1173. if (!metaData.baseName.empty())
  1174. continue;
  1175. Map<UINT32, SPtr<Pass>, std::greater<UINT32>> passes;
  1176. for (auto& passData : entry.second.passes)
  1177. {
  1178. PASS_DESC passDesc;
  1179. if (!passData.blendIsDefault)
  1180. passDesc.blendState = BlendState::create(passData.blendDesc);
  1181. if (!passData.rasterizerIsDefault)
  1182. passDesc.rasterizerState = RasterizerState::create(passData.rasterizerDesc);
  1183. if (!passData.depthStencilIsDefault)
  1184. passDesc.depthStencilState = DepthStencilState::create(passData.depthStencilDesc);
  1185. GPU_PROGRAM_DESC desc;
  1186. desc.entryPoint = "main";
  1187. desc.language = metaData.language;
  1188. if (!passData.vertexCode.empty())
  1189. {
  1190. desc.source = passData.commonCode + passData.vertexCode;
  1191. desc.type = GPT_VERTEX_PROGRAM;
  1192. desc.profile = getProfile(metaData.renderAPI, GPT_VERTEX_PROGRAM);
  1193. passDesc.vertexProgram = GpuProgram::create(desc);
  1194. }
  1195. if (!passData.fragmentCode.empty())
  1196. {
  1197. desc.source = passData.commonCode + passData.fragmentCode;
  1198. desc.type = GPT_FRAGMENT_PROGRAM;
  1199. desc.profile = getProfile(metaData.renderAPI, GPT_FRAGMENT_PROGRAM);
  1200. passDesc.fragmentProgram = GpuProgram::create(desc);
  1201. }
  1202. if (!passData.geometryCode.empty())
  1203. {
  1204. desc.source = passData.commonCode + passData.geometryCode;
  1205. desc.type = GPT_GEOMETRY_PROGRAM;
  1206. desc.profile = getProfile(metaData.renderAPI, GPT_GEOMETRY_PROGRAM);
  1207. passDesc.geometryProgram = GpuProgram::create(desc);
  1208. }
  1209. if (!passData.hullCode.empty())
  1210. {
  1211. desc.source = passData.commonCode + passData.hullCode;
  1212. desc.type = GPT_HULL_PROGRAM;
  1213. desc.profile = getProfile(metaData.renderAPI, GPT_HULL_PROGRAM);
  1214. passDesc.hullProgram = GpuProgram::create(desc);
  1215. }
  1216. if (!passData.domainCode.empty())
  1217. {
  1218. desc.source = passData.commonCode + passData.domainCode;
  1219. desc.type = GPT_DOMAIN_PROGRAM;
  1220. desc.profile = getProfile(metaData.renderAPI, GPT_DOMAIN_PROGRAM);
  1221. passDesc.domainProgram = GpuProgram::create(desc);
  1222. }
  1223. if (!passData.computeCode.empty())
  1224. {
  1225. desc.source = passData.commonCode + passData.computeCode;
  1226. desc.type = GPT_COMPUTE_PROGRAM;
  1227. desc.profile = getProfile(metaData.renderAPI, GPT_COMPUTE_PROGRAM);
  1228. passDesc.computeProgram = GpuProgram::create(desc);
  1229. }
  1230. passDesc.stencilRefValue = passData.stencilRefValue;
  1231. SPtr<Pass> pass = Pass::create(passDesc);
  1232. if (pass != nullptr)
  1233. passes[passData.seqIdx] = pass;
  1234. }
  1235. Vector<SPtr<Pass>> orderedPasses;
  1236. for (auto& KVP : passes)
  1237. orderedPasses.push_back(KVP.second);
  1238. if (orderedPasses.size() > 0)
  1239. {
  1240. SPtr<Technique> technique = Technique::create(metaData.renderAPI, metaData.renderer,
  1241. metaData.tags, orderedPasses);
  1242. techniques.push_back(technique);
  1243. }
  1244. }
  1245. Vector<String> includes;
  1246. IncludeLink* includeLink = parseState->includes;
  1247. while(includeLink != nullptr)
  1248. {
  1249. String includeFilename = includeLink->data->filename;
  1250. auto iterFind = std::find(includes.begin(), includes.end(), includeFilename);
  1251. if (iterFind == includes.end())
  1252. includes.push_back(includeFilename);
  1253. includeLink = includeLink->next;
  1254. }
  1255. parseStateDelete(parseState);
  1256. output.shader = Shader::_createPtr(name, shaderDesc, techniques);
  1257. output.shader->setIncludeFiles(includes);
  1258. return output;
  1259. }
  1260. String BSLFXCompiler::removeQuotes(const char* input)
  1261. {
  1262. UINT32 len = (UINT32)strlen(input);
  1263. String output(len - 2, ' ');
  1264. for (UINT32 i = 0; i < (len - 2); i++)
  1265. output[i] = input[i + 1];
  1266. return output;
  1267. }
  1268. GpuProgramProfile BSLFXCompiler::getProfile(const StringID& renderAPI, GpuProgramType type)
  1269. {
  1270. StringID target = renderAPI;
  1271. if (target == RenderAPIAny)
  1272. target = RenderAPICore::instance().getName();
  1273. if (target == RenderAPIDX11 || target == RenderAPIOpenGL)
  1274. {
  1275. switch (type)
  1276. {
  1277. case GPT_VERTEX_PROGRAM:
  1278. return GPP_VS_5_0;
  1279. case GPT_FRAGMENT_PROGRAM:
  1280. return GPP_FS_5_0;
  1281. case GPT_GEOMETRY_PROGRAM:
  1282. return GPP_GS_5_0;
  1283. case GPT_HULL_PROGRAM:
  1284. return GPP_HS_5_0;
  1285. case GPT_DOMAIN_PROGRAM:
  1286. return GPP_DS_5_0;
  1287. case GPT_COMPUTE_PROGRAM:
  1288. return GPP_CS_5_0;
  1289. }
  1290. }
  1291. else if (target == RenderAPIDX9)
  1292. {
  1293. switch (type)
  1294. {
  1295. case GPT_VERTEX_PROGRAM:
  1296. return GPP_VS_3_0;
  1297. case GPT_FRAGMENT_PROGRAM:
  1298. return GPP_FS_3_0;
  1299. default:
  1300. break;
  1301. }
  1302. }
  1303. return GPP_NONE;
  1304. }
  1305. HTexture BSLFXCompiler::getBuiltinTexture(const String& name)
  1306. {
  1307. if (StringUtil::compare(name, String("white"), false) == 0)
  1308. return BuiltinResources::getTexture(BuiltinTexture::White);
  1309. else if (StringUtil::compare(name, String("black"), false) == 0)
  1310. return BuiltinResources::getTexture(BuiltinTexture::Black);
  1311. else if (StringUtil::compare(name, String("normal"), false) == 0)
  1312. return BuiltinResources::getTexture(BuiltinTexture::Normal);
  1313. return HTexture();
  1314. }
  1315. }