BsSLFXCompiler.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsSLFXCompiler.h"
  4. #include "RenderAPI/BsGpuProgram.h"
  5. #include <regex>
  6. #include "Material/BsShader.h"
  7. #include "Material/BsTechnique.h"
  8. #include "Material/BsPass.h"
  9. #include "RenderAPI/BsSamplerState.h"
  10. #include "RenderAPI/BsRenderAPI.h"
  11. #include "Debug/BsDebug.h"
  12. #include "Material/BsShaderManager.h"
  13. #include "Material/BsShaderInclude.h"
  14. #include "Math/BsMatrix4.h"
  15. #include "Resources/BsBuiltinResources.h"
  16. #define XSC_ENABLE_LANGUAGE_EXT 1
  17. #include "Xsc/Xsc.h"
  18. #include "Material/BsShaderVariation.h"
  19. extern "C" {
  20. #include "BsMMAlloc.h"
  21. #include "BsParserFX.h"
  22. #include "BsLexerFX.h"
  23. }
  24. using namespace std;
  25. namespace bs
  26. {
  27. // Print out the FX AST, only for debug purposes
  28. void SLFXDebugPrint(ASTFXNode* node, String indent)
  29. {
  30. LOGDBG(indent + "NODE " + toString(node->type));
  31. for (int i = 0; i < node->options->count; i++)
  32. {
  33. OptionDataType odt = OPTION_LOOKUP[(int)node->options->entries[i].type].dataType;
  34. if (odt == ODT_Complex)
  35. {
  36. LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type));
  37. SLFXDebugPrint(node->options->entries[i].value.nodePtr, indent + "\t");
  38. continue;
  39. }
  40. String value;
  41. switch (odt)
  42. {
  43. case ODT_Bool:
  44. value = toString(node->options->entries[i].value.intValue != 0);
  45. break;
  46. case ODT_Int:
  47. value = toString(node->options->entries[i].value.intValue);
  48. break;
  49. case ODT_Float:
  50. value = toString(node->options->entries[i].value.floatValue);
  51. break;
  52. case ODT_String:
  53. value = node->options->entries[i].value.strValue;
  54. break;
  55. case ODT_Matrix:
  56. {
  57. Matrix4 mat4 = *(Matrix4*)(node->options->entries[i].value.matrixValue);
  58. value = toString(mat4);
  59. }
  60. break;
  61. default:
  62. break;
  63. }
  64. LOGDBG(indent + toString(i) + ". " + toString(node->options->entries[i].type) + " = " + value);
  65. }
  66. }
  67. class XscLog : public Xsc::Log
  68. {
  69. public:
  70. void SubmitReport(const Xsc::Report& report) override
  71. {
  72. switch (report.Type())
  73. {
  74. case Xsc::ReportTypes::Info:
  75. mInfos.push_back({ FullIndent(), report });
  76. break;
  77. case Xsc::ReportTypes::Warning:
  78. mWarnings.push_back({ FullIndent(), report });
  79. break;
  80. case Xsc::ReportTypes::Error:
  81. mErrors.push_back({ FullIndent(), report });
  82. break;
  83. }
  84. }
  85. void getMessages(StringStream& output)
  86. {
  87. printAndClearReports(output, mInfos);
  88. printAndClearReports(output, mWarnings, (mWarnings.size() == 1 ? "WARNING" : "WARNINGS"));
  89. printAndClearReports(output, mErrors, (mErrors.size() == 1 ? "ERROR" : "ERRORS"));
  90. }
  91. private:
  92. struct IndentReport
  93. {
  94. std::string indent;
  95. Xsc::Report report;
  96. };
  97. static void printMultiLineString(StringStream& output, const std::string& str, const std::string& indent)
  98. {
  99. // Determine at which position the actual text begins (excluding the "error (X:Y) : " or the like)
  100. auto textStartPos = str.find(" : ");
  101. if (textStartPos != std::string::npos)
  102. textStartPos += 3;
  103. else
  104. textStartPos = 0;
  105. std::string newLineIndent(textStartPos, ' ');
  106. size_t start = 0;
  107. bool useNewLineIndent = false;
  108. while (start < str.size())
  109. {
  110. output << indent;
  111. if (useNewLineIndent)
  112. output << newLineIndent;
  113. // Print next line
  114. auto end = str.find('\n', start);
  115. if (end != std::string::npos)
  116. {
  117. output << str.substr(start, end - start);
  118. start = end + 1;
  119. }
  120. else
  121. {
  122. output << str.substr(start);
  123. start = end;
  124. }
  125. output << std::endl;
  126. useNewLineIndent = true;
  127. }
  128. }
  129. void printReport(StringStream& output, const IndentReport& r)
  130. {
  131. // Print optional context description
  132. if (!r.report.Context().empty())
  133. printMultiLineString(output, r.report.Context(), r.indent);
  134. // Print report message
  135. const auto& msg = r.report.Message();
  136. printMultiLineString(output, msg, r.indent);
  137. // Print optional line and line-marker
  138. if (r.report.HasLine())
  139. {
  140. const auto& line = r.report.Line();
  141. const auto& marker = r.report.Marker();
  142. // Print line
  143. output << r.indent << line << std::endl;
  144. // Print line marker
  145. output << r.indent << marker << std::endl;
  146. }
  147. // Print optional hints
  148. for (const auto& hint : r.report.GetHints())
  149. output << r.indent << hint << std::endl;
  150. }
  151. void printAndClearReports(StringStream& output, Vector<IndentReport>& reports, const String& headline = "")
  152. {
  153. if (!reports.empty())
  154. {
  155. if (!headline.empty())
  156. {
  157. String s = toString((UINT32)reports.size()) + " " + headline;
  158. output << s << std::endl;
  159. output << String(s.size(), '-') << std::endl;
  160. }
  161. for (const auto& r : reports)
  162. printReport(output, r);
  163. reports.clear();
  164. }
  165. }
  166. Vector<IndentReport> mInfos;
  167. Vector<IndentReport> mWarnings;
  168. Vector<IndentReport> mErrors;
  169. };
  170. GpuParamObjectType ReflTypeToTextureType(Xsc::Reflection::BufferType type)
  171. {
  172. switch(type)
  173. {
  174. case Xsc::Reflection::BufferType::RWTexture1D: return GPOT_RWTEXTURE1D;
  175. case Xsc::Reflection::BufferType::RWTexture1DArray: return GPOT_RWTEXTURE1DARRAY;
  176. case Xsc::Reflection::BufferType::RWTexture2D: return GPOT_RWTEXTURE2D;
  177. case Xsc::Reflection::BufferType::RWTexture2DArray: return GPOT_RWTEXTURE2DARRAY;
  178. case Xsc::Reflection::BufferType::RWTexture3D: return GPOT_RWTEXTURE3D;
  179. case Xsc::Reflection::BufferType::Texture1D: return GPOT_TEXTURE1D;
  180. case Xsc::Reflection::BufferType::Texture1DArray: return GPOT_TEXTURE1DARRAY;
  181. case Xsc::Reflection::BufferType::Texture2D: return GPOT_TEXTURE2D;
  182. case Xsc::Reflection::BufferType::Texture2DArray: return GPOT_TEXTURE2DARRAY;
  183. case Xsc::Reflection::BufferType::Texture3D: return GPOT_TEXTURE3D;
  184. case Xsc::Reflection::BufferType::TextureCube: return GPOT_TEXTURECUBE;
  185. case Xsc::Reflection::BufferType::TextureCubeArray: return GPOT_TEXTURECUBEARRAY;
  186. case Xsc::Reflection::BufferType::Texture2DMS: return GPOT_TEXTURE2DMS;
  187. case Xsc::Reflection::BufferType::Texture2DMSArray: return GPOT_TEXTURE2DMSARRAY;
  188. default: return GPOT_UNKNOWN;
  189. }
  190. }
  191. GpuParamObjectType ReflTypeToBufferType(Xsc::Reflection::BufferType type)
  192. {
  193. switch(type)
  194. {
  195. case Xsc::Reflection::BufferType::Buffer: return GPOT_RWTYPED_BUFFER;
  196. case Xsc::Reflection::BufferType::StructuredBuffer: return GPOT_STRUCTURED_BUFFER;
  197. case Xsc::Reflection::BufferType::ByteAddressBuffer: return GPOT_BYTE_BUFFER;
  198. case Xsc::Reflection::BufferType::RWBuffer: return GPOT_RWTYPED_BUFFER;
  199. case Xsc::Reflection::BufferType::RWStructuredBuffer: return GPOT_RWSTRUCTURED_BUFFER;
  200. case Xsc::Reflection::BufferType::RWByteAddressBuffer: return GPOT_RWBYTE_BUFFER;
  201. case Xsc::Reflection::BufferType::AppendStructuredBuffer: return GPOT_RWAPPEND_BUFFER;
  202. case Xsc::Reflection::BufferType::ConsumeStructuredBuffer: return GPOT_RWCONSUME_BUFFER;
  203. default: return GPOT_UNKNOWN;
  204. }
  205. }
  206. GpuParamDataType ReflTypeToDataType(Xsc::Reflection::DataType type)
  207. {
  208. switch (type)
  209. {
  210. case Xsc::Reflection::DataType::Bool: return GPDT_BOOL;
  211. case Xsc::Reflection::DataType::Float: return GPDT_FLOAT1;
  212. case Xsc::Reflection::DataType::Float2: return GPDT_FLOAT2;
  213. case Xsc::Reflection::DataType::Float3: return GPDT_FLOAT3;
  214. case Xsc::Reflection::DataType::Float4: return GPDT_FLOAT4;
  215. case Xsc::Reflection::DataType::UInt:
  216. case Xsc::Reflection::DataType::Int:
  217. return GPDT_INT1;
  218. case Xsc::Reflection::DataType::UInt2:
  219. case Xsc::Reflection::DataType::Int2:
  220. return GPDT_INT2;
  221. case Xsc::Reflection::DataType::UInt3:
  222. case Xsc::Reflection::DataType::Int3:
  223. return GPDT_INT3;
  224. case Xsc::Reflection::DataType::UInt4:
  225. case Xsc::Reflection::DataType::Int4:
  226. return GPDT_INT4;
  227. case Xsc::Reflection::DataType::Float2x2: return GPDT_MATRIX_2X2;
  228. case Xsc::Reflection::DataType::Float2x3: return GPDT_MATRIX_2X3;
  229. case Xsc::Reflection::DataType::Float2x4: return GPDT_MATRIX_2X4;
  230. case Xsc::Reflection::DataType::Float3x2: return GPDT_MATRIX_3X4;
  231. case Xsc::Reflection::DataType::Float3x3: return GPDT_MATRIX_3X3;
  232. case Xsc::Reflection::DataType::Float3x4: return GPDT_MATRIX_3X4;
  233. case Xsc::Reflection::DataType::Float4x2: return GPDT_MATRIX_4X2;
  234. case Xsc::Reflection::DataType::Float4x3: return GPDT_MATRIX_4X3;
  235. case Xsc::Reflection::DataType::Float4x4: return GPDT_MATRIX_4X4;
  236. default: return GPDT_UNKNOWN;
  237. }
  238. }
  239. HTexture getBuiltinTexture(UINT32 idx)
  240. {
  241. if (idx == 1)
  242. return BuiltinResources::getTexture(BuiltinTexture::White);
  243. else if (idx == 2)
  244. return BuiltinResources::getTexture(BuiltinTexture::Black);
  245. else if (idx == 3)
  246. return BuiltinResources::getTexture(BuiltinTexture::Normal);
  247. return HTexture();
  248. }
  249. TextureAddressingMode parseTexAddrMode(Xsc::Reflection::TextureAddressMode addrMode)
  250. {
  251. switch (addrMode)
  252. {
  253. case Xsc::Reflection::TextureAddressMode::Border:
  254. return TAM_BORDER;
  255. case Xsc::Reflection::TextureAddressMode::Clamp:
  256. return TAM_CLAMP;
  257. case Xsc::Reflection::TextureAddressMode::Mirror:
  258. case Xsc::Reflection::TextureAddressMode::MirrorOnce:
  259. return TAM_MIRROR;
  260. case Xsc::Reflection::TextureAddressMode::Wrap:
  261. default:
  262. return TAM_WRAP;
  263. }
  264. }
  265. CompareFunction parseCompFunction(Xsc::Reflection::ComparisonFunc compFunc)
  266. {
  267. switch(compFunc)
  268. {
  269. case Xsc::Reflection::ComparisonFunc::Always:
  270. default:
  271. return CMPF_ALWAYS_PASS;
  272. case Xsc::Reflection::ComparisonFunc::Never:
  273. return CMPF_ALWAYS_FAIL;
  274. case Xsc::Reflection::ComparisonFunc::Equal:
  275. return CMPF_EQUAL;
  276. case Xsc::Reflection::ComparisonFunc::Greater:
  277. return CMPF_GREATER;
  278. case Xsc::Reflection::ComparisonFunc::GreaterEqual:
  279. return CMPF_GREATER_EQUAL;
  280. case Xsc::Reflection::ComparisonFunc::Less:
  281. return CMPF_LESS;
  282. case Xsc::Reflection::ComparisonFunc::LessEqual:
  283. return CMPF_LESS_EQUAL;
  284. case Xsc::Reflection::ComparisonFunc::NotEqual:
  285. return CMPF_NOT_EQUAL;
  286. }
  287. }
  288. SPtr<SamplerState> parseSamplerState(const Xsc::Reflection::SamplerState& sampState)
  289. {
  290. SAMPLER_STATE_DESC desc;
  291. desc.addressMode.u = parseTexAddrMode(sampState.addressU);
  292. desc.addressMode.v = parseTexAddrMode(sampState.addressV);
  293. desc.addressMode.w = parseTexAddrMode(sampState.addressW);
  294. desc.borderColor[0] = sampState.borderColor[0];
  295. desc.borderColor[1] = sampState.borderColor[1];
  296. desc.borderColor[2] = sampState.borderColor[2];
  297. desc.borderColor[3] = sampState.borderColor[3];
  298. desc.comparisonFunc = parseCompFunction(sampState.comparisonFunc);
  299. desc.maxAniso = sampState.maxAnisotropy;
  300. desc.mipMax = sampState.maxLOD;
  301. desc.mipMin = sampState.minLOD;
  302. desc.mipmapBias = sampState.mipLODBias;
  303. switch (sampState.filter)
  304. {
  305. case Xsc::Reflection::Filter::MinMagMipPoint:
  306. case Xsc::Reflection::Filter::ComparisonMinMagMipPoint:
  307. desc.minFilter = FO_POINT;
  308. desc.magFilter = FO_POINT;
  309. desc.mipFilter = FO_POINT;
  310. break;
  311. case Xsc::Reflection::Filter::MinMagPointMipLinear:
  312. case Xsc::Reflection::Filter::ComparisonMinMagPointMipLinear:
  313. desc.minFilter = FO_POINT;
  314. desc.magFilter = FO_POINT;
  315. desc.mipFilter = FO_LINEAR;
  316. break;
  317. case Xsc::Reflection::Filter::MinPointMagLinearMipPoint:
  318. case Xsc::Reflection::Filter::ComparisonMinPointMagLinearMipPoint:
  319. desc.minFilter = FO_POINT;
  320. desc.magFilter = FO_LINEAR;
  321. desc.mipFilter = FO_POINT;
  322. break;
  323. case Xsc::Reflection::Filter::MinPointMagMipLinear:
  324. case Xsc::Reflection::Filter::ComparisonMinPointMagMipLinear:
  325. desc.minFilter = FO_POINT;
  326. desc.magFilter = FO_LINEAR;
  327. desc.mipFilter = FO_LINEAR;
  328. break;
  329. case Xsc::Reflection::Filter::MinLinearMagMipPoint:
  330. case Xsc::Reflection::Filter::ComparisonMinLinearMagMipPoint:
  331. desc.minFilter = FO_LINEAR;
  332. desc.magFilter = FO_POINT;
  333. desc.mipFilter = FO_POINT;
  334. break;
  335. case Xsc::Reflection::Filter::MinLinearMagPointMipLinear:
  336. case Xsc::Reflection::Filter::ComparisonMinLinearMagPointMipLinear:
  337. desc.minFilter = FO_LINEAR;
  338. desc.magFilter = FO_POINT;
  339. desc.mipFilter = FO_LINEAR;
  340. break;
  341. case Xsc::Reflection::Filter::MinMagLinearMipPoint:
  342. case Xsc::Reflection::Filter::ComparisonMinMagLinearMipPoint:
  343. desc.minFilter = FO_LINEAR;
  344. desc.magFilter = FO_LINEAR;
  345. desc.mipFilter = FO_POINT;
  346. break;
  347. case Xsc::Reflection::Filter::MinMagMipLinear:
  348. case Xsc::Reflection::Filter::ComparisonMinMagMipLinear:
  349. desc.minFilter = FO_LINEAR;
  350. desc.magFilter = FO_LINEAR;
  351. desc.mipFilter = FO_LINEAR;
  352. break;
  353. case Xsc::Reflection::Filter::Anisotropic:
  354. case Xsc::Reflection::Filter::ComparisonAnisotropic:
  355. desc.minFilter = FO_ANISOTROPIC;
  356. desc.magFilter = FO_ANISOTROPIC;
  357. desc.minFilter = FO_ANISOTROPIC;
  358. break;
  359. default:
  360. break;
  361. }
  362. return SamplerState::create(desc);
  363. }
  364. void parseParameters(const Xsc::Reflection::ReflectionData& reflData, SHADER_DESC& desc)
  365. {
  366. for(auto& entry : reflData.uniforms)
  367. {
  368. if ((entry.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0)
  369. continue;
  370. String ident = entry.ident.c_str();
  371. switch(entry.type)
  372. {
  373. case Xsc::Reflection::UniformType::UniformBuffer:
  374. desc.setParamBlockAttribs(entry.ident.c_str(), false, GPBU_STATIC);
  375. break;
  376. case Xsc::Reflection::UniformType::Buffer:
  377. {
  378. GpuParamObjectType objType = ReflTypeToTextureType((Xsc::Reflection::BufferType)entry.baseType);
  379. if(objType != GPOT_UNKNOWN)
  380. {
  381. // Ignore parameters that were already registered in some previous variation. Note that this implies
  382. // you cannot have same names for different parameters in different variations.
  383. if (desc.textureParams.find(ident) != desc.textureParams.end())
  384. continue;
  385. if (entry.defaultValue == -1)
  386. desc.addParameter(ident, ident, objType);
  387. else
  388. desc.addParameter(ident, ident, objType, getBuiltinTexture(entry.defaultValue));
  389. }
  390. else
  391. {
  392. // Ignore parameters that were already registered in some previous variation. Note that this implies
  393. // you cannot have same names for different parameters in different variations.
  394. if (desc.bufferParams.find(ident) != desc.bufferParams.end())
  395. continue;
  396. objType = ReflTypeToBufferType((Xsc::Reflection::BufferType)entry.baseType);
  397. desc.addParameter(ident, ident, objType);
  398. }
  399. }
  400. break;
  401. case Xsc::Reflection::UniformType::Sampler:
  402. {
  403. auto findIter = reflData.samplerStates.find(entry.ident);
  404. if (findIter != reflData.samplerStates.end())
  405. {
  406. // Ignore parameters that were already registered in some previous variation. Note that this implies
  407. // you cannot have same names for different parameters in different variations.
  408. if(desc.samplerParams.find(ident) != desc.samplerParams.end())
  409. continue;
  410. String alias = findIter->second.alias.c_str();
  411. if(findIter->second.isNonDefault)
  412. {
  413. SPtr<SamplerState> defaultVal = parseSamplerState(findIter->second);
  414. desc.addParameter(ident, ident, GPOT_SAMPLER2D, defaultVal);
  415. if (!alias.empty())
  416. desc.addParameter(ident, alias, GPOT_SAMPLER2D, defaultVal);
  417. }
  418. else
  419. {
  420. desc.addParameter(ident, ident, GPOT_SAMPLER2D);
  421. if (!alias.empty())
  422. desc.addParameter(ident, alias, GPOT_SAMPLER2D);
  423. }
  424. }
  425. else
  426. {
  427. desc.addParameter(ident, ident, GPOT_SAMPLER2D);
  428. }
  429. break;
  430. }
  431. case Xsc::Reflection::UniformType::Variable:
  432. {
  433. bool isBlockInternal = false;
  434. if(entry.uniformBlock != -1)
  435. {
  436. std::string blockName = reflData.constantBuffers[entry.uniformBlock].ident;
  437. for (auto& uniform : reflData.uniforms)
  438. {
  439. if (uniform.type == Xsc::Reflection::UniformType::UniformBuffer && uniform.ident == blockName)
  440. {
  441. isBlockInternal = (uniform.flags & Xsc::Reflection::Uniform::Flags::Internal) != 0;
  442. break;
  443. }
  444. }
  445. }
  446. if (!isBlockInternal)
  447. {
  448. GpuParamDataType type = ReflTypeToDataType((Xsc::Reflection::DataType)entry.baseType);
  449. if ((entry.flags & Xsc::Reflection::Uniform::Flags::Color) != 0 &&
  450. (type == GPDT_FLOAT3 || type == GPDT_FLOAT4))
  451. {
  452. type = GPDT_COLOR;
  453. }
  454. if (entry.defaultValue == -1)
  455. desc.addParameter(ident, ident, type);
  456. else
  457. {
  458. const Xsc::Reflection::DefaultValue& defVal = reflData.defaultValues[entry.defaultValue];
  459. desc.addParameter(ident, ident, type, StringID::NONE, 1, 0, (UINT8*)defVal.matrix);
  460. }
  461. }
  462. }
  463. break;
  464. case Xsc::Reflection::UniformType::Struct:
  465. break;
  466. default: ;
  467. }
  468. }
  469. }
  470. String crossCompile(const String& hlsl, GpuProgramType type, bool vulkan, bool optionalEntry, UINT32& startBindingSlot,
  471. SHADER_DESC* shaderDesc = nullptr, Vector<GpuProgramType>* detectedTypes = nullptr)
  472. {
  473. SPtr<StringStream> input = bs_shared_ptr_new<StringStream>();
  474. if (vulkan)
  475. *input << "#define VULKAN 1" << std::endl;
  476. else
  477. *input << "#define OPENGL 1" << std::endl;
  478. *input << hlsl;
  479. Xsc::ShaderInput inputDesc;
  480. inputDesc.shaderVersion = Xsc::InputShaderVersion::HLSL5;
  481. inputDesc.sourceCode = input;
  482. inputDesc.extensions = Xsc::Extensions::LayoutAttribute;
  483. switch (type)
  484. {
  485. case GPT_VERTEX_PROGRAM:
  486. inputDesc.shaderTarget = Xsc::ShaderTarget::VertexShader;
  487. inputDesc.entryPoint = "vsmain";
  488. break;
  489. case GPT_GEOMETRY_PROGRAM:
  490. inputDesc.shaderTarget = Xsc::ShaderTarget::GeometryShader;
  491. inputDesc.entryPoint = "gsmain";
  492. break;
  493. case GPT_HULL_PROGRAM:
  494. inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationControlShader;
  495. inputDesc.entryPoint = "hsmain";
  496. break;
  497. case GPT_DOMAIN_PROGRAM:
  498. inputDesc.shaderTarget = Xsc::ShaderTarget::TessellationEvaluationShader;
  499. inputDesc.entryPoint = "dsmain";
  500. break;
  501. case GPT_FRAGMENT_PROGRAM:
  502. inputDesc.shaderTarget = Xsc::ShaderTarget::FragmentShader;
  503. inputDesc.entryPoint = "fsmain";
  504. break;
  505. case GPT_COMPUTE_PROGRAM:
  506. inputDesc.shaderTarget = Xsc::ShaderTarget::ComputeShader;
  507. inputDesc.entryPoint = "csmain";
  508. break;
  509. default:
  510. break;
  511. }
  512. StringStream output;
  513. Xsc::ShaderOutput outputDesc;
  514. outputDesc.sourceCode = &output;
  515. outputDesc.options.autoBinding = vulkan;
  516. outputDesc.options.autoBindingStartSlot = startBindingSlot;
  517. outputDesc.options.separateShaders = true;
  518. outputDesc.options.separateSamplers = false;
  519. outputDesc.nameMangling.inputPrefix = "bs_";
  520. outputDesc.nameMangling.outputPrefix = "bs_";
  521. outputDesc.nameMangling.useAlwaysSemantics = true;
  522. outputDesc.nameMangling.renameBufferFields = true;
  523. if (vulkan)
  524. outputDesc.shaderVersion = Xsc::OutputShaderVersion::VKSL450;
  525. else
  526. outputDesc.shaderVersion = Xsc::OutputShaderVersion::GLSL450;
  527. XscLog log;
  528. Xsc::Reflection::ReflectionData reflectionData;
  529. bool compileSuccess = Xsc::CompileShader(inputDesc, outputDesc, &log, &reflectionData);
  530. if (!compileSuccess)
  531. {
  532. // If enabled, don't fail if entry point isn't found
  533. bool done = true;
  534. if(optionalEntry)
  535. {
  536. bool entryFound = false;
  537. for (auto& entry : reflectionData.functions)
  538. {
  539. if(entry.ident == inputDesc.entryPoint)
  540. {
  541. entryFound = true;
  542. break;
  543. }
  544. }
  545. if (!entryFound)
  546. done = false;
  547. }
  548. if (done)
  549. {
  550. StringStream logOutput;
  551. log.getMessages(logOutput);
  552. LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
  553. return "";
  554. }
  555. }
  556. for (auto& entry : reflectionData.constantBuffers)
  557. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  558. for (auto& entry : reflectionData.textures)
  559. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  560. for (auto& entry : reflectionData.storageBuffers)
  561. startBindingSlot = std::max(startBindingSlot, entry.location + 1u);
  562. if(detectedTypes != nullptr)
  563. {
  564. for(auto& entry : reflectionData.functions)
  565. {
  566. if (entry.ident == "vsmain")
  567. detectedTypes->push_back(GPT_VERTEX_PROGRAM);
  568. else if (entry.ident == "fsmain")
  569. detectedTypes->push_back(GPT_FRAGMENT_PROGRAM);
  570. else if (entry.ident == "gsmain")
  571. detectedTypes->push_back(GPT_GEOMETRY_PROGRAM);
  572. else if (entry.ident == "dsmain")
  573. detectedTypes->push_back(GPT_DOMAIN_PROGRAM);
  574. else if (entry.ident == "hsmain")
  575. detectedTypes->push_back(GPT_HULL_PROGRAM);
  576. else if (entry.ident == "csmain")
  577. detectedTypes->push_back(GPT_COMPUTE_PROGRAM);
  578. }
  579. // If no entry points found, and error occurred, report error
  580. if(!compileSuccess && detectedTypes->size() == 0)
  581. {
  582. StringStream logOutput;
  583. log.getMessages(logOutput);
  584. LOGERR("Shader cross compilation failed. Log: \n\n" + logOutput.str());
  585. return "";
  586. }
  587. }
  588. if (shaderDesc != nullptr)
  589. parseParameters(reflectionData, *shaderDesc);
  590. return output.str();
  591. }
  592. // Convert HLSL code to GLSL
  593. String HLSLtoGLSL(const String& hlsl, GpuProgramType type, bool vulkan, UINT32& startBindingSlot)
  594. {
  595. return crossCompile(hlsl, type, vulkan, false, startBindingSlot);
  596. }
  597. void reflectHLSL(const String& hlsl, SHADER_DESC& shaderDesc, Vector<GpuProgramType>& entryPoints)
  598. {
  599. UINT32 dummy = 0;
  600. crossCompile(hlsl, GPT_VERTEX_PROGRAM, false, true, dummy, &shaderDesc, &entryPoints);
  601. }
  602. BSLFXCompileResult BSLFXCompiler::compile(const String& name, const String& source,
  603. const UnorderedMap<String, String>& defines)
  604. {
  605. String parsedSource = source;
  606. // Run the lexer/parser and generate the AST
  607. ParseState* parseState = parseStateCreate();
  608. BSLFXCompileResult output = parseFX(parseState, parsedSource.c_str(), defines);
  609. if(!output.errorMessage.empty())
  610. parseStateDelete(parseState);
  611. else
  612. {
  613. // Only enable for debug purposes
  614. //SLFXDebugPrint(parseState->rootNode, "");
  615. if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
  616. {
  617. parseStateDelete(parseState);
  618. output.errorMessage = "Root not is null or not a shader.";
  619. return output;
  620. }
  621. // Parse global shader options & technique meta-data
  622. SHADER_DESC shaderDesc;
  623. Vector<pair<ASTFXNode*, TechniqueMetaData>> techniqueMetaData;
  624. //// Go in reverse because options are added in reverse order during parsing
  625. for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
  626. {
  627. NodeOption* option = &parseState->rootNode->options->entries[i];
  628. switch (option->type)
  629. {
  630. case OT_Options:
  631. parseOptions(option->value.nodePtr, shaderDesc);
  632. break;
  633. case OT_Technique:
  634. {
  635. // We initially parse only meta-data, so we can handle out-of-order technique definitions
  636. TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
  637. techniqueMetaData.push_back(std::make_pair(option->value.nodePtr, metaData));
  638. break;
  639. }
  640. default:
  641. break;
  642. }
  643. }
  644. // Inherit variations from mixins
  645. bool* techniqueWasParsed = bs_stack_alloc<bool>((UINT32)techniqueMetaData.size());
  646. std::function<bool(const TechniqueMetaData&, TechniqueMetaData&)> parseInherited =
  647. [&](const TechniqueMetaData& metaData, TechniqueMetaData& combinedMetaData)
  648. {
  649. for (auto riter = metaData.includes.rbegin(); riter != metaData.includes.rend(); ++riter)
  650. {
  651. const String& includes = *riter;
  652. UINT32 baseIdx = -1;
  653. for (UINT32 i = 0; i < (UINT32)techniqueMetaData.size(); i++)
  654. {
  655. auto& entry = techniqueMetaData[i];
  656. if (!entry.second.isMixin)
  657. continue;
  658. if (entry.second.name == includes)
  659. {
  660. bool matches = entry.second.language == metaData.language || entry.second.language == "Any";
  661. // We want the last matching technique, in order to allow techniques to override each other
  662. if (matches)
  663. baseIdx = i;
  664. }
  665. }
  666. if (baseIdx != (UINT32)-1)
  667. {
  668. auto& entry = techniqueMetaData[baseIdx];
  669. // Was already parsed previously, don't parse it multiple times (happens when multiple techniques
  670. // include the same mixin)
  671. if (techniqueWasParsed[baseIdx])
  672. continue;
  673. if (!parseInherited(entry.second, combinedMetaData))
  674. return false;
  675. for(auto& variation : entry.second.variations)
  676. combinedMetaData.variations.push_back(variation);
  677. techniqueWasParsed[baseIdx] = true;
  678. }
  679. else
  680. {
  681. output.errorMessage = "Mixin \"" + includes + "\" cannot be found.";
  682. return false;
  683. }
  684. }
  685. return true;
  686. };
  687. for (auto& entry : techniqueMetaData)
  688. {
  689. TechniqueMetaData& metaData = entry.second;
  690. if (metaData.isMixin)
  691. continue;
  692. bs_zero_out(techniqueWasParsed, techniqueMetaData.size());
  693. TechniqueMetaData combinedMetaData = metaData;
  694. if (!parseInherited(metaData, combinedMetaData))
  695. {
  696. parseStateDelete(parseState);
  697. bs_stack_free(techniqueWasParsed);
  698. return output;
  699. }
  700. entry.second = combinedMetaData;
  701. }
  702. bs_stack_free(techniqueWasParsed);
  703. parseStateDelete(parseState);
  704. // Build a list of different variations and re-parse the source using the relevant defines
  705. Vector<SPtr<Technique>> techniques;
  706. UnorderedSet<String> includes;
  707. for (auto& entry : techniqueMetaData)
  708. {
  709. TechniqueMetaData& metaData = entry.second;
  710. if (metaData.isMixin)
  711. continue;
  712. // Generate a list of variations
  713. Vector<ShaderVariation> variations;
  714. if (metaData.variations.empty())
  715. variations.push_back(ShaderVariation());
  716. else
  717. {
  718. Vector<const VariationData*> todo;
  719. for (UINT32 i = 0; i < (UINT32)metaData.variations.size(); i++)
  720. todo.push_back(&metaData.variations[i]);
  721. while (!todo.empty())
  722. {
  723. const VariationData* current = todo.back();
  724. todo.erase(todo.end() - 1);
  725. // Variation parameter that's either defined or isn't
  726. if (current->values.empty())
  727. {
  728. // This is the first variation parameter, register new variations
  729. if (variations.empty())
  730. {
  731. ShaderVariation a;
  732. ShaderVariation b;
  733. b.addParam(ShaderVariation::Param(current->identifier, 1));
  734. variations.push_back(a);
  735. variations.push_back(b);
  736. }
  737. else // Duplicate existing variations, and add the parameter
  738. {
  739. UINT32 numVariations = (UINT32)variations.size();
  740. for (UINT32 i = 0; i < numVariations; i++)
  741. {
  742. // Make a copy
  743. variations.push_back(variations[i]);
  744. // Add the parameter to existing variation
  745. variations[i].addParam(ShaderVariation::Param(current->identifier, 1));
  746. }
  747. }
  748. }
  749. else // Variation parameter with multiple values
  750. {
  751. // This is the first variation parameter, register new variations
  752. if (variations.empty())
  753. {
  754. for (UINT32 i = 0; i < (UINT32)current->values.size(); i++)
  755. {
  756. ShaderVariation variation;
  757. variation.addParam(ShaderVariation::Param(current->identifier, current->values[i]));
  758. variations.push_back(variation);
  759. }
  760. }
  761. else // Duplicate existing variations, and add the parameter
  762. {
  763. UINT32 numVariations = (UINT32)variations.size();
  764. for (UINT32 i = 0; i < numVariations; i++)
  765. {
  766. for (UINT32 j = 1; j < (UINT32)current->values.size(); j++)
  767. {
  768. ShaderVariation copy = variations[i];
  769. copy.addParam(ShaderVariation::Param(current->identifier, current->values[j]));
  770. variations.push_back(copy);
  771. }
  772. variations[i].addParam(ShaderVariation::Param(current->identifier, current->values[0]));
  773. }
  774. }
  775. }
  776. }
  777. }
  778. // For every variation, re-parse the file with relevant defines
  779. for(auto& variation : variations)
  780. {
  781. UnorderedMap<String, String> globalDefines = defines;
  782. UnorderedMap<String, String> variationDefines = variation.getDefines().getAll();
  783. for(auto& define : variationDefines)
  784. globalDefines[define.first] = define.second;
  785. ParseState* variationParseState = parseStateCreate();
  786. output = parseFX(variationParseState, parsedSource.c_str(), globalDefines);
  787. if (!output.errorMessage.empty())
  788. parseStateDelete(variationParseState);
  789. else
  790. {
  791. Vector<String> codeBlocks;
  792. CodeString* codeString = variationParseState->codeStrings;
  793. while (codeString != nullptr)
  794. {
  795. while ((INT32)codeBlocks.size() <= codeString->index)
  796. codeBlocks.push_back(String());
  797. codeBlocks[codeString->index] = String(codeString->code, codeString->size);
  798. codeString = codeString->next;
  799. }
  800. output = parseTechnique(variationParseState, entry.second.name, codeBlocks, variation, techniques,
  801. includes, shaderDesc);
  802. if(!output.errorMessage.empty())
  803. return output;
  804. }
  805. }
  806. }
  807. // Generate a shader from the parsed techniques
  808. Vector<String> includeArray;
  809. for (auto& entry : includes)
  810. includeArray.push_back(entry);
  811. output.shader = Shader::_createPtr(name, shaderDesc, techniques);
  812. output.shader->setIncludeFiles(includeArray);
  813. // Verify GPU programs compile correctly
  814. StringStream gpuProgError;
  815. bool hasError = false;
  816. if (output.shader != nullptr)
  817. {
  818. Vector<SPtr<Technique>> techniques = output.shader->getCompatibleTechniques();
  819. for (auto& technique : techniques)
  820. {
  821. UINT32 numPasses = technique->getNumPasses();
  822. for (UINT32 i = 0; i < numPasses; i++)
  823. {
  824. SPtr<Pass> pass = technique->getPass(i);
  825. auto checkCompileStatus = [&](const String& prefix, const SPtr<GpuProgram>& prog)
  826. {
  827. if (prog != nullptr)
  828. {
  829. prog->blockUntilCoreInitialized();
  830. if (!prog->isCompiled())
  831. {
  832. hasError = true;
  833. gpuProgError << prefix << ": " << prog->getCompileErrorMessage() << std::endl;
  834. }
  835. }
  836. };
  837. checkCompileStatus("Vertex program", pass->getVertexProgram());
  838. checkCompileStatus("Fragment program", pass->getFragmentProgram());
  839. checkCompileStatus("Geometry program", pass->getGeometryProgram());
  840. checkCompileStatus("Hull program", pass->getHullProgram());
  841. checkCompileStatus("Domain program", pass->getDomainProgram());
  842. checkCompileStatus("Compute program", pass->getComputeProgram());
  843. }
  844. }
  845. }
  846. if (hasError)
  847. {
  848. output.errorMessage = "Failed compiling GPU program(s): " + gpuProgError.str();
  849. output.errorLine = 0;
  850. output.errorColumn = 0;
  851. }
  852. }
  853. return output;
  854. }
  855. BSLFXCompileResult BSLFXCompiler::parseFX(ParseState* parseState, const char* source, const UnorderedMap<String, String>& defines)
  856. {
  857. for(auto& define : defines)
  858. {
  859. if (define.first.size() == 0)
  860. continue;
  861. addDefine(parseState, define.first.c_str());
  862. if(define.second.size() > 0)
  863. addDefineExpr(parseState, define.second.c_str());
  864. }
  865. yyscan_t scanner;
  866. YY_BUFFER_STATE state;
  867. BSLFXCompileResult output;
  868. if (yylex_init_extra(parseState, &scanner))
  869. {
  870. output.errorMessage = "Internal error: Lexer failed to initialize.";
  871. return output;
  872. }
  873. // If debug output from lexer is needed uncomment this and add %debug option to lexer file
  874. //yyset_debug(true, scanner);
  875. // If debug output from parser is needed uncomment this and add %debug option to parser file
  876. //yydebug = true;
  877. state = yy_scan_string(source, scanner);
  878. if (yyparse(parseState, scanner))
  879. {
  880. if (parseState->hasError > 0)
  881. {
  882. output.errorMessage = parseState->errorMessage;
  883. output.errorLine = parseState->errorLine;
  884. output.errorColumn = parseState->errorColumn;
  885. if (parseState->errorFile != nullptr)
  886. output.errorFile = parseState->errorFile;
  887. }
  888. else
  889. output.errorMessage = "Internal error: Parsing failed.";
  890. return output;
  891. }
  892. yy_delete_buffer(state, scanner);
  893. yylex_destroy(scanner);
  894. return output;
  895. }
  896. BSLFXCompiler::TechniqueMetaData BSLFXCompiler::parseTechniqueMetaData(ASTFXNode* technique)
  897. {
  898. TechniqueMetaData metaData;
  899. metaData.language = "hlsl";
  900. metaData.isMixin = technique->type == NT_Mixin;
  901. for (int i = 0; i < technique->options->count; i++)
  902. {
  903. NodeOption* option = &technique->options->entries[i];
  904. switch (option->type)
  905. {
  906. case OT_Tags:
  907. {
  908. ASTFXNode* tagsNode = option->value.nodePtr;
  909. for (int j = 0; j < tagsNode->options->count; j++)
  910. {
  911. NodeOption* tagOption = &tagsNode->options->entries[j];
  912. if (tagOption->type == OT_TagValue)
  913. metaData.tags.push_back(removeQuotes(tagOption->value.strValue));
  914. }
  915. }
  916. break;
  917. case OT_Variations:
  918. {
  919. ASTFXNode* variationsNode = option->value.nodePtr;
  920. for (int j = 0; j < variationsNode->options->count; j++)
  921. {
  922. NodeOption* variationOption = &variationsNode->options->entries[j];
  923. if(variationOption->type == OT_Variation)
  924. parseVariations(metaData, variationOption->value.nodePtr);
  925. }
  926. }
  927. break;
  928. case OT_FeatureSet:
  929. metaData.featureSet = option->value.strValue;
  930. break;
  931. case OT_Identifier:
  932. metaData.name = option->value.strValue;
  933. break;
  934. case OT_Mixin:
  935. metaData.includes.push_back(option->value.strValue);
  936. break;
  937. default:
  938. break;
  939. }
  940. }
  941. return metaData;
  942. }
  943. void BSLFXCompiler::parseVariations(TechniqueMetaData& metaData, ASTFXNode* variations)
  944. {
  945. assert(variations->type == NT_Variation);
  946. VariationData variationData;
  947. for (int i = 0; i < variations->options->count; i++)
  948. {
  949. NodeOption* option = &variations->options->entries[i];
  950. switch (option->type)
  951. {
  952. case OT_Identifier:
  953. variationData.identifier = option->value.strValue;
  954. break;
  955. case OT_VariationValue:
  956. variationData.values.push_back(option->value.intValue);
  957. break;
  958. default:
  959. break;
  960. }
  961. }
  962. if (!variationData.identifier.empty())
  963. metaData.variations.push_back(variationData);
  964. }
  965. QueueSortType BSLFXCompiler::parseSortType(CullAndSortModeValue sortType)
  966. {
  967. switch (sortType)
  968. {
  969. case CASV_BackToFront:
  970. return QueueSortType::BackToFront;
  971. case CASV_FrontToBack:
  972. return QueueSortType::FrontToBack;
  973. case CASV_None:
  974. return QueueSortType::None;
  975. default:
  976. break;
  977. }
  978. return QueueSortType::None;
  979. }
  980. CompareFunction BSLFXCompiler::parseCompFunc(CompFuncValue compFunc)
  981. {
  982. switch (compFunc)
  983. {
  984. case CFV_Pass:
  985. return CMPF_ALWAYS_PASS;
  986. case CFV_Fail:
  987. return CMPF_ALWAYS_FAIL;
  988. case CFV_LT:
  989. return CMPF_LESS;
  990. case CFV_LTE:
  991. return CMPF_LESS_EQUAL;
  992. case CFV_EQ:
  993. return CMPF_EQUAL;
  994. case CFV_NEQ:
  995. return CMPF_NOT_EQUAL;
  996. case CFV_GT:
  997. return CMPF_GREATER;
  998. case CFV_GTE:
  999. return CMPF_GREATER_EQUAL;
  1000. }
  1001. return CMPF_ALWAYS_PASS;
  1002. }
  1003. BlendFactor BSLFXCompiler::parseBlendFactor(OpValue factor)
  1004. {
  1005. switch (factor)
  1006. {
  1007. case OV_One:
  1008. return BF_ONE;
  1009. case OV_Zero:
  1010. return BF_ZERO;
  1011. case OV_DestColor:
  1012. return BF_DEST_COLOR;
  1013. case OV_SrcColor:
  1014. return BF_SOURCE_COLOR;
  1015. case OV_InvDestColor:
  1016. return BF_INV_DEST_COLOR;
  1017. case OV_InvSrcColor:
  1018. return BF_INV_SOURCE_COLOR;
  1019. case OV_DestAlpha:
  1020. return BF_DEST_ALPHA;
  1021. case OV_SrcAlpha:
  1022. return BF_SOURCE_ALPHA;
  1023. case OV_InvDestAlpha:
  1024. return BF_INV_DEST_ALPHA;
  1025. case OV_InvSrcAlpha:
  1026. return BF_INV_SOURCE_ALPHA;
  1027. default:
  1028. break;
  1029. }
  1030. return BF_ONE;
  1031. }
  1032. BlendOperation BSLFXCompiler::parseBlendOp(BlendOpValue op)
  1033. {
  1034. switch (op)
  1035. {
  1036. case BOV_Add:
  1037. return BO_ADD;
  1038. case BOV_Max:
  1039. return BO_MAX;
  1040. case BOV_Min:
  1041. return BO_MIN;
  1042. case BOV_Subtract:
  1043. return BO_SUBTRACT;
  1044. case BOV_RevSubtract:
  1045. return BO_REVERSE_SUBTRACT;
  1046. }
  1047. return BO_ADD;
  1048. }
  1049. StencilOperation BSLFXCompiler::parseStencilOp(OpValue op)
  1050. {
  1051. switch (op)
  1052. {
  1053. case OV_Keep:
  1054. return SOP_KEEP;
  1055. case OV_Zero:
  1056. return SOP_ZERO;
  1057. case OV_Replace:
  1058. return SOP_REPLACE;
  1059. case OV_Incr:
  1060. return SOP_INCREMENT;
  1061. case OV_Decr:
  1062. return SOP_DECREMENT;
  1063. case OV_IncrWrap:
  1064. return SOP_INCREMENT_WRAP;
  1065. case OV_DecrWrap:
  1066. return SOP_DECREMENT_WRAP;
  1067. case OV_Invert:
  1068. return SOP_INVERT;
  1069. default:
  1070. break;
  1071. }
  1072. return SOP_KEEP;
  1073. }
  1074. CullingMode BSLFXCompiler::parseCullMode(CullAndSortModeValue cm)
  1075. {
  1076. switch (cm)
  1077. {
  1078. case CASV_None:
  1079. return CULL_NONE;
  1080. case CASV_CW:
  1081. return CULL_CLOCKWISE;
  1082. case CASV_CCW:
  1083. return CULL_COUNTERCLOCKWISE;
  1084. default:
  1085. break;
  1086. }
  1087. return CULL_COUNTERCLOCKWISE;
  1088. }
  1089. PolygonMode BSLFXCompiler::parseFillMode(FillModeValue fm)
  1090. {
  1091. if (fm == FMV_Wire)
  1092. return PM_WIREFRAME;
  1093. return PM_SOLID;
  1094. }
  1095. void BSLFXCompiler::parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  1096. {
  1097. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  1098. return;
  1099. for (int i = 0; i < stencilOpNode->options->count; i++)
  1100. {
  1101. NodeOption* option = &stencilOpNode->options->entries[i];
  1102. switch (option->type)
  1103. {
  1104. case OT_Fail:
  1105. desc.frontStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  1106. break;
  1107. case OT_ZFail:
  1108. desc.frontStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  1109. break;
  1110. case OT_PassOp:
  1111. desc.frontStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  1112. break;
  1113. case OT_CompareFunc:
  1114. desc.frontStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. void BSLFXCompiler::parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode)
  1122. {
  1123. if (stencilOpNode == nullptr || stencilOpNode->type != NT_StencilOp)
  1124. return;
  1125. for (int i = 0; i < stencilOpNode->options->count; i++)
  1126. {
  1127. NodeOption* option = &stencilOpNode->options->entries[i];
  1128. switch (option->type)
  1129. {
  1130. case OT_Fail:
  1131. desc.backStencilFailOp = parseStencilOp((OpValue)option->value.intValue);
  1132. break;
  1133. case OT_ZFail:
  1134. desc.backStencilZFailOp = parseStencilOp((OpValue)option->value.intValue);
  1135. break;
  1136. case OT_PassOp:
  1137. desc.backStencilPassOp = parseStencilOp((OpValue)option->value.intValue);
  1138. break;
  1139. case OT_CompareFunc:
  1140. desc.backStencilComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  1141. break;
  1142. default:
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. void BSLFXCompiler::parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  1148. {
  1149. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  1150. return;
  1151. for (int i = 0; i < blendDefNode->options->count; i++)
  1152. {
  1153. NodeOption* option = &blendDefNode->options->entries[i];
  1154. switch (option->type)
  1155. {
  1156. case OT_Source:
  1157. desc.srcBlend = parseBlendFactor((OpValue)option->value.intValue);
  1158. break;
  1159. case OT_Dest:
  1160. desc.dstBlend = parseBlendFactor((OpValue)option->value.intValue);
  1161. break;
  1162. case OT_Op:
  1163. desc.blendOp = parseBlendOp((BlendOpValue)option->value.intValue);
  1164. break;
  1165. default:
  1166. break;
  1167. }
  1168. }
  1169. }
  1170. void BSLFXCompiler::parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode)
  1171. {
  1172. if (blendDefNode == nullptr || blendDefNode->type != NT_BlendDef)
  1173. return;
  1174. for (int i = 0; i < blendDefNode->options->count; i++)
  1175. {
  1176. NodeOption* option = &blendDefNode->options->entries[i];
  1177. switch (option->type)
  1178. {
  1179. case OT_Source:
  1180. desc.srcBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  1181. break;
  1182. case OT_Dest:
  1183. desc.dstBlendAlpha = parseBlendFactor((OpValue)option->value.intValue);
  1184. break;
  1185. case OT_Op:
  1186. desc.blendOpAlpha = parseBlendOp((BlendOpValue)option->value.intValue);
  1187. break;
  1188. default:
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. void BSLFXCompiler::parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode)
  1194. {
  1195. if (targetNode == nullptr || targetNode->type != NT_Target)
  1196. return;
  1197. UINT32 index = 0;
  1198. for (int i = 0; i < targetNode->options->count; i++)
  1199. {
  1200. NodeOption* option = &targetNode->options->entries[i];
  1201. switch (option->type)
  1202. {
  1203. case OT_Index:
  1204. index = option->value.intValue;
  1205. break;
  1206. default:
  1207. break;
  1208. }
  1209. }
  1210. if (index >= BS_MAX_MULTIPLE_RENDER_TARGETS)
  1211. return;
  1212. RENDER_TARGET_BLEND_STATE_DESC& rtDesc = desc.renderTargetDesc[index];
  1213. for (int i = 0; i < targetNode->options->count; i++)
  1214. {
  1215. NodeOption* option = &targetNode->options->entries[i];
  1216. switch (option->type)
  1217. {
  1218. case OT_Enabled:
  1219. rtDesc.blendEnable = option->value.intValue > 0;
  1220. break;
  1221. case OT_Color:
  1222. parseColorBlendDef(rtDesc, option->value.nodePtr);
  1223. break;
  1224. case OT_Alpha:
  1225. parseAlphaBlendDef(rtDesc, option->value.nodePtr);
  1226. break;
  1227. case OT_WriteMask:
  1228. rtDesc.renderTargetWriteMask = option->value.intValue;
  1229. break;
  1230. default:
  1231. break;
  1232. }
  1233. }
  1234. }
  1235. bool BSLFXCompiler::parseBlendState(PassData& desc, ASTFXNode* blendNode)
  1236. {
  1237. if (blendNode == nullptr || blendNode->type != NT_Blend)
  1238. return false;
  1239. bool isDefault = true;
  1240. for (int i = 0; i < blendNode->options->count; i++)
  1241. {
  1242. NodeOption* option = &blendNode->options->entries[i];
  1243. switch (option->type)
  1244. {
  1245. case OT_AlphaToCoverage:
  1246. desc.blendDesc.alphaToCoverageEnable = option->value.intValue > 0;
  1247. isDefault = false;
  1248. break;
  1249. case OT_IndependantBlend:
  1250. desc.blendDesc.independantBlendEnable = option->value.intValue > 0;
  1251. isDefault = false;
  1252. break;
  1253. case OT_Target:
  1254. parseRenderTargetBlendState(desc.blendDesc, option->value.nodePtr);
  1255. isDefault = false;
  1256. break;
  1257. default:
  1258. break;
  1259. }
  1260. }
  1261. return !isDefault;
  1262. }
  1263. bool BSLFXCompiler::parseRasterizerState(PassData& desc, ASTFXNode* rasterNode)
  1264. {
  1265. if (rasterNode == nullptr || rasterNode->type != NT_Raster)
  1266. return false;
  1267. bool isDefault = true;
  1268. for (int i = 0; i < rasterNode->options->count; i++)
  1269. {
  1270. NodeOption* option = &rasterNode->options->entries[i];
  1271. switch (option->type)
  1272. {
  1273. case OT_FillMode:
  1274. desc.rasterizerDesc.polygonMode = parseFillMode((FillModeValue)option->value.intValue);
  1275. isDefault = false;
  1276. break;
  1277. case OT_CullMode:
  1278. desc.rasterizerDesc.cullMode = parseCullMode((CullAndSortModeValue)option->value.intValue);
  1279. isDefault = false;
  1280. break;
  1281. case OT_DepthBias:
  1282. desc.rasterizerDesc.depthBias = option->value.floatValue;
  1283. isDefault = false;
  1284. break;
  1285. case OT_SDepthBias:
  1286. desc.rasterizerDesc.slopeScaledDepthBias = option->value.floatValue;
  1287. isDefault = false;
  1288. break;
  1289. case OT_DepthClip:
  1290. desc.rasterizerDesc.depthClipEnable = option->value.intValue > 0;
  1291. isDefault = false;
  1292. break;
  1293. case OT_Scissor:
  1294. desc.rasterizerDesc.scissorEnable = option->value.intValue > 0;
  1295. isDefault = false;
  1296. break;
  1297. case OT_Multisample:
  1298. desc.rasterizerDesc.multisampleEnable = option->value.intValue > 0;
  1299. isDefault = false;
  1300. break;
  1301. case OT_AALine:
  1302. desc.rasterizerDesc.antialiasedLineEnable = option->value.intValue > 0;
  1303. isDefault = false;
  1304. break;
  1305. default:
  1306. break;
  1307. }
  1308. }
  1309. return !isDefault;
  1310. }
  1311. bool BSLFXCompiler::parseDepthState(PassData& passData, ASTFXNode* depthNode)
  1312. {
  1313. if (depthNode == nullptr || depthNode->type != NT_Depth)
  1314. return false;
  1315. bool isDefault = true;
  1316. for (int i = 0; i < depthNode->options->count; i++)
  1317. {
  1318. NodeOption* option = &depthNode->options->entries[i];
  1319. switch (option->type)
  1320. {
  1321. case OT_DepthRead:
  1322. passData.depthStencilDesc.depthReadEnable = option->value.intValue > 0;
  1323. isDefault = false;
  1324. break;
  1325. case OT_DepthWrite:
  1326. passData.depthStencilDesc.depthWriteEnable = option->value.intValue > 0;
  1327. isDefault = false;
  1328. break;
  1329. case OT_CompareFunc:
  1330. passData.depthStencilDesc.depthComparisonFunc = parseCompFunc((CompFuncValue)option->value.intValue);
  1331. isDefault = false;
  1332. break;
  1333. default:
  1334. break;
  1335. }
  1336. }
  1337. return !isDefault;
  1338. }
  1339. bool BSLFXCompiler::parseStencilState(PassData& passData, ASTFXNode* stencilNode)
  1340. {
  1341. if (stencilNode == nullptr || stencilNode->type != NT_Stencil)
  1342. return false;
  1343. bool isDefault = true;
  1344. for (int i = 0; i < stencilNode->options->count; i++)
  1345. {
  1346. NodeOption* option = &stencilNode->options->entries[i];
  1347. switch (option->type)
  1348. {
  1349. case OT_Enabled:
  1350. passData.depthStencilDesc.stencilEnable = option->value.intValue > 0;
  1351. isDefault = false;
  1352. break;
  1353. case OT_StencilReadMask:
  1354. passData.depthStencilDesc.stencilReadMask = (UINT8)option->value.intValue;
  1355. isDefault = false;
  1356. break;
  1357. case OT_StencilWriteMask:
  1358. passData.depthStencilDesc.stencilWriteMask = (UINT8)option->value.intValue;
  1359. isDefault = false;
  1360. break;
  1361. case OT_StencilOpFront:
  1362. parseStencilFront(passData.depthStencilDesc, option->value.nodePtr);
  1363. isDefault = false;
  1364. break;
  1365. case OT_StencilOpBack:
  1366. parseStencilBack(passData.depthStencilDesc, option->value.nodePtr);
  1367. isDefault = false;
  1368. break;
  1369. case OT_StencilRef:
  1370. passData.stencilRefValue = option->value.intValue;
  1371. break;
  1372. default:
  1373. break;
  1374. }
  1375. }
  1376. return !isDefault;
  1377. }
  1378. void BSLFXCompiler::parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData)
  1379. {
  1380. if (codeNode == nullptr || (codeNode->type != NT_Code))
  1381. {
  1382. return;
  1383. }
  1384. UINT32 index = (UINT32)-1;
  1385. for (int j = 0; j < codeNode->options->count; j++)
  1386. {
  1387. if (codeNode->options->entries[j].type == OT_Index)
  1388. index = codeNode->options->entries[j].value.intValue;
  1389. }
  1390. if (index != (UINT32)-1 && index < (UINT32)codeBlocks.size())
  1391. {
  1392. switch (codeNode->type)
  1393. {
  1394. case NT_Code:
  1395. passData.code += codeBlocks[index];
  1396. break;
  1397. default:
  1398. break;
  1399. }
  1400. }
  1401. }
  1402. void BSLFXCompiler::parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData)
  1403. {
  1404. if (passNode == nullptr || passNode->type != NT_Pass)
  1405. return;
  1406. for (int i = 0; i < passNode->options->count; i++)
  1407. {
  1408. NodeOption* option = &passNode->options->entries[i];
  1409. switch (option->type)
  1410. {
  1411. case OT_Blend:
  1412. passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
  1413. break;
  1414. case OT_Raster:
  1415. passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
  1416. break;
  1417. case OT_Depth:
  1418. passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
  1419. break;
  1420. case OT_Stencil:
  1421. passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
  1422. break;
  1423. case OT_Code:
  1424. parseCodeBlock(option->value.nodePtr, codeBlocks, passData);
  1425. break;
  1426. default:
  1427. break;
  1428. }
  1429. }
  1430. }
  1431. void BSLFXCompiler::parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData)
  1432. {
  1433. if (techniqueNode == nullptr || (techniqueNode->type != NT_Technique && techniqueNode->type != NT_Mixin))
  1434. return;
  1435. // There must always be at least one pass
  1436. if(techniqueData.passes.empty())
  1437. {
  1438. techniqueData.passes.push_back(PassData());
  1439. techniqueData.passes.back().seqIdx = 0;
  1440. }
  1441. PassData combinedCommonPassData;
  1442. UINT32 nextPassIdx = 0;
  1443. // Go in reverse because options are added in reverse order during parsing
  1444. for (int i = techniqueNode->options->count - 1; i >= 0; i--)
  1445. {
  1446. NodeOption* option = &techniqueNode->options->entries[i];
  1447. switch (option->type)
  1448. {
  1449. case OT_Pass:
  1450. {
  1451. UINT32 passIdx = nextPassIdx;
  1452. PassData* passData = nullptr;
  1453. for (auto& entry : techniqueData.passes)
  1454. {
  1455. if (entry.seqIdx == passIdx)
  1456. passData = &entry;
  1457. }
  1458. if (passData == nullptr)
  1459. {
  1460. techniqueData.passes.push_back(PassData());
  1461. passData = &techniqueData.passes.back();
  1462. passData->seqIdx = passIdx;
  1463. }
  1464. nextPassIdx = std::max(nextPassIdx, passIdx) + 1;
  1465. passData->code = combinedCommonPassData.code + passData->code;
  1466. ASTFXNode* passNode = option->value.nodePtr;
  1467. parsePass(passNode, codeBlocks, *passData);
  1468. }
  1469. break;
  1470. case OT_Code:
  1471. {
  1472. PassData commonPassData;
  1473. parseCodeBlock(option->value.nodePtr, codeBlocks, commonPassData);
  1474. for (auto& passData : techniqueData.passes)
  1475. passData.code += commonPassData.code;
  1476. combinedCommonPassData.code += commonPassData.code;
  1477. }
  1478. break;
  1479. default:
  1480. break;
  1481. }
  1482. }
  1483. // Parse common pass states
  1484. for (int i = 0; i < techniqueNode->options->count; i++)
  1485. {
  1486. NodeOption* option = &techniqueNode->options->entries[i];
  1487. switch (option->type)
  1488. {
  1489. case OT_Blend:
  1490. for (auto& passData : techniqueData.passes)
  1491. passData.blendIsDefault &= !parseBlendState(passData, option->value.nodePtr);
  1492. break;
  1493. case OT_Raster:
  1494. for (auto& passData : techniqueData.passes)
  1495. passData.rasterizerIsDefault &= !parseRasterizerState(passData, option->value.nodePtr);
  1496. break;
  1497. case OT_Depth:
  1498. for (auto& passData : techniqueData.passes)
  1499. passData.depthStencilIsDefault &= !parseDepthState(passData, option->value.nodePtr);
  1500. break;
  1501. case OT_Stencil:
  1502. for (auto& passData : techniqueData.passes)
  1503. passData.depthStencilIsDefault &= !parseStencilState(passData, option->value.nodePtr);
  1504. break;
  1505. default:
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. void BSLFXCompiler::parseOptions(ASTFXNode* optionsNode, SHADER_DESC& shaderDesc)
  1511. {
  1512. if (optionsNode == nullptr || optionsNode->type != NT_Options)
  1513. return;
  1514. for (int i = optionsNode->options->count - 1; i >= 0; i--)
  1515. {
  1516. NodeOption* option = &optionsNode->options->entries[i];
  1517. switch (option->type)
  1518. {
  1519. case OT_Separable:
  1520. shaderDesc.separablePasses = option->value.intValue > 1;
  1521. break;
  1522. case OT_Sort:
  1523. shaderDesc.queueSortType = parseSortType((CullAndSortModeValue)option->value.intValue);
  1524. break;
  1525. case OT_Priority:
  1526. shaderDesc.queuePriority = option->value.intValue;
  1527. break;
  1528. case OT_Transparent:
  1529. shaderDesc.flags |= (UINT32)ShaderFlags::Transparent;
  1530. break;
  1531. default:
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. BSLFXCompileResult BSLFXCompiler::parseTechnique(ParseState* parseState, const String& name,
  1537. const Vector<String>& codeBlocks, const ShaderVariation& variation, Vector<SPtr<Technique>>& techniques,
  1538. UnorderedSet<String>& includes, SHADER_DESC& shaderDesc)
  1539. {
  1540. BSLFXCompileResult output;
  1541. if (parseState->rootNode == nullptr || parseState->rootNode->type != NT_Shader)
  1542. {
  1543. parseStateDelete(parseState);
  1544. output.errorMessage = "Root not is null or not a shader.";
  1545. return output;
  1546. }
  1547. Vector<pair<ASTFXNode*, TechniqueData>> techniqueData;
  1548. // Go in reverse because options are added in reverse order during parsing
  1549. for (int i = parseState->rootNode->options->count - 1; i >= 0; i--)
  1550. {
  1551. NodeOption* option = &parseState->rootNode->options->entries[i];
  1552. switch (option->type)
  1553. {
  1554. case OT_Technique:
  1555. {
  1556. // We initially parse only meta-data, so we can handle out-of-order technique definitions
  1557. TechniqueMetaData metaData = parseTechniqueMetaData(option->value.nodePtr);
  1558. // Skip all techniques except the one we're parsing
  1559. if(metaData.name != name && !metaData.isMixin)
  1560. continue;
  1561. techniqueData.push_back(std::make_pair(option->value.nodePtr, TechniqueData()));
  1562. TechniqueData& data = techniqueData.back().second;
  1563. data.metaData = metaData;
  1564. break;
  1565. }
  1566. default:
  1567. break;
  1568. }
  1569. }
  1570. bool* techniqueWasParsed = bs_stack_alloc<bool>((UINT32)techniqueData.size());
  1571. std::function<bool(const TechniqueMetaData&, TechniqueData&)> parseInherited =
  1572. [&](const TechniqueMetaData& metaData, TechniqueData& outTechnique)
  1573. {
  1574. for (auto riter = metaData.includes.rbegin(); riter != metaData.includes.rend(); ++riter)
  1575. {
  1576. const String& includes = *riter;
  1577. UINT32 baseIdx = -1;
  1578. for(UINT32 i = 0; i < (UINT32)techniqueData.size(); i++)
  1579. {
  1580. auto& entry = techniqueData[i];
  1581. if (!entry.second.metaData.isMixin)
  1582. continue;
  1583. if (entry.second.metaData.name == includes)
  1584. {
  1585. bool matches =
  1586. (entry.second.metaData.language == metaData.language ||
  1587. entry.second.metaData.language == "Any");
  1588. // We want the last matching technique, in order to allow techniques to override each other
  1589. if (matches)
  1590. baseIdx = i;
  1591. }
  1592. }
  1593. if (baseIdx != (UINT32)-1)
  1594. {
  1595. auto& entry = techniqueData[baseIdx];
  1596. // Was already parsed previously, don't parse it multiple times (happens when multiple techniques
  1597. // include the same mixin)
  1598. if (techniqueWasParsed[baseIdx])
  1599. continue;
  1600. if (!parseInherited(entry.second.metaData, outTechnique))
  1601. return false;
  1602. parseTechnique(entry.first, codeBlocks, outTechnique);
  1603. techniqueWasParsed[baseIdx] = true;
  1604. }
  1605. else
  1606. {
  1607. output.errorMessage = "Mixin \"" + includes + "\" cannot be found.";
  1608. return false;
  1609. }
  1610. }
  1611. return true;
  1612. };
  1613. // Actually parse techniques
  1614. for (auto& entry : techniqueData)
  1615. {
  1616. const TechniqueMetaData& metaData = entry.second.metaData;
  1617. if (metaData.isMixin)
  1618. continue;
  1619. bs_zero_out(techniqueWasParsed, techniqueData.size());
  1620. if (!parseInherited(metaData, entry.second))
  1621. {
  1622. parseStateDelete(parseState);
  1623. bs_stack_free(techniqueWasParsed);
  1624. return output;
  1625. }
  1626. parseTechnique(entry.first, codeBlocks, entry.second);
  1627. }
  1628. bs_stack_free(techniqueWasParsed);
  1629. IncludeLink* includeLink = parseState->includes;
  1630. while(includeLink != nullptr)
  1631. {
  1632. String includeFilename = includeLink->data->filename;
  1633. auto iterFind = std::find(includes.begin(), includes.end(), includeFilename);
  1634. if (iterFind == includes.end())
  1635. includes.insert(includeFilename);
  1636. includeLink = includeLink->next;
  1637. }
  1638. parseStateDelete(parseState);
  1639. // Parse extended HLSL code and generate per-program code, also convert to GLSL/VKSL
  1640. UINT32 end = (UINT32)techniqueData.size();
  1641. for(UINT32 i = 0; i < end; i++)
  1642. {
  1643. const TechniqueMetaData& metaData = techniqueData[i].second.metaData;
  1644. if (metaData.isMixin)
  1645. continue;
  1646. TechniqueData& hlslTechnique = techniqueData[i].second;
  1647. TechniqueData glslTechnique = techniqueData[i].second;
  1648. // When working with OpenGL, lower-end feature sets are supported. For other backends, high-end is always assumed.
  1649. if(glslTechnique.metaData.featureSet == "HighEnd")
  1650. glslTechnique.metaData.language = "glsl";
  1651. else
  1652. glslTechnique.metaData.language = "glsl4_1";
  1653. TechniqueData vkslTechnique = techniqueData[i].second;
  1654. vkslTechnique.metaData.language = "vksl";
  1655. UINT32 numPasses = (UINT32)hlslTechnique.passes.size();
  1656. for(UINT32 j = 0; j < numPasses; j++)
  1657. {
  1658. PassData& hlslPassData = hlslTechnique.passes[j];
  1659. PassData& glslPassData = glslTechnique.passes[j];
  1660. PassData& vkslPassData = vkslTechnique.passes[j];
  1661. // Clean non-standard HLSL
  1662. static const std::regex regex("\\[\\s*layout\\s*\\(.*\\)\\s*\\]|\\[\\s*internal\\s*\\]|\\[\\s*color\\s*\\]|\\[\\s*alias\\s*\\(.*\\)\\s*\\]");
  1663. hlslPassData.code = regex_replace(hlslPassData.code, regex, "");
  1664. // Find valid entry points and parameters
  1665. // Note: XShaderCompiler needs to do a full pass when doing reflection, and for each individual program
  1666. // type. If performance is ever important here it could be good to update XShaderCompiler so it can
  1667. // somehow save the AST and then re-use it for multiple actions.
  1668. Vector<GpuProgramType> types;
  1669. reflectHLSL(glslPassData.code, shaderDesc, types);
  1670. UINT32 glslBinding = 0;
  1671. UINT32 vkslBinding = 0;
  1672. // Cross-compile for all detected shader types
  1673. // Note: I'm just copying HLSL code as-is. This code will contain all entry points which could have
  1674. // an effect on compile time. It would be ideal to remove dead code depending on program type. This would
  1675. // involve adding a HLSL code generator to XShaderCompiler.
  1676. for(auto& type : types)
  1677. {
  1678. switch(type)
  1679. {
  1680. case GPT_VERTEX_PROGRAM:
  1681. hlslPassData.vertexCode = hlslPassData.code;
  1682. glslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM, false, glslBinding);
  1683. vkslPassData.vertexCode = HLSLtoGLSL(glslPassData.code, GPT_VERTEX_PROGRAM, true, vkslBinding);
  1684. break;
  1685. case GPT_FRAGMENT_PROGRAM:
  1686. hlslPassData.fragmentCode = hlslPassData.code;
  1687. glslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM, false, glslBinding);
  1688. vkslPassData.fragmentCode = HLSLtoGLSL(glslPassData.code, GPT_FRAGMENT_PROGRAM, true, vkslBinding);
  1689. break;
  1690. case GPT_GEOMETRY_PROGRAM:
  1691. hlslPassData.geometryCode = hlslPassData.code;
  1692. glslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM, false, glslBinding);
  1693. vkslPassData.geometryCode = HLSLtoGLSL(glslPassData.code, GPT_GEOMETRY_PROGRAM, true, vkslBinding);
  1694. break;
  1695. case GPT_HULL_PROGRAM:
  1696. hlslPassData.hullCode = hlslPassData.code;
  1697. glslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM, false, glslBinding);
  1698. vkslPassData.hullCode = HLSLtoGLSL(glslPassData.code, GPT_HULL_PROGRAM, true, vkslBinding);
  1699. break;
  1700. case GPT_DOMAIN_PROGRAM:
  1701. hlslPassData.domainCode = hlslPassData.code;
  1702. glslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM, false, glslBinding);
  1703. vkslPassData.domainCode = HLSLtoGLSL(glslPassData.code, GPT_DOMAIN_PROGRAM, true, vkslBinding);
  1704. break;
  1705. case GPT_COMPUTE_PROGRAM:
  1706. hlslPassData.computeCode = hlslPassData.code;
  1707. glslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM, false, glslBinding);
  1708. vkslPassData.computeCode = HLSLtoGLSL(glslPassData.code, GPT_COMPUTE_PROGRAM, true, vkslBinding);
  1709. break;
  1710. default:
  1711. break;
  1712. }
  1713. }
  1714. }
  1715. techniqueData.push_back(std::make_pair(nullptr, glslTechnique));
  1716. techniqueData.push_back(std::make_pair(nullptr, vkslTechnique));
  1717. }
  1718. for(auto& entry : techniqueData)
  1719. {
  1720. const TechniqueMetaData& metaData = entry.second.metaData;
  1721. if (metaData.isMixin)
  1722. continue;
  1723. Map<UINT32, SPtr<Pass>, std::greater<UINT32>> passes;
  1724. for (auto& passData : entry.second.passes)
  1725. {
  1726. PASS_DESC passDesc;
  1727. if (!passData.blendIsDefault)
  1728. passDesc.blendState = BlendState::create(passData.blendDesc);
  1729. if (!passData.rasterizerIsDefault)
  1730. passDesc.rasterizerState = RasterizerState::create(passData.rasterizerDesc);
  1731. if (!passData.depthStencilIsDefault)
  1732. passDesc.depthStencilState = DepthStencilState::create(passData.depthStencilDesc);
  1733. auto createProgram =
  1734. [](const String& language, const String& entry, const String& code, GpuProgramType type) -> SPtr<GpuProgram>
  1735. {
  1736. if (code.empty())
  1737. return nullptr;
  1738. GPU_PROGRAM_DESC desc;
  1739. desc.language = language;
  1740. desc.entryPoint = entry;
  1741. desc.source = code;
  1742. desc.type = type;
  1743. return GpuProgram::create(desc);
  1744. };
  1745. bool isHLSL = metaData.language == "hlsl";
  1746. passDesc.vertexProgram = createProgram(
  1747. metaData.language,
  1748. isHLSL ? "vsmain" : "main",
  1749. passData.vertexCode,
  1750. GPT_VERTEX_PROGRAM);
  1751. passDesc.fragmentProgram = createProgram(
  1752. metaData.language,
  1753. isHLSL ? "fsmain" : "main",
  1754. passData.fragmentCode,
  1755. GPT_FRAGMENT_PROGRAM);
  1756. passDesc.geometryProgram = createProgram(
  1757. metaData.language,
  1758. isHLSL ? "gsmain" : "main",
  1759. passData.geometryCode,
  1760. GPT_GEOMETRY_PROGRAM);
  1761. passDesc.hullProgram = createProgram(
  1762. metaData.language,
  1763. isHLSL ? "hsmain" : "main",
  1764. passData.hullCode,
  1765. GPT_HULL_PROGRAM);
  1766. passDesc.domainProgram = createProgram(
  1767. metaData.language,
  1768. isHLSL ? "dsmain" : "main",
  1769. passData.domainCode,
  1770. GPT_DOMAIN_PROGRAM);
  1771. passDesc.computeProgram = createProgram(
  1772. metaData.language,
  1773. isHLSL ? "csmain" : "main",
  1774. passData.computeCode,
  1775. GPT_COMPUTE_PROGRAM);
  1776. passDesc.stencilRefValue = passData.stencilRefValue;
  1777. SPtr<Pass> pass = Pass::create(passDesc);
  1778. if (pass != nullptr)
  1779. passes[passData.seqIdx] = pass;
  1780. }
  1781. Vector<SPtr<Pass>> orderedPasses;
  1782. for (auto& KVP : passes)
  1783. orderedPasses.push_back(KVP.second);
  1784. if (orderedPasses.size() > 0)
  1785. {
  1786. SPtr<Technique> technique = Technique::create(metaData.language, metaData.tags, variation, orderedPasses);
  1787. techniques.push_back(technique);
  1788. }
  1789. }
  1790. return output;
  1791. }
  1792. String BSLFXCompiler::removeQuotes(const char* input)
  1793. {
  1794. UINT32 len = (UINT32)strlen(input);
  1795. String output(len - 2, ' ');
  1796. for (UINT32 i = 0; i < (len - 2); i++)
  1797. output[i] = input[i + 1];
  1798. return output;
  1799. }
  1800. }