2019-11-03 03:07:21 +01:00
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
|
|
|
|
{
|
|
|
|
static class BindlessToIndexed
|
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
public static void RunPass(BasicBlock block, ShaderConfig config)
|
2019-11-03 03:07:21 +01:00
|
|
|
{
|
|
|
|
// We can turn a bindless texture access into a indexed access,
|
|
|
|
// as long the following conditions are true:
|
|
|
|
// - The handle is loaded using a LDC instruction.
|
|
|
|
// - The handle is loaded from the constant buffer with the handles (CB2 for NVN).
|
|
|
|
// - The load has a constant offset.
|
|
|
|
// The base offset of the array of handles on the constant buffer is the constant offset.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(texOp.GetSource(0).AsgOp is Operation handleAsgOp))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handleAsgOp.Inst != Instruction.LoadConstant)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand ldcSrc0 = handleAsgOp.GetSource(0);
|
|
|
|
Operand ldcSrc1 = handleAsgOp.GetSource(1);
|
|
|
|
|
|
|
|
if (ldcSrc0.Type != OperandType.Constant || ldcSrc0.Value != 2)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:29:58 +01:00
|
|
|
if (!(ldcSrc1.AsgOp is Operation shrOp) || shrOp.Inst != Instruction.ShiftRightU32)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(shrOp.GetSource(0).AsgOp is Operation addOp) || addOp.Inst != Instruction.Add)
|
2019-11-03 03:07:21 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand addSrc1 = addOp.GetSource(1);
|
|
|
|
|
|
|
|
if (addSrc1.Type != OperandType.Constant)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
TurnIntoIndexed(config, texOp, addSrc1.Value / 4);
|
2019-11-03 03:07:21 +01:00
|
|
|
|
|
|
|
Operand index = Local();
|
|
|
|
|
|
|
|
Operand source = addOp.GetSource(0);
|
|
|
|
|
2020-02-14 11:29:58 +01:00
|
|
|
Operation shrBy3 = new Operation(Instruction.ShiftRightU32, index, source, Const(3));
|
2019-11-03 03:07:21 +01:00
|
|
|
|
2020-02-14 11:29:58 +01:00
|
|
|
block.Operations.AddBefore(node, shrBy3);
|
2019-11-03 03:07:21 +01:00
|
|
|
|
|
|
|
texOp.SetSource(0, index);
|
|
|
|
}
|
|
|
|
}
|
2021-05-19 23:15:26 +02:00
|
|
|
|
|
|
|
private static void TurnIntoIndexed(ShaderConfig config, TextureOperation texOp, int handle)
|
|
|
|
{
|
|
|
|
texOp.TurnIntoIndexed(handle);
|
|
|
|
config.SetUsedTexture(texOp.Inst, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, handle);
|
|
|
|
}
|
2019-11-03 03:07:21 +01:00
|
|
|
}
|
|
|
|
}
|