GlslDecompiler.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Ryujinx.Graphics.Gal.Shader
  7. {
  8. public class GlslDecompiler
  9. {
  10. private delegate string GetInstExpr(ShaderIrOp Op);
  11. private Dictionary<ShaderIrInst, GetInstExpr> InstsExpr;
  12. private enum OperType
  13. {
  14. Bool,
  15. F32,
  16. I32
  17. }
  18. private const string IdentationStr = " ";
  19. private const int MaxVertexInput = 3;
  20. private GlslDecl Decl;
  21. private ShaderHeader Header, HeaderB;
  22. private ShaderIrBlock[] Blocks, BlocksB;
  23. private StringBuilder SB;
  24. public GlslDecompiler()
  25. {
  26. InstsExpr = new Dictionary<ShaderIrInst, GetInstExpr>()
  27. {
  28. { ShaderIrInst.Abs, GetAbsExpr },
  29. { ShaderIrInst.Add, GetAddExpr },
  30. { ShaderIrInst.And, GetAndExpr },
  31. { ShaderIrInst.Asr, GetAsrExpr },
  32. { ShaderIrInst.Band, GetBandExpr },
  33. { ShaderIrInst.Bnot, GetBnotExpr },
  34. { ShaderIrInst.Bor, GetBorExpr },
  35. { ShaderIrInst.Bxor, GetBxorExpr },
  36. { ShaderIrInst.Ceil, GetCeilExpr },
  37. { ShaderIrInst.Ceq, GetCeqExpr },
  38. { ShaderIrInst.Cge, GetCgeExpr },
  39. { ShaderIrInst.Cgt, GetCgtExpr },
  40. { ShaderIrInst.Clamps, GetClampsExpr },
  41. { ShaderIrInst.Clampu, GetClampuExpr },
  42. { ShaderIrInst.Cle, GetCleExpr },
  43. { ShaderIrInst.Clt, GetCltExpr },
  44. { ShaderIrInst.Cne, GetCneExpr },
  45. { ShaderIrInst.Cut, GetCutExpr },
  46. { ShaderIrInst.Exit, GetExitExpr },
  47. { ShaderIrInst.Fabs, GetAbsExpr },
  48. { ShaderIrInst.Fadd, GetAddExpr },
  49. { ShaderIrInst.Fceq, GetCeqExpr },
  50. { ShaderIrInst.Fcequ, GetCequExpr },
  51. { ShaderIrInst.Fcge, GetCgeExpr },
  52. { ShaderIrInst.Fcgeu, GetCgeuExpr },
  53. { ShaderIrInst.Fcgt, GetCgtExpr },
  54. { ShaderIrInst.Fcgtu, GetCgtuExpr },
  55. { ShaderIrInst.Fclamp, GetFclampExpr },
  56. { ShaderIrInst.Fcle, GetCleExpr },
  57. { ShaderIrInst.Fcleu, GetCleuExpr },
  58. { ShaderIrInst.Fclt, GetCltExpr },
  59. { ShaderIrInst.Fcltu, GetCltuExpr },
  60. { ShaderIrInst.Fcnan, GetCnanExpr },
  61. { ShaderIrInst.Fcne, GetCneExpr },
  62. { ShaderIrInst.Fcneu, GetCneuExpr },
  63. { ShaderIrInst.Fcnum, GetCnumExpr },
  64. { ShaderIrInst.Fcos, GetFcosExpr },
  65. { ShaderIrInst.Fex2, GetFex2Expr },
  66. { ShaderIrInst.Ffma, GetFfmaExpr },
  67. { ShaderIrInst.Flg2, GetFlg2Expr },
  68. { ShaderIrInst.Floor, GetFloorExpr },
  69. { ShaderIrInst.Fmax, GetMaxExpr },
  70. { ShaderIrInst.Fmin, GetMinExpr },
  71. { ShaderIrInst.Fmul, GetMulExpr },
  72. { ShaderIrInst.Fneg, GetNegExpr },
  73. { ShaderIrInst.Frcp, GetFrcpExpr },
  74. { ShaderIrInst.Frsq, GetFrsqExpr },
  75. { ShaderIrInst.Fsin, GetFsinExpr },
  76. { ShaderIrInst.Fsqrt, GetFsqrtExpr },
  77. { ShaderIrInst.Ftos, GetFtosExpr },
  78. { ShaderIrInst.Ftou, GetFtouExpr },
  79. { ShaderIrInst.Ipa, GetIpaExpr },
  80. { ShaderIrInst.Kil, GetKilExpr },
  81. { ShaderIrInst.Lsl, GetLslExpr },
  82. { ShaderIrInst.Lsr, GetLsrExpr },
  83. { ShaderIrInst.Max, GetMaxExpr },
  84. { ShaderIrInst.Min, GetMinExpr },
  85. { ShaderIrInst.Mul, GetMulExpr },
  86. { ShaderIrInst.Neg, GetNegExpr },
  87. { ShaderIrInst.Not, GetNotExpr },
  88. { ShaderIrInst.Or, GetOrExpr },
  89. { ShaderIrInst.Stof, GetStofExpr },
  90. { ShaderIrInst.Sub, GetSubExpr },
  91. { ShaderIrInst.Texb, GetTexbExpr },
  92. { ShaderIrInst.Texq, GetTexqExpr },
  93. { ShaderIrInst.Texs, GetTexsExpr },
  94. { ShaderIrInst.Trunc, GetTruncExpr },
  95. { ShaderIrInst.Txlf, GetTxlfExpr },
  96. { ShaderIrInst.Utof, GetUtofExpr },
  97. { ShaderIrInst.Xor, GetXorExpr }
  98. };
  99. }
  100. public GlslProgram Decompile(
  101. IGalMemory Memory,
  102. long VpAPosition,
  103. long VpBPosition,
  104. GalShaderType ShaderType)
  105. {
  106. Header = new ShaderHeader(Memory, VpAPosition);
  107. HeaderB = new ShaderHeader(Memory, VpBPosition);
  108. Blocks = ShaderDecoder.Decode(Memory, VpAPosition);
  109. BlocksB = ShaderDecoder.Decode(Memory, VpBPosition);
  110. GlslDecl DeclVpA = new GlslDecl(Blocks, ShaderType);
  111. GlslDecl DeclVpB = new GlslDecl(BlocksB, ShaderType);
  112. Decl = GlslDecl.Merge(DeclVpA, DeclVpB);
  113. return Decompile();
  114. }
  115. public GlslProgram Decompile(IGalMemory Memory, long Position, GalShaderType ShaderType)
  116. {
  117. Header = new ShaderHeader(Memory, Position);
  118. HeaderB = null;
  119. Blocks = ShaderDecoder.Decode(Memory, Position);
  120. BlocksB = null;
  121. Decl = new GlslDecl(Blocks, ShaderType);
  122. return Decompile();
  123. }
  124. private GlslProgram Decompile()
  125. {
  126. SB = new StringBuilder();
  127. SB.AppendLine("#version 410 core");
  128. PrintDeclHeader();
  129. PrintDeclTextures();
  130. PrintDeclUniforms();
  131. PrintDeclAttributes();
  132. PrintDeclInAttributes();
  133. PrintDeclOutAttributes();
  134. PrintDeclGprs();
  135. PrintDeclPreds();
  136. if (BlocksB != null)
  137. {
  138. PrintBlockScope(Blocks[0], null, null, "void " + GlslDecl.ProgramAName + "()", IdentationStr);
  139. SB.AppendLine();
  140. PrintBlockScope(BlocksB[0], null, null, "void " + GlslDecl.ProgramBName + "()", IdentationStr);
  141. }
  142. else
  143. {
  144. PrintBlockScope(Blocks[0], null, null, "void " + GlslDecl.ProgramName + "()", IdentationStr);
  145. }
  146. SB.AppendLine();
  147. PrintMain();
  148. string GlslCode = SB.ToString();
  149. List<ShaderDeclInfo> TextureInfo = new List<ShaderDeclInfo>();
  150. TextureInfo.AddRange(Decl.Textures.Values);
  151. TextureInfo.AddRange(IterateCbTextures());
  152. return new GlslProgram(GlslCode, TextureInfo, Decl.Uniforms.Values);
  153. }
  154. private void PrintDeclHeader()
  155. {
  156. if (Decl.ShaderType == GalShaderType.Geometry)
  157. {
  158. int MaxVertices = Header.MaxOutputVertexCount;
  159. string OutputTopology;
  160. switch (Header.OutputTopology)
  161. {
  162. case ShaderHeader.PointList: OutputTopology = "points"; break;
  163. case ShaderHeader.LineStrip: OutputTopology = "line_strip"; break;
  164. case ShaderHeader.TriangleStrip: OutputTopology = "triangle_strip"; break;
  165. default: throw new InvalidOperationException();
  166. }
  167. SB.AppendLine("#extension GL_ARB_enhanced_layouts : require");
  168. SB.AppendLine();
  169. SB.AppendLine("// Stubbed. Maxwell geometry shaders don't inform input geometry type");
  170. SB.AppendLine("layout(triangles) in;" + Environment.NewLine);
  171. SB.AppendLine($"layout({OutputTopology}, max_vertices = {MaxVertices}) out;");
  172. SB.AppendLine();
  173. }
  174. }
  175. private void PrintDeclTextures()
  176. {
  177. foreach (ShaderDeclInfo DeclInfo in IterateCbTextures())
  178. {
  179. SB.AppendLine("uniform sampler2D " + DeclInfo.Name + ";");
  180. }
  181. PrintDecls(Decl.Textures, "uniform sampler2D");
  182. }
  183. private IEnumerable<ShaderDeclInfo> IterateCbTextures()
  184. {
  185. HashSet<string> Names = new HashSet<string>();
  186. foreach (ShaderDeclInfo DeclInfo in Decl.CbTextures.Values.OrderBy(DeclKeySelector))
  187. {
  188. if (Names.Add(DeclInfo.Name))
  189. {
  190. yield return DeclInfo;
  191. }
  192. }
  193. }
  194. private void PrintDeclUniforms()
  195. {
  196. if (Decl.ShaderType == GalShaderType.Vertex)
  197. {
  198. SB.AppendLine("layout (std140) uniform " + GlslDecl.ExtraUniformBlockName + "{");
  199. SB.AppendLine(IdentationStr + "vec2 " + GlslDecl.FlipUniformName + ";");
  200. SB.AppendLine("};");
  201. }
  202. SB.AppendLine();
  203. foreach (ShaderDeclInfo DeclInfo in Decl.Uniforms.Values.OrderBy(DeclKeySelector))
  204. {
  205. SB.AppendLine($"layout (std140) uniform {DeclInfo.Name} {{");
  206. SB.AppendLine($"{IdentationStr}vec4 {DeclInfo.Name}_data[{GlslDecl.MaxUboSize}];");
  207. SB.AppendLine("};");
  208. }
  209. if (Decl.Uniforms.Count > 0)
  210. {
  211. SB.AppendLine();
  212. }
  213. }
  214. private void PrintDeclAttributes()
  215. {
  216. string GeometryArray = (Decl.ShaderType == GalShaderType.Geometry) ? "[" + MaxVertexInput + "]" : "";
  217. PrintDecls(Decl.Attributes, Suffix: GeometryArray);
  218. }
  219. private void PrintDeclInAttributes()
  220. {
  221. if (Decl.ShaderType == GalShaderType.Fragment)
  222. {
  223. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";");
  224. }
  225. if (Decl.ShaderType == GalShaderType.Geometry)
  226. {
  227. if (Decl.InAttributes.Count > 0)
  228. {
  229. SB.AppendLine("in Vertex {");
  230. foreach (ShaderDeclInfo DeclInfo in Decl.InAttributes.Values.OrderBy(DeclKeySelector))
  231. {
  232. if (DeclInfo.Index >= 0)
  233. {
  234. SB.AppendLine(IdentationStr + "layout (location = " + DeclInfo.Index + ") vec4 " + DeclInfo.Name + "; ");
  235. }
  236. }
  237. SB.AppendLine("} block_in[];" + Environment.NewLine);
  238. }
  239. }
  240. else
  241. {
  242. PrintDeclAttributes(Decl.InAttributes.Values, "in");
  243. }
  244. }
  245. private void PrintDeclOutAttributes()
  246. {
  247. if (Decl.ShaderType != GalShaderType.Fragment)
  248. {
  249. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") out vec4 " + GlslDecl.PositionOutAttrName + ";");
  250. }
  251. PrintDeclAttributes(Decl.OutAttributes.Values, "out");
  252. }
  253. private void PrintDeclAttributes(IEnumerable<ShaderDeclInfo> Decls, string InOut)
  254. {
  255. int Count = 0;
  256. foreach (ShaderDeclInfo DeclInfo in Decls.OrderBy(DeclKeySelector))
  257. {
  258. if (DeclInfo.Index >= 0)
  259. {
  260. SB.AppendLine("layout (location = " + DeclInfo.Index + ") " + InOut + " vec4 " + DeclInfo.Name + ";");
  261. Count++;
  262. }
  263. }
  264. if (Count > 0)
  265. {
  266. SB.AppendLine();
  267. }
  268. }
  269. private void PrintDeclGprs()
  270. {
  271. PrintDecls(Decl.Gprs);
  272. }
  273. private void PrintDeclPreds()
  274. {
  275. PrintDecls(Decl.Preds, "bool");
  276. }
  277. private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null, string Suffix = "")
  278. {
  279. foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector))
  280. {
  281. string Name;
  282. if (CustomType != null)
  283. {
  284. Name = CustomType + " " + DeclInfo.Name + Suffix + ";";
  285. }
  286. else if (DeclInfo.Name == GlslDecl.FragmentOutputName)
  287. {
  288. Name = "layout (location = 0) out vec4 " + DeclInfo.Name + Suffix + ";" + Environment.NewLine;
  289. }
  290. else
  291. {
  292. Name = GetDecl(DeclInfo) + Suffix + ";";
  293. }
  294. SB.AppendLine(Name);
  295. }
  296. if (Dict.Count > 0)
  297. {
  298. SB.AppendLine();
  299. }
  300. }
  301. private int DeclKeySelector(ShaderDeclInfo DeclInfo)
  302. {
  303. return DeclInfo.Cbuf << 24 | DeclInfo.Index;
  304. }
  305. private string GetDecl(ShaderDeclInfo DeclInfo)
  306. {
  307. if (DeclInfo.Size == 4)
  308. {
  309. return "vec4 " + DeclInfo.Name;
  310. }
  311. else
  312. {
  313. return "float " + DeclInfo.Name;
  314. }
  315. }
  316. private void PrintMain()
  317. {
  318. SB.AppendLine("void main() {");
  319. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.InAttributes)
  320. {
  321. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  322. {
  323. continue;
  324. }
  325. ShaderDeclInfo DeclInfo = KV.Value;
  326. if (Decl.ShaderType == GalShaderType.Geometry)
  327. {
  328. for (int Vertex = 0; Vertex < MaxVertexInput; Vertex++)
  329. {
  330. string Dst = Attr.Name + "[" + Vertex + "]";
  331. string Src = "block_in[" + Vertex + "]." + DeclInfo.Name;
  332. SB.AppendLine(IdentationStr + Dst + " = " + Src + ";");
  333. }
  334. }
  335. else
  336. {
  337. SB.AppendLine(IdentationStr + Attr.Name + " = " + DeclInfo.Name + ";");
  338. }
  339. }
  340. if (BlocksB != null)
  341. {
  342. SB.AppendLine(IdentationStr + GlslDecl.ProgramAName + "();");
  343. SB.AppendLine(IdentationStr + GlslDecl.ProgramBName + "();");
  344. }
  345. else
  346. {
  347. SB.AppendLine(IdentationStr + GlslDecl.ProgramName + "();");
  348. }
  349. if (Decl.ShaderType != GalShaderType.Geometry)
  350. {
  351. PrintAttrToOutput();
  352. }
  353. SB.AppendLine("}");
  354. }
  355. private void PrintAttrToOutput(string Identation = IdentationStr)
  356. {
  357. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.OutAttributes)
  358. {
  359. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  360. {
  361. continue;
  362. }
  363. ShaderDeclInfo DeclInfo = KV.Value;
  364. string Name = Attr.Name;
  365. if (Decl.ShaderType == GalShaderType.Geometry)
  366. {
  367. Name += "[0]";
  368. }
  369. SB.AppendLine(Identation + DeclInfo.Name + " = " + Name + ";");
  370. }
  371. if (Decl.ShaderType == GalShaderType.Vertex)
  372. {
  373. SB.AppendLine(Identation + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";");
  374. }
  375. if (Decl.ShaderType != GalShaderType.Fragment)
  376. {
  377. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
  378. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + ".w = 1;");
  379. }
  380. }
  381. private void PrintBlockScope(
  382. ShaderIrBlock Block,
  383. ShaderIrBlock EndBlock,
  384. ShaderIrBlock LoopBlock,
  385. string ScopeName,
  386. string Identation,
  387. bool IsDoWhile = false)
  388. {
  389. string UpIdent = Identation.Substring(0, Identation.Length - IdentationStr.Length);
  390. if (IsDoWhile)
  391. {
  392. SB.AppendLine(UpIdent + "do {");
  393. }
  394. else
  395. {
  396. SB.AppendLine(UpIdent + ScopeName + " {");
  397. }
  398. while (Block != null && Block != EndBlock)
  399. {
  400. ShaderIrNode[] Nodes = Block.GetNodes();
  401. Block = PrintNodes(Block, EndBlock, LoopBlock, Identation, Nodes);
  402. }
  403. if (IsDoWhile)
  404. {
  405. SB.AppendLine(UpIdent + "} " + ScopeName + ";");
  406. }
  407. else
  408. {
  409. SB.AppendLine(UpIdent + "}");
  410. }
  411. }
  412. private ShaderIrBlock PrintNodes(
  413. ShaderIrBlock Block,
  414. ShaderIrBlock EndBlock,
  415. ShaderIrBlock LoopBlock,
  416. string Identation,
  417. params ShaderIrNode[] Nodes)
  418. {
  419. /*
  420. * Notes about control flow and if-else/loop generation:
  421. * The code assumes that the program has sane control flow,
  422. * that is, there's no jumps to a location after another jump or
  423. * jump target (except for the end of an if-else block), and backwards
  424. * jumps to a location before the last loop dominator.
  425. * Such cases needs to be transformed on a step before the GLSL code
  426. * generation to ensure that we have sane graphs to work with.
  427. * TODO: Such transformation is not yet implemented.
  428. */
  429. string NewIdent = Identation + IdentationStr;
  430. ShaderIrBlock LoopTail = GetLoopTailBlock(Block);
  431. if (LoopTail != null && LoopBlock != Block)
  432. {
  433. //Shoock! kuma shock! We have a loop here!
  434. //The entire sequence needs to be inside a do-while block.
  435. ShaderIrBlock LoopEnd = GetDownBlock(LoopTail);
  436. PrintBlockScope(Block, LoopEnd, Block, "while (false)", NewIdent, IsDoWhile: true);
  437. return LoopEnd;
  438. }
  439. foreach (ShaderIrNode Node in Nodes)
  440. {
  441. if (Node is ShaderIrCond Cond)
  442. {
  443. string IfExpr = GetSrcExpr(Cond.Pred, true);
  444. if (Cond.Not)
  445. {
  446. IfExpr = "!(" + IfExpr + ")";
  447. }
  448. if (Cond.Child is ShaderIrOp Op && Op.Inst == ShaderIrInst.Bra)
  449. {
  450. //Branch is a loop branch and would result in infinite recursion.
  451. if (Block.Branch.Position <= Block.Position)
  452. {
  453. SB.AppendLine(Identation + "if (" + IfExpr + ") {");
  454. SB.AppendLine(Identation + IdentationStr + "continue;");
  455. SB.AppendLine(Identation + "}");
  456. continue;
  457. }
  458. string SubScopeName = "if (!" + IfExpr + ")";
  459. PrintBlockScope(Block.Next, Block.Branch, LoopBlock, SubScopeName, NewIdent);
  460. ShaderIrBlock IfElseEnd = GetUpBlock(Block.Branch).Branch;
  461. if (IfElseEnd?.Position > Block.Branch.Position)
  462. {
  463. PrintBlockScope(Block.Branch, IfElseEnd, LoopBlock, "else", NewIdent);
  464. return IfElseEnd;
  465. }
  466. return Block.Branch;
  467. }
  468. else
  469. {
  470. SB.AppendLine(Identation + "if (" + IfExpr + ") {");
  471. PrintNodes(Block, EndBlock, LoopBlock, NewIdent, Cond.Child);
  472. SB.AppendLine(Identation + "}");
  473. }
  474. }
  475. else if (Node is ShaderIrAsg Asg)
  476. {
  477. if (IsValidOutOper(Asg.Dst))
  478. {
  479. string Expr = GetSrcExpr(Asg.Src, true);
  480. Expr = GetExprWithCast(Asg.Dst, Asg.Src, Expr);
  481. SB.AppendLine(Identation + GetDstOperName(Asg.Dst) + " = " + Expr + ";");
  482. }
  483. }
  484. else if (Node is ShaderIrOp Op)
  485. {
  486. if (Op.Inst == ShaderIrInst.Bra)
  487. {
  488. if (Block.Branch.Position <= Block.Position)
  489. {
  490. SB.AppendLine(Identation + "continue;");
  491. }
  492. }
  493. else if (Op.Inst == ShaderIrInst.Emit)
  494. {
  495. PrintAttrToOutput(Identation);
  496. SB.AppendLine(Identation + "EmitVertex();");
  497. }
  498. else
  499. {
  500. SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
  501. }
  502. }
  503. else if (Node is ShaderIrCmnt Cmnt)
  504. {
  505. SB.AppendLine(Identation + "// " + Cmnt.Comment);
  506. }
  507. else
  508. {
  509. throw new InvalidOperationException();
  510. }
  511. }
  512. return Block.Next;
  513. }
  514. private ShaderIrBlock GetUpBlock(ShaderIrBlock Block)
  515. {
  516. return Blocks.FirstOrDefault(x => x.EndPosition == Block.Position);
  517. }
  518. private ShaderIrBlock GetDownBlock(ShaderIrBlock Block)
  519. {
  520. return Blocks.FirstOrDefault(x => x.Position == Block.EndPosition);
  521. }
  522. private ShaderIrBlock GetLoopTailBlock(ShaderIrBlock LoopHead)
  523. {
  524. ShaderIrBlock Tail = null;
  525. foreach (ShaderIrBlock Block in LoopHead.Sources)
  526. {
  527. if (Block.Position >= LoopHead.Position)
  528. {
  529. if (Tail == null || Tail.Position < Block.Position)
  530. {
  531. Tail = Block;
  532. }
  533. }
  534. }
  535. return Tail;
  536. }
  537. private bool IsValidOutOper(ShaderIrNode Node)
  538. {
  539. if (Node is ShaderIrOperGpr Gpr && Gpr.IsConst)
  540. {
  541. return false;
  542. }
  543. else if (Node is ShaderIrOperPred Pred && Pred.IsConst)
  544. {
  545. return false;
  546. }
  547. return true;
  548. }
  549. private string GetDstOperName(ShaderIrNode Node)
  550. {
  551. if (Node is ShaderIrOperAbuf Abuf)
  552. {
  553. return GetOutAbufName(Abuf);
  554. }
  555. else if (Node is ShaderIrOperGpr Gpr)
  556. {
  557. return GetName(Gpr);
  558. }
  559. else if (Node is ShaderIrOperPred Pred)
  560. {
  561. return GetName(Pred);
  562. }
  563. throw new ArgumentException(nameof(Node));
  564. }
  565. private string GetSrcExpr(ShaderIrNode Node, bool Entry = false)
  566. {
  567. switch (Node)
  568. {
  569. case ShaderIrOperAbuf Abuf: return GetName (Abuf);
  570. case ShaderIrOperCbuf Cbuf: return GetName (Cbuf);
  571. case ShaderIrOperGpr Gpr: return GetName (Gpr);
  572. case ShaderIrOperImm Imm: return GetValue(Imm);
  573. case ShaderIrOperImmf Immf: return GetValue(Immf);
  574. case ShaderIrOperPred Pred: return GetName (Pred);
  575. case ShaderIrOp Op:
  576. string Expr;
  577. if (InstsExpr.TryGetValue(Op.Inst, out GetInstExpr GetExpr))
  578. {
  579. Expr = GetExpr(Op);
  580. }
  581. else
  582. {
  583. throw new NotImplementedException(Op.Inst.ToString());
  584. }
  585. if (!Entry && NeedsParentheses(Op))
  586. {
  587. Expr = "(" + Expr + ")";
  588. }
  589. return Expr;
  590. default: throw new ArgumentException(nameof(Node));
  591. }
  592. }
  593. private static bool NeedsParentheses(ShaderIrOp Op)
  594. {
  595. switch (Op.Inst)
  596. {
  597. case ShaderIrInst.Ipa:
  598. case ShaderIrInst.Texq:
  599. case ShaderIrInst.Texs:
  600. case ShaderIrInst.Txlf:
  601. return false;
  602. }
  603. return true;
  604. }
  605. private string GetName(ShaderIrOperCbuf Cbuf)
  606. {
  607. if (!Decl.Uniforms.TryGetValue(Cbuf.Index, out ShaderDeclInfo DeclInfo))
  608. {
  609. throw new InvalidOperationException();
  610. }
  611. if (Cbuf.Offs != null)
  612. {
  613. string Offset = "floatBitsToInt(" + GetSrcExpr(Cbuf.Offs) + ")";
  614. string Index = "(" + Cbuf.Pos * 4 + " + " + Offset + ")";
  615. return $"{DeclInfo.Name}_data[{Index} / 16][({Index} / 4) % 4]";
  616. }
  617. else
  618. {
  619. return $"{DeclInfo.Name}_data[{Cbuf.Pos / 4}][{Cbuf.Pos % 4}]";
  620. }
  621. }
  622. private string GetOutAbufName(ShaderIrOperAbuf Abuf)
  623. {
  624. if (Decl.ShaderType == GalShaderType.Geometry)
  625. {
  626. switch (Abuf.Offs)
  627. {
  628. case GlslDecl.LayerAttr: return "gl_Layer";
  629. }
  630. }
  631. return GetAttrTempName(Abuf);
  632. }
  633. private string GetName(ShaderIrOperAbuf Abuf)
  634. {
  635. //Handle special scalar read-only attributes here.
  636. if (Decl.ShaderType == GalShaderType.Vertex)
  637. {
  638. switch (Abuf.Offs)
  639. {
  640. case GlslDecl.VertexIdAttr: return "gl_VertexID";
  641. case GlslDecl.InstanceIdAttr: return "gl_InstanceID";
  642. }
  643. }
  644. else if (Decl.ShaderType == GalShaderType.TessEvaluation)
  645. {
  646. switch (Abuf.Offs)
  647. {
  648. case GlslDecl.TessCoordAttrX: return "gl_TessCoord.x";
  649. case GlslDecl.TessCoordAttrY: return "gl_TessCoord.y";
  650. case GlslDecl.TessCoordAttrZ: return "gl_TessCoord.z";
  651. }
  652. }
  653. else if (Decl.ShaderType == GalShaderType.Fragment)
  654. {
  655. switch (Abuf.Offs)
  656. {
  657. //Note: It's a guess that Maxwell's face is 1 when gl_FrontFacing == true
  658. case GlslDecl.FaceAttr: return "(gl_FrontFacing ? 1 : 0)";
  659. }
  660. }
  661. return GetAttrTempName(Abuf);
  662. }
  663. private string GetAttrTempName(ShaderIrOperAbuf Abuf)
  664. {
  665. int Index = Abuf.Offs >> 4;
  666. int Elem = (Abuf.Offs >> 2) & 3;
  667. string Swizzle = "." + GetAttrSwizzle(Elem);
  668. if (!Decl.Attributes.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  669. {
  670. //Handle special vec4 attributes here
  671. //(for example, index 7 is aways gl_Position).
  672. if (Index == GlslDecl.GlPositionVec4Index)
  673. {
  674. string Name =
  675. Decl.ShaderType != GalShaderType.Vertex &&
  676. Decl.ShaderType != GalShaderType.Geometry ? GlslDecl.PositionOutAttrName : "gl_Position";
  677. return Name + Swizzle;
  678. }
  679. throw new InvalidOperationException();
  680. }
  681. if (Decl.ShaderType == GalShaderType.Geometry)
  682. {
  683. string Vertex = "floatBitsToInt(" + GetSrcExpr(Abuf.Vertex) + ")";
  684. return DeclInfo.Name + "[" + Vertex + "]" + Swizzle;
  685. }
  686. else
  687. {
  688. return DeclInfo.Name + Swizzle;
  689. }
  690. }
  691. private string GetName(ShaderIrOperGpr Gpr)
  692. {
  693. return Gpr.IsConst ? "0" : GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  694. }
  695. private string GetValue(ShaderIrOperImm Imm)
  696. {
  697. //Only use hex is the value is too big and would likely be hard to read as int.
  698. if (Imm.Value > 0xfff ||
  699. Imm.Value < -0xfff)
  700. {
  701. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  702. }
  703. else
  704. {
  705. return GetIntConst(Imm.Value);
  706. }
  707. }
  708. private string GetValue(ShaderIrOperImmf Immf)
  709. {
  710. return GetFloatConst(Immf.Value);
  711. }
  712. private string GetName(ShaderIrOperPred Pred)
  713. {
  714. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  715. }
  716. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  717. {
  718. int VecIndex = Index >> 2;
  719. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  720. {
  721. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  722. {
  723. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  724. }
  725. }
  726. if (!Dict.TryGetValue(Index, out DeclInfo))
  727. {
  728. throw new InvalidOperationException();
  729. }
  730. return DeclInfo.Name;
  731. }
  732. private string GetAttrSwizzle(int Elem)
  733. {
  734. return "xyzw".Substring(Elem, 1);
  735. }
  736. private string GetAbsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  737. private string GetAddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  738. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  739. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  740. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  741. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  742. private string GetBorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "||");
  743. private string GetBxorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^^");
  744. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  745. private string GetClampsExpr(ShaderIrOp Op)
  746. {
  747. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  748. GetOperExpr(Op, Op.OperandB) + ", " +
  749. GetOperExpr(Op, Op.OperandC) + ")";
  750. }
  751. private string GetClampuExpr(ShaderIrOp Op)
  752. {
  753. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  754. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  755. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  756. }
  757. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  758. private string GetCequExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "==");
  759. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  760. private string GetCgeuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">=");
  761. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  762. private string GetCgtuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">");
  763. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  764. private string GetCleuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<=");
  765. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  766. private string GetCltuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<");
  767. private string GetCnanExpr(ShaderIrOp Op) => GetUnaryCall(Op, "isnan");
  768. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  769. private string GetCutExpr(ShaderIrOp Op) => "EndPrimitive()";
  770. private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!=");
  771. private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan");
  772. private string GetExitExpr(ShaderIrOp Op) => "return";
  773. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  774. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  775. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  776. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  777. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  778. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  779. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  780. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  781. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  782. private string GetFsqrtExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sqrt");
  783. private string GetFtosExpr(ShaderIrOp Op)
  784. {
  785. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  786. }
  787. private string GetFtouExpr(ShaderIrOp Op)
  788. {
  789. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  790. }
  791. private string GetIpaExpr(ShaderIrOp Op) => GetSrcExpr(Op.OperandA);
  792. private string GetKilExpr(ShaderIrOp Op) => "discard";
  793. private string GetLslExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<<");
  794. private string GetLsrExpr(ShaderIrOp Op)
  795. {
  796. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  797. GetOperExpr(Op, Op.OperandB) + ")";
  798. }
  799. private string GetMaxExpr(ShaderIrOp Op) => GetBinaryCall(Op, "max");
  800. private string GetMinExpr(ShaderIrOp Op) => GetBinaryCall(Op, "min");
  801. private string GetMulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  802. private string GetNegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  803. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  804. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  805. private string GetStofExpr(ShaderIrOp Op)
  806. {
  807. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  808. }
  809. private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-");
  810. private string GetTexbExpr(ShaderIrOp Op)
  811. {
  812. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  813. if (!Decl.CbTextures.TryGetValue(Op, out ShaderDeclInfo DeclInfo))
  814. {
  815. throw new InvalidOperationException();
  816. }
  817. string Coords = GetTexSamplerCoords(Op);
  818. string Ch = "rgba".Substring(Meta.Elem, 1);
  819. return "texture(" + DeclInfo.Name + ", " + Coords + ")." + Ch;
  820. }
  821. private string GetTexqExpr(ShaderIrOp Op)
  822. {
  823. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  824. string Ch = "xyzw".Substring(Meta.Elem, 1);
  825. if (Meta.Info == ShaderTexqInfo.Dimension)
  826. {
  827. string Sampler = GetTexSamplerName(Op);
  828. string Lod = GetOperExpr(Op, Op.OperandA); //???
  829. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  830. }
  831. else
  832. {
  833. throw new NotImplementedException(Meta.Info.ToString());
  834. }
  835. }
  836. private string GetTexsExpr(ShaderIrOp Op)
  837. {
  838. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  839. string Sampler = GetTexSamplerName(Op);
  840. string Coords = GetTexSamplerCoords(Op);
  841. string Ch = "rgba".Substring(Meta.Elem, 1);
  842. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  843. }
  844. private string GetTxlfExpr(ShaderIrOp Op)
  845. {
  846. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  847. string Sampler = GetTexSamplerName(Op);
  848. string Coords = GetITexSamplerCoords(Op);
  849. string Ch = "rgba".Substring(Meta.Elem, 1);
  850. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  851. }
  852. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  853. private string GetUtofExpr(ShaderIrOp Op)
  854. {
  855. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  856. }
  857. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  858. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  859. {
  860. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  861. }
  862. private string GetBinaryCall(ShaderIrOp Op, string FuncName)
  863. {
  864. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  865. GetOperExpr(Op, Op.OperandB) + ")";
  866. }
  867. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  868. {
  869. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  870. GetOperExpr(Op, Op.OperandB) + ", " +
  871. GetOperExpr(Op, Op.OperandC) + ")";
  872. }
  873. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  874. {
  875. return Opr + GetOperExpr(Op, Op.OperandA);
  876. }
  877. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  878. {
  879. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  880. GetOperExpr(Op, Op.OperandB);
  881. }
  882. private string GetBinaryExprWithNaN(ShaderIrOp Op, string Opr)
  883. {
  884. string A = GetOperExpr(Op, Op.OperandA);
  885. string B = GetOperExpr(Op, Op.OperandB);
  886. string NaNCheck =
  887. " || isnan(" + A + ")" +
  888. " || isnan(" + B + ")";
  889. return A + " " + Opr + " " + B + NaNCheck;
  890. }
  891. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  892. {
  893. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  894. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  895. GetOperExpr(Op, Op.OperandC);
  896. }
  897. private string GetTexSamplerName(ShaderIrOp Op)
  898. {
  899. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  900. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  901. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  902. {
  903. throw new InvalidOperationException();
  904. }
  905. return DeclInfo.Name;
  906. }
  907. private string GetTexSamplerCoords(ShaderIrOp Op)
  908. {
  909. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  910. GetOperExpr(Op, Op.OperandB) + ")";
  911. }
  912. private string GetITexSamplerCoords(ShaderIrOp Op)
  913. {
  914. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  915. GetOperExpr(Op, Op.OperandB) + ")";
  916. }
  917. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  918. {
  919. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  920. }
  921. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  922. {
  923. //Note: The "DstType" (of the cast) is the type that the operation
  924. //uses on the source operands, while the "SrcType" is the destination
  925. //type of the operand result (if it is a operation) or just the type
  926. //of the variable for registers/uniforms/attributes.
  927. OperType DstType = GetSrcNodeType(Dst);
  928. OperType SrcType = GetDstNodeType(Src);
  929. if (DstType != SrcType)
  930. {
  931. //Check for invalid casts
  932. //(like bool to int/float and others).
  933. if (SrcType != OperType.F32 &&
  934. SrcType != OperType.I32)
  935. {
  936. throw new InvalidOperationException();
  937. }
  938. switch (Src)
  939. {
  940. case ShaderIrOperGpr Gpr:
  941. {
  942. //When the Gpr is ZR, just return the 0 value directly,
  943. //since the float encoding for 0 is 0.
  944. if (Gpr.IsConst)
  945. {
  946. return "0";
  947. }
  948. break;
  949. }
  950. case ShaderIrOperImm Imm:
  951. {
  952. //For integer immediates being used as float,
  953. //it's better (for readability) to just return the float value.
  954. if (DstType == OperType.F32)
  955. {
  956. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  957. if (!float.IsNaN(Value) && !float.IsInfinity(Value))
  958. {
  959. return GetFloatConst(Value);
  960. }
  961. }
  962. break;
  963. }
  964. }
  965. switch (DstType)
  966. {
  967. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  968. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  969. }
  970. }
  971. return Expr;
  972. }
  973. private static string GetIntConst(int Value)
  974. {
  975. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  976. return Value < 0 ? "(" + Expr + ")" : Expr;
  977. }
  978. private static string GetFloatConst(float Value)
  979. {
  980. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  981. return Value < 0 ? "(" + Expr + ")" : Expr;
  982. }
  983. private static OperType GetDstNodeType(ShaderIrNode Node)
  984. {
  985. //Special case instructions with the result type different
  986. //from the input types (like integer <-> float conversion) here.
  987. if (Node is ShaderIrOp Op)
  988. {
  989. switch (Op.Inst)
  990. {
  991. case ShaderIrInst.Stof:
  992. case ShaderIrInst.Txlf:
  993. case ShaderIrInst.Utof:
  994. return OperType.F32;
  995. case ShaderIrInst.Ftos:
  996. case ShaderIrInst.Ftou:
  997. return OperType.I32;
  998. }
  999. }
  1000. return GetSrcNodeType(Node);
  1001. }
  1002. private static OperType GetSrcNodeType(ShaderIrNode Node)
  1003. {
  1004. switch (Node)
  1005. {
  1006. case ShaderIrOperAbuf Abuf:
  1007. return Abuf.Offs == GlslDecl.LayerAttr ||
  1008. Abuf.Offs == GlslDecl.InstanceIdAttr ||
  1009. Abuf.Offs == GlslDecl.VertexIdAttr ||
  1010. Abuf.Offs == GlslDecl.FaceAttr
  1011. ? OperType.I32
  1012. : OperType.F32;
  1013. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  1014. case ShaderIrOperGpr Gpr: return OperType.F32;
  1015. case ShaderIrOperImm Imm: return OperType.I32;
  1016. case ShaderIrOperImmf Immf: return OperType.F32;
  1017. case ShaderIrOperPred Pred: return OperType.Bool;
  1018. case ShaderIrOp Op:
  1019. if (Op.Inst > ShaderIrInst.B_Start &&
  1020. Op.Inst < ShaderIrInst.B_End)
  1021. {
  1022. return OperType.Bool;
  1023. }
  1024. else if (Op.Inst > ShaderIrInst.F_Start &&
  1025. Op.Inst < ShaderIrInst.F_End)
  1026. {
  1027. return OperType.F32;
  1028. }
  1029. else if (Op.Inst > ShaderIrInst.I_Start &&
  1030. Op.Inst < ShaderIrInst.I_End)
  1031. {
  1032. return OperType.I32;
  1033. }
  1034. break;
  1035. }
  1036. throw new ArgumentException(nameof(Node));
  1037. }
  1038. }
  1039. }