2
0

BsSLFXCompiler.cpp 45 KB

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