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);
|
|
|
|
|
|
|
|
if (source.AsgOp is Operation asgOperation)
|
|
|
|
{
|
|
|
|
int storageIndex = SearchForStorageBase(asgOperation, sbStart, sbEnd);
|
|
|
|
|
|
|
|
if (storageIndex >= 0)
|
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
node = ReplaceGlobalWithStorage(node, config, storageIndex);
|
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;
|
|
|
|
|
|
|
|
Operation storageOp;
|
|
|
|
|
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
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
Operand baseAddrLow = Cbuf(0, GetStorageCbOffset(config.Stage, storageIndex));
|
|
|
|
|
|
|
|
Operand baseAddrTrunc = Local();
|
|
|
|
|
2019-12-16 05:59:46 +01:00
|
|
|
Operand alignMask = Const(-config.QueryInfo(QueryInfoName.StorageBufferOffsetAlignment));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int SearchForStorageBase(Operation operation, int sbStart, int sbEnd)
|
|
|
|
{
|
|
|
|
Queue<Operation> assignments = new Queue<Operation>();
|
|
|
|
|
|
|
|
assignments.Enqueue(operation);
|
|
|
|
|
|
|
|
while (assignments.TryDequeue(out operation))
|
|
|
|
{
|
|
|
|
for (int index = 0; index < operation.SourcesCount; index++)
|
|
|
|
{
|
|
|
|
Operand source = operation.GetSource(index);
|
|
|
|
|
|
|
|
if (source.Type == OperandType.ConstantBuffer)
|
|
|
|
{
|
|
|
|
int slot = source.GetCbufSlot();
|
|
|
|
int offset = source.GetCbufOffset();
|
|
|
|
|
|
|
|
if (slot == 0 && offset >= sbStart && offset < sbEnd)
|
|
|
|
{
|
|
|
|
int storageIndex = (offset - sbStart) / StorageDescSize;
|
|
|
|
|
|
|
|
return storageIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source.AsgOp is Operation asgOperation)
|
|
|
|
{
|
|
|
|
assignments.Enqueue(asgOperation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|