| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using Ryujinx.Graphics.Shader.Decoders;
- using Ryujinx.Graphics.Shader.IntermediateRepresentation;
- using Ryujinx.Graphics.Shader.Translation;
- using System.Collections.Generic;
- using System.Linq;
- using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
- using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
- namespace Ryujinx.Graphics.Shader.Instructions
- {
- static partial class InstEmit
- {
- public static void Bra(EmitterContext context)
- {
- EmitBranch(context, context.CurrBlock.Branch.Address);
- }
- public static void Brk(EmitterContext context)
- {
- EmitBrkOrSync(context);
- }
- public static void Brx(EmitterContext context)
- {
- OpCodeBranchIndir op = (OpCodeBranchIndir)context.CurrOp;
- if (op.PossibleTargets.Count == 0)
- {
- context.Config.GpuAccessor.Log($"Failed to find targets for BRX instruction at 0x{op.Address:X}.");
- return;
- }
- int offset = (int)op.Address + 8 + op.Offset;
- Operand address = context.IAdd(Register(op.Ra), Const(offset));
- // Sorting the target addresses in descending order improves the code,
- // since it will always check the most distant targets first, then the
- // near ones. This can be easily transformed into if/else statements.
- IOrderedEnumerable<Block> sortedTargets = op.PossibleTargets.OrderByDescending(x => x.Address);
- Block lastTarget = sortedTargets.LastOrDefault();
- foreach (Block possibleTarget in sortedTargets)
- {
- Operand label = context.GetLabel(possibleTarget.Address);
- if (possibleTarget != lastTarget)
- {
- context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)possibleTarget.Address)));
- }
- else
- {
- context.Branch(label);
- }
- }
- }
- public static void Cal(EmitterContext context)
- {
- OpCodeBranch op = (OpCodeBranch)context.CurrOp;
- context.Call(context.GetFunctionId(op.GetAbsoluteAddress()), false);
- }
- public static void Depbar(EmitterContext context)
- {
- }
- public static void Exit(EmitterContext context)
- {
- if (context.IsNonMain)
- {
- context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
- return;
- }
- OpCodeExit op = (OpCodeExit)context.CurrOp;
- // TODO: Figure out how this is supposed to work in the
- // presence of other condition codes.
- if (op.Condition == Condition.Always)
- {
- context.Return();
- }
- }
- public static void Kil(EmitterContext context)
- {
- context.Discard();
- }
- public static void Nop(EmitterContext context)
- {
- }
- public static void Pbk(EmitterContext context)
- {
- EmitPbkOrSsy(context);
- }
- public static void Ret(EmitterContext context)
- {
- if (context.IsNonMain)
- {
- context.Return();
- }
- else
- {
- context.Config.GpuAccessor.Log("Invalid return on main function.");
- }
- }
- public static void Ssy(EmitterContext context)
- {
- EmitPbkOrSsy(context);
- }
- public static void Sync(EmitterContext context)
- {
- EmitBrkOrSync(context);
- }
- private static void EmitPbkOrSsy(EmitterContext context)
- {
- OpCodePush op = (OpCodePush)context.CurrOp;
- foreach (KeyValuePair<OpCodeBranchPop, Operand> kv in op.PopOps)
- {
- OpCodeBranchPop opSync = kv.Key;
- Operand local = kv.Value;
- int pushOpIndex = opSync.Targets[op];
- context.Copy(local, Const(pushOpIndex));
- }
- }
- private static void EmitBrkOrSync(EmitterContext context)
- {
- OpCodeBranchPop op = (OpCodeBranchPop)context.CurrOp;
- if (op.Targets.Count == 1)
- {
- // If we have only one target, then the SSY/PBK is basically
- // a branch, we can produce better codegen for this case.
- OpCodePush pushOp = op.Targets.Keys.First();
- EmitBranch(context, pushOp.GetAbsoluteAddress());
- }
- else
- {
- // TODO: Support CC here aswell (condition).
- foreach (KeyValuePair<OpCodePush, int> kv in op.Targets)
- {
- OpCodePush pushOp = kv.Key;
- Operand label = context.GetLabel(pushOp.GetAbsoluteAddress());
- Operand local = pushOp.PopOps[op];
- int pushOpIndex = kv.Value;
- context.BranchIfTrue(label, context.ICompareEqual(local, Const(pushOpIndex)));
- }
- }
- }
- private static void EmitBranch(EmitterContext context, ulong address)
- {
- OpCode op = context.CurrOp;
- // If we're branching to the next instruction, then the branch
- // is useless and we can ignore it.
- if (address == op.Address + 8)
- {
- return;
- }
- Operand label = context.GetLabel(address);
- Operand pred = Register(op.Predicate);
- if (op is OpCodeConditional opCond && opCond.Condition != Condition.Always)
- {
- Operand cond = GetCondition(context, opCond.Condition);
- if (op.Predicate.IsPT)
- {
- pred = cond;
- }
- else if (op.InvertPredicate)
- {
- pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
- }
- else
- {
- pred = context.BitwiseAnd(pred, cond);
- }
- context.BranchIfTrue(label, pred);
- }
- else if (op.Predicate.IsPT)
- {
- context.Branch(label);
- }
- else if (op.InvertPredicate)
- {
- context.BranchIfFalse(label, pred);
- }
- else
- {
- context.BranchIfTrue(label, pred);
- }
- }
- private static Operand GetCondition(EmitterContext context, Condition cond)
- {
- // TODO: More condition codes, figure out how they work.
- switch (cond)
- {
- case Condition.Equal:
- case Condition.EqualUnordered:
- return GetZF();
- case Condition.NotEqual:
- case Condition.NotEqualUnordered:
- return context.BitwiseNot(GetZF());
- }
- return Const(IrConsts.True);
- }
- }
- }
|