GlslDecompiler.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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, Header);
  111. GlslDecl DeclVpB = new GlslDecl(BlocksB, ShaderType, HeaderB);
  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, Header);
  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. PrintDeclSsy();
  137. if (BlocksB != null)
  138. {
  139. PrintBlockScope(Blocks, GlslDecl.BasicBlockAName);
  140. SB.AppendLine();
  141. PrintBlockScope(BlocksB, GlslDecl.BasicBlockBName);
  142. }
  143. else
  144. {
  145. PrintBlockScope(Blocks, GlslDecl.BasicBlockName);
  146. }
  147. SB.AppendLine();
  148. PrintMain();
  149. string GlslCode = SB.ToString();
  150. List<ShaderDeclInfo> TextureInfo = new List<ShaderDeclInfo>();
  151. TextureInfo.AddRange(Decl.Textures.Values);
  152. TextureInfo.AddRange(IterateCbTextures());
  153. return new GlslProgram(GlslCode, TextureInfo, Decl.Uniforms.Values);
  154. }
  155. private void PrintDeclHeader()
  156. {
  157. if (Decl.ShaderType == GalShaderType.Geometry)
  158. {
  159. int MaxVertices = Header.MaxOutputVertexCount;
  160. string OutputTopology;
  161. switch (Header.OutputTopology)
  162. {
  163. case ShaderHeader.PointList: OutputTopology = "points"; break;
  164. case ShaderHeader.LineStrip: OutputTopology = "line_strip"; break;
  165. case ShaderHeader.TriangleStrip: OutputTopology = "triangle_strip"; break;
  166. default: throw new InvalidOperationException();
  167. }
  168. SB.AppendLine("#extension GL_ARB_enhanced_layouts : require");
  169. SB.AppendLine();
  170. SB.AppendLine("// Stubbed. Maxwell geometry shaders don't inform input geometry type");
  171. SB.AppendLine("layout(triangles) in;" + Environment.NewLine);
  172. SB.AppendLine($"layout({OutputTopology}, max_vertices = {MaxVertices}) out;");
  173. SB.AppendLine();
  174. }
  175. }
  176. private void PrintDeclTextures()
  177. {
  178. foreach (ShaderDeclInfo DeclInfo in IterateCbTextures())
  179. {
  180. SB.AppendLine("uniform sampler2D " + DeclInfo.Name + ";");
  181. }
  182. PrintDecls(Decl.Textures, "uniform sampler2D");
  183. }
  184. private IEnumerable<ShaderDeclInfo> IterateCbTextures()
  185. {
  186. HashSet<string> Names = new HashSet<string>();
  187. foreach (ShaderDeclInfo DeclInfo in Decl.CbTextures.Values.OrderBy(DeclKeySelector))
  188. {
  189. if (Names.Add(DeclInfo.Name))
  190. {
  191. yield return DeclInfo;
  192. }
  193. }
  194. }
  195. private void PrintDeclUniforms()
  196. {
  197. if (Decl.ShaderType == GalShaderType.Vertex)
  198. {
  199. //Memory layout here is [flip_x, flip_y, instance, unused]
  200. //It's using 4 bytes, not 8
  201. SB.AppendLine("layout (std140) uniform " + GlslDecl.ExtraUniformBlockName + " {");
  202. SB.AppendLine(IdentationStr + "vec2 " + GlslDecl.FlipUniformName + ";");
  203. SB.AppendLine(IdentationStr + "int " + GlslDecl.InstanceUniformName + ";");
  204. SB.AppendLine("};");
  205. }
  206. SB.AppendLine();
  207. foreach (ShaderDeclInfo DeclInfo in Decl.Uniforms.Values.OrderBy(DeclKeySelector))
  208. {
  209. SB.AppendLine($"layout (std140) uniform {DeclInfo.Name} {{");
  210. SB.AppendLine($"{IdentationStr}vec4 {DeclInfo.Name}_data[{GlslDecl.MaxUboSize}];");
  211. SB.AppendLine("};");
  212. }
  213. if (Decl.Uniforms.Count > 0)
  214. {
  215. SB.AppendLine();
  216. }
  217. }
  218. private void PrintDeclAttributes()
  219. {
  220. string GeometryArray = (Decl.ShaderType == GalShaderType.Geometry) ? "[" + MaxVertexInput + "]" : "";
  221. PrintDecls(Decl.Attributes, Suffix: GeometryArray);
  222. }
  223. private void PrintDeclInAttributes()
  224. {
  225. if (Decl.ShaderType == GalShaderType.Fragment)
  226. {
  227. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";");
  228. }
  229. if (Decl.ShaderType == GalShaderType.Geometry)
  230. {
  231. if (Decl.InAttributes.Count > 0)
  232. {
  233. SB.AppendLine("in Vertex {");
  234. foreach (ShaderDeclInfo DeclInfo in Decl.InAttributes.Values.OrderBy(DeclKeySelector))
  235. {
  236. if (DeclInfo.Index >= 0)
  237. {
  238. SB.AppendLine(IdentationStr + "layout (location = " + DeclInfo.Index + ") vec4 " + DeclInfo.Name + "; ");
  239. }
  240. }
  241. SB.AppendLine("} block_in[];" + Environment.NewLine);
  242. }
  243. }
  244. else
  245. {
  246. PrintDeclAttributes(Decl.InAttributes.Values, "in");
  247. }
  248. }
  249. private void PrintDeclOutAttributes()
  250. {
  251. if (Decl.ShaderType == GalShaderType.Fragment)
  252. {
  253. for (int Attachment = 0; Attachment < 8; Attachment++)
  254. {
  255. if (Header.OmapTargets[Attachment].Enabled)
  256. {
  257. SB.AppendLine("layout (location = " + Attachment + ") out vec4 " + GlslDecl.FragmentOutputName + Attachment + ";");
  258. }
  259. }
  260. }
  261. else
  262. {
  263. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") out vec4 " + GlslDecl.PositionOutAttrName + ";");
  264. }
  265. PrintDeclAttributes(Decl.OutAttributes.Values, "out");
  266. }
  267. private void PrintDeclAttributes(IEnumerable<ShaderDeclInfo> Decls, string InOut)
  268. {
  269. int Count = 0;
  270. foreach (ShaderDeclInfo DeclInfo in Decls.OrderBy(DeclKeySelector))
  271. {
  272. if (DeclInfo.Index >= 0)
  273. {
  274. SB.AppendLine("layout (location = " + DeclInfo.Index + ") " + InOut + " vec4 " + DeclInfo.Name + ";");
  275. Count++;
  276. }
  277. }
  278. if (Count > 0)
  279. {
  280. SB.AppendLine();
  281. }
  282. }
  283. private void PrintDeclGprs()
  284. {
  285. PrintDecls(Decl.Gprs);
  286. }
  287. private void PrintDeclPreds()
  288. {
  289. PrintDecls(Decl.Preds, "bool");
  290. }
  291. private void PrintDeclSsy()
  292. {
  293. SB.AppendLine("uint " + GlslDecl.SsyCursorName + ";");
  294. SB.AppendLine("uint " + GlslDecl.SsyStackName + "[" + GlslDecl.SsyStackSize + "];" + Environment.NewLine);
  295. }
  296. private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null, string Suffix = "")
  297. {
  298. foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector))
  299. {
  300. string Name;
  301. if (CustomType != null)
  302. {
  303. Name = CustomType + " " + DeclInfo.Name + Suffix + ";";
  304. }
  305. else if (DeclInfo.Name.Contains(GlslDecl.FragmentOutputName))
  306. {
  307. Name = "layout (location = " + DeclInfo.Index / 4 + ") out vec4 " + DeclInfo.Name + Suffix + ";";
  308. }
  309. else
  310. {
  311. Name = GetDecl(DeclInfo) + Suffix + ";";
  312. }
  313. SB.AppendLine(Name);
  314. }
  315. if (Dict.Count > 0)
  316. {
  317. SB.AppendLine();
  318. }
  319. }
  320. private int DeclKeySelector(ShaderDeclInfo DeclInfo)
  321. {
  322. return DeclInfo.Cbuf << 24 | DeclInfo.Index;
  323. }
  324. private string GetDecl(ShaderDeclInfo DeclInfo)
  325. {
  326. if (DeclInfo.Size == 4)
  327. {
  328. return "vec4 " + DeclInfo.Name;
  329. }
  330. else
  331. {
  332. return "float " + DeclInfo.Name;
  333. }
  334. }
  335. private void PrintMain()
  336. {
  337. SB.AppendLine("void main() {");
  338. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.InAttributes)
  339. {
  340. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  341. {
  342. continue;
  343. }
  344. ShaderDeclInfo DeclInfo = KV.Value;
  345. if (Decl.ShaderType == GalShaderType.Geometry)
  346. {
  347. for (int Vertex = 0; Vertex < MaxVertexInput; Vertex++)
  348. {
  349. string Dst = Attr.Name + "[" + Vertex + "]";
  350. string Src = "block_in[" + Vertex + "]." + DeclInfo.Name;
  351. SB.AppendLine(IdentationStr + Dst + " = " + Src + ";");
  352. }
  353. }
  354. else
  355. {
  356. SB.AppendLine(IdentationStr + Attr.Name + " = " + DeclInfo.Name + ";");
  357. }
  358. }
  359. SB.AppendLine(IdentationStr + "uint pc;");
  360. if (BlocksB != null)
  361. {
  362. PrintProgram(Blocks, GlslDecl.BasicBlockAName);
  363. PrintProgram(BlocksB, GlslDecl.BasicBlockBName);
  364. }
  365. else
  366. {
  367. PrintProgram(Blocks, GlslDecl.BasicBlockName);
  368. }
  369. if (Decl.ShaderType != GalShaderType.Geometry)
  370. {
  371. PrintAttrToOutput();
  372. }
  373. if (Decl.ShaderType == GalShaderType.Fragment)
  374. {
  375. if (Header.OmapDepth)
  376. {
  377. SB.AppendLine(IdentationStr + "gl_FragDepth = " + GlslDecl.GetGprName(Header.DepthRegister) + ";");
  378. }
  379. int GprIndex = 0;
  380. for (int Attachment = 0; Attachment < 8; Attachment++)
  381. {
  382. string Output = GlslDecl.FragmentOutputName + Attachment;
  383. OmapTarget Target = Header.OmapTargets[Attachment];
  384. for (int Component = 0; Component < 4; Component++)
  385. {
  386. if (Target.ComponentEnabled(Component))
  387. {
  388. SB.AppendLine(IdentationStr + Output + "[" + Component + "] = " + GlslDecl.GetGprName(GprIndex) + ";");
  389. GprIndex++;
  390. }
  391. }
  392. }
  393. }
  394. SB.AppendLine("}");
  395. }
  396. private void PrintProgram(ShaderIrBlock[] Blocks, string Name)
  397. {
  398. const string Ident1 = IdentationStr;
  399. const string Ident2 = Ident1 + IdentationStr;
  400. const string Ident3 = Ident2 + IdentationStr;
  401. const string Ident4 = Ident3 + IdentationStr;
  402. SB.AppendLine(Ident1 + "pc = " + GetBlockPosition(Blocks[0]) + ";");
  403. SB.AppendLine(Ident1 + "do {");
  404. SB.AppendLine(Ident2 + "switch (pc) {");
  405. foreach (ShaderIrBlock Block in Blocks)
  406. {
  407. string FunctionName = Block.Position.ToString("x8");
  408. SB.AppendLine(Ident3 + "case 0x" + FunctionName + ": pc = " + Name + "_" + FunctionName + "(); break;");
  409. }
  410. SB.AppendLine(Ident3 + "default:");
  411. SB.AppendLine(Ident4 + "pc = 0;");
  412. SB.AppendLine(Ident4 + "break;");
  413. SB.AppendLine(Ident2 + "}");
  414. SB.AppendLine(Ident1 + "} while (pc != 0);");
  415. }
  416. private void PrintAttrToOutput(string Identation = IdentationStr)
  417. {
  418. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.OutAttributes)
  419. {
  420. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  421. {
  422. continue;
  423. }
  424. ShaderDeclInfo DeclInfo = KV.Value;
  425. string Name = Attr.Name;
  426. if (Decl.ShaderType == GalShaderType.Geometry)
  427. {
  428. Name += "[0]";
  429. }
  430. SB.AppendLine(Identation + DeclInfo.Name + " = " + Name + ";");
  431. }
  432. if (Decl.ShaderType == GalShaderType.Vertex)
  433. {
  434. SB.AppendLine(Identation + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";");
  435. }
  436. if (Decl.ShaderType != GalShaderType.Fragment)
  437. {
  438. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
  439. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + ".w = 1;");
  440. }
  441. }
  442. private void PrintBlockScope(ShaderIrBlock[] Blocks, string Name)
  443. {
  444. foreach (ShaderIrBlock Block in Blocks)
  445. {
  446. SB.AppendLine("uint " + Name + "_" + Block.Position.ToString("x8") + "() {");
  447. PrintNodes(Block, Block.GetNodes());
  448. SB.AppendLine("}" + Environment.NewLine);
  449. }
  450. }
  451. private void PrintNode(ShaderIrBlock Block, ShaderIrNode Node, string Identation)
  452. {
  453. if (Node is ShaderIrCond Cond)
  454. {
  455. string IfExpr = GetSrcExpr(Cond.Pred, true);
  456. if (Cond.Not)
  457. {
  458. IfExpr = "!(" + IfExpr + ")";
  459. }
  460. SB.AppendLine(Identation + "if (" + IfExpr + ") {");
  461. if (Cond.Child is ShaderIrOp Op && Op.Inst == ShaderIrInst.Bra)
  462. {
  463. SB.AppendLine(Identation + IdentationStr + "return " + GetBlockPosition(Block.Branch) + ";");
  464. }
  465. else
  466. {
  467. PrintNode(Block, Cond.Child, Identation + IdentationStr);
  468. }
  469. SB.AppendLine(Identation + "}");
  470. }
  471. else if (Node is ShaderIrAsg Asg)
  472. {
  473. if (IsValidOutOper(Asg.Dst))
  474. {
  475. string Expr = GetSrcExpr(Asg.Src, true);
  476. Expr = GetExprWithCast(Asg.Dst, Asg.Src, Expr);
  477. SB.AppendLine(Identation + GetDstOperName(Asg.Dst) + " = " + Expr + ";");
  478. }
  479. }
  480. else if (Node is ShaderIrOp Op)
  481. {
  482. switch (Op.Inst)
  483. {
  484. case ShaderIrInst.Bra:
  485. {
  486. SB.AppendLine(Identation + "return " + GetBlockPosition(Block.Branch) + ";");
  487. break;
  488. }
  489. case ShaderIrInst.Emit:
  490. {
  491. PrintAttrToOutput(Identation);
  492. SB.AppendLine(Identation + "EmitVertex();");
  493. break;
  494. }
  495. case ShaderIrInst.Ssy:
  496. {
  497. string StackIndex = GlslDecl.SsyStackName + "[" + GlslDecl.SsyCursorName + "]";
  498. int TargetPosition = (Op.OperandA as ShaderIrOperImm).Value;
  499. string Target = "0x" + TargetPosition.ToString("x8") + "u";
  500. SB.AppendLine(Identation + StackIndex + " = " + Target + ";");
  501. SB.AppendLine(Identation + GlslDecl.SsyCursorName + "++;");
  502. break;
  503. }
  504. case ShaderIrInst.Sync:
  505. {
  506. SB.AppendLine(Identation + GlslDecl.SsyCursorName + "--;");
  507. string Target = GlslDecl.SsyStackName + "[" + GlslDecl.SsyCursorName + "]";
  508. SB.AppendLine(Identation + "return " + Target + ";");
  509. break;
  510. }
  511. default:
  512. SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
  513. break;
  514. }
  515. }
  516. else if (Node is ShaderIrCmnt Cmnt)
  517. {
  518. SB.AppendLine(Identation + "// " + Cmnt.Comment);
  519. }
  520. else
  521. {
  522. throw new InvalidOperationException();
  523. }
  524. }
  525. private void PrintNodes(ShaderIrBlock Block, ShaderIrNode[] Nodes)
  526. {
  527. foreach (ShaderIrNode Node in Nodes)
  528. {
  529. PrintNode(Block, Node, IdentationStr);
  530. }
  531. if (Nodes.Length > 0)
  532. {
  533. ShaderIrNode Last = Nodes[Nodes.Length - 1];
  534. bool UnconditionalFlowChange = false;
  535. if (Last is ShaderIrOp Op)
  536. {
  537. switch (Op.Inst)
  538. {
  539. case ShaderIrInst.Bra:
  540. case ShaderIrInst.Exit:
  541. case ShaderIrInst.Kil:
  542. case ShaderIrInst.Sync:
  543. UnconditionalFlowChange = true;
  544. break;
  545. }
  546. }
  547. if (!UnconditionalFlowChange)
  548. {
  549. SB.AppendLine(IdentationStr + "return " + GetBlockPosition(Block.Next) + ";");
  550. }
  551. }
  552. }
  553. private bool IsValidOutOper(ShaderIrNode Node)
  554. {
  555. if (Node is ShaderIrOperGpr Gpr && Gpr.IsConst)
  556. {
  557. return false;
  558. }
  559. else if (Node is ShaderIrOperPred Pred && Pred.IsConst)
  560. {
  561. return false;
  562. }
  563. return true;
  564. }
  565. private string GetDstOperName(ShaderIrNode Node)
  566. {
  567. if (Node is ShaderIrOperAbuf Abuf)
  568. {
  569. return GetOutAbufName(Abuf);
  570. }
  571. else if (Node is ShaderIrOperGpr Gpr)
  572. {
  573. return GetName(Gpr);
  574. }
  575. else if (Node is ShaderIrOperPred Pred)
  576. {
  577. return GetName(Pred);
  578. }
  579. throw new ArgumentException(nameof(Node));
  580. }
  581. private string GetSrcExpr(ShaderIrNode Node, bool Entry = false)
  582. {
  583. switch (Node)
  584. {
  585. case ShaderIrOperAbuf Abuf: return GetName (Abuf);
  586. case ShaderIrOperCbuf Cbuf: return GetName (Cbuf);
  587. case ShaderIrOperGpr Gpr: return GetName (Gpr);
  588. case ShaderIrOperImm Imm: return GetValue(Imm);
  589. case ShaderIrOperImmf Immf: return GetValue(Immf);
  590. case ShaderIrOperPred Pred: return GetName (Pred);
  591. case ShaderIrOp Op:
  592. string Expr;
  593. if (InstsExpr.TryGetValue(Op.Inst, out GetInstExpr GetExpr))
  594. {
  595. Expr = GetExpr(Op);
  596. }
  597. else
  598. {
  599. throw new NotImplementedException(Op.Inst.ToString());
  600. }
  601. if (!Entry && NeedsParentheses(Op))
  602. {
  603. Expr = "(" + Expr + ")";
  604. }
  605. return Expr;
  606. default: throw new ArgumentException(nameof(Node));
  607. }
  608. }
  609. private static bool NeedsParentheses(ShaderIrOp Op)
  610. {
  611. switch (Op.Inst)
  612. {
  613. case ShaderIrInst.Ipa:
  614. case ShaderIrInst.Texq:
  615. case ShaderIrInst.Texs:
  616. case ShaderIrInst.Txlf:
  617. return false;
  618. }
  619. return true;
  620. }
  621. private string GetName(ShaderIrOperCbuf Cbuf)
  622. {
  623. if (!Decl.Uniforms.TryGetValue(Cbuf.Index, out ShaderDeclInfo DeclInfo))
  624. {
  625. throw new InvalidOperationException();
  626. }
  627. if (Cbuf.Offs != null)
  628. {
  629. string Offset = "floatBitsToInt(" + GetSrcExpr(Cbuf.Offs) + ")";
  630. string Index = "(" + Cbuf.Pos * 4 + " + " + Offset + ")";
  631. return $"{DeclInfo.Name}_data[{Index} / 16][({Index} / 4) % 4]";
  632. }
  633. else
  634. {
  635. return $"{DeclInfo.Name}_data[{Cbuf.Pos / 4}][{Cbuf.Pos % 4}]";
  636. }
  637. }
  638. private string GetOutAbufName(ShaderIrOperAbuf Abuf)
  639. {
  640. if (Decl.ShaderType == GalShaderType.Geometry)
  641. {
  642. switch (Abuf.Offs)
  643. {
  644. case GlslDecl.LayerAttr: return "gl_Layer";
  645. }
  646. }
  647. return GetAttrTempName(Abuf);
  648. }
  649. private string GetName(ShaderIrOperAbuf Abuf)
  650. {
  651. //Handle special scalar read-only attributes here.
  652. if (Decl.ShaderType == GalShaderType.Vertex)
  653. {
  654. switch (Abuf.Offs)
  655. {
  656. case GlslDecl.VertexIdAttr: return "gl_VertexID";
  657. case GlslDecl.InstanceIdAttr: return GlslDecl.InstanceUniformName;
  658. }
  659. }
  660. else if (Decl.ShaderType == GalShaderType.TessEvaluation)
  661. {
  662. switch (Abuf.Offs)
  663. {
  664. case GlslDecl.TessCoordAttrX: return "gl_TessCoord.x";
  665. case GlslDecl.TessCoordAttrY: return "gl_TessCoord.y";
  666. case GlslDecl.TessCoordAttrZ: return "gl_TessCoord.z";
  667. }
  668. }
  669. else if (Decl.ShaderType == GalShaderType.Fragment)
  670. {
  671. switch (Abuf.Offs)
  672. {
  673. case GlslDecl.PointCoordAttrX: return "gl_PointCoord.x";
  674. case GlslDecl.PointCoordAttrY: return "gl_PointCoord.y";
  675. //Note: It's a guess that Maxwell's face is 1 when gl_FrontFacing == true
  676. case GlslDecl.FaceAttr: return "(gl_FrontFacing ? 1 : 0)";
  677. }
  678. }
  679. return GetAttrTempName(Abuf);
  680. }
  681. private string GetAttrTempName(ShaderIrOperAbuf Abuf)
  682. {
  683. int Index = Abuf.Offs >> 4;
  684. int Elem = (Abuf.Offs >> 2) & 3;
  685. string Swizzle = "." + GetAttrSwizzle(Elem);
  686. if (!Decl.Attributes.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  687. {
  688. //Handle special vec4 attributes here
  689. //(for example, index 7 is always gl_Position).
  690. if (Index == GlslDecl.GlPositionVec4Index)
  691. {
  692. string Name =
  693. Decl.ShaderType != GalShaderType.Vertex &&
  694. Decl.ShaderType != GalShaderType.Geometry ? GlslDecl.PositionOutAttrName : "gl_Position";
  695. return Name + Swizzle;
  696. }
  697. else if (Abuf.Offs == GlslDecl.PointSizeAttr)
  698. {
  699. return "gl_PointSize";
  700. }
  701. }
  702. if (DeclInfo.Index >= 16)
  703. {
  704. throw new InvalidOperationException($"Shader attribute offset {Abuf.Offs} is invalid.");
  705. }
  706. if (Decl.ShaderType == GalShaderType.Geometry)
  707. {
  708. string Vertex = "floatBitsToInt(" + GetSrcExpr(Abuf.Vertex) + ")";
  709. return DeclInfo.Name + "[" + Vertex + "]" + Swizzle;
  710. }
  711. else
  712. {
  713. return DeclInfo.Name + Swizzle;
  714. }
  715. }
  716. private string GetName(ShaderIrOperGpr Gpr)
  717. {
  718. return Gpr.IsConst ? "0" : GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  719. }
  720. private string GetValue(ShaderIrOperImm Imm)
  721. {
  722. //Only use hex is the value is too big and would likely be hard to read as int.
  723. if (Imm.Value > 0xfff ||
  724. Imm.Value < -0xfff)
  725. {
  726. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  727. }
  728. else
  729. {
  730. return GetIntConst(Imm.Value);
  731. }
  732. }
  733. private string GetValue(ShaderIrOperImmf Immf)
  734. {
  735. return GetFloatConst(Immf.Value);
  736. }
  737. private string GetName(ShaderIrOperPred Pred)
  738. {
  739. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  740. }
  741. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  742. {
  743. int VecIndex = Index & ~3;
  744. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  745. {
  746. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  747. {
  748. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  749. }
  750. }
  751. if (!Dict.TryGetValue(Index, out DeclInfo))
  752. {
  753. throw new InvalidOperationException();
  754. }
  755. return DeclInfo.Name;
  756. }
  757. private string GetAttrSwizzle(int Elem)
  758. {
  759. return "xyzw".Substring(Elem, 1);
  760. }
  761. private string GetAbsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  762. private string GetAddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  763. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  764. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  765. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  766. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  767. private string GetBorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "||");
  768. private string GetBxorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^^");
  769. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  770. private string GetClampsExpr(ShaderIrOp Op)
  771. {
  772. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  773. GetOperExpr(Op, Op.OperandB) + ", " +
  774. GetOperExpr(Op, Op.OperandC) + ")";
  775. }
  776. private string GetClampuExpr(ShaderIrOp Op)
  777. {
  778. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  779. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  780. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  781. }
  782. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  783. private string GetCequExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "==");
  784. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  785. private string GetCgeuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">=");
  786. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  787. private string GetCgtuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">");
  788. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  789. private string GetCleuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<=");
  790. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  791. private string GetCltuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<");
  792. private string GetCnanExpr(ShaderIrOp Op) => GetUnaryCall(Op, "isnan");
  793. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  794. private string GetCutExpr(ShaderIrOp Op) => "EndPrimitive()";
  795. private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!=");
  796. private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan");
  797. private string GetExitExpr(ShaderIrOp Op) => "return 0u";
  798. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  799. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  800. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  801. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  802. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  803. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  804. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  805. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  806. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  807. private string GetFsqrtExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sqrt");
  808. private string GetFtosExpr(ShaderIrOp Op)
  809. {
  810. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  811. }
  812. private string GetFtouExpr(ShaderIrOp Op)
  813. {
  814. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  815. }
  816. private string GetIpaExpr(ShaderIrOp Op) => GetSrcExpr(Op.OperandA);
  817. private string GetKilExpr(ShaderIrOp Op) => "discard";
  818. private string GetLslExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<<");
  819. private string GetLsrExpr(ShaderIrOp Op)
  820. {
  821. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  822. GetOperExpr(Op, Op.OperandB) + ")";
  823. }
  824. private string GetMaxExpr(ShaderIrOp Op) => GetBinaryCall(Op, "max");
  825. private string GetMinExpr(ShaderIrOp Op) => GetBinaryCall(Op, "min");
  826. private string GetMulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  827. private string GetNegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  828. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  829. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  830. private string GetStofExpr(ShaderIrOp Op)
  831. {
  832. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  833. }
  834. private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-");
  835. private string GetTexbExpr(ShaderIrOp Op)
  836. {
  837. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  838. if (!Decl.CbTextures.TryGetValue(Op, out ShaderDeclInfo DeclInfo))
  839. {
  840. throw new InvalidOperationException();
  841. }
  842. string Coords = GetTexSamplerCoords(Op);
  843. string Ch = "rgba".Substring(Meta.Elem, 1);
  844. return "texture(" + DeclInfo.Name + ", " + Coords + ")." + Ch;
  845. }
  846. private string GetTexqExpr(ShaderIrOp Op)
  847. {
  848. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  849. string Ch = "xyzw".Substring(Meta.Elem, 1);
  850. if (Meta.Info == ShaderTexqInfo.Dimension)
  851. {
  852. string Sampler = GetTexSamplerName(Op);
  853. string Lod = GetOperExpr(Op, Op.OperandA); //???
  854. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  855. }
  856. else
  857. {
  858. throw new NotImplementedException(Meta.Info.ToString());
  859. }
  860. }
  861. private string GetTexsExpr(ShaderIrOp Op)
  862. {
  863. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  864. string Sampler = GetTexSamplerName(Op);
  865. string Coords = GetTexSamplerCoords(Op);
  866. string Ch = "rgba".Substring(Meta.Elem, 1);
  867. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  868. }
  869. private string GetTxlfExpr(ShaderIrOp Op)
  870. {
  871. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  872. string Sampler = GetTexSamplerName(Op);
  873. string Coords = GetITexSamplerCoords(Op);
  874. string Ch = "rgba".Substring(Meta.Elem, 1);
  875. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  876. }
  877. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  878. private string GetUtofExpr(ShaderIrOp Op)
  879. {
  880. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  881. }
  882. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  883. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  884. {
  885. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  886. }
  887. private string GetBinaryCall(ShaderIrOp Op, string FuncName)
  888. {
  889. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  890. GetOperExpr(Op, Op.OperandB) + ")";
  891. }
  892. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  893. {
  894. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  895. GetOperExpr(Op, Op.OperandB) + ", " +
  896. GetOperExpr(Op, Op.OperandC) + ")";
  897. }
  898. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  899. {
  900. return Opr + GetOperExpr(Op, Op.OperandA);
  901. }
  902. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  903. {
  904. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  905. GetOperExpr(Op, Op.OperandB);
  906. }
  907. private string GetBinaryExprWithNaN(ShaderIrOp Op, string Opr)
  908. {
  909. string A = GetOperExpr(Op, Op.OperandA);
  910. string B = GetOperExpr(Op, Op.OperandB);
  911. string NaNCheck =
  912. " || isnan(" + A + ")" +
  913. " || isnan(" + B + ")";
  914. return A + " " + Opr + " " + B + NaNCheck;
  915. }
  916. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  917. {
  918. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  919. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  920. GetOperExpr(Op, Op.OperandC);
  921. }
  922. private string GetTexSamplerName(ShaderIrOp Op)
  923. {
  924. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  925. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  926. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  927. {
  928. throw new InvalidOperationException();
  929. }
  930. return DeclInfo.Name;
  931. }
  932. private string GetTexSamplerCoords(ShaderIrOp Op)
  933. {
  934. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  935. GetOperExpr(Op, Op.OperandB) + ")";
  936. }
  937. private string GetITexSamplerCoords(ShaderIrOp Op)
  938. {
  939. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  940. GetOperExpr(Op, Op.OperandB) + ")";
  941. }
  942. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  943. {
  944. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  945. }
  946. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  947. {
  948. //Note: The "DstType" (of the cast) is the type that the operation
  949. //uses on the source operands, while the "SrcType" is the destination
  950. //type of the operand result (if it is a operation) or just the type
  951. //of the variable for registers/uniforms/attributes.
  952. OperType DstType = GetSrcNodeType(Dst);
  953. OperType SrcType = GetDstNodeType(Src);
  954. if (DstType != SrcType)
  955. {
  956. //Check for invalid casts
  957. //(like bool to int/float and others).
  958. if (SrcType != OperType.F32 &&
  959. SrcType != OperType.I32)
  960. {
  961. throw new InvalidOperationException();
  962. }
  963. switch (Src)
  964. {
  965. case ShaderIrOperGpr Gpr:
  966. {
  967. //When the Gpr is ZR, just return the 0 value directly,
  968. //since the float encoding for 0 is 0.
  969. if (Gpr.IsConst)
  970. {
  971. return "0";
  972. }
  973. break;
  974. }
  975. case ShaderIrOperImm Imm:
  976. {
  977. //For integer immediates being used as float,
  978. //it's better (for readability) to just return the float value.
  979. if (DstType == OperType.F32)
  980. {
  981. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  982. if (!float.IsNaN(Value) && !float.IsInfinity(Value))
  983. {
  984. return GetFloatConst(Value);
  985. }
  986. }
  987. break;
  988. }
  989. }
  990. switch (DstType)
  991. {
  992. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  993. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  994. }
  995. }
  996. return Expr;
  997. }
  998. private static string GetIntConst(int Value)
  999. {
  1000. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  1001. return Value < 0 ? "(" + Expr + ")" : Expr;
  1002. }
  1003. private static string GetFloatConst(float Value)
  1004. {
  1005. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  1006. return Value < 0 ? "(" + Expr + ")" : Expr;
  1007. }
  1008. private static OperType GetDstNodeType(ShaderIrNode Node)
  1009. {
  1010. //Special case instructions with the result type different
  1011. //from the input types (like integer <-> float conversion) here.
  1012. if (Node is ShaderIrOp Op)
  1013. {
  1014. switch (Op.Inst)
  1015. {
  1016. case ShaderIrInst.Stof:
  1017. case ShaderIrInst.Txlf:
  1018. case ShaderIrInst.Utof:
  1019. return OperType.F32;
  1020. case ShaderIrInst.Ftos:
  1021. case ShaderIrInst.Ftou:
  1022. return OperType.I32;
  1023. }
  1024. }
  1025. return GetSrcNodeType(Node);
  1026. }
  1027. private static OperType GetSrcNodeType(ShaderIrNode Node)
  1028. {
  1029. switch (Node)
  1030. {
  1031. case ShaderIrOperAbuf Abuf:
  1032. return Abuf.Offs == GlslDecl.LayerAttr ||
  1033. Abuf.Offs == GlslDecl.InstanceIdAttr ||
  1034. Abuf.Offs == GlslDecl.VertexIdAttr ||
  1035. Abuf.Offs == GlslDecl.FaceAttr
  1036. ? OperType.I32
  1037. : OperType.F32;
  1038. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  1039. case ShaderIrOperGpr Gpr: return OperType.F32;
  1040. case ShaderIrOperImm Imm: return OperType.I32;
  1041. case ShaderIrOperImmf Immf: return OperType.F32;
  1042. case ShaderIrOperPred Pred: return OperType.Bool;
  1043. case ShaderIrOp Op:
  1044. if (Op.Inst > ShaderIrInst.B_Start &&
  1045. Op.Inst < ShaderIrInst.B_End)
  1046. {
  1047. return OperType.Bool;
  1048. }
  1049. else if (Op.Inst > ShaderIrInst.F_Start &&
  1050. Op.Inst < ShaderIrInst.F_End)
  1051. {
  1052. return OperType.F32;
  1053. }
  1054. else if (Op.Inst > ShaderIrInst.I_Start &&
  1055. Op.Inst < ShaderIrInst.I_End)
  1056. {
  1057. return OperType.I32;
  1058. }
  1059. break;
  1060. }
  1061. throw new ArgumentException(nameof(Node));
  1062. }
  1063. private static string GetBlockPosition(ShaderIrBlock Block)
  1064. {
  1065. if (Block != null)
  1066. {
  1067. return "0x" + Block.Position.ToString("x8") + "u";
  1068. }
  1069. else
  1070. {
  1071. return "0u";
  1072. }
  1073. }
  1074. }
  1075. }