ShaderDecoder.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Gal.Shader
  3. {
  4. static class ShaderDecoder
  5. {
  6. private const bool AddDbgComments = true;
  7. public static ShaderIrBlock[] Decode(IGalMemory Memory, long Start)
  8. {
  9. Dictionary<long, ShaderIrBlock> Visited = new Dictionary<long, ShaderIrBlock>();
  10. Dictionary<long, ShaderIrBlock> VisitedEnd = new Dictionary<long, ShaderIrBlock>();
  11. Queue<ShaderIrBlock> Blocks = new Queue<ShaderIrBlock>();
  12. ShaderIrBlock Enqueue(long Position, ShaderIrBlock Source = null)
  13. {
  14. if (!Visited.TryGetValue(Position, out ShaderIrBlock Output))
  15. {
  16. Output = new ShaderIrBlock(Position);
  17. Blocks.Enqueue(Output);
  18. Visited.Add(Position, Output);
  19. }
  20. if (Source != null)
  21. {
  22. Output.Sources.Add(Source);
  23. }
  24. return Output;
  25. }
  26. ShaderIrBlock Entry = Enqueue(Start);
  27. while (Blocks.Count > 0)
  28. {
  29. ShaderIrBlock Current = Blocks.Dequeue();
  30. FillBlock(Memory, Current);
  31. //Set child blocks. "Branch" is the block the branch instruction
  32. //points to (when taken), "Next" is the block at the next address,
  33. //executed when the branch is not taken. For Unconditional Branches
  34. //or end of shader, Next is null.
  35. if (Current.Nodes.Count > 0)
  36. {
  37. ShaderIrNode LastNode = Current.GetLastNode();
  38. ShaderIrOp Op = GetInnermostOp(LastNode);
  39. if (Op?.Inst == ShaderIrInst.Bra)
  40. {
  41. int Offset = ((ShaderIrOperImm)Op.OperandA).Value;
  42. long Target = Current.EndPosition + Offset;
  43. Current.Branch = Enqueue(Target, Current);
  44. }
  45. if (NodeHasNext(LastNode))
  46. {
  47. Current.Next = Enqueue(Current.EndPosition);
  48. }
  49. }
  50. //If we have on the graph two blocks with the same end position,
  51. //then we need to split the bigger block and have two small blocks,
  52. //the end position of the bigger "Current" block should then be == to
  53. //the position of the "Smaller" block.
  54. while (VisitedEnd.TryGetValue(Current.EndPosition, out ShaderIrBlock Smaller))
  55. {
  56. if (Current.Position > Smaller.Position)
  57. {
  58. ShaderIrBlock Temp = Smaller;
  59. Smaller = Current;
  60. Current = Temp;
  61. }
  62. Current.EndPosition = Smaller.Position;
  63. Current.Next = Smaller;
  64. Current.Branch = null;
  65. Current.Nodes.RemoveRange(
  66. Current.Nodes.Count - Smaller.Nodes.Count,
  67. Smaller.Nodes.Count);
  68. VisitedEnd[Smaller.EndPosition] = Smaller;
  69. }
  70. VisitedEnd.Add(Current.EndPosition, Current);
  71. }
  72. //Make and sort Graph blocks array by position.
  73. ShaderIrBlock[] Graph = new ShaderIrBlock[Visited.Count];
  74. while (Visited.Count > 0)
  75. {
  76. ulong FirstPos = ulong.MaxValue;
  77. foreach (ShaderIrBlock Block in Visited.Values)
  78. {
  79. if (FirstPos > (ulong)Block.Position)
  80. FirstPos = (ulong)Block.Position;
  81. }
  82. ShaderIrBlock Current = Visited[(long)FirstPos];
  83. do
  84. {
  85. Graph[Graph.Length - Visited.Count] = Current;
  86. Visited.Remove(Current.Position);
  87. Current = Current.Next;
  88. }
  89. while (Current != null);
  90. }
  91. return Graph;
  92. }
  93. private static void FillBlock(IGalMemory Memory, ShaderIrBlock Block)
  94. {
  95. long Position = Block.Position;
  96. do
  97. {
  98. //Ignore scheduling instructions, which are written every 32 bytes.
  99. if ((Position & 0x1f) == 0)
  100. {
  101. Position += 8;
  102. continue;
  103. }
  104. uint Word0 = (uint)Memory.ReadInt32(Position + 0);
  105. uint Word1 = (uint)Memory.ReadInt32(Position + 4);
  106. Position += 8;
  107. long OpCode = Word0 | (long)Word1 << 32;
  108. ShaderDecodeFunc Decode = ShaderOpCodeTable.GetDecoder(OpCode);
  109. if (AddDbgComments)
  110. {
  111. string DbgOpCode = $"0x{(Position - 8):x16}: 0x{OpCode:x16} ";
  112. DbgOpCode += (Decode?.Method.Name ?? "???");
  113. if (Decode == ShaderDecode.Bra)
  114. {
  115. int Offset = ((int)(OpCode >> 20) << 8) >> 8;
  116. long Target = Position + Offset;
  117. DbgOpCode += " (0x" + Target.ToString("x16") + ")";
  118. }
  119. Block.AddNode(new ShaderIrCmnt(DbgOpCode));
  120. }
  121. if (Decode == null)
  122. {
  123. continue;
  124. }
  125. Decode(Block, OpCode);
  126. }
  127. while (!IsFlowChange(Block.GetLastNode()));
  128. Block.EndPosition = Position;
  129. }
  130. private static bool IsFlowChange(ShaderIrNode Node)
  131. {
  132. return !NodeHasNext(GetInnermostOp(Node));
  133. }
  134. private static ShaderIrOp GetInnermostOp(ShaderIrNode Node)
  135. {
  136. if (Node is ShaderIrCond Cond)
  137. {
  138. Node = Cond.Child;
  139. }
  140. return Node is ShaderIrOp Op ? Op : null;
  141. }
  142. private static bool NodeHasNext(ShaderIrNode Node)
  143. {
  144. if (!(Node is ShaderIrOp Op))
  145. {
  146. return true;
  147. }
  148. return Op.Inst != ShaderIrInst.Exit &&
  149. Op.Inst != ShaderIrInst.Bra;
  150. }
  151. }
  152. }