GlslDecompiler.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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.Contains(GlslDecl.FragmentOutputName))
  287. {
  288. Name = "layout (location = " + DeclInfo.Index / 4 + ") out vec4 " + DeclInfo.Name + Suffix + ";";
  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. case GlslDecl.PointCoordAttrX: return "gl_PointCoord.x";
  658. case GlslDecl.PointCoordAttrY: return "gl_PointCoord.y";
  659. //Note: It's a guess that Maxwell's face is 1 when gl_FrontFacing == true
  660. case GlslDecl.FaceAttr: return "(gl_FrontFacing ? 1 : 0)";
  661. }
  662. }
  663. return GetAttrTempName(Abuf);
  664. }
  665. private string GetAttrTempName(ShaderIrOperAbuf Abuf)
  666. {
  667. int Index = Abuf.Offs >> 4;
  668. int Elem = (Abuf.Offs >> 2) & 3;
  669. string Swizzle = "." + GetAttrSwizzle(Elem);
  670. if (!Decl.Attributes.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  671. {
  672. //Handle special vec4 attributes here
  673. //(for example, index 7 is always gl_Position).
  674. if (Index == GlslDecl.GlPositionVec4Index)
  675. {
  676. string Name =
  677. Decl.ShaderType != GalShaderType.Vertex &&
  678. Decl.ShaderType != GalShaderType.Geometry ? GlslDecl.PositionOutAttrName : "gl_Position";
  679. return Name + Swizzle;
  680. }
  681. else if (Abuf.Offs == GlslDecl.PointSizeAttr)
  682. {
  683. return "gl_PointSize";
  684. }
  685. }
  686. if (DeclInfo.Index >= 16)
  687. {
  688. throw new InvalidOperationException($"Shader attribute offset {Abuf.Offs} is invalid.");
  689. }
  690. if (Decl.ShaderType == GalShaderType.Geometry)
  691. {
  692. string Vertex = "floatBitsToInt(" + GetSrcExpr(Abuf.Vertex) + ")";
  693. return DeclInfo.Name + "[" + Vertex + "]" + Swizzle;
  694. }
  695. else
  696. {
  697. return DeclInfo.Name + Swizzle;
  698. }
  699. }
  700. private string GetName(ShaderIrOperGpr Gpr)
  701. {
  702. return Gpr.IsConst ? "0" : GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  703. }
  704. private string GetValue(ShaderIrOperImm Imm)
  705. {
  706. //Only use hex is the value is too big and would likely be hard to read as int.
  707. if (Imm.Value > 0xfff ||
  708. Imm.Value < -0xfff)
  709. {
  710. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  711. }
  712. else
  713. {
  714. return GetIntConst(Imm.Value);
  715. }
  716. }
  717. private string GetValue(ShaderIrOperImmf Immf)
  718. {
  719. return GetFloatConst(Immf.Value);
  720. }
  721. private string GetName(ShaderIrOperPred Pred)
  722. {
  723. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  724. }
  725. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  726. {
  727. int VecIndex = Index & ~3;
  728. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  729. {
  730. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  731. {
  732. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  733. }
  734. }
  735. if (!Dict.TryGetValue(Index, out DeclInfo))
  736. {
  737. throw new InvalidOperationException();
  738. }
  739. return DeclInfo.Name;
  740. }
  741. private string GetAttrSwizzle(int Elem)
  742. {
  743. return "xyzw".Substring(Elem, 1);
  744. }
  745. private string GetAbsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  746. private string GetAddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  747. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  748. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  749. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  750. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  751. private string GetBorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "||");
  752. private string GetBxorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^^");
  753. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  754. private string GetClampsExpr(ShaderIrOp Op)
  755. {
  756. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  757. GetOperExpr(Op, Op.OperandB) + ", " +
  758. GetOperExpr(Op, Op.OperandC) + ")";
  759. }
  760. private string GetClampuExpr(ShaderIrOp Op)
  761. {
  762. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  763. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  764. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  765. }
  766. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  767. private string GetCequExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "==");
  768. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  769. private string GetCgeuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">=");
  770. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  771. private string GetCgtuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">");
  772. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  773. private string GetCleuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<=");
  774. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  775. private string GetCltuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<");
  776. private string GetCnanExpr(ShaderIrOp Op) => GetUnaryCall(Op, "isnan");
  777. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  778. private string GetCutExpr(ShaderIrOp Op) => "EndPrimitive()";
  779. private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!=");
  780. private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan");
  781. private string GetExitExpr(ShaderIrOp Op) => "return";
  782. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  783. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  784. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  785. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  786. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  787. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  788. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  789. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  790. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  791. private string GetFsqrtExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sqrt");
  792. private string GetFtosExpr(ShaderIrOp Op)
  793. {
  794. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  795. }
  796. private string GetFtouExpr(ShaderIrOp Op)
  797. {
  798. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  799. }
  800. private string GetIpaExpr(ShaderIrOp Op) => GetSrcExpr(Op.OperandA);
  801. private string GetKilExpr(ShaderIrOp Op) => "discard";
  802. private string GetLslExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<<");
  803. private string GetLsrExpr(ShaderIrOp Op)
  804. {
  805. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  806. GetOperExpr(Op, Op.OperandB) + ")";
  807. }
  808. private string GetMaxExpr(ShaderIrOp Op) => GetBinaryCall(Op, "max");
  809. private string GetMinExpr(ShaderIrOp Op) => GetBinaryCall(Op, "min");
  810. private string GetMulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  811. private string GetNegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  812. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  813. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  814. private string GetStofExpr(ShaderIrOp Op)
  815. {
  816. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  817. }
  818. private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-");
  819. private string GetTexbExpr(ShaderIrOp Op)
  820. {
  821. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  822. if (!Decl.CbTextures.TryGetValue(Op, out ShaderDeclInfo DeclInfo))
  823. {
  824. throw new InvalidOperationException();
  825. }
  826. string Coords = GetTexSamplerCoords(Op);
  827. string Ch = "rgba".Substring(Meta.Elem, 1);
  828. return "texture(" + DeclInfo.Name + ", " + Coords + ")." + Ch;
  829. }
  830. private string GetTexqExpr(ShaderIrOp Op)
  831. {
  832. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  833. string Ch = "xyzw".Substring(Meta.Elem, 1);
  834. if (Meta.Info == ShaderTexqInfo.Dimension)
  835. {
  836. string Sampler = GetTexSamplerName(Op);
  837. string Lod = GetOperExpr(Op, Op.OperandA); //???
  838. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  839. }
  840. else
  841. {
  842. throw new NotImplementedException(Meta.Info.ToString());
  843. }
  844. }
  845. private string GetTexsExpr(ShaderIrOp Op)
  846. {
  847. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  848. string Sampler = GetTexSamplerName(Op);
  849. string Coords = GetTexSamplerCoords(Op);
  850. string Ch = "rgba".Substring(Meta.Elem, 1);
  851. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  852. }
  853. private string GetTxlfExpr(ShaderIrOp Op)
  854. {
  855. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  856. string Sampler = GetTexSamplerName(Op);
  857. string Coords = GetITexSamplerCoords(Op);
  858. string Ch = "rgba".Substring(Meta.Elem, 1);
  859. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  860. }
  861. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  862. private string GetUtofExpr(ShaderIrOp Op)
  863. {
  864. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  865. }
  866. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  867. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  868. {
  869. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  870. }
  871. private string GetBinaryCall(ShaderIrOp Op, string FuncName)
  872. {
  873. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  874. GetOperExpr(Op, Op.OperandB) + ")";
  875. }
  876. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  877. {
  878. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  879. GetOperExpr(Op, Op.OperandB) + ", " +
  880. GetOperExpr(Op, Op.OperandC) + ")";
  881. }
  882. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  883. {
  884. return Opr + GetOperExpr(Op, Op.OperandA);
  885. }
  886. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  887. {
  888. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  889. GetOperExpr(Op, Op.OperandB);
  890. }
  891. private string GetBinaryExprWithNaN(ShaderIrOp Op, string Opr)
  892. {
  893. string A = GetOperExpr(Op, Op.OperandA);
  894. string B = GetOperExpr(Op, Op.OperandB);
  895. string NaNCheck =
  896. " || isnan(" + A + ")" +
  897. " || isnan(" + B + ")";
  898. return A + " " + Opr + " " + B + NaNCheck;
  899. }
  900. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  901. {
  902. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  903. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  904. GetOperExpr(Op, Op.OperandC);
  905. }
  906. private string GetTexSamplerName(ShaderIrOp Op)
  907. {
  908. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  909. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  910. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  911. {
  912. throw new InvalidOperationException();
  913. }
  914. return DeclInfo.Name;
  915. }
  916. private string GetTexSamplerCoords(ShaderIrOp Op)
  917. {
  918. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  919. GetOperExpr(Op, Op.OperandB) + ")";
  920. }
  921. private string GetITexSamplerCoords(ShaderIrOp Op)
  922. {
  923. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  924. GetOperExpr(Op, Op.OperandB) + ")";
  925. }
  926. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  927. {
  928. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  929. }
  930. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  931. {
  932. //Note: The "DstType" (of the cast) is the type that the operation
  933. //uses on the source operands, while the "SrcType" is the destination
  934. //type of the operand result (if it is a operation) or just the type
  935. //of the variable for registers/uniforms/attributes.
  936. OperType DstType = GetSrcNodeType(Dst);
  937. OperType SrcType = GetDstNodeType(Src);
  938. if (DstType != SrcType)
  939. {
  940. //Check for invalid casts
  941. //(like bool to int/float and others).
  942. if (SrcType != OperType.F32 &&
  943. SrcType != OperType.I32)
  944. {
  945. throw new InvalidOperationException();
  946. }
  947. switch (Src)
  948. {
  949. case ShaderIrOperGpr Gpr:
  950. {
  951. //When the Gpr is ZR, just return the 0 value directly,
  952. //since the float encoding for 0 is 0.
  953. if (Gpr.IsConst)
  954. {
  955. return "0";
  956. }
  957. break;
  958. }
  959. case ShaderIrOperImm Imm:
  960. {
  961. //For integer immediates being used as float,
  962. //it's better (for readability) to just return the float value.
  963. if (DstType == OperType.F32)
  964. {
  965. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  966. if (!float.IsNaN(Value) && !float.IsInfinity(Value))
  967. {
  968. return GetFloatConst(Value);
  969. }
  970. }
  971. break;
  972. }
  973. }
  974. switch (DstType)
  975. {
  976. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  977. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  978. }
  979. }
  980. return Expr;
  981. }
  982. private static string GetIntConst(int Value)
  983. {
  984. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  985. return Value < 0 ? "(" + Expr + ")" : Expr;
  986. }
  987. private static string GetFloatConst(float Value)
  988. {
  989. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  990. return Value < 0 ? "(" + Expr + ")" : Expr;
  991. }
  992. private static OperType GetDstNodeType(ShaderIrNode Node)
  993. {
  994. //Special case instructions with the result type different
  995. //from the input types (like integer <-> float conversion) here.
  996. if (Node is ShaderIrOp Op)
  997. {
  998. switch (Op.Inst)
  999. {
  1000. case ShaderIrInst.Stof:
  1001. case ShaderIrInst.Txlf:
  1002. case ShaderIrInst.Utof:
  1003. return OperType.F32;
  1004. case ShaderIrInst.Ftos:
  1005. case ShaderIrInst.Ftou:
  1006. return OperType.I32;
  1007. }
  1008. }
  1009. return GetSrcNodeType(Node);
  1010. }
  1011. private static OperType GetSrcNodeType(ShaderIrNode Node)
  1012. {
  1013. switch (Node)
  1014. {
  1015. case ShaderIrOperAbuf Abuf:
  1016. return Abuf.Offs == GlslDecl.LayerAttr ||
  1017. Abuf.Offs == GlslDecl.InstanceIdAttr ||
  1018. Abuf.Offs == GlslDecl.VertexIdAttr ||
  1019. Abuf.Offs == GlslDecl.FaceAttr
  1020. ? OperType.I32
  1021. : OperType.F32;
  1022. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  1023. case ShaderIrOperGpr Gpr: return OperType.F32;
  1024. case ShaderIrOperImm Imm: return OperType.I32;
  1025. case ShaderIrOperImmf Immf: return OperType.F32;
  1026. case ShaderIrOperPred Pred: return OperType.Bool;
  1027. case ShaderIrOp Op:
  1028. if (Op.Inst > ShaderIrInst.B_Start &&
  1029. Op.Inst < ShaderIrInst.B_End)
  1030. {
  1031. return OperType.Bool;
  1032. }
  1033. else if (Op.Inst > ShaderIrInst.F_Start &&
  1034. Op.Inst < ShaderIrInst.F_End)
  1035. {
  1036. return OperType.F32;
  1037. }
  1038. else if (Op.Inst > ShaderIrInst.I_Start &&
  1039. Op.Inst < ShaderIrInst.I_End)
  1040. {
  1041. return OperType.I32;
  1042. }
  1043. break;
  1044. }
  1045. throw new ArgumentException(nameof(Node));
  1046. }
  1047. }
  1048. }