BsSLFXCompiler.cpp 45 KB

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