BsSLFXCompiler.cpp 37 KB

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