Implement CAL and RET shader instructions (#1618)
* Add support for CAL and RET shader instructions * Remove unused stuff * Fix a bug that could cause the wrong values to be passed to a function * Avoid repopulating function id dictionary every time * PR feedback * Fix vertex shader A/B merge
This commit is contained in:
parent
973a615d40
commit
49f970d5bd
33 changed files with 1337 additions and 401 deletions
|
@ -8,51 +8,108 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
|||
{
|
||||
static class StructuredProgram
|
||||
{
|
||||
public static StructuredProgramInfo MakeStructuredProgram(BasicBlock[] blocks, ShaderConfig config)
|
||||
public static StructuredProgramInfo MakeStructuredProgram(Function[] functions, ShaderConfig config)
|
||||
{
|
||||
PhiFunctions.Remove(blocks);
|
||||
StructuredProgramContext context = new StructuredProgramContext(config);
|
||||
|
||||
StructuredProgramContext context = new StructuredProgramContext(blocks.Length, config);
|
||||
|
||||
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
|
||||
for (int funcIndex = 0; funcIndex < functions.Length; funcIndex++)
|
||||
{
|
||||
BasicBlock block = blocks[blkIndex];
|
||||
Function function = functions[funcIndex];
|
||||
|
||||
context.EnterBlock(block);
|
||||
BasicBlock[] blocks = function.Blocks;
|
||||
|
||||
foreach (INode node in block.Operations)
|
||||
VariableType returnType = function.ReturnsValue ? VariableType.S32 : VariableType.None;
|
||||
|
||||
VariableType[] inArguments = new VariableType[function.InArgumentsCount];
|
||||
VariableType[] outArguments = new VariableType[function.OutArgumentsCount];
|
||||
|
||||
for (int i = 0; i < inArguments.Length; i++)
|
||||
{
|
||||
Operation operation = (Operation)node;
|
||||
inArguments[i] = VariableType.S32;
|
||||
}
|
||||
|
||||
if (IsBranchInst(operation.Inst))
|
||||
for (int i = 0; i < outArguments.Length; i++)
|
||||
{
|
||||
outArguments[i] = VariableType.S32;
|
||||
}
|
||||
|
||||
context.EnterFunction(blocks.Length, function.Name, returnType, inArguments, outArguments);
|
||||
|
||||
PhiFunctions.Remove(blocks);
|
||||
|
||||
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
|
||||
{
|
||||
BasicBlock block = blocks[blkIndex];
|
||||
|
||||
context.EnterBlock(block);
|
||||
|
||||
for (LinkedListNode<INode> opNode = block.Operations.First; opNode != null; opNode = opNode.Next)
|
||||
{
|
||||
context.LeaveBlock(block, operation);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddOperation(context, operation);
|
||||
Operation operation = (Operation)opNode.Value;
|
||||
|
||||
if (IsBranchInst(operation.Inst))
|
||||
{
|
||||
context.LeaveBlock(block, operation);
|
||||
}
|
||||
else if (operation.Inst != Instruction.CallOutArgument)
|
||||
{
|
||||
AddOperation(context, opNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GotoElimination.Eliminate(context.GetGotos());
|
||||
|
||||
AstOptimizer.Optimize(context);
|
||||
|
||||
context.LeaveFunction();
|
||||
}
|
||||
|
||||
GotoElimination.Eliminate(context.GetGotos());
|
||||
|
||||
AstOptimizer.Optimize(context);
|
||||
|
||||
return context.Info;
|
||||
}
|
||||
|
||||
private static void AddOperation(StructuredProgramContext context, Operation operation)
|
||||
private static void AddOperation(StructuredProgramContext context, LinkedListNode<INode> opNode)
|
||||
{
|
||||
Operation operation = (Operation)opNode.Value;
|
||||
|
||||
Instruction inst = operation.Inst;
|
||||
|
||||
IAstNode[] sources = new IAstNode[operation.SourcesCount];
|
||||
bool isCall = inst == Instruction.Call;
|
||||
|
||||
for (int index = 0; index < sources.Length; index++)
|
||||
int sourcesCount = operation.SourcesCount;
|
||||
|
||||
List<Operand> callOutOperands = new List<Operand>();
|
||||
|
||||
if (isCall)
|
||||
{
|
||||
LinkedListNode<INode> scan = opNode.Next;
|
||||
|
||||
while (scan != null && scan.Value is Operation nextOp && nextOp.Inst == Instruction.CallOutArgument)
|
||||
{
|
||||
callOutOperands.Add(nextOp.Dest);
|
||||
scan = scan.Next;
|
||||
}
|
||||
|
||||
sourcesCount += callOutOperands.Count;
|
||||
}
|
||||
|
||||
IAstNode[] sources = new IAstNode[sourcesCount];
|
||||
|
||||
for (int index = 0; index < operation.SourcesCount; index++)
|
||||
{
|
||||
sources[index] = context.GetOperandUse(operation.GetSource(index));
|
||||
}
|
||||
|
||||
if (isCall)
|
||||
{
|
||||
for (int index = 0; index < callOutOperands.Count; index++)
|
||||
{
|
||||
sources[operation.SourcesCount + index] = context.GetOperandDef(callOutOperands[index]);
|
||||
}
|
||||
|
||||
callOutOperands.Clear();
|
||||
}
|
||||
|
||||
AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
|
||||
{
|
||||
return new AstTextureOperation(
|
||||
|
@ -98,8 +155,6 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
|||
AddSBufferUse(context.Info.SBuffers, operation);
|
||||
}
|
||||
|
||||
AstAssignment assignment;
|
||||
|
||||
// If all the sources are bool, it's better to use short-circuiting
|
||||
// logical operations, rather than forcing a cast to int and doing
|
||||
// a bitwise operation with the value, as it is likely to be used as
|
||||
|
@ -152,16 +207,14 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
|||
}
|
||||
else if (!isCopy)
|
||||
{
|
||||
source = new AstOperation(inst, operation.Index, sources);
|
||||
source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
source = sources[0];
|
||||
}
|
||||
|
||||
assignment = new AstAssignment(dest, source);
|
||||
|
||||
context.AddNode(assignment);
|
||||
context.AddNode(new AstAssignment(dest, source));
|
||||
}
|
||||
else if (operation.Inst == Instruction.Comment)
|
||||
{
|
||||
|
@ -182,7 +235,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
|||
AddSBufferUse(context.Info.SBuffers, operation);
|
||||
}
|
||||
|
||||
context.AddNode(new AstOperation(inst, operation.Index, sources));
|
||||
context.AddNode(new AstOperation(inst, operation.Index, sources, operation.SourcesCount));
|
||||
}
|
||||
|
||||
// Those instructions needs to be emulated by using helper functions,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue