mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2025-05-12 18:57:42 +02:00

* dotnet format style --severity info Some changes were manually reverted. * Restore a few unused methods and variables * Silence dotnet format IDE0060 warnings * Silence dotnet format IDE0052 warnings * Silence dotnet format IDE0059 warnings * Address or silence dotnet format CA1069 warnings * Address or silence dotnet format CA2211 warnings * Address review comments * Fix formatting for switch expressions * Address most dotnet format whitespace warnings * Apply dotnet format whitespace formatting A few of them have been manually reverted and the corresponding warning was silenced * Format if-blocks correctly * Run dotnet format whitespace after rebase * Run dotnet format style after rebase * Run dotnet format whitespace after rebase * Run dotnet format style after rebase * Run dotnet format after rebase and remove unused usings - analyzers - style - whitespace * Disable 'prefer switch expression' rule * Add comments to disabled warnings * Fix naming rule violation, Convert shader properties to auto-property and convert values to const * Simplify properties and array initialization, Use const when possible, Remove trailing commas * Run dotnet format after rebase * Address IDE0251 warnings * Address a few disabled IDE0060 warnings * Silence IDE0060 in .editorconfig * Run dotnet format after rebase * Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas" This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e. * dotnet format whitespace after rebase * First dotnet format pass * Fix naming rule violations * Add trailing commas * Remove unused members and most unnecessary value assignments * Remove more unnecessary assignments * Remove NRE suppressor
322 lines
10 KiB
C#
322 lines
10 KiB
C#
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.IntermediateRepresentation.OperandHelper;
|
|
|
|
namespace Ryujinx.Graphics.Shader.Instructions
|
|
{
|
|
static partial class InstEmit
|
|
{
|
|
public static void Bra(EmitterContext context)
|
|
{
|
|
context.GetOp<InstBra>();
|
|
|
|
EmitBranch(context, context.CurrBlock.Successors[^1].Address);
|
|
}
|
|
|
|
public static void Brk(EmitterContext context)
|
|
{
|
|
context.GetOp<InstBrk>();
|
|
|
|
EmitBrkContSync(context);
|
|
}
|
|
|
|
public static void Brx(EmitterContext context)
|
|
{
|
|
InstBrx op = context.GetOp<InstBrx>();
|
|
InstOp currOp = context.CurrOp;
|
|
int startIndex = context.CurrBlock.HasNext() ? 1 : 0;
|
|
|
|
if (context.CurrBlock.Successors.Count <= startIndex)
|
|
{
|
|
context.Config.GpuAccessor.Log($"Failed to find targets for BRX instruction at 0x{currOp.Address:X}.");
|
|
return;
|
|
}
|
|
|
|
int offset = (int)currOp.GetAbsoluteAddress();
|
|
|
|
Operand address = context.IAdd(Register(op.SrcA, RegisterType.Gpr), Const(offset));
|
|
|
|
var targets = context.CurrBlock.Successors.Skip(startIndex);
|
|
|
|
bool allTargetsSinglePred = true;
|
|
int total = context.CurrBlock.Successors.Count - startIndex;
|
|
int count = 0;
|
|
|
|
foreach (var target in targets.OrderBy(x => x.Address))
|
|
{
|
|
if (++count < total && (target.Predecessors.Count > 1 || target.Address <= context.CurrBlock.Address))
|
|
{
|
|
allTargetsSinglePred = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (allTargetsSinglePred)
|
|
{
|
|
// Chain blocks, each target block will check if the BRX target address
|
|
// matches its own address, if not, it jumps to the next target which will do the same check,
|
|
// until it reaches the last possible target, which executed unconditionally.
|
|
// We can only do this if the BRX block is the only predecessor of all target blocks.
|
|
// Additionally, this is not supported for blocks located before the current block,
|
|
// since it will be too late to insert a label, but this is something that can be improved
|
|
// in the future if necessary.
|
|
|
|
var sortedTargets = targets.OrderBy(x => x.Address);
|
|
|
|
Block currentTarget = null;
|
|
ulong firstTargetAddress = 0;
|
|
|
|
foreach (Block nextTarget in sortedTargets)
|
|
{
|
|
if (currentTarget != null)
|
|
{
|
|
if (currentTarget.Address != nextTarget.Address)
|
|
{
|
|
context.SetBrxTarget(currentTarget.Address, address, (int)currentTarget.Address, nextTarget.Address);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
firstTargetAddress = nextTarget.Address;
|
|
}
|
|
|
|
currentTarget = nextTarget;
|
|
}
|
|
|
|
context.Branch(context.GetLabel(firstTargetAddress));
|
|
}
|
|
else
|
|
{
|
|
// Emit the branches sequentially.
|
|
// This generates slightly worse code, but should work for all cases.
|
|
|
|
var sortedTargets = targets.OrderByDescending(x => x.Address);
|
|
ulong lastTargetAddress = ulong.MaxValue;
|
|
|
|
count = 0;
|
|
|
|
foreach (Block target in sortedTargets)
|
|
{
|
|
Operand label = context.GetLabel(target.Address);
|
|
|
|
if (++count < total)
|
|
{
|
|
if (target.Address != lastTargetAddress)
|
|
{
|
|
context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)target.Address)));
|
|
}
|
|
|
|
lastTargetAddress = target.Address;
|
|
}
|
|
else
|
|
{
|
|
context.Branch(label);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Cal(EmitterContext context)
|
|
{
|
|
context.GetOp<InstCal>();
|
|
|
|
DecodedFunction function = context.Program.GetFunctionByAddress(context.CurrOp.GetAbsoluteAddress());
|
|
|
|
if (function.IsCompilerGenerated)
|
|
{
|
|
switch (function.Type)
|
|
{
|
|
case FunctionType.BuiltInFSIBegin:
|
|
context.FSIBegin();
|
|
break;
|
|
case FunctionType.BuiltInFSIEnd:
|
|
context.FSIEnd();
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
context.Call(function.Id, false);
|
|
}
|
|
}
|
|
|
|
public static void Cont(EmitterContext context)
|
|
{
|
|
context.GetOp<InstCont>();
|
|
|
|
EmitBrkContSync(context);
|
|
}
|
|
|
|
public static void Exit(EmitterContext context)
|
|
{
|
|
InstExit op = context.GetOp<InstExit>();
|
|
|
|
if (context.IsNonMain)
|
|
{
|
|
context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
|
|
return;
|
|
}
|
|
|
|
if (op.Ccc == Ccc.T)
|
|
{
|
|
context.PrepareForReturn();
|
|
context.Return();
|
|
}
|
|
else
|
|
{
|
|
Operand cond = GetCondition(context, op.Ccc, IrConsts.False);
|
|
|
|
// If the condition is always false, we don't need to do anything.
|
|
if (cond.Type != OperandType.Constant || cond.Value != IrConsts.False)
|
|
{
|
|
Operand lblSkip = Label();
|
|
context.BranchIfFalse(lblSkip, cond);
|
|
context.PrepareForReturn();
|
|
context.Return();
|
|
context.MarkLabel(lblSkip);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Kil(EmitterContext context)
|
|
{
|
|
context.GetOp<InstKil>();
|
|
|
|
context.Discard();
|
|
}
|
|
|
|
public static void Pbk(EmitterContext context)
|
|
{
|
|
context.GetOp<InstPbk>();
|
|
|
|
EmitPbkPcntSsy(context);
|
|
}
|
|
|
|
public static void Pcnt(EmitterContext context)
|
|
{
|
|
context.GetOp<InstPcnt>();
|
|
|
|
EmitPbkPcntSsy(context);
|
|
}
|
|
|
|
public static void Ret(EmitterContext context)
|
|
{
|
|
context.GetOp<InstRet>();
|
|
|
|
if (context.IsNonMain)
|
|
{
|
|
context.Return();
|
|
}
|
|
else
|
|
{
|
|
context.Config.GpuAccessor.Log("Invalid return on main function.");
|
|
}
|
|
}
|
|
|
|
public static void Ssy(EmitterContext context)
|
|
{
|
|
context.GetOp<InstSsy>();
|
|
|
|
EmitPbkPcntSsy(context);
|
|
}
|
|
|
|
public static void Sync(EmitterContext context)
|
|
{
|
|
context.GetOp<InstSync>();
|
|
|
|
EmitBrkContSync(context);
|
|
}
|
|
|
|
private static void EmitPbkPcntSsy(EmitterContext context)
|
|
{
|
|
var consumers = context.CurrBlock.PushOpCodes.First(x => x.Op.Address == context.CurrOp.Address).Consumers;
|
|
|
|
foreach (KeyValuePair<Block, Operand> kv in consumers)
|
|
{
|
|
Block consumerBlock = kv.Key;
|
|
Operand local = kv.Value;
|
|
|
|
int id = consumerBlock.SyncTargets[context.CurrOp.Address].PushOpId;
|
|
|
|
context.Copy(local, Const(id));
|
|
}
|
|
}
|
|
|
|
private static void EmitBrkContSync(EmitterContext context)
|
|
{
|
|
var targets = context.CurrBlock.SyncTargets;
|
|
|
|
if (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.
|
|
EmitBranch(context, targets.Values.First().PushOpInfo.Op.GetAbsoluteAddress());
|
|
}
|
|
else
|
|
{
|
|
// TODO: Support CC here as well (condition).
|
|
foreach (SyncTarget target in targets.Values)
|
|
{
|
|
PushOpInfo pushOpInfo = target.PushOpInfo;
|
|
|
|
Operand label = context.GetLabel(pushOpInfo.Op.GetAbsoluteAddress());
|
|
Operand local = pushOpInfo.Consumers[context.CurrBlock];
|
|
|
|
context.BranchIfTrue(label, context.ICompareEqual(local, Const(target.PushOpId)));
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void EmitBranch(EmitterContext context, ulong address)
|
|
{
|
|
InstOp op = context.CurrOp;
|
|
InstConditional opCond = new(op.RawOpCode);
|
|
|
|
// 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(opCond.Pred, RegisterType.Predicate);
|
|
|
|
if (opCond.Ccc != Ccc.T)
|
|
{
|
|
Operand cond = GetCondition(context, opCond.Ccc);
|
|
|
|
if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
|
|
{
|
|
pred = cond;
|
|
}
|
|
else if (opCond.PredInv)
|
|
{
|
|
pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
|
|
}
|
|
else
|
|
{
|
|
pred = context.BitwiseAnd(pred, cond);
|
|
}
|
|
|
|
context.BranchIfTrue(label, pred);
|
|
}
|
|
else if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
|
|
{
|
|
context.Branch(label);
|
|
}
|
|
else if (opCond.PredInv)
|
|
{
|
|
context.BranchIfFalse(label, pred);
|
|
}
|
|
else
|
|
{
|
|
context.BranchIfTrue(label, pred);
|
|
}
|
|
}
|
|
}
|
|
}
|