BsSLFXCompiler.cpp 38 KB

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