BsSLFXCompiler.cpp 49 KB

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