BsSLFXCompiler.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. passDesc.stencilRefValue = passData.stencilRefValue;
  1083. PassPtr pass = Pass::create(passDesc);
  1084. if (pass != nullptr)
  1085. passes[passData.seqIdx] = pass;
  1086. }
  1087. Vector<PassPtr> orderedPasses;
  1088. for (auto& KVP : passes)
  1089. orderedPasses.push_back(KVP.second);
  1090. if (orderedPasses.size() > 0)
  1091. {
  1092. TechniquePtr technique = Technique::create(techniqueData.renderAPI, techniqueData.renderer, orderedPasses);
  1093. techniques.push_back(technique);
  1094. }
  1095. }
  1096. Vector<String> includes;
  1097. IncludeLink* includeLink = parseState->includes;
  1098. while(includeLink != nullptr)
  1099. {
  1100. String includeFilename = includeLink->data->filename;
  1101. auto iterFind = std::find(includes.begin(), includes.end(), includeFilename);
  1102. if (iterFind == includes.end())
  1103. includes.push_back(includeFilename);
  1104. includeLink = includeLink->next;
  1105. }
  1106. parseStateDelete(parseState);
  1107. output.shader = Shader::_createPtr(name, shaderDesc, techniques);
  1108. output.shader->setIncludeFiles(includes);
  1109. return output;
  1110. }
  1111. String BSLFXCompiler::removeQuotes(const char* input)
  1112. {
  1113. UINT32 len = (UINT32)strlen(input);
  1114. String output(len - 2, ' ');
  1115. for (UINT32 i = 0; i < (len - 2); i++)
  1116. output[i] = input[i + 1];
  1117. return output;
  1118. }
  1119. GpuProgramProfile BSLFXCompiler::getProfile(const StringID& renderAPI, GpuProgramType type)
  1120. {
  1121. StringID target = renderAPI;
  1122. if (target == RenderAPIAny)
  1123. target = RenderAPICore::instance().getName();
  1124. if (target == RenderAPIDX11 || target == RenderAPIOpenGL)
  1125. {
  1126. switch (type)
  1127. {
  1128. case GPT_VERTEX_PROGRAM:
  1129. return GPP_VS_5_0;
  1130. case GPT_FRAGMENT_PROGRAM:
  1131. return GPP_FS_5_0;
  1132. case GPT_GEOMETRY_PROGRAM:
  1133. return GPP_GS_5_0;
  1134. case GPT_HULL_PROGRAM:
  1135. return GPP_HS_5_0;
  1136. case GPT_DOMAIN_PROGRAM:
  1137. return GPP_DS_5_0;
  1138. case GPT_COMPUTE_PROGRAM:
  1139. return GPP_CS_5_0;
  1140. }
  1141. }
  1142. else if (target == RenderAPIDX9)
  1143. {
  1144. switch (type)
  1145. {
  1146. case GPT_VERTEX_PROGRAM:
  1147. return GPP_VS_3_0;
  1148. case GPT_FRAGMENT_PROGRAM:
  1149. return GPP_FS_3_0;
  1150. }
  1151. }
  1152. return GPP_NONE;
  1153. }
  1154. HTexture BSLFXCompiler::getBuiltinTexture(const String& name)
  1155. {
  1156. if (StringUtil::compare(name, String("white"), false) == 0)
  1157. return BuiltinResources::getTexture(BuiltinTexture::White);
  1158. else if (StringUtil::compare(name, String("black"), false) == 0)
  1159. return BuiltinResources::getTexture(BuiltinTexture::Black);
  1160. else if (StringUtil::compare(name, String("normal"), false) == 0)
  1161. return BuiltinResources::getTexture(BuiltinTexture::Normal);
  1162. return HTexture();
  1163. }
  1164. }