2018-05-29 23:37:10 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
namespace Ryujinx.Graphics.Gal.Shader
|
|
|
|
{
|
|
|
|
static class ShaderDecoder
|
|
|
|
{
|
2018-07-19 05:33:27 +00:00
|
|
|
private const long HeaderSize = 0x50;
|
|
|
|
|
2018-05-17 18:25:42 +00:00
|
|
|
private const bool AddDbgComments = true;
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
public static ShaderIrBlock[] Decode(IGalMemory memory, long start)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
Dictionary<int, ShaderIrBlock> visited = new Dictionary<int, ShaderIrBlock>();
|
|
|
|
Dictionary<int, ShaderIrBlock> visitedEnd = new Dictionary<int, ShaderIrBlock>();
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
Queue<ShaderIrBlock> blocks = new Queue<ShaderIrBlock>();
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
long beginning = start + HeaderSize;
|
2018-09-18 04:30:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrBlock Enqueue(int position, ShaderIrBlock source = null)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!visited.TryGetValue(position, out ShaderIrBlock output))
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
output = new ShaderIrBlock(position);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
blocks.Enqueue(output);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
visited.Add(position, output);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (source != null)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
output.Sources.Add(source);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return output;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrBlock entry = Enqueue(0);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
while (blocks.Count > 0)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrBlock current = blocks.Dequeue();
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
FillBlock(memory, current, beginning);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
|
|
|
//Set child blocks. "Branch" is the block the branch instruction
|
|
|
|
//points to (when taken), "Next" is the block at the next address,
|
|
|
|
//executed when the branch is not taken. For Unconditional Branches
|
|
|
|
//or end of shader, Next is null.
|
2019-03-04 01:45:25 +00:00
|
|
|
if (current.Nodes.Count > 0)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrNode lastNode = current.GetLastNode();
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrOp innerOp = GetInnermostOp(lastNode);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (innerOp?.Inst == ShaderIrInst.Bra)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int target = ((ShaderIrOperImm)innerOp.OperandA).Value;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
current.Branch = Enqueue(target, current);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderIrNode node in current.Nodes)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
innerOp = GetInnermostOp(node);
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (innerOp is ShaderIrOp currOp && currOp.Inst == ShaderIrInst.Ssy)
|
2018-09-18 04:30:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int target = ((ShaderIrOperImm)currOp.OperandA).Value;
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
Enqueue(target, current);
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (NodeHasNext(lastNode))
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
current.Next = Enqueue(current.EndPosition);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//If we have on the graph two blocks with the same end position,
|
|
|
|
//then we need to split the bigger block and have two small blocks,
|
|
|
|
//the end position of the bigger "Current" block should then be == to
|
|
|
|
//the position of the "Smaller" block.
|
2019-03-04 01:45:25 +00:00
|
|
|
while (visitedEnd.TryGetValue(current.EndPosition, out ShaderIrBlock smaller))
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (current.Position > smaller.Position)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrBlock temp = smaller;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
smaller = current;
|
|
|
|
current = temp;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
current.EndPosition = smaller.Position;
|
|
|
|
current.Next = smaller;
|
|
|
|
current.Branch = null;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
current.Nodes.RemoveRange(
|
|
|
|
current.Nodes.Count - smaller.Nodes.Count,
|
|
|
|
smaller.Nodes.Count);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
visitedEnd[smaller.EndPosition] = smaller;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
visitedEnd.Add(current.EndPosition, current);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Make and sort Graph blocks array by position.
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrBlock[] graph = new ShaderIrBlock[visited.Count];
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
while (visited.Count > 0)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
uint firstPos = uint.MaxValue;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderIrBlock block in visited.Values)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (firstPos > (uint)block.Position)
|
|
|
|
firstPos = (uint)block.Position;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrBlock current = visited[(int)firstPos];
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2018-05-29 23:37:10 +00:00
|
|
|
do
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
graph[graph.Length - visited.Count] = current;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
visited.Remove(current.Position);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
current = current.Next;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
while (current != null);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return graph;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static void FillBlock(IGalMemory memory, ShaderIrBlock block, long beginning)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int position = block.Position;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-05-17 18:25:42 +00:00
|
|
|
//Ignore scheduling instructions, which are written every 32 bytes.
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((position & 0x1f) == 0)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
position += 8;
|
2018-04-10 19:50:32 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
uint word0 = (uint)memory.ReadInt32(position + beginning + 0);
|
|
|
|
uint word1 = (uint)memory.ReadInt32(position + beginning + 4);
|
2018-05-23 01:43:31 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
position += 8;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
long opCode = word0 | (long)word1 << 32;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderDecodeFunc decode = ShaderOpCodeTable.GetDecoder(opCode);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-05-17 18:25:42 +00:00
|
|
|
if (AddDbgComments)
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string dbgOpCode = $"0x{(position - 8):x16}: 0x{opCode:x16} ";
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
dbgOpCode += (decode?.Method.Name ?? "???");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (decode == ShaderDecode.Bra || decode == ShaderDecode.Ssy)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int offset = ((int)(opCode >> 20) << 8) >> 8;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
long target = position + offset;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
dbgOpCode += " (0x" + target.ToString("x16") + ")";
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
block.AddNode(new ShaderIrCmnt(dbgOpCode));
|
2018-05-17 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (decode == null)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
decode(block, opCode, position);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
while (!IsFlowChange(block.GetLastNode()));
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
block.EndPosition = position;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static bool IsFlowChange(ShaderIrNode node)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return !NodeHasNext(GetInnermostOp(node));
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static ShaderIrOp GetInnermostOp(ShaderIrNode node)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (node is ShaderIrCond cond)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
node = cond.Child;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return node is ShaderIrOp op ? op : null;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static bool NodeHasNext(ShaderIrNode node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!(node is ShaderIrOp op))
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return op.Inst != ShaderIrInst.Exit &&
|
|
|
|
op.Inst != ShaderIrInst.Bra;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|