2018-05-30 01:37:10 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-04-08 21:17:35 +02:00
|
|
|
namespace Ryujinx.Graphics.Gal.Shader
|
|
|
|
{
|
|
|
|
static class ShaderDecoder
|
|
|
|
{
|
2018-07-19 07:33:27 +02:00
|
|
|
private const long HeaderSize = 0x50;
|
|
|
|
|
2018-05-17 20:25:42 +02:00
|
|
|
private const bool AddDbgComments = true;
|
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
public static ShaderIrBlock[] Decode(IGalMemory Memory, long Start)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
Dictionary<int, ShaderIrBlock> Visited = new Dictionary<int, ShaderIrBlock>();
|
|
|
|
Dictionary<int, ShaderIrBlock> VisitedEnd = new Dictionary<int, ShaderIrBlock>();
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
Queue<ShaderIrBlock> Blocks = new Queue<ShaderIrBlock>();
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
long Beginning = Start + HeaderSize;
|
|
|
|
|
|
|
|
ShaderIrBlock Enqueue(int Position, ShaderIrBlock Source = null)
|
2018-05-30 01:37:10 +02:00
|
|
|
{
|
|
|
|
if (!Visited.TryGetValue(Position, out ShaderIrBlock Output))
|
|
|
|
{
|
|
|
|
Output = new ShaderIrBlock(Position);
|
|
|
|
|
|
|
|
Blocks.Enqueue(Output);
|
|
|
|
|
|
|
|
Visited.Add(Position, Output);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Source != null)
|
|
|
|
{
|
|
|
|
Output.Sources.Add(Source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
ShaderIrBlock Entry = Enqueue(0);
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
while (Blocks.Count > 0)
|
|
|
|
{
|
|
|
|
ShaderIrBlock Current = Blocks.Dequeue();
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
FillBlock(Memory, Current, Beginning);
|
2018-05-30 01:37:10 +02: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.
|
|
|
|
if (Current.Nodes.Count > 0)
|
|
|
|
{
|
|
|
|
ShaderIrNode LastNode = Current.GetLastNode();
|
|
|
|
|
2018-08-31 18:14:04 +02:00
|
|
|
ShaderIrOp InnerOp = GetInnermostOp(LastNode);
|
2018-05-30 01:37:10 +02:00
|
|
|
|
2018-08-31 18:14:04 +02:00
|
|
|
if (InnerOp?.Inst == ShaderIrInst.Bra)
|
2018-05-30 01:37:10 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
int Target = ((ShaderIrOperImm)InnerOp.OperandA).Value;
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
Current.Branch = Enqueue(Target, Current);
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:14:04 +02:00
|
|
|
foreach (ShaderIrNode Node in Current.Nodes)
|
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
InnerOp = GetInnermostOp(Node);
|
2018-08-31 18:14:04 +02:00
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
if (InnerOp is ShaderIrOp CurrOp && CurrOp.Inst == ShaderIrInst.Ssy)
|
|
|
|
{
|
|
|
|
int Target = ((ShaderIrOperImm)CurrOp.OperandA).Value;
|
2018-08-31 18:14:04 +02:00
|
|
|
|
2018-10-17 23:02:23 +02:00
|
|
|
Enqueue(Target, Current);
|
2018-08-31 18:14:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
if (NodeHasNext(LastNode))
|
|
|
|
{
|
|
|
|
Current.Next = Enqueue(Current.EndPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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.
|
|
|
|
while (VisitedEnd.TryGetValue(Current.EndPosition, out ShaderIrBlock Smaller))
|
|
|
|
{
|
|
|
|
if (Current.Position > Smaller.Position)
|
|
|
|
{
|
|
|
|
ShaderIrBlock Temp = Smaller;
|
|
|
|
|
|
|
|
Smaller = Current;
|
|
|
|
Current = Temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Current.EndPosition = Smaller.Position;
|
|
|
|
Current.Next = Smaller;
|
|
|
|
Current.Branch = null;
|
|
|
|
|
|
|
|
Current.Nodes.RemoveRange(
|
|
|
|
Current.Nodes.Count - Smaller.Nodes.Count,
|
|
|
|
Smaller.Nodes.Count);
|
|
|
|
|
|
|
|
VisitedEnd[Smaller.EndPosition] = Smaller;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisitedEnd.Add(Current.EndPosition, Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make and sort Graph blocks array by position.
|
|
|
|
ShaderIrBlock[] Graph = new ShaderIrBlock[Visited.Count];
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
while (Visited.Count > 0)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
uint FirstPos = uint.MaxValue;
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
foreach (ShaderIrBlock Block in Visited.Values)
|
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
if (FirstPos > (uint)Block.Position)
|
|
|
|
FirstPos = (uint)Block.Position;
|
2018-05-30 01:37:10 +02:00
|
|
|
}
|
2018-05-17 20:25:42 +02:00
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
ShaderIrBlock Current = Visited[(int)FirstPos];
|
2018-05-17 20:25:42 +02:00
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
Graph[Graph.Length - Visited.Count] = Current;
|
|
|
|
|
|
|
|
Visited.Remove(Current.Position);
|
|
|
|
|
|
|
|
Current = Current.Next;
|
|
|
|
}
|
|
|
|
while (Current != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Graph;
|
|
|
|
}
|
|
|
|
|
2018-07-19 07:33:27 +02:00
|
|
|
private static void FillBlock(IGalMemory Memory, ShaderIrBlock Block, long Beginning)
|
2018-05-30 01:37:10 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
int Position = Block.Position;
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-05-17 20:25:42 +02:00
|
|
|
//Ignore scheduling instructions, which are written every 32 bytes.
|
2018-09-18 06:30:35 +02:00
|
|
|
if ((Position & 0x1f) == 0)
|
2018-04-10 21:50:32 +02:00
|
|
|
{
|
2018-05-23 03:43:31 +02:00
|
|
|
Position += 8;
|
2018-04-10 21:50:32 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
uint Word0 = (uint)Memory.ReadInt32(Position + Beginning + 0);
|
|
|
|
uint Word1 = (uint)Memory.ReadInt32(Position + Beginning + 4);
|
2018-05-23 03:43:31 +02:00
|
|
|
|
|
|
|
Position += 8;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
long OpCode = Word0 | (long)Word1 << 32;
|
|
|
|
|
|
|
|
ShaderDecodeFunc Decode = ShaderOpCodeTable.GetDecoder(OpCode);
|
|
|
|
|
2018-05-17 20:25:42 +02:00
|
|
|
if (AddDbgComments)
|
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
string DbgOpCode = $"0x{(Position - 8):x16}: 0x{OpCode:x16} ";
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
DbgOpCode += (Decode?.Method.Name ?? "???");
|
2018-05-17 20:25:42 +02:00
|
|
|
|
2018-10-17 23:02:23 +02:00
|
|
|
if (Decode == ShaderDecode.Bra || Decode == ShaderDecode.Ssy)
|
2018-05-30 01:37:10 +02:00
|
|
|
{
|
|
|
|
int Offset = ((int)(OpCode >> 20) << 8) >> 8;
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
long Target = Position + Offset;
|
2018-05-30 01:37:10 +02:00
|
|
|
|
|
|
|
DbgOpCode += " (0x" + Target.ToString("x16") + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
Block.AddNode(new ShaderIrCmnt(DbgOpCode));
|
2018-05-17 20:25:42 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 21:17:35 +02:00
|
|
|
if (Decode == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:14:04 +02:00
|
|
|
Decode(Block, OpCode, Position);
|
2018-05-30 01:37:10 +02:00
|
|
|
}
|
|
|
|
while (!IsFlowChange(Block.GetLastNode()));
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
Block.EndPosition = Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsFlowChange(ShaderIrNode Node)
|
|
|
|
{
|
|
|
|
return !NodeHasNext(GetInnermostOp(Node));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ShaderIrOp GetInnermostOp(ShaderIrNode Node)
|
|
|
|
{
|
|
|
|
if (Node is ShaderIrCond Cond)
|
|
|
|
{
|
|
|
|
Node = Cond.Child;
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
return Node is ShaderIrOp Op ? Op : null;
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 01:37:10 +02:00
|
|
|
private static bool NodeHasNext(ShaderIrNode Node)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-05-30 01:37:10 +02:00
|
|
|
if (!(Node is ShaderIrOp Op))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Op.Inst != ShaderIrInst.Exit &&
|
|
|
|
Op.Inst != ShaderIrInst.Bra;
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|