BsSLFXCompiler.cpp 39 KB

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