BsSLFXCompiler.cpp 37 KB

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