2020-05-27 15:07:10 +01:00
|
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
|
|
|
|
|
{
|
|
|
|
|
class BindlessElimination
|
|
|
|
|
{
|
2021-03-09 20:27:44 +00:00
|
|
|
|
private static Operation FindBranchSource(BasicBlock block)
|
|
|
|
|
{
|
|
|
|
|
foreach (BasicBlock sourceBlock in block.Predecessors)
|
|
|
|
|
{
|
|
|
|
|
if (sourceBlock.Operations.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
Operation lastOp = sourceBlock.Operations.Last.Value as Operation;
|
|
|
|
|
|
|
|
|
|
if (lastOp != null &&
|
|
|
|
|
((sourceBlock.Next == block && lastOp.Inst == Instruction.BranchIfFalse) ||
|
|
|
|
|
(sourceBlock.Branch == block && lastOp.Inst == Instruction.BranchIfTrue)))
|
|
|
|
|
{
|
|
|
|
|
return lastOp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool BlockConditionsMatch(BasicBlock currentBlock, BasicBlock queryBlock)
|
|
|
|
|
{
|
|
|
|
|
// Check if all the conditions for the query block are satisfied by the current block.
|
|
|
|
|
// Just checks the top-most conditional for now.
|
|
|
|
|
|
|
|
|
|
Operation currentBranch = FindBranchSource(currentBlock);
|
|
|
|
|
Operation queryBranch = FindBranchSource(queryBlock);
|
|
|
|
|
|
|
|
|
|
Operand currentCondition = currentBranch?.GetSource(0);
|
|
|
|
|
Operand queryCondition = queryBranch?.GetSource(0);
|
|
|
|
|
|
|
|
|
|
// The condition should be the same operand instance.
|
|
|
|
|
|
|
|
|
|
return currentBranch != null && queryBranch != null &&
|
|
|
|
|
currentBranch.Inst == queryBranch.Inst &&
|
|
|
|
|
currentCondition == queryCondition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Operand FindLastOperation(Operand source, BasicBlock block)
|
|
|
|
|
{
|
|
|
|
|
if (source.AsgOp is PhiNode phiNode)
|
|
|
|
|
{
|
|
|
|
|
// This source can have a different value depending on a previous branch.
|
|
|
|
|
// Ensure that conditions met for that branch are also met for the current one.
|
|
|
|
|
// Prefer the latest sources for the phi node.
|
|
|
|
|
|
|
|
|
|
for (int i = phiNode.SourcesCount - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
BasicBlock phiBlock = phiNode.GetBlock(i);
|
|
|
|
|
|
|
|
|
|
if (BlockConditionsMatch(block, phiBlock))
|
|
|
|
|
{
|
|
|
|
|
return phiNode.GetSource(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 00:48:44 +01:00
|
|
|
|
public static void RunPass(BasicBlock block, ShaderConfig config)
|
2020-05-27 15:07:10 +01:00
|
|
|
|
{
|
|
|
|
|
// We can turn a bindless into regular access by recognizing the pattern
|
|
|
|
|
// produced by the compiler for separate texture and sampler.
|
|
|
|
|
// We check for the following conditions:
|
2020-11-09 22:35:04 +00:00
|
|
|
|
// - The handle is a constant buffer value.
|
2020-05-27 15:07:10 +01:00
|
|
|
|
// - The handle is the result of a bitwise OR logical operation.
|
2020-11-09 22:35:04 +00:00
|
|
|
|
// - Both sources of the OR operation comes from a constant buffer.
|
2020-05-27 15:07:10 +01:00
|
|
|
|
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
|
|
|
|
{
|
|
|
|
|
if (!(node.Value is TextureOperation texOp))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((texOp.Flags & TextureFlags.Bindless) == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 22:35:04 +00:00
|
|
|
|
if (texOp.Inst == Instruction.Lod ||
|
|
|
|
|
texOp.Inst == Instruction.TextureSample ||
|
|
|
|
|
texOp.Inst == Instruction.TextureSize)
|
2020-05-27 15:07:10 +01:00
|
|
|
|
{
|
2021-03-09 20:27:44 +00:00
|
|
|
|
Operand bindlessHandle = FindLastOperation(texOp.GetSource(0), block);
|
2020-11-09 22:35:04 +00:00
|
|
|
|
|
|
|
|
|
if (bindlessHandle.Type == OperandType.ConstantBuffer)
|
|
|
|
|
{
|
|
|
|
|
texOp.SetHandle(bindlessHandle.GetCbufOffset(), bindlessHandle.GetCbufSlot());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(bindlessHandle.AsgOp is Operation handleCombineOp))
|
2020-07-04 00:48:44 +01:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-05-27 15:07:10 +01:00
|
|
|
|
|
2020-07-04 00:48:44 +01:00
|
|
|
|
if (handleCombineOp.Inst != Instruction.BitwiseOr)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-05-27 15:07:10 +01:00
|
|
|
|
|
2021-03-09 20:27:44 +00:00
|
|
|
|
Operand src0 = FindLastOperation(handleCombineOp.GetSource(0), block);
|
|
|
|
|
Operand src1 = FindLastOperation(handleCombineOp.GetSource(1), block);
|
2020-05-27 15:07:10 +01:00
|
|
|
|
|
2020-11-09 22:35:04 +00:00
|
|
|
|
if (src0.Type != OperandType.ConstantBuffer ||
|
|
|
|
|
src1.Type != OperandType.ConstantBuffer || src0.GetCbufSlot() != src1.GetCbufSlot())
|
2020-07-04 00:48:44 +01:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 22:35:04 +00:00
|
|
|
|
texOp.SetHandle(src0.GetCbufOffset() | (src1.GetCbufOffset() << 16), src0.GetCbufSlot());
|
2020-05-27 15:07:10 +01:00
|
|
|
|
}
|
2020-07-04 00:48:44 +01:00
|
|
|
|
else if (texOp.Inst == Instruction.ImageLoad || texOp.Inst == Instruction.ImageStore)
|
|
|
|
|
{
|
2021-03-09 20:27:44 +00:00
|
|
|
|
Operand src0 = FindLastOperation(texOp.GetSource(0), block);
|
2020-05-27 15:07:10 +01:00
|
|
|
|
|
2020-11-09 22:35:04 +00:00
|
|
|
|
if (src0.Type == OperandType.ConstantBuffer)
|
2020-07-04 00:48:44 +01:00
|
|
|
|
{
|
2020-11-09 22:35:04 +00:00
|
|
|
|
texOp.SetHandle(src0.GetCbufOffset(), src0.GetCbufSlot());
|
2020-07-04 00:48:44 +01:00
|
|
|
|
texOp.Format = config.GetTextureFormat(texOp.Handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 15:07:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|