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
|
|
|
|
|
{
|
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-04-18 11:31:39 +01:00
|
|
|
|
Operand bindlessHandle = Utils.FindLastOperation(texOp.GetSource(0), block);
|
2021-09-02 04:17:43 +01:00
|
|
|
|
bool rewriteSamplerType = texOp.Inst == Instruction.TextureSize;
|
2020-11-09 22:35:04 +00:00
|
|
|
|
|
|
|
|
|
if (bindlessHandle.Type == OperandType.ConstantBuffer)
|
|
|
|
|
{
|
2021-09-02 04:17:43 +01:00
|
|
|
|
SetHandle(config, texOp, bindlessHandle.GetCbufOffset(), bindlessHandle.GetCbufSlot(), rewriteSamplerType);
|
2020-11-09 22:35:04 +00:00
|
|
|
|
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-04-18 11:31:39 +01:00
|
|
|
|
Operand src0 = Utils.FindLastOperation(handleCombineOp.GetSource(0), block);
|
|
|
|
|
Operand src1 = Utils.FindLastOperation(handleCombineOp.GetSource(1), block);
|
2020-05-27 15:07:10 +01:00
|
|
|
|
|
2021-06-08 23:42:25 +01:00
|
|
|
|
if (src0.Type != OperandType.ConstantBuffer || src1.Type != OperandType.ConstantBuffer)
|
2020-07-04 00:48:44 +01:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 23:42:25 +01:00
|
|
|
|
SetHandle(
|
|
|
|
|
config,
|
|
|
|
|
texOp,
|
2021-06-20 19:48:12 +01:00
|
|
|
|
src0.GetCbufOffset() | ((src1.GetCbufOffset() + 1) << 16),
|
2021-09-02 04:17:43 +01:00
|
|
|
|
src0.GetCbufSlot() | ((src1.GetCbufSlot() + 1) << 16),
|
|
|
|
|
rewriteSamplerType);
|
2020-05-27 15:07:10 +01:00
|
|
|
|
}
|
2021-08-31 06:51:57 +01:00
|
|
|
|
else if (texOp.Inst == Instruction.ImageLoad ||
|
|
|
|
|
texOp.Inst == Instruction.ImageStore ||
|
|
|
|
|
texOp.Inst == Instruction.ImageAtomic)
|
2020-07-04 00:48:44 +01:00
|
|
|
|
{
|
2021-04-18 11:31:39 +01:00
|
|
|
|
Operand src0 = Utils.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
|
|
|
|
{
|
2021-05-19 22:15:26 +01:00
|
|
|
|
int cbufOffset = src0.GetCbufOffset();
|
|
|
|
|
int cbufSlot = src0.GetCbufSlot();
|
2021-08-31 06:51:57 +01:00
|
|
|
|
|
|
|
|
|
if (texOp.Inst == Instruction.ImageAtomic)
|
|
|
|
|
{
|
|
|
|
|
texOp.Format = config.GetTextureFormatAtomic(cbufOffset, cbufSlot);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
texOp.Format = config.GetTextureFormat(cbufOffset, cbufSlot);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 04:17:43 +01:00
|
|
|
|
SetHandle(config, texOp, cbufOffset, cbufSlot, false);
|
2020-07-04 00:48:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 15:07:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-19 22:15:26 +01:00
|
|
|
|
|
2021-09-02 04:17:43 +01:00
|
|
|
|
private static void SetHandle(ShaderConfig config, TextureOperation texOp, int cbufOffset, int cbufSlot, bool rewriteSamplerType)
|
2021-05-19 22:15:26 +01:00
|
|
|
|
{
|
|
|
|
|
texOp.SetHandle(cbufOffset, cbufSlot);
|
2021-09-02 04:17:43 +01:00
|
|
|
|
|
|
|
|
|
if (rewriteSamplerType)
|
|
|
|
|
{
|
|
|
|
|
texOp.Type = config.GpuAccessor.QuerySamplerType(cbufOffset, cbufSlot);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 22:15:26 +01:00
|
|
|
|
config.SetUsedTexture(texOp.Inst, texOp.Type, texOp.Format, texOp.Flags, cbufSlot, cbufOffset);
|
|
|
|
|
}
|
2020-05-27 15:07:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|