2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
using static Ryujinx.Graphics.Shader.Translation.GlobalMemory;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
|
|
|
|
{
|
|
|
|
static class GlobalToStorage
|
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
public static void RunPass(BasicBlock block, ShaderConfig config)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
int sbStart = GetStorageBaseCbOffset(config.Stage);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
int sbEnd = sbStart + StorageDescsSize;
|
|
|
|
|
|
|
|
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
|
|
|
|
{
|
|
|
|
if (!(node.Value is Operation operation))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
if (UsesGlobalMemory(operation.Inst))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
Operand source = operation.GetSource(0);
|
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
int storageIndex = SearchForStorageBase(block, source, sbStart, sbEnd);
|
|
|
|
|
|
|
|
if (storageIndex >= 0)
|
|
|
|
{
|
|
|
|
// Storage buffers are implemented using global memory access.
|
|
|
|
// If we know from where the base address of the access is loaded,
|
|
|
|
// we can guess which storage buffer it is accessing.
|
|
|
|
// We can then replace the global memory access with a storage
|
|
|
|
// buffer access.
|
|
|
|
node = ReplaceGlobalWithStorage(node, config, storageIndex);
|
|
|
|
}
|
|
|
|
else if (config.Stage == ShaderStage.Compute && operation.Inst == Instruction.LoadGlobal)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-04-18 12:31:39 +02:00
|
|
|
// Here we effectively try to replace a LDG instruction with LDC.
|
|
|
|
// The hardware only supports a limited amount of constant buffers
|
|
|
|
// so NVN "emulates" more constant buffers using global memory access.
|
|
|
|
// Here we try to replace the global access back to a constant buffer
|
|
|
|
// load.
|
|
|
|
storageIndex = SearchForStorageBase(block, source, UbeBaseOffset, UbeBaseOffset + UbeDescsSize);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (storageIndex >= 0)
|
|
|
|
{
|
2021-04-18 12:31:39 +02:00
|
|
|
node = ReplaceLdgWithLdc(node, config, storageIndex);
|
2020-02-11 01:10:05 +01:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
private static LinkedListNode<INode> ReplaceGlobalWithStorage(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
Operation operation = (Operation)node.Value;
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
Operand GetStorageOffset()
|
2019-11-08 21:29:41 +01:00
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
Operand addrLow = operation.GetSource(0);
|
2019-11-08 21:29:41 +01:00
|
|
|
|
2020-01-06 23:27:50 +01:00
|
|
|
Operand baseAddrLow = Cbuf(0, GetStorageCbOffset(config.Stage, storageIndex));
|
2019-12-01 03:53:09 +01:00
|
|
|
|
|
|
|
Operand baseAddrTrunc = Local();
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
|
2019-12-01 03:53:09 +01:00
|
|
|
|
|
|
|
Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
|
|
|
|
|
|
|
|
node.List.AddBefore(node, andOp);
|
|
|
|
|
|
|
|
Operand byteOffset = Local();
|
|
|
|
Operand wordOffset = Local();
|
|
|
|
|
|
|
|
Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
|
|
|
|
Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
|
|
|
|
|
|
|
|
node.List.AddBefore(node, subOp);
|
|
|
|
node.List.AddBefore(node, shrOp);
|
|
|
|
|
|
|
|
return wordOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand[] sources = new Operand[operation.SourcesCount];
|
2019-11-08 21:29:41 +01:00
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
sources[0] = Const(storageIndex);
|
|
|
|
sources[1] = GetStorageOffset();
|
|
|
|
|
|
|
|
for (int index = 2; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
sources[index] = operation.GetSource(index);
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:10:05 +01:00
|
|
|
Operation storageOp;
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
if (operation.Inst.IsAtomic())
|
|
|
|
{
|
2019-11-08 21:29:41 +01:00
|
|
|
Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
storageOp = new Operation(inst, operation.Dest, sources);
|
2019-11-08 21:29:41 +01:00
|
|
|
}
|
|
|
|
else if (operation.Inst == Instruction.LoadGlobal)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
storageOp = new Operation(Instruction.StoreStorage, null, sources);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
operation.SetSource(index, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListNode<INode> oldNode = node;
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
node = node.List.AddBefore(node, storageOp);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
node.List.Remove(oldNode);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:10:05 +01:00
|
|
|
private static LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
|
|
|
|
{
|
|
|
|
Operation operation = (Operation)node.Value;
|
|
|
|
|
|
|
|
Operand GetCbufOffset()
|
|
|
|
{
|
|
|
|
Operand addrLow = operation.GetSource(0);
|
|
|
|
|
|
|
|
Operand baseAddrLow = Cbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
|
|
|
|
|
|
|
|
Operand baseAddrTrunc = Local();
|
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
|
2020-02-11 01:10:05 +01:00
|
|
|
|
|
|
|
Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
|
|
|
|
|
|
|
|
node.List.AddBefore(node, andOp);
|
|
|
|
|
|
|
|
Operand byteOffset = Local();
|
|
|
|
Operand wordOffset = Local();
|
|
|
|
|
|
|
|
Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
|
|
|
|
Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
|
|
|
|
|
|
|
|
node.List.AddBefore(node, subOp);
|
|
|
|
node.List.AddBefore(node, shrOp);
|
|
|
|
|
|
|
|
return wordOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand[] sources = new Operand[operation.SourcesCount];
|
|
|
|
|
|
|
|
sources[0] = Const(UbeFirstCbuf + storageIndex);
|
|
|
|
sources[1] = GetCbufOffset();
|
|
|
|
|
|
|
|
for (int index = 2; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
sources[index] = operation.GetSource(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
|
|
|
|
|
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
operation.SetSource(index, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListNode<INode> oldNode = node;
|
|
|
|
|
|
|
|
node = node.List.AddBefore(node, ldcOp);
|
|
|
|
|
|
|
|
node.List.Remove(oldNode);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-04-18 12:31:39 +02:00
|
|
|
globalAddress = Utils.FindLastOperation(globalAddress, block);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
if (globalAddress.Type == OperandType.ConstantBuffer)
|
|
|
|
{
|
|
|
|
return GetStorageIndex(globalAddress, sbStart, sbEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation operation = globalAddress.AsgOp as Operation;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
if (operation == null || operation.Inst != Instruction.Add)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-04-18 12:31:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand src1 = operation.GetSource(0);
|
|
|
|
Operand src2 = operation.GetSource(1);
|
|
|
|
|
|
|
|
if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
|
|
|
|
(src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
|
|
|
|
{
|
|
|
|
if (src1.Type == OperandType.LocalVariable)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-04-18 12:31:39 +02:00
|
|
|
operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
if (operation == null || operation.Inst != Instruction.Add)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
Operand source = operation.GetSource(index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-04-18 12:31:39 +02:00
|
|
|
if (storageIndex != -1)
|
|
|
|
{
|
|
|
|
return storageIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
|
|
|
|
{
|
|
|
|
if (operand.Type == OperandType.ConstantBuffer)
|
|
|
|
{
|
|
|
|
int slot = operand.GetCbufSlot();
|
|
|
|
int offset = operand.GetCbufOffset();
|
|
|
|
|
|
|
|
if (slot == 0 && offset >= sbStart && offset < sbEnd)
|
|
|
|
{
|
|
|
|
int storageIndex = (offset - sbStart) / StorageDescSize;
|
|
|
|
|
|
|
|
return storageIndex;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|