BsSLFXCompiler.cpp 50 KB

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