ShaderDecoder.cs 6.8 KB

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